
var init_complete=false
var width
var height
var selected_index_navigation_1 = -1
var has_scrolled = false;
updateSize();

jQuery(document).ready(function(){
	init();
 });
 
 function init(){
	hideHTML();
	 initResize();
	 initBG();
	 initMainStrip();
	 initMenu();
	 initButtons()
	 initRightClick();
	 initLinkFocusOverride();
	 initDisableDrag();
	 initBrowserDetect()
	  
	initScrollBars()
	initReveal()
	init_complete = true
	//setSize()
	
 }

 var content_items
 var bottom_height
 function initReveal(){
	
	content_items  = jQuery("#main_blurb, .contact_details, #enquiry_form, #gallery, #blurb_image, #footer #content_strip div" );
		content_items.css("visibility","visible");
		 resizeScrollbars(true)
	content_items.css("display","none");

	var myStrip = jQuery("#main_strip, #main_strip_full");
	var myHeight = myStrip.height();
	var myTop = myStrip.css("padding-top");
	var myBottom = myStrip.css("padding-bottom");
	
	myStrip.css("height",0);
	myStrip.css("padding-top",0);
	myStrip.css("padding-bottom",0);
	myStrip.delay(750).animate({"height":myHeight,"padding-top":myTop,"padding-bottom":myBottom},500,"easeInOutQuint",fadeOnContent);
	// 
	 var myBottomStrip = jQuery("#footer #content_strip")
	 if(myBottomStrip.css("visibility")!="hidden"){
		 myBottomStrip.css("display","block");
		bottom_height = myBottomStrip.height();
		myBottomStrip.css("height",0)	
		jQuery("#images_container #images").css("left",40);
	 }
	
 }
 function fadeOnContent(){
  content_items.fadeIn(1000,revealBottomStrip);
 }
 function revealBottomStrip(){
	var myBottom = jQuery("#footer #content_strip")
	myBottom.animate({"height":bottom_height},1000,"easeInOutQuint");
 }
