//Values
document.pageWidth = 0;
document.pageHeight = 0;
document.minWidth = 700;
document.minHeight = 300;
document.suggestingMobile = false;
document.firstRun = true;
document.scrolTo = 0;
for(key in document.pages){
	if(document.pages[key] == window.location.hash.split("#")[1]){
		document.pageRequsted = key;
	}
}
if(1>document.pageRequsted || document.pageRequsted == undefined || isNaN(document.pageRequsted)){
	document.pageRequsted = 0;
}
else if(document.pageRequsted>=document.pages.length){
	document.pageRequsted = document.pages.length-1;
}
document.currentPage = document.pageRequsted;
document.errorMessages = {"mobile":	"Your browser's viewport's resolution has to be atleast "+document.minWidth+"x"+document.minHeight+" to view the normal version.<br/ ><a href=\"mobile\">Click here to view the mobile version</a> or resize your browser.",
"nojs": "You have disabled JavaScript.<br />Please enable it and refresh this page or <a href=\"mobile\">click here to view the mobile version</a>."
}
//Functions
function updatePageWidth(force){
	//Set width
	width = $(window).width()
	//Suggest mobile version
	if(document.suggestingMobile == false && (document.minWidth>width || document.minHeight>$(window).height())){
		document.suggestingMobile = true;
		$("#desktop").css("display", "none");
		$("body").css("min-width", "0px");
		$("#alert-content").html(document.errorMessages["mobile"]);
		$("#alert").css("display", "block");
	}
	else if(width>=document.minWidth+document.srollBar && $(window).height()>=document.minHeight+document.srollBar && document.suggestingMobile){
		document.suggestingMobile = false;
		$("#alert-content").html("");
		$("#alert").css("display", "none");
		$("body").css("min-width", document.minWidth+"px");
		$("#desktop").css("display", "block");
	}
	$(".maximized").css("max-height", $(window).height()+"px");
	if(document.minWidth>width){
		width = document.minWidth;
	}
	if(document.pageWidth != width || force){
		document.pageWidth = width;
		margin = ((width-document.minWidth)/2);
		if(0>margin){
			margin = 0;
		}
		if(margin == 0){
			$("body").css("overflow-x", "auto");
		}
		else{
			$("body").css("overflow-x", "hidden");
		}
		$(".page").css("margin-left", margin+"px");
		$(".page").css("margin-right", margin+"px");
		$("#pages").css("width", width+"px");
		$("#pages-inner").css("margin-left", (-(document.currentPage*width))+"px");
		$("#pages-inner").css("width", (document.pages.length*width)+"px");
	}
	if(document.firstRun && document.suggestingMobile == false){
		document.firstRun = false;
		$("#alert-content").html("");
		$("#alert").css("display", "none");
		$("#desktop").css("display", "block");
	}
}
function selectPage(select){
	if(select == -1){
		$("body").css("overflow-y", "hidden");
		for(id=0; document.pages.length>id; id++){
			$("#page-"+id).css("height", "auto");
		}
		//Scrollbar fix
		updatePageWidth(true);
	}
	else{
		setTimeout("window.location = '#"+document.pages[select]+"';",10);
		$("body").css("overflow-y", "auto");
		for(id=0; document.pages.length>id; id++){
			if(select == id){
				$("#page-"+id).css("height", "auto");
			}
			else{
				$("#page-"+id).css("overflow", "hidden");
				$("#page-"+id).css("height", "100px");
			}
		}
		//alert($("#page-3").attr("clientHeight")+"|"+($(window).height()-145));
	}
}
function goTo(id){
	if(id>=0 && document.pages.length>id && document.currentPage !== id){
		selectPage(-1);
		selectButton(id);
		document.currentPage = id;
		//Reset
		$('#pages-inner').stop();
		if($.browser.webkit){
			$("body").stop();
		}
		else{
			$("html").stop();
		}
		//Animate
		$('#pages-inner').animate({
			marginLeft: -(id*document.pageWidth)
		}, 600, function(){ selectPage(document.currentPage); });
		$.scrollTo(0, 600);
	}
}
function selectButton(id){
	for(id2=0; document.pages.length>id2; id2++){
		if(id == id2){
			$("#header-inner b:nth-child("+(id2+1)+")").addClass("activate");
		}
		else if($("#header-inner b:nth-child("+(id2+1)+")").hasClass('activate')){
			$("#header-inner b:nth-child("+(id2+1)+")").removeClass("activate");
		}
	}
}
//Email hider
function setAddresses(id, address, protocol){
	realaddress = "";
	for(i=address.length; i>=0; i--){
		realaddress += address.substr(i,1);
	};
	$("#"+id).attr("href", protocol+":"+realaddress);
	$("#"+id).html(realaddress);
}
//Run
$(document).ready(function(){
	document.srollBar = $.getScrollbarWidth();
	selectPage(document.pageRequsted);
	selectButton(document.pageRequsted);
	setInterval('updatePageWidth(false)', 100);
	//Contact
	$("#contact-name").focus(function(){
		if($(this).val() == 'Name'){
			$(this).val('')
		}
	}).focusout(function(){
		if($(this).val() == ''){
			$(this).val('Name')
		}
	});
	$("#contact-message").focus(function(){
		if($(this).val() == 'Message'){
			$(this).val('')
		}
	}).focusout(function(){
		if($(this).val() == ''){
			$(this).val('Message')
		}
	});
	//Email addresses
	setAddresses("contact-details-email", "ten.nenosaal@illo", "mailto");
	setAddresses("contact-details-xmpp", "moc.liamg@saal.illo", "xmpp");
	setAddresses("contact-details-msn", "moc.liamtoh@saal.illo", "msn");
	//Buttons
	$(".button").mousedown(function(){
		return false;
	})
	//Control panel
	$("#open-control-panel").click(function(){
		$("#alert-content").html("<div style='width:600px; margin-left:auto; margin-right:auto;'><h1>Control Panel</h1><table><tr><td><b style='float:left;'>Username</b></td><td><b style='float:left;'>Password</b></td></tr><tr><td><input type='text' name='username' /></td><td><input type='password' name='password'></td></tr></table><center><button id='login'>Login</button><button id='cancel-login'>Cancel</button></center></div>");
		$("#alert").css("opacity", "0");
		$("#alert").css("display", "block");
		$('#alert').animate({
			opacity: 1
		}, 600, function(){ document.scrolTo = $("html").scrollTop()+$("body").scrollTop(); $("#desktop").css("display", "none"); });
		$("#cancel-login").click(function(){
			$("#alert").stop();
			$("#desktop").css("display", "block");
			if(document.scrolTo>0){
				$.scrollTo(document.scrolTo, 0);
				document.scrolTo = 0;
			}
			$('#alert').animate({
				opacity: 0
			}, 600, function(){ $("#alert").css("display", "none"); $("#alert-content").html(""); });
		
		});
	});
	//Maximize images
	$("img.maximize").click(function(){
		$("#alert").click(function(){
			$("#alert").css("cursor", "default");
			$("#alert").stop();
			$("#desktop").css("display", "block");
			if(document.scrolTo>0){
				$.scrollTo(document.scrolTo, 0);
				document.scrolTo = 0;
			}
			$('#alert').animate({
				opacity: 0
			}, 300, function(){ $("#alert").css("display", "none"); $("#alert-content").html(""); $("#maximized-text").css("display", "none"); });
			$("#alert").unbind('click');
			
		});
		$("#maximized-text").css("display", "block");
		$("#maximized-text").html($(this).attr('alt'));
		$("#alert").css("cursor", "pointer");
		$("#alert-content").html("<img class=\"maximized\" src=\""+$(this).attr("src").replace("/thumbs", "")+"\" alt=\""+$(this).attr("alt")+"\">");
		$("#alert").css("opacity", "0");
		$("#alert").css("display", "block");
		$('#alert').animate({
			opacity: 1
		}, 600, function(){ document.scrolTo = $("html").scrollTop()+$("body").scrollTop(); $("#desktop").css("display", "none"); });
	});
});

