//First, perform language check and see if splash page should be skipped.
var skip = true;

url = window.location.href.indexOf("?");
if (url == -1) {	
//if there is no language switch in the url, check to see if the browser language is Spanish. If so, load the Spanish page (index_es.html) instead.
//If not, proceed to load the page as normal (with splash)
//If this page has been called with a language switch [doesn't actually matter what] then the splash page should be skipped
	var language = window.navigator.userLanguage || window.navigator.language;
	language = language.slice(0,2);
	if (language == "es") {
		window.location.replace("index_es.html")
	}
}
else {
	skip = true;
}


//start with some preloading of the necessary first few images and only then run Fadestrip
preloadImages  = new Array();
preloadImages[0] = new Image();
preloadImages[0].src = "assets/logo_strip.jpg"
preloadImages[1] = new Image();
preloadImages[1].src = "assets/splash.jpg"
preloadImages[2] = new Image();
preloadImages[2].src = "assets/escondrijo_strip.png"
preloadImages[3] = new Image();
preloadImages[3].src = "assets/text_alojamiento.png"
preloadImages[4] = new Image();
preloadImages[4].src = "assets/text_houserental.png"
preloadImages[5] = new Image();
preloadImages[5].src = "assets/text_casaalquiler.png"
preloadImages[6] = new Image();
preloadImages[6].src = "assets/text_guesthouse.png"

preloadImages[1].onload = function(){
	FadeStrip();
}

logo = new Image();
logo.src = "assets/logo_salmon.jpg";

homePics = new Array();
hammockPics = new Array();
woodburnerPics = new Array();
luzPics = new Array();
locationPics = new Array();
roomPics = new Array();

//homepage pics
homePics[0] = new Image();
homePics[0].src = "assets/house_door.jpg";
homePics[0].alt = "Welcome to escondrijo";
homePics[1] = new Image();
homePics[1].src = "assets/house_mainTerrace.jpg";
homePics[1].alt = "escondrijo main terrace";
homePics[2] = new Image();
homePics[2].src = "assets/house_mist.jpg";
homePics[2].alt = "Misty morning view from terrace";
homePics[3] = new Image();
homePics[3].src = "assets/house_patio1.jpg";
homePics[3].alt = "escondrijo patio";
homePics[4] = new Image();
homePics[4].src = "assets/house_staircase.jpg";
homePics[4].alt = "The old staircase";
homePics[5] = new Image();
homePics[5].src = "assets/house_balconera.jpg";
homePics[5].alt = "A traditional balcony";
homePics[6] = new Image();
homePics[6].src = "assets/house_entrada.jpg";
homePics[6].alt = "Entrance with handpainted tiles";
homePics[7] = new Image();
homePics[7].src = "assets/house_arco.jpg";
homePics[7].alt = "The house straddles el arco de Xauen";
homePics[8] = new Image();
homePics[8].src = "assets/house_insideTerrace.jpg";
homePics[8].alt = "Main staircase";	

function preloadRest(){
	//Only trigger this on the window.onload function after everything else has loaded.
	//hammock room pics
	hammockPics[0] = new Image();
	hammockPics[0].src = "assets/hammock_bed.jpg";
	hammockPics[0].alt = "Mezzanine leading to terrace";
	hammockPics[1] = new Image();
	hammockPics[1].src = "assets/hammock_hammock.jpg";
	hammockPics[1].alt = "The cosy hammock chair";
	hammockPics[2] = new Image();
	hammockPics[2].src = "assets/hammock_coffeeTable.jpg";
	hammockPics[2].alt = "Sofa bed and facilities for snacks";
	hammockPics[3] = new Image();
	hammockPics[3].src = "assets/hammock_terrace1.jpg";
	hammockPics[3].alt = "Hammock room private terrace";
	hammockPics[4] = new Image();
	hammockPics[4].src = "assets/hammock_terrace2.jpg";
	hammockPics[4].alt = "Hammock room private terrace";
	hammockPics[5] = new Image();
	hammockPics[5].src = "assets/hammock_bathroom.jpg";
	hammockPics[5].alt = "Hammock bathroom";
	//woodburner room pics
	woodburnerPics[0] = new Image();
	woodburnerPics[0].src = "assets/woodburner_bedroom.jpg";
	woodburnerPics[0].alt = "Woodburner bedroom";
	woodburnerPics[1] = new Image();
	woodburnerPics[1].src = "assets/woodburner_salon_new.jpg";
	woodburnerPics[1].alt = "Woodburner sitting area";
	woodburnerPics[2] = new Image();
	woodburnerPics[2].src = "assets/woodburner_terrace_table.jpg";
	woodburnerPics[2].alt = "Relax on your private terrace";
	woodburnerPics[3] = new Image();
	woodburnerPics[3].src = "assets/woodburner_terrace_view.jpg";
	woodburnerPics[3].alt = "Woodburner terrace views";
	woodburnerPics[4] = new Image();
	woodburnerPics[4].src = "assets/woodburner_from_terrace.jpg";
	woodburnerPics[4].alt = "Woodburner large private terrace";
	//luz pics
	luzPics[0] = new Image();
	luzPics[0].src = "assets/luz_bedroom.jpg";
	luzPics[0].alt = "Luz main bedroom";
	luzPics[1] = new Image();
	luzPics[1].src = "assets/luz_mesa.jpg";
	luzPics[1].alt = "Seating in Luz bedroom";
	luzPics[2] = new Image();
	luzPics[2].src = "assets/luz_bathroom.jpg";
	luzPics[2].alt = "Luz bathroom";
	luzPics[3] = new Image();
	luzPics[3].src = "assets/luz_seating.jpg";
	luzPics[3].alt = "Luz additional seating";
	luzPics[4] = new Image();
	luzPics[4].src = "assets/luz_twinroom.jpg";
	luzPics[4].alt = "Luz second bedroom";	
	//location pics
	locationPics[0] = new Image();
	locationPics[0].src = "assets/location_vejer.jpg";
	locationPics[0].alt = "Vejer de la Frontera, Costa de la Luz";
	locationPics[1] = new Image();
	locationPics[1].src = "assets/location_plaza.jpg";
	locationPics[1].alt = "The Plaza de Espa&ntilde;a in Vejer";
	locationPics[2] = new Image();
	locationPics[2].src = "assets/location_military.jpg";
	locationPics[2].alt = "Unspoilt coastline of Costa de la Luz";
	locationPics[3] = new Image();
	locationPics[3].src = "assets/location_molino.jpg";
	locationPics[3].alt = "Parapenting near old windmill";
	locationPics[4] = new Image();
	locationPics[4].src = "assets/location_kitesurf.jpg";
	locationPics[4].alt = "Kitesurfing in Tarifa";
	locationPics[5] = new Image();
	locationPics[5].src = "assets/location_trafalgar.jpg";
	locationPics[5].alt = "Lighthouse at site of Battle of Trafalgar";
	//set rooms pics
	roomPics[0] = hammockPics[0];
	roomPics[0].alt = "Hammock room";
	roomPics[1] = woodburnerPics[0];
	roomPics[2] = luzPics[0];
}