function hideHTML(){
	jQuery(".html_hide").css("display","none");
}
//
//
//**************************************************************
//
// SIZING
//
//**************************************************************
function updateSize(){
	width = document.documentElement.clientWidth ;
	height = document.documentElement.clientHeight ;
}
 function initResize(){
	updateSize()
	jQuery(window).resize(setSize);
 }
 function setSize() {
	updateSize();
	if(init_complete){
		sizeBG();
		sizeOverlay();
		resizeScrollbars()
	}
}
//
//
//**************************************************************
//
// MENU
//
//**************************************************************
var _menu_rollover_height = -3;
var _section_id="";
function initMenu(){
	var myMenuItems = jQuery("#navigation_1 > ul > li")
	myMenuItems.hover(menuOver,menuOut);
	myMenuItems.each(function (i) {
		var myItem = jQuery(this)
		var myBGPath = "{site_url}images/site/transparent_5.png"
		myItem.css("background-image","url("+myBGPath+")")
		myItem.css("width",myItem.width()+14);
		if(myItem.hasClass("has_sub_menu")){ 
   			myItem.click(menuClickSub);
   		}else{
   			myItem.click(menuClick)
   		}
	});
	var mySelectionString = "#"+_section_id
	if(mySelectionString != "#"){
		var mySelected = jQuery(mySelectionString)
		selected_index_navigation_1 = mySelected.index();
		selected_item_navigation_1 = mySelected
		mySelected.css("font-weight","bold")
	}
}
function menuOver(){
	var myItem = jQuery(this);
	menuItemOver(myItem)

}
function menuItemOver(thisItem){
	var myItem = thisItem
	var myLink = jQuery("a",myItem);
	var myIndex = jQuery(this).index();
	var myY = myIndex==selected_index_navigation_1?0:_menu_rollover_height;
	myLink.stop(true,false).animate({"top":myY},{duration:400,easing:"easeOutQuart",queue:true})
	//myLink.animate({"letter-spacing":2},{duration:300,queue:true})
	if(myItem.hasClass("has_sub_menu")){
		var mySection = myItem.attr("id");
		var myItem = jQuery("#sub_"+mySection)
		if(myItem.css("z-index")!=-5){
			myItem.css("z-index",1000)
		}
		myItem.css("top","auto")
		myItem.stop(true,true).slideDown();
	}
}
function menuOut(){
	var myItem = jQuery(this);
	var myLink = jQuery("a",myItem);
	var myIndex = jQuery(this).index();
//	if(myIndex!=selected_index_navigation_1){
		myLink.stop(true,false).animate({"left":0},{duration:0,queue:true})
		myLink.animate({"top":0},{duration:200,queue:true,easing:"easeOutSine"})
		//myLink.animate({"letter-spacing":0},{duration:300,queue:false,easing:"linear"})
	//}
	if(myItem.hasClass("has_sub_menu")){
		var mySection = myItem.attr("id");
		jQuery("#sub_"+mySection).stop(true,true).slideUp();
	}
}
function menuClick(){
	//alert("click");
	var myItem = jQuery(this);
	if(selected_index_navigation_1 != myItem.index()&& !myItem.hasClass("has_sub_menu")){
		if(selected_item_navigation_1!=null){
			var mySelectedLink = jQuery("a",selected_item_navigation_1);
			mySelectedLink.css("font-weight","normal")
			mySelectedLink.stop(true,false).animate({"top":0,"letter-spacing":0},{duration:300,queue:true,easing:"easeInOutQuad"})
			//mySelectedLink.animate({"letter-spacing":0},{duration:300,queue:true,easing:"linear"})
		}
		selected_item_navigation_1 = myItem
		selected_index_navigation_1 = myItem.index();
		var myLink = jQuery("a",myItem);
		
		myLink.stop(true,false).animate({"left":0,"font-weight":"bold"},{duration:50,easing:"linear",queue:true})
		myLink.animate({"top":_menu_rollover_height,"letter-spacing":.5},{duration:200,easing:"easeOutQuad",queue:true})
		myLink.css("font-weight","bold")
		jQuery("#main_content").fadeOut(500);
		//myLink.animate({"letter-spacing":2},{duration:300,queue:true})
	}
}
function menuClickSub(){
	var myItem = jQuery(this);
	if(myItem.hasClass("has_sub_menu")){
		// var mySection = myItem.attr("id");
		// jQuery("#sub_"+mySection).stop(true,true).slideUp();
		// jQuery("#main_content").fadeOut(500);
		// myItem.unbind('mouseenter mouseleave');
	}
}
//
//
//**************************************************************
//
// BACKGROUND
//
//**************************************************************
var slides 
var bg_images = ["images/backgrounds/home-background.jpg"];
function initBG(){
	var myProps = {sizing:"scale_max",align_h:"left",width:width, height:height, auto_play:false}
	slides = new slideshow(bg_images,jQuery("#page_background"),myProps);
}
function sizeBG(){
	slides.size(width,height);
}
//
//
//**************************************************************
//
// MAIN STRIP
//
//
//**************************************************************
function initMainStrip(){
	jQuery("#main_strip #background").fadeTo(1,.9);
	var myMainContent = jQuery("#main_content")
	myMainContent.hide();
	myMainContent.animate({"top":0},{duration:1000,easing:"linear",queue:true})
	myMainContent.animate({opacity: "show"},{duration:1000,easing:"linear",queue:true})
	//myMainContent.fadeIn(4000);
//	jQuery("#main_strip").fadeIn(400);
}
//
//
//**************************************************************
//
// BUTTONS
//
//**************************************************************
myThing = "nothing"
function initButtons(){
	jQuery(".button").hover(function(){buttonOver(jQuery(this));},function(){ buttonOut(jQuery(this));});
	jQuery(".button").addClass("button_out");
}
function buttonOver(thisElement){
	thisElement.removeClass("button_out");
	thisElement.addClass("button_over");
}
function buttonOut(thisElement){
	thisElement.removeClass("button_over");
	thisElement.addClass("button_out");
}
//
//
//**************************************************************
//
// SCROLLBARS
//
//**************************************************************
function initScrollBars(){

	jQuery('.scroll_pane').jScrollPane({showArrows:true,horizontalGutter: 30,verticalGutter: 10,contentWidth:0});

}
function resizeScrollbars(thisImmediate){

		jQuery('.scroll_pane').each(
			function()
			{
				
				var api = $(this).data('jsp');
				
				var throttleTimeout;
				
				if (jQuery.browser.msie && thisImmediate!=true) {
					// IE fires multiple resize events while you are dragging the browser window which
					// causes it to crash if you try to update the scrollpane on every one. So we need
					// to throttle it to fire a maximum of once every 50 milliseconds...
					if (!throttleTimeout) {
						throttleTimeout = setTimeout(
							function()
							{
								api.reinitialise();
								throttleTimeout = null;
							},
							50
						);
					}
				} else {
					api.reinitialise();
				}
				//
				var myScroll = api.getIsScrollableV()
				if(!myScroll){
					if(has_scrolled){
						jQuery(this).css("margin-left",-25);
					}
				}else{
					
					jQuery(this).css("margin-left",0);
					has_scrolled = true
				}
			}
		)
}
//
//
//**************************************************************
//
// MISC
//
//**************************************************************
function initRightClick(){
	 jQuery(function() {jQuery(this).bind("contextmenu", function(e) {e.preventDefault();});}); 
}
function initLinkFocusOverride(){
	jQuery("a").each(function(){this.onMouseup = this.blur();});
}	
function initDisableDrag(){
	document.ondragstart = function () { return false; };
}
function sizeOverlay(){
	jQuery("#overlay").css("width",jQuery(window).width()+25);
	jQuery("#overlay").css("height",jQuery(window).height()+25);
}

function clearDefault(el) {
	if (el.defaultValue==el.value) el.value = ""
}

//
//
//***************************************************************
//
// BROWSER DETECTION
//
//***************************************************************
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
function initBrowserDetect(){
	BrowserDetect.init();
	if(BrowserDetect.browser=="Explorer"){
		jQuery("body").css("font-size","8pt");
	}
}

