var photos_selected_index = 0;
var photos_required_index = 0;
var photos_max_index = 0;
var photos_min_index = 0;
var photos_isMoving = false;
var photos_direction = 1;
var photos_counter;
var photos_speed = 5;

var element_bspace = 50;
var element_width = 90;
var element_hspace = 50;

var flag_displayer = -1;
var opacity_displayer_min = 0;
var opacity_displayer_max = 0.5;
var opacity_displayer = 0;
var opacity_speed = 0.2;

var flag_photo_displayer = -1;
var opacity_photo_displayer_min = 0;
var opacity_photo_displayer_max = 1;
var opacity_photo_displayer = 0;
var opacity_photo_speed = 0.2;
var opacity_ie = 0;
	
var photo_container_max_height = 410;
var photo_container_min_height = 50;
var photo_container_max_width = 550;
var photo_container_min_width = 50;

var photo_container_inc_height = 10;
var photo_container_inc_width = 10;
	

function tick_photos() {	
	
	/* Displaying the background layout */
	if ((flag_displayer == 1) && (opacity_displayer <= opacity_displayer_max) ) {

		opacity_displayer += parseFloat(opacity_speed);
		opacity_ie = Math.ceil(parseFloat(opacity_displayer)*100);
		
		document.getElementById("photo_displayer").style.opacity = opacity_displayer;
		document.getElementById("photo_displayer").style.filter = 'alpha (opacity=' +opacity_ie +')';
		
		if (opacity_displayer >= opacity_displayer_max) {
			flag_displayer =-1;			
		}
	}
	
	/* Hiding the background layout */
	if ((flag_displayer == 0) && (opacity_displayer >= opacity_displayer_min) ) {
		
		opacity_displayer -= parseFloat(opacity_speed);
		opacity_ie = Math.ceil(parseFloat(opacity_displayer)*100);
		
		document.getElementById("photo_displayer").style.opacity = opacity_displayer;
		document.getElementById("photo_displayer").style.filter = 'alpha (opacity=' +opacity_ie +')';
		
		if (opacity_displayer <= opacity_displayer_min) {
			flag_displayer =-1;
			document.getElementById("photo_displayer").style.display ="none";
		}
	}

	/* Displaying the photo container */
	if ((flag_photo_displayer == 1) && (opacity_photo_displayer < opacity_photo_displayer_max) ) {

		opacity_photo_displayer += parseFloat(opacity_photo_speed);
		opacity_ie = Math.ceil(parseFloat(opacity_photo_displayer)*100);
		
		document.getElementById("photo_container").style.opacity = opacity_photo_displayer;
		document.getElementById("photo_container").style.filter = 'alpha (opacity=' +opacity_ie +')';
		
		//if (opacity_photo_displayer >= opacity_photo_displayer_max) flag_photo_displayer =-1;
	}
	
	/* Animating the photo container */
	if (flag_photo_displayer == 1) {
		
		photo_container_height = document.getElementById("photo_container").style.height.replace("px","");
				
		if ( parseInt(photo_container_height) < parseInt(photo_container_max_height) ) {						
			document.getElementById("photo_container").style.height = (parseInt(photo_container_height) + parseInt(photo_container_inc_height) ) +"px";						
		}else {
		
			photo_container_width = document.getElementById("photo_container").style.width.replace("px","");	
			photo_container_left = document.getElementById("photo_container").style.left.replace("px","");
			
			if ( parseInt(photo_container_width) < parseInt(photo_container_max_width) ) {			
				
				document.getElementById("photo_container").style.width = (parseInt(photo_container_width) + parseInt(photo_container_inc_width) ) +"px";
				document.getElementById("photo_container").style.left = (parseInt(photo_container_left) - parseInt(photo_container_inc_width/2) ) +"px";
				
			} else {
								
				document.getElementById("lightbox_left_border").style.display = "";
				document.getElementById("lightbox_right_border").style.display = "";
				document.getElementById("lightbox_btn_close").style.display = "";
				document.getElementById("photo_img").style.display = "";				
				flag_photo_displayer =-1;
			}
		}		
	}
	
	/* Hiding the photo container */
	if ((flag_photo_displayer == 0) && (opacity_photo_displayer > opacity_photo_displayer_min) ) {
		
		opacity_photo_displayer -= parseFloat(opacity_photo_speed);
		opacity_ie = Math.ceil(parseFloat(opacity_photo_displayer)*100);
		
		document.getElementById("photo_container").style.opacity = opacity_photo_displayer;
		document.getElementById("photo_container").style.filter = 'alpha (opacity=' +opacity_ie +')';
		
		if (opacity_photo_displayer <= opacity_photo_displayer_min) {
			
			flag_photo_displayer =-1;
			document.getElementById("photo_container").style.display ="none";			
			document.getElementById("photo_container").style.width = photo_container_min_width +"px";
			document.getElementById("photo_container").style.height = photo_container_min_height +"px";
			document.getElementById("lightbox_btn_close").style.display = "none";
			document.getElementById("lightbox_left_border").style.display = "none";
			document.getElementById("lightbox_right_border").style.display = "none";						
			document.getElementById("photo_img").style.display = "none";				
		}
	}
	
	
	/* Setting the selected photo index */
	if (photos_selected_index == photos_required_index) return false;

	photos_isMoving = true;
	var v_array = document.getElementById('div_bien_photos').getElementsByTagName("div");
	
	photos_counter++;	
	for (var i=0;i<=v_array.length-1;i++) {				
		v_array[i].style.left = (parseInt(v_array[i].style.left.replace("px","")) + (photos_speed*photos_direction)) +"px";
	}	
	
	if (  Math.round(parseInt(photos_counter)*parseInt(photos_speed)) >= 160) {
		
		photos_selected_index = photos_required_index;
		photos_isMoving = false;		
	}		
}

