
var current_album_picture_index = 0;
var album_pictures_count = -1;



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);
	
}
















/* FRIENDS HOUSE 2009.12.13 */

var comment_class_name = 'comm_ent';

function SetPictureNameAndComment(elem){
	
	var span_els = elem.getElementsByTagName("span");
	var comment_elem = null;
	
	for(i=0; i < span_els.length; i++){
		//alert(span_els[i].className);
		if(span_els[i].className == comment_class_name){
			comment_elem = span_els[i];
			break;
		}
	}
	
	var h_name = document.getElementById('photo_name');
	if(h_name){
		h_name.innerHTML=elem.title;
	}
	
	if(comment_elem){
		var p_comment = document.getElementById('photo_comment');
		if(p_comment){
			p_comment.innerHTML=comment_elem.innerHTML;
		}
	}
	
	//alert(comment_elem.innerHTML);	
	//alert(elem.title);
	
	return false;
}


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 PreloadAlbumPictures(){
	var pictures_count = CountAlbumPictures();
	//alert(CountAlbumPictures());
	for( var i=0; i < pictures_count; i++){
		var tmp_id = GetPictureIdbyIndex(i);
		var picture_link = document.getElementById(tmp_id);
		//alert(picture_link.childNodes[0].src);
		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.src);
		//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';
	   }
	}
	return result;
}



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.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];
			*/
			var big_photo_imgs = big_photo_div.getElementsByTagName('img');
			if(big_photo_imgs.length == 0) return;
			var big_photo_img = big_photo_imgs[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 image_src = thumb_src.replace(thumb_name, pict_name);
			
			big_photo_img.src = image_src;
			//big_photo_img.alt = picture_link.childNodes[0].alt;
			//alert(big_photo_img.src);
			
			if(positioning == 'v'){
				big_photo_div.className = 'mainPhoto foto_v';
			} else{
				big_photo_div.className = 'mainPhoto foto_h';
			}						
			
			picture_link.className = 'active';
			
			big_photo_div.style.visibility='visible';
			
			SetPictureNameAndComment(picture_link);
			
		}
		
	}
}


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 = '';
			}
		}
	}
}

