// JavaScript Document
/*
********************************
Created by Dzmitry Isakau
www.contrasso.com
********************************
*/

var $pg_t;
var $pg_del = 0.1; var $pg_speed = 1;
var $pg_pic_id;
var $pg_pic_pause = 8000;
if(!$pg_thumbs) var $pg_thumbs = new Array();
if(!$pg_cats) var $pg_cats = new Array();
if(!$pg_titles) var $pg_titles = new Array();
if(!$pg_cat_id) var $pg_cat_id = 1;

function pg_hgThumbs($obj){
	i = 0;
	a = document.getElementsByTagName("div");
	while(element = a[i++]) {
		if(element.className == "pg_th1") element.className = 'pg_th0';
    }

	$obj.className = 'pg_th1';
}

function pg_chPic($mode){
	var $nid;
	if(!$pg_pic_id) $nid = 0;
	else $nid = $pg_pic_id;
	
	//alert($nid);
	
	if($mode == 1) $nid++;
	else $nid--;
	if($nid < 0) $nid = $pg_photos.length - 1;
	if($nid == $pg_photos.length) $nid = 0;
	
	pg_flipPic($nid);
}

function pg_flipPic($id){
	$pg_pic_id = $id;
	var $obj = document.getElementById('pg_thumb'+$id);
	pg_hgThumbs($obj);
	//1 goto transparent	
	pg_fadeOut(1, $pg_del, $pg_speed);
}

function pg_fadeOut($val, $del, $speed){
	var $new = $val - $del;
	$new = Math.round($new*100)/100;
	$newIE = $new * 100;
	document.getElementById('pg_photo').style.opacity = $new;
	document.getElementById('pg_photo').style.filter = 'alpha(opacity='+$newIE+')';
	if($new >= 0) $pg_t = setTimeout('pg_fadeOut('+$new+', '+$del+', '+$speed+')', $speed);
	else{
		clearTimeout($pg_t);
		pg_reloadPic();
	}
}

function pg_reloadPic(){
	var $photo_name = $pg_path+$pg_photos[$pg_pic_id];
	var $target_photo = new Image();
    $target_photo.onload = function() {
        document.getElementById('pg_photo').innerHTML = '<img src="'+$photo_name+'" alt="">';
		document.getElementById('pg_title').innerHTML = $pg_titles[$pg_pic_id];
		//3 goto opaque
		pg_fadeIn(0, $pg_del, $pg_speed);
    }
    $target_photo.src = $photo_name;
}

function pg_fadeIn($val, $del, $speed){
	var $new = $val + $del;
	$new = Math.round($new*100)/100;	
	$newIE = $new * 100;
	document.getElementById('pg_photo').style.opacity = $new;
	document.getElementById('pg_photo').style.filter = 'alpha(opacity='+$newIE+')';
	if($new <= 1) $pg_t = setTimeout('pg_fadeIn('+$new+', '+$del+', '+$speed+')', $speed);
	else{
		clearTimeout($pg_t);
		//to make pics load continously
		//setTimeout('pg_flipPic('+$pg_pic_id+')', $pg_pic_pause);
	}
}

//****************************************************************************
function pg_reloadThumbs(){
	var $thumbs_html = '';
	for(var $i=0; $i < $pg_thumbs.length; $i++){
		$thumbs_html += '<div id="pg_thumb'+$i+'" class="pg_th';
		if($i == 0) $thumbs_html += '1';
		else $thumbs_html += '0';
		$thumbs_html += '"><img src="'+$pg_path+$pg_thumbs[$i]+'" alt="" onclick="pg_flipPic('+$i+');"></div>';
	}
	
	document.getElementById('pg_thumbs').innerHTML = $thumbs_html;
}

function pg_reloadCats(){
	var $c_html = '';
	
	if($pg_cats.length > 0){
		$c_html += '<div class="cls pg_cats">';
		var $c_tabs = '<div class="pg_tabL"><img src="/images/a.gif" alt=""></div><div class="pg_tabR"><img src="/images/a.gif" alt=""></div>';
		for(var $i=0; $i < $pg_cats.length; $i++){
			if($pg_cats[$i] != ' '){
				var $c_v = $pg_cats[$i].replace('_', ' ');

				if($pg_cat_id == $i) $c_html += '<div class="pg_cats_on">'+$c_tabs+'<div class="pg_cats_pad">'+$c_v+'</div></div>';
				else $c_html += '<div class="pg_cats_off">'+$c_tabs+'<div class="pg_cats_pad"><a href="javascript://" onclick="pg_changeDir('+$i+');">'+$c_v+'</a></div></div>';
			}
		}
		$c_html += '</div>';
	}else{
		$c_html += '<div class="cls pg_barT"><img src="images/a.gif" alt=""></div>';
	}

	document.getElementById('pg_cats').innerHTML = $c_html;
}

function pg_changeDir($dir_id){
	
	$pg_cat_id = $dir_id;

	var httpObject = getHTTPObject();
	if(httpObject==null) return;
	
	var $url="/photogallery/pg_changedir.php?&id="+$pg_cat_id;
	//alert($url);
	httpObject.open("GET", $url, true);
	httpObject.send(null);
	httpObject.onreadystatechange = function(){
		if(httpObject.readyState == 4 && httpObject.status == 200){
			var $result = httpObject.responseText;
			var $split = $result.split('|');
			
			$pg_photos = $split[0].split(':');
			$pg_path = $split[1];
			$pg_thumbs = $split[2].split(':');
			$pg_cats = $split[3].split(':');
			$pg_titles = $split[4].split(':');

			pg_reloadThumbs();
			pg_reloadCats();
			
			$pg_pic_id = 0;
			
			var $photo_name = $pg_path+$pg_photos[$pg_pic_id];
			document.getElementById('pg_photo').innerHTML = '<img src="'+$photo_name+'" alt="">';
			document.getElementById('pg_title').innerHTML = $pg_titles[$pg_pic_id];
		}
	}
}

function getHTTPObject(){
	var xmlHttp=null;
	try{
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
	}catch(e){
 		// Internet Explorer
 		try{
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
 		}catch(e){
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
 		}
	}
	return xmlHttp;
}