function photos_move_left() {		
	
	if (photos_isMoving) return false;
	if (parseInt(photos_selected_index) <= parseInt(photos_min_index)) return false;
	photos_required_index --;
	photos_direction = -1;
	if ((parseInt(photos_required_index) - parseInt(photos_selected_index))<0) photos_direction = 1;
	photos_counter = 0;	
}


function photos_move_right() {

	if (photos_isMoving) return false;
	if (parseInt(photos_selected_index) >= parseInt(photos_max_index)) return false;
	photos_required_index ++;		
	
	photos_direction = -1;
	if ((parseInt(photos_required_index) - parseInt(photos_selected_index))<0) photos_direction = 1;
	photos_counter = 0;
}


function display_photo_displayer(v_filename) {
	
	flag_displayer = 1;
	flag_photo_displayer = 1;	
	
	var photo_width = 480;
	var photo_height = 400;
	var screen_width = screen.width;
	var screen_height = screen.height;
	
	if (v_filename == undefined) {
		if (document.getElementById("main_photo"))
			v_filename = document.getElementById("main_photo").src;
	}
	
	if (v_filename.length<=0) return false;
	
	v_left = parseInt(screen_width/2) - parseInt(photo_width/4);
	v_top = parseInt(screen_height/2) - parseInt(photo_height/2);
	
	document.getElementById("photo_container").style.left = v_left +"px";
	document.getElementById("photo_container").style.top = v_top +"px";
	//document.getElementById("photo_container").style.width = photo_width +"px";
	//document.getElementById("photo_container").style.height = photo_height +"px";	
	document.getElementById("photo_container").style.width = photo_container_min_width +"px";
	document.getElementById("photo_container").style.height = photo_container_min_height +"px";	

	document.getElementById("photo_img").src = v_filename;
	document.getElementById("photo_img").style.width = photo_width +"px";
	document.getElementById("photo_img").style.height = (photo_height-20) +"px";		
	
	document.getElementById("photo_displayer").style.display ="";
	document.getElementById("photo_container").style.display ="";	
}

function load_main_photo(v_filename) {
	
	document.getElementById("main_photo").src = v_filename;
}

function hide_photo_displayer() {
	
	flag_displayer = 0;
	flag_photo_displayer = 0;
}


function set_photo_index() {
	
	var v_selected_filename = "";
	if (document.getElementById("photo_img"))
		v_selected_filename = document.getElementById("photo_img").src;
	
	
	if (v_selected_filename.length<=0) return false;

	v_array = v_selected_filename.split("/");
	v_selected_filename = "./pictures/" +v_array[v_array.length-1];
	
	for (var i=0;i<=10;i++) {
		
		if (document.getElementById("photo_" +i))
			if (document.getElementById("photo_" +i).value == v_selected_filename) {
				v_index = document.getElementById("photo_" +i).id.substring(6,document.getElementById("photo_" +i).id.length);
				document.getElementById("photo_index").value = v_index;
			}
		
	}
}
function next_photo() {
	
	set_photo_index();
		
	var v_index = document.getElementById("photo_index").value
	
	v_index++;
	
	if (document.getElementById("photo_" +v_index)) {
		v_filename = document.getElementById("photo_" +v_index).value;
		if (v_filename.length>0) {
			document.getElementById("photo_img").src = v_filename;
		}
	}			
}

function previous_photo() {
	
	set_photo_index();	
	
	var v_index = document.getElementById("photo_index").value
	
	v_index--;
	
	if (document.getElementById("photo_" +v_index)) {
		v_filename = document.getElementById("photo_" +v_index).value;
		if (v_filename.length>0) {
			document.getElementById("photo_img").src = v_filename;
		}
	}				
}


function show_button(v_button) {
	if (document.getElementById(v_button))	document.getElementById(v_button).style.display ="";
}

function hide_button(v_button) {
	if (document.getElementById(v_button))	document.getElementById(v_button).style.display ="none";	
}
