// JavaScript Document

var header_interval_id;
var header_interval_time=9000;
var header_visible=0;
var header_photowidth=868;

function header_scroll_right(numheaders) {
	var newheader, showheader;
	
	clearInterval(header_interval_id);
	
	newheader=(header_visible+1)%numheaders;
	
	hideheader=header_visible+1;
	showheader=newheader+1;

	$('header-'+showheader).setStyle("left:"+header_photowidth+"px");
	$('header-'+showheader).show();
	new Effect.Move('header-'+showheader, { x: 0, y: 0, mode: 'absolute' });
	new Effect.Move('header-'+hideheader, { x: -header_photowidth, y: 0, mode: 'absolute' });
	
	header_visible=newheader;

	if (header_interval_id>0) header_interval_id=setInterval("header_scroll_right("+numheaders+")",header_interval_time);
}

function header_scroll_left(numheaders) {
	var newheader, showheader;
	
	clearInterval(header_interval_id);
	
	newheader=(header_visible-1); if (newheader<0) newheader=numheaders-1;
	
	hideheader=header_visible+1;
	showheader=newheader+1;

	$('header-'+showheader).setStyle("left:-"+header_photowidth+"px");
	$('header-'+showheader).show();
	new Effect.Move('header-'+showheader, { x: 0, y: 0, mode: 'absolute' });
	new Effect.Move('header-'+hideheader, { x: header_photowidth, y: 0, mode: 'absolute' });
	
	header_visible=newheader;

	if (header_interval_id>0) header_interval_id=setInterval("header_scroll_right("+numheaders+")",header_interval_time);
}

//////////////////////////////////////////////////////////////

var photo_interval_id;
var photo_interval_time=6000;
var photo_visible=0;

function photo_scroll_right(numphotos) {
	var newphoto, showphoto;
	
	//clearInterval(photo_interval_id);
	
	newphoto=(photo_visible+1)%numphotos;
	
	hidephoto=photo_visible+1;
	showphoto=newphoto+1;

	$('photo-'+showphoto).setStyle("left:255px");
	$('photo-'+showphoto).show();
	new Effect.Move('photo-'+showphoto, { x: 0, y: 0, mode: 'absolute' });
	new Effect.Move('photo-'+hidephoto, { x: -255, y: 0, mode: 'absolute' });
	
	photo_visible=newphoto;

	if (photo_interval_id>0) photo_interval_id=setInterval("photo_scroll_right("+numphotos+")",photo_interval_time);
}

function photo_scroll_left(numphotos) {
	var newphoto, showphoto;
	
	//clearInterval(photo_interval_id);
	
	newphoto=(photo_visible-1); if (newphoto<0) newphoto=numphotos-1;
	
	hidephoto=photo_visible+1;
	showphoto=newphoto+1;

	$('photo-'+showphoto).setStyle("left:-255px");
	$('photo-'+showphoto).show();
	new Effect.Move('photo-'+showphoto, { x: 0, y: 0, mode: 'absolute' });
	new Effect.Move('photo-'+hidephoto, { x: 255, y: 0, mode: 'absolute' });
	
	photo_visible=newphoto;

	if (photo_interval_id>0) photo_interval_id=setInterval("photo_scroll_right("+numphotos+")",photo_interval_time);
}

//////////////////////////////////////////////////////////////

var thumbnails_position=0;
var thumbnails_items;
var currentphotoid=1;

function thumbnails_bottom() {
	new Effect.Move('thumbnails-list-content', { x: 0, y: -71, mode: 'relative' });
	thumbnails_position++;
	if (thumbnails_position==thumbnails_items-4) $('fast-button-bottom').hide();
	if (thumbnails_position>0) $('fast-button-top').show();
}
function thumbnails_top() {
	new Effect.Move('thumbnails-list-content', { x: 0, y: +71, mode: 'relative' });
	thumbnails_position--;
	if (thumbnails_position<=0) $('fast-button-top').hide();
	if (thumbnails_position<thumbnails_items-4) $('fast-button-bottom').show();
}

function open_photo(photoid,categoryid,subcategoryid) {

	var img="gallery/"+categoryid+"/"+subcategoryid+"/photo-"+photoid+".jpg";

	$('thumbnail-'+currentphotoid).removeClassName('active');
	$('thumbnail-'+photoid).addClassName('active');
	currentphotoid=photoid;
	
/*
	// Prototype version - doesn't work in IE
	document.getElementById("loading").style.visibility="visible";
	
	new Ajax.Request(img, {
		method: 'get',
		contentType: 'text/html; charset=windows-1251',
		onSuccess: function(response) {
			if (response.readyState==4) {
				if (response.status==200) {
					$('photo').src=img;
					document.getElementById("loading").style.visibility="hidden";
				}
			}
		}
	});
*/


	// Non-Prototype version
	function XMLHTTP(){
		var cxm = null;
		try{
			cxm = new XMLHttpRequest();
		}catch(e){
			cxm = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return cxm;
	}
	
	$('photo').setOpacity(0.3);
	document.getElementById("loading").style.visibility="visible";
	
	var im = document.getElementById("photo");
	var param="";
	
	cmx = XMLHTTP();
	cmx.open("get", img, true);
	cmx.setRequestHeader('Content-Type', "application/x-www-form-urlencoded");
	cmx.onreadystatechange = function(){
		if (cmx.readyState == 4){
			if(cmx.status == 200){    
				im.src=img;
				document.getElementById("loading").style.visibility="hidden";
				$('photo').setOpacity(1.0);
				return;
			}
		}
	};
	cmx.send(null);
}

function popup(page) {
	window.open(page,"popup","width=550,height=650,scrollbars=1,menubar=0,resizable=0,location=0,status=0");
}


///// GUESTBOOK /////
var guestbook_current=1;
var guestbook_items;
function guestbook_scroll_down() {
	//alert($('message-'+guestbook_current).getHeight());
	new Effect.Move('guestbook-messages', { x:0, y:($('message-'+guestbook_current).getHeight()+2)*-1, mode:'relative' } );
	guestbook_current++;
	if (guestbook_current>=guestbook_items) $('guestbook-messages-more').hide();
}


///// FORM VALIDATION /////
function check_contact_form() {
	var fields=new Array("name","lastname","email","phone","checkin","checkout");
	var error=false;

	for (var i in fields) {
		if (i<fields.length && $F(fields[i])=="") {
			error=true;
		}
	}
	if (error) alert("Los campos marcados con * son obligatorios");

	if (!error && !$('privacypolicy').checked) {
		alert("Debes leer y aceptar las condiciones de reserva y la política de privacidad");
		error=true;
	}
	
	return !error;
}

function check_guestbook_form() {
	var fields=new Array("name","email","text");
	var error=false;

	for (var i in fields) {
		if (i<fields.length && $F(fields[i])=="") {
			error=true;
		}
	}
	if (error) alert("Los campos marcados con * son obligatorios");

	if (!error && !$('privacypolicy').checked) {
		alert("Debes leer y aceptar la política de privacidad");
		error=true;
	}

	return !error;
}






