var LAN = {}

LAN.settings = {};

$(document).ready(function(){

	
	LAN.settings.page = $("title").text();
	LAN.settings.isHomePage = $("#outer").hasClass("outerHome");
	LAN.settings.isApple = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i));
	LAN.settings.isTouchDevice = LAN.isTouchDevice();
	
	var loc = window.location+"";
	LAN.settings.pageURL=loc.replace(/.*?([a-z0-9_]+\.html)/i,"$1");

	
	// rollover states
	
	$(".links a img, .banner a img").hover(
		LAN.bannerMouseOver, 
		LAN.bannerMouseOut
	);

	// fix the present page's banner on its over state
	
	$("#banner a").each(function(){
		
		var thisLink=$(this).attr("href");
		
		if (thisLink==LAN.settings.pageURL){
			var myImg=$(this).children("img");
			LAN.bannerMouseOver(true,myImg);
			myImg.unbind();

		}
		
	});
	
	if (LAN.settings.isApple || LAN.settings.isTouchDevice){
		// leave the content alone - no overflow
	}
	else {
		// probably desktop, allow overflowed content
		$("#content").css({"overflow":"auto"});
	}

	
	// re-centre page on resize
	$(window).resize(function(){
	  LAN.setPage();
	});
	
	// photo lightboxes
	
	LAN.lightbox();

	// centre page layout
	LAN.setPage();	
	
	// show the page
	
	$("#outer").fadeIn(500);
	
	// set page again because some browsers haven't registered height properly until now
	LAN.setPage();
	

});

LAN.bannerMouseOver = function (useImgObj,imgObj) {
	var me = useImgObj===true ? imgObj : $(this);
	var mySrc = me.attr("src");
	if (mySrc.indexOf("_over")>=0){return;}
	else {me.attr( "src", mySrc.replace(/\./,"_over.") );}
};

LAN.bannerMouseOut = function (useImgObj,imgObj) {
	var me = useImgObj===true ?imgObj : $(this);
	var mySrc = me.attr("src");
	if (mySrc.indexOf("_over")==-1){return;}
	else {me.attr( "src", mySrc.replace(/_over\./,".") );}
};
LAN.setPage = function(){
	
	
	
	var lockWidthAt = 900;
	
	var availableHeight = $(window).height();
	var availableWidth = $(window).width();
	
	
	var useMobileVersion = (availableWidth < 400 || LAN.settings.isApple);
	
	// height
	
	var outerPanelHeight = parseInt($("#outer").css("height").split("px")[0],10);
	var difference = availableHeight-outerPanelHeight;
	
	if (useMobileVersion){
		$("#content").addClass("mobileContent");
	}
	else {
		$("#content").removeClass("mobileContent");
	}
	
	// only apply centering and background removal to homepage
	if (LAN.settings.isHomePage){

		/*if (useMobileVersion){
			//$("#outer").removeClass("lantana_bg");
			$("#outer").addClass("mobileWidth");
			$("#inner,.links").addClass("posAbsolute").addClass("mobileWidth");
			
		}
		else {
			//$("#outer").addClass("lantana_bg")
			$("#outer").removeClass("mobileWidth");
			$("#inner,.links").removeClass("posAbsolute").removeClass("mobileWidth");
		}*/

	
		if (difference>0){
			// if page is larger enough to fit it, remove the (-)margin-top from "outer"
			$("#outer").css({"margin-top":0});
		}
		else {
			// move inner up a maximum of 100
			$("#outer").css({"margin-top":(difference<-100?-100:difference)});
		}
	
	}
	else {
		
		//alert(availableHeight);
		//alert($("#banner").height());
		
		$("#content").css({"height":(availableHeight-$("#banner").height()-50)});
		
	}
	
	// width
	
	var outerPanelWidth = parseInt($("#outer").css("width").split("px")[0],10);
	var innerPanelWidth = parseInt($("#inner").css("width").split("px")[0],10);
	var leftMargin = (availableWidth-outerPanelWidth)/2;
	var innerLeftMargin = (outerPanelWidth-innerPanelWidth)/2;
	var noMargin = (0-(outerPanelWidth-innerPanelWidth)/2);
	
	var locked = $("#outer").hasClass("lockToWidth");
	
	if (locked && availableWidth<=lockWidthAt){
		$("#outer").css({"margin-left":noMargin});
	}
	else if (availableWidth < innerPanelWidth){
		// keep the right hand navigation visible if screen becomes smaller than 900px
		$("#outer").css({"margin-left":(availableWidth-innerPanelWidth-innerLeftMargin)});
	}
	else {
		$("#outer").css({"margin-left":leftMargin});
	}
	
};

LAN.lightbox = function(){

	// c.f. http://colorpowered.com/colorbox/
	
	var opts = {
		maxWidth:"95%",
		maxHeight:"95%",
		current:"{current} of {total}"
	}
	
	// if ($(window.width)>1024){opts["maxWidth"]="95%";
	
	$("a[rel='imgGrp']").colorbox(opts); 

};

LAN.isTouchDevice = function() {  
  try {  
    document.createEvent("TouchEvent");  
    return true;  
  } catch (e) {  
    return false;  
  }  
}

