// JavaScript Document
<!--
$(document).ready(function() {

	// Load data
	$("#contact_box").load("content/contact.html");

	// Event listener!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});	

	$("#info_dialog_opener").click(function(){
		centerPopup();
		loadPopup();
	});
	
	$("#popup_info_close").click(function(){
		disablePopup();
	});

	$("#background_popup_modal_box").click(function(){
		disablePopup();
	});

	$("#es_ES_Button").click(function(){
		toggleLanguage('es_ES');
	});

	$("#en_EN_Button").click(function(){
		toggleLanguage('en_EN');
	});
});


var selectedInfoLocale = "es_ES";

function toggleLanguage(lang){
	switch (lang) {
		case "es_ES":
			$("#info_area_box").load("content/info.html #es_ES", 
				function(response, status, xhr) { /* do nothing; */ });
			selectedInfoLocale = 'es_ES';
			$("#es_ES_Button").css({
				"font-weight": "bold"
			});
			$("#en_EN_Button").css({
				"font-weight": "normal"
			});
			break;
		case "en_EN":
			$("#info_area_box").load("content/info.html #en_EN", 
				function(response, status, xhr) { /* do nothing; */ });
			selectedInfoLocale = 'en_EN';
			$("#en_EN_Button").css({
				"font-weight": "bold"
			});
			$("#es_ES_Button").css({
				"font-weight": "normal"
			});
		break;
	}
}


//Setup the PopUp
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//Loading popup with jQuery
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#background_popup_modal_box").css({
			"opacity": "0.5"
		});
		$("#background_popup_modal_box").fadeIn("slow");
		$("#popup_info_box").fadeIn("slow");
		popupStatus = 1;
	
		toggleLanguage(selectedInfoLocale);
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#background_popup_modal_box").fadeOut("slow");
		$("#popup_info_box").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup_info_box").height();
	var popupWidth = $("#popup_info_box").width();
	//centering
	$("#popup_info_box").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#background_popup_modal_box").css({
		"height": windowHeight
	});
}

function swapImgRestore() { //v3.0
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function swapImage() { //v3.0
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