function getBrowserHeight()
{
	if (window.innerHeight)
	{
		return window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight != 0)
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		return document.body.clientHeight;
	}	
	return 0;
};

//global var declarations
var thisPic;
var imgCt;
var currentPics;
var pictureProgress = false;
var inProgress = false;
var overlayFlag = null;

function resetPics(whichPics) {
	if (currentPics != whichPics) {
		currentPics = whichPics;
		thisPic = -1;
		imgCt = currentPics.length - 1;
		chgSlide(1);
	}
}

function chgSlide(direction) {
//	if ($(":animated").length != 0) return false
	if (document.images) {
		thisPic = thisPic + direction;
		if (thisPic > imgCt) {
			thisPic = 0;
		}
		if (thisPic < 0) {
			thisPic = imgCt;
		}
		if (document.getElementById('picture').src != null) {
			//fade in 
			$("#picture").fadeTo(300,0,function(){
			document.getElementById('picture').src = currentPics[thisPic].src;
			document.getElementById('picture_title').innerHTML = currentPics[thisPic].alt;
			$("#picture").fadeTo(1000,1)
			})
		}
	}
}

function chgReview(toClose,next) {
	toCloseParent = toClose.parentNode.parentNode;
	toCloseParent.style.display = 'none';
	document.getElementById('review'+next).style.display = 'block';
}

function toggleContents(content,collapse){
		if ($(":animated").length != 0) return false
		$(content).children(":not(:first)").fadeToggle(500)
		$(content).children(":first").fadeToggle(500)
		if (collapse) {
			$(content).animate({
				height: "1.2em",
				width: "5em"
			},500)
		}
		else {
			$(content).animate({
				height: "32em",
				width: "21.9286em"
			})
		}
}

function chgPicsandText(content,whichPics){
	//roll resetPics and overlayToggle into single function to trap clicking when animation in progress
	if ($(":animated").length != 0) return false
	overlayToggle(content)
	resetPics(whichPics)
}

function overlayToggle(content){
//	alert('length= ' + $(":animated").length);
	$(".overlay_box:visible").css("display","none");
	content.style.display = 'block';	
}

re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
function validateEmail() {
	address = document.getElementById('email');
	address.style.background = "#fff";
	if (address.value != "" && !re.test(address.value)) {
		address.value = "Invalid address entered";
		address.style.background = "#fcc";
		setTimeout("address.focus();",1);
		setTimeout("address.select();",1);
	}
}
function validateEmail2() {
	address = document.getElementById('emailCheck');
	address.style.background = "#fff";
	if (address.value != document.getElementById('email').value) {
		address.value = "Does not match";
		address.style.background = "#fcc";
		setTimeout("address.focus();",1);
		setTimeout("address.select();",1);
	}
}

function resizing() {
	newHeight = Math.floor(getBrowserHeight()/42);
	if (newHeight < 10) {newHeight = 10};
	if (newHeight > 14) {newHeight = 14};
	document.body.style.fontSize = newHeight + "px";
}

function toggle_tab(tabId) {
	if ($(":animated").length != 0) return false
	if ($(tabId).width() > 0) {
		$(tabId).animate({
			width: '0em'
		},"fast")
	}
	else {
		$(tabId).animate({
			width: '56em'
		},"fast")
	}
}

fOnload=function(){
//First up, set the em size based on browser height
pixelHeight = Math.floor(getBrowserHeight()/42);
if (pixelHeight < 10) {pixelHeight = 10};
if (pixelHeight > 14) {pixelHeight = 14};
document.body.style.fontSize = pixelHeight + "px";

//set the initial Pics to be home page pics
resetPics(homePics);
//turn on home text box
document.getElementById('content_thehome').style.display = 'block';

//disable bubble of click event on hammock etc. otherwise the click registers on 'hammock' and then on 'rooms' too
$("li ul li").click(function(event){
	event.stopPropagation();
})

//preload remaining images
preloadRest();
}
//END OF ONLOAD

window.onload = fOnload;
