
var current_album_picture_index = 0;
var album_pictures_count = -1;

function ShowPicture(picture_link_id){

	if(picture_link_id){
		//ap_123
		ResetActivePicture(); //unset active thumb 
		
		var picture_link = document.getElementById(picture_link_id);
		
		if(!picture_link) return;
		
		var positioning = picture_link.id.substr(picture_link.id.length-1);
		
		var id_without_pos = picture_link.id.substr(0, picture_link.id.length-1);
		//alert(id_without_pos);
		
		//var index = parseInt(picture_link.id.substr(3));
		var index = parseInt(id_without_pos.substr(3));
		current_album_picture_index = index;
		var next_index = index+1;
		var prev_index = index-1;
		
		var thumb_src = picture_link.childNodes[0].src;
		if(!thumb_src) return false;
		
		var tmp_array = thumb_src.split('/');
		var thumb_name = tmp_array[tmp_array.length-1];
		
		
		
		if(!thumb_name) return false;
		//alert(thumb_name);
		
		
		
		var thumbs_cont = document.getElementById('thumbs_container');
		
		if(thumbs_cont){
			//var thumbs_count = thumbs_cont.childNodes.length;
			var thumbs_count = thumbs_cont.getElementsByTagName('div').length;
			//alert("Index: "+index + " Thumbs count: "+thumbs_count);
			
			var prev = document.getElementById('ap_prev');
			var next = document.getElementById('ap_next');
			var big_photo_div = document.getElementById('ap_big_photo');			
			if(!prev || !next || !big_photo_div) return false;
			var big_photo_img = big_photo_div.childNodes[0];
			if(!big_photo_img) return false;
			
			big_photo_div.style.visibility='hidden';
			big_photo_img.src = 'images/blank.gif';
			
			
			if(index == 0){//if first thumb
				prev.style.visibility = 'hidden';
				if(thumbs_count > 1){
					next.style.visibility = 'visible';
					//next.childNodes[0].onclick=function() {ShowPicture(next_index);return false;};
					//next.childNodes[0].onclick= function() {alert("OK"); return false;};
					//alert('ShowPicture("ap_'+next_index+'");return false;');
					//alert(next.childNodes[0].onclick);
				} else{
					next.style.visibility = 'hidden';
				}
			} else if(index == thumbs_count-1){ //if last thumb
				prev.style.visibility = 'visible';
				next.style.visibility = 'hidden';
				//prev.childNodes[0].onclick='ShowPicture("ap_'+prev_index+'");return false;';
			} else{
				prev.style.visibility = 'visible';
				next.style.visibility = 'visible';
				//next.childNodes[0].onclick='ShowPicture("ap_'+next_index+'");return false;';
				//prev.childNodes[0].onclick='ShowPicture("ap_'+prev_index+'");return false;';
			}
			
			var pict_name = thumb_name.substr(6);			
			
			/*
			var tmp_image = new Image(); 
			
			tmp_image.src = thumb_src.replace(thumb_name, pict_name);					
			var counter = 0;
			
			while((!tmp_image.complete || tmp_image.width == 0) && counter < 100){
				tmp_image.src = thumb_src.replace(thumb_name, pict_name);
				counter++;
			}
						
			if(tmp_image.width > tmp_image.height){
				big_photo_div.className = 'foto_h';
				//alert("W > H: "+tmp_image.width+">"+tmp_image.height);
			} else if(tmp_image.width < tmp_image.height){
				big_photo_div.className = 'foto_v';
				//alert("W < H: "+tmp_image.width+"<"+tmp_image.height);
			} else{
				big_photo_div.className = 'foto_h';
			}
			*/
			
			var image_src = thumb_src.replace(thumb_name, pict_name);
			
			big_photo_img.src = image_src;
			
			if(positioning == 'v'){
				big_photo_div.className = 'foto_v';
			} else{
				big_photo_div.className = 'foto_h';
			}						
			
			picture_link.className = 'active';
			
			big_photo_div.style.visibility='visible';
			
			//big_photo_img.style.display = 'inline';
			
			//alert(big_photo.width + " x " + big_photo.height);
		}
		
	}
}


function ChangePicture(index){
	if(album_pictures_count < 0){
		CountAlbumPictures();
	}
	
	var new_index = current_album_picture_index + index;
	if(new_index < 0 || new_index > album_pictures_count-1) return false;

	//var pict_id = 'ap_'+new_index;
	var pict_id = GetPictureIdbyIndex(new_index);
	//alert(pict_id);
	ShowPicture(pict_id);
	
}


function CountAlbumPictures(){
	var thumbs_cont = document.getElementById('thumbs_container');
	if(thumbs_cont){
			album_pictures_count = thumbs_cont.getElementsByTagName('div').length;//thumbs_cont.childNodes.length;
			return album_pictures_count;			
	}
	return -1;
}


function ResetActivePicture(){
	var thumbs_cont = document.getElementById('thumbs_container');
		
	if(thumbs_cont){
		var thumbs_count = thumbs_cont.getElementsByTagName('div').length;
		for( var i=0; i < thumbs_count; i++){
			var tmp_id = 'ap_'+i;
			var tmp_elem = document.getElementById(tmp_id+'v');
			if(!tmp_elem){
				tmp_elem = document.getElementById(tmp_id+'h');
			}
			
			if(tmp_elem){
				tmp_elem.className = '';
			}
		}
	}
}


function PreloadAlbumPictures(){
	var pictures_count = CountAlbumPictures();
	for( var i=0; i < pictures_count; i++){
		//var tmp_id = 'ap_'+i;
		var tmp_id = GetPictureIdbyIndex(i);
		var picture_link = document.getElementById(tmp_id);
		var thumb_src = picture_link.childNodes[0].src;
		var tmp_array = thumb_src.split('/');
		var thumb_name = tmp_array[tmp_array.length-1];
		var pict_name = thumb_name.substr(6);			
			
		var tmp_image = new Image(); 			
		tmp_image.src = thumb_src.replace(thumb_name, pict_name);
		//alert(tmp_image.width+"  "+tmp_image.height);		
	}
	
}



function GetPictureIdbyIndex(pict_index){
    var result = '';
	if(pict_index >= 0){
	   var tmp = 'ap_'+pict_index+'v';
	   var tmp_el = document.getElementById(tmp);
	   if(tmp_el){
			result = tmp;
	   } else{
			result = 'ap_'+pict_index+'h';
			/*
			tmp = 'ap_'+pict_index+'h';
			tmp_el = document.getElementById(tmp);
			if(tmp_el){
				result = tmp;
			}
			*/
	   }
	}
	return result;
}
