// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

hush = function () {
    var count=0;
	var browserwidth = $(window).width();
    var browserheight = $(window).height();
	var imageWidth = 1600;
	var imageHeight = 1063;
	var currentLeft=0;	
	var dimensions;
	var newwidth;
    var newheight;
    var newleft;
    var newtop;	
	var z=1;
	var tabOpen="";
	var webFont="Clarendon LT Std";
	var sectionArr=new Array();
	var sections=new Array("home","news","brasserie","privatedining","cocktaillounge","alfresco","recruitment");
	var bgImages=new Array("/hush/includes/themes/hush/images/backgrounds/home.jpg","/hush/includes/themes/hush/images/backgrounds/wallpaper.jpg","/hush/includes/themes/hush/images/backgrounds/cocktailbar.jpg","/hush/includes/themes/hush/images/backgrounds/privaterooms.jpg","/hush/includes/themes/hush/images/backgrounds/menu.jpg","/hush/includes/themes/hush/images/backgrounds/alfresco.jpg","/hush/includes/themes/hush/images/backgrounds/recruitment.jpg");
	var btns=new Array("menus","signup","contact");
    return {
        init: function () {
			
			jQuery.reject({
				imagePath: '/hush/includes/themes/hush/images/',
				closeESC: true, // Allow closing of window with esc key  
				closeCookie: true, // If cookies should be used to remmember if the window was closed (see cookieSettings for more options)  
				// Cookie settings are only used if closeCookie is true  
				cookieSettings: {  
					path: '/', // Path for the cookie to be saved on (should be root domain in most cases)  
					expires: 0 // Expiration Date (in seconds), 0 (default) means it ends with the current session  
				}  
			}); // Default Settings  
			
            hush.createWebFonts();
			hush.preloadBGImages();
			hush.calcBGImageSize();
			hush.scaleBGImage();
			hush.screenScroller();
			hush.subNavScroller();
			hush.subNavScrollerPrivate();
			hush.mainContent();
			hush.footerBtns();
			hush.navBtns();
			hush.updateDateTime();
			setInterval(hush.updateDateTime, 1000);
			hush.reservations();
			hush.defaultText();
			hush.checkForCompletedSignup();
			hush.checkForCompletedEnquiry();
        },
		checkForCompletedSignup : function() {
			if(hush.gup()["signup"]) {
				$("#btn_signup").click();
			}
		},
		checkForCompletedEnquiry : function() {
			if(hush.gup()["enquiry"]) {
				$("#nav_privatedining a").click();
			}
		},
		createWebFonts : function() {
			Cufon.replace('#navigation a', {hover: true,fontFamily: webFont});
			Cufon.replace('.footer-bg p', {hover: true,fontFamily: webFont});
			Cufon.replace('.switch a', {hover: true,fontFamily: webFont});
			Cufon.replace('.wrapper h2', {hover: true,fontFamily: webFont});
			Cufon.replace('#sub-menu a', {hover: true,fontFamily: webFont});
			Cufon.replace('.tab', {hover: true,fontFamily: webFont});
			Cufon.replace('#sub-menu-private a', {hover: true,fontFamily: webFont});
		},
		preloadBGImages : function() {
			jQuery.imgpreload(bgImages,
			{
				each: function()
				{
					jQuery("#container"+count+" img.bgimage").fadeIn();
					count++;
				}
			});	
		},
		calcWindowSize : function(imgw, imgh) {
			var ratio = imgh / imgw;
			var newheight = 0;
			var newwidth = 0;
			if ((browserheight / browserwidth) > ratio) {
				newheight = browserheight;
				newwidth = Math.round(browserheight / ratio);
			} else {
				newheight = Math.round(browserwidth * ratio);
				newwidth = browserwidth;
			}
			
			var newleft = Math.round((browserwidth - newwidth) / 2);
			var newtop = Math.round((browserheight - newheight) / 2);
			var rcarr = [newwidth, newheight, newleft, newtop];
			return rcarr;
		},
		calcBGImageSize : function() {
			dimensions = hush.calcWindowSize(imageWidth, imageHeight);
			newwidth = dimensions[0];
			newheight = dimensions[1];
			newleft = dimensions[2];
			newtop = dimensions[3];	
			// create sections
			for(var x=0;x<7;x++) {
				sectionArr.push("container"+x);	
			}			
		},
		scaleBGImage : function() {
			browserwidth = $(window).width();
			browserheight = $(window).height();
			dimensions = hush.calcWindowSize(imageWidth, imageHeight);
			newwidth = dimensions[0];
			newheight = dimensions[1];
			newleft = dimensions[2];
			newtop = dimensions[3];	
			var docWidth = $(document).width(); 
			$('div#reservations_bg').css("width",docWidth);
			var docHeight = $(document).height(); 
			$('div#reservations_bg').css("height",docHeight);
			for (var x=0;x<sectionArr.length;x++) {
				$("#"+sectionArr[x]).css('width', newwidth + 'px');
				$("#"+sectionArr[x]).css('height', newheight + 'px');
				$("#"+sectionArr[x]+" img:first").css('width', newwidth + 'px');
				$("#"+sectionArr[x]+" img:first").css('height', newheight + 'px');
				$("#"+sectionArr[x]+" img:first").css('left', 0 + 'px');
				$("#"+sectionArr[x]+" img:first").css('top', 0 + 'px');
				$("#"+sectionArr[x]+" img:first").css('z-index', z);
				currentLeft=currentLeft+newwidth;
				z++;
				$("#"+sectionArr[x]+" div:first").css('z-index', z);
				z++;
			}	
			$(window).bind('resize', function () {
				z=1;
				currentLeft=0;
				hush.scaleBGImage();
			});
		},
		screenScroller : function() {
			$('#screen').serialScroll({
				target:'#sections',
				items:'.subcontainers.active', 
				prev:'img.prev',
				next:'img.next',// Selector to the 'next' button (absolute too)
				axis:'x',// The default is 'y' scroll on both ways
				navigation:'#test li a.slider',
				duration:700,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
				force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
				constant:false, // constant speed
				
				onBefore:function( e, elem, $pane, $items, pos ){ 
					 //those arguments with a $ are jqueryfied, elem isn't.
					e.preventDefault();
					if( this.blur )
						this.blur();
				},
				onAfter: function() {
				document.getElementById('sections').style.display = 'none';
				document.getElementById('sections').style.display = 'block';
					/*var t = document.createTextNode('temp text'); 
					document.getElementById('sections').appendChild(t); 
					setTimeout(function() { t.parentNode.removeChild(t); }, 0); */
				}
			});
		},
		subNavScroller : function() {
			$('#sub-screen').serialScroll({
				target:'#sub-sections',
				items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
				axis:'x',// The default is 'y' scroll on both ways
				navigation:'#sub-navigation li a',
				duration:900,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
				force: false, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
				constant: false, // constant speed
				lock:false, // Ignore events if already animating (true by default)	
				onBefore:function( e, elem, $pane, $items, pos ){
					e.preventDefault();
					
					if( this.blur )
						this.blur();
					
				}
			});	
		},
		subNavScrollerPrivate : function() {
			$('#sub-screen-private').serialScroll({
				target:'#sub-sections-private',
				items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
				axis:'x',// The default is 'y' scroll on both ways
				navigation:'#sub-navigation-private li a',
				duration:900,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
				force: false, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
				constant: false, // constant speed
				lock:false, // Ignore events if already animating (true by default)	
				onBefore:function( e, elem, $pane, $items, pos ){
					if (e.id=='p-text') {
						$('#sub-sections-private form').css("visibility","hidden");
					} 
					e.preventDefault();
					if( this.blur )
						this.blur();
				},
				onAfter:function( e, elem, $pane, $items, pos ) {
					if (e.id=='p-text') {
						$('#sub-sections-private form').css("visibility","hidden");
					} else {
						$('#sub-sections-private form').css("visibility","visible");
					}
				}
			});	
		},
		mainContent : function() {
			$(".panel").css("display","block");
			$("#menus.panel").css("display","none");
			$("#signup.panel").css("display","none");
			$("#contact.panel").css("display","none");
			
			$(".off").click(function () { 	
				for (var x=0;x<sections.length;x++) {
					var nHeight=$('#'+sections[x]).height();
					$('#'+sections[x]).animate({
						top: -nHeight+85
					}, 500, function() {});
				}
				$(".on").fadeIn();
				$(".off").hide();
				return false;
			});
	
			//Turn on and off Button	 		
			$(".on").click(function () { 
				
				$('.footer-bg .icon a').removeClass('selected');
				Cufon.replace('.footer-bg .icon a');
				hush.closeBottomPanel();
				for (var x=0;x<sections.length;x++) {
					var nHeight=$('#'+sections[x]).height();
					$('#'+sections[x]).animate({
						top: 0
					}, 500, function() {});
				}
				$(".off").fadeIn();
				$(".on").hide();
				return false;
			});	
		},
		footerBtns : function() {
			for (var x=0;x<btns.length;x++) {
				$("#btn_"+btns[x]).click(
					function( event ){
						
						var container = $('#'+this.id.split("_")[1]);
						// Prevent the default event.
						event.preventDefault();
		
						// Toggle the slide based on its current
						// visibility.
						if (container.is( ":visible" )){
							$('.footer-bg .icon a').removeClass('selected');
							Cufon.replace('.footer-bg .icon a');
							// Hide - slide up.
							container.slideUp( 500 );
		
						} else {
							hush.closeBottomPanel();
							$('.footer-bg .icon a').removeClass('selected');
							Cufon.replace('.footer-bg .icon a');
							$(this).addClass('selected');
							// Show - slide down.
							container.slideDown( 500 );
							$(".off").click();
						}
					}
				);
			}	
		},
		navBtns : function() {
			$('#sub-navigation li a').click(function () {
				$('#sub-navigation li a').removeClass('selected');
				Cufon.replace('#sub-navigation li a');
				$(this).addClass('selected');
			});
			
			$('#sub-navigation-private li a').click(function () {
				$('#sub-navigation-private li a').removeClass('selected');
				Cufon.replace('#sub-navigation-private li a');
				$(this).addClass('selected');
			});
			
			$('#navigation li a').click(function () {
				if (this.classList!="reservations") {
					$('#navigation li a').removeClass('selected');
					Cufon.replace('#navigation a');
					$(this).addClass('selected');
				}
			});
			$('#logo a').click(function(){
				$('#navigation a.home').click();
				$('#navigation li a').removeClass('selected');	
				$('#navigation a.home').addClass('selected');
				Cufon.replace('#navigation a');	
			});	
		},
		updateDateTime : function() {
		   var currentTime = new Date ( );
		  
		   var currentDay = currentTime.getDay ( );
		   var currentDate = currentTime.getDate ( );
		   var currentMon = currentTime.getMonth ( );
		   var currentYr = currentTime.getFullYear ( );
		   var currentHours = currentTime.getHours ( );
		   var currentMinutes = currentTime.getMinutes ( );
		   var currentSeconds = currentTime.getSeconds ( );
		   var aMonth = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
			
		   // Pad the minutes and seconds with leading zeros, if required
		   currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
		   currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
		   currentHours = ( currentHours < 10 ? "0" : "" ) + currentHours;
		 
		   // Choose either "AM" or "PM" as appropriate
		   var timeOfDay = ( currentHours < 12 ) ? "am" : "pm";
		
		   // Convert the hours component to 12-hour format if needed
		   currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
		   
		   
		   function ordi(n){
			  var s='th';
			  if(n===1 || n==21 || n==31) s='st';
			  if(n===2 || n==22) s='nd';
			  if(n===3 || n==23) s='rd';
			  currentA=s;
		   }
		   ordi(currentDate);
		   /*if ( currentDate == 1 || currentData == 21) {
			   currentA = "st" ;
		   } ;
		   if ( currentDate == 2 || currentData == 22) {
			   currentA = "nd" ;
		   } ;
		   if ( currentDate == 3 ) {
			   currentA = "rd" ;
		   } ;
		   if ( currentDate >= 4 ) {
			   currentA = "th" ;
		   } ;*/
		   // Convert an hours component of "0" to "12"
		   currentHours = ( currentHours == 0 ) ? 12 : currentHours;
		
		   // Compose the string for display
		   var currentTimeString = currentHours + ":" + currentMinutes + timeOfDay;
		   var currentDateString = currentDate + currentA + " " + aMonth[currentMon]
			
		   // Update the time display
		   $('#date-ticker').html(currentDateString);
		   $('#time-ticker').html(currentTimeString);
		   Cufon.replace('#date-ticker');
		   Cufon.replace('#time-ticker');
		},
		reservations : function() {
	
			$("a.reservations, a#btn_reservations").click(function(){
				if ($('#restaurantdiary').attr('src')==null || $('#restaurantdiary').attr('src')=="") {
					$('#restaurantdiary').attr('src', 'http://app.restaurantdiary.com/ts/find.asp?id_prov=3692');
				}
				if($.browser.msie){
					var cssObj = {
						backgroundImage: "none",
						backgroundColor: "black",
						filter: "alpha(opacity=80)"
					}
					$("div#reservations_bg").css(cssObj).fadeIn('slow');
					$("div#reservations_menu").fadeIn('slow');
					return false; 
		
				} else {
					$("div#reservations_bg").fadeIn('slow');
					$("div#reservations_menu").fadeIn('slow');
					return false;
				}
			});
			
			$("#close_reservations").click(function(){
				$("div#reservations_bg").fadeOut('slow');
				$("div#reservations_menu").fadeOut('slow');
				return false; 
			});
			
			$("div#reservations_bg").click(function(){
				$("div#reservations_bg").fadeOut('slow');
				$("div#reservations_menu").fadeOut('slow');
				return false; 
			});
			
			var docWidth = $(document).width(); 
			$('div#reservations_bg').css("width",docWidth);
			var docHeight = $(document).height(); 
			$('div#reservations_bg').css("height",docHeight);
			
			$('div#reservations_menu div a').hover(function() {
				$(this).parent().addClass('hovered');
			}, function() {
				$(this).parent().removeClass('hovered');
			});	
		},
		closeBottomPanel : function() {
			for(var x=0;x<btns.length;x++) {
				if ($('#'+btns[x]).is( ":visible" )) {
					$('.footer-bg .icon a').removeClass('selected');
					Cufon.replace('.footer-bg .icon a');
					$('#'+btns[x]).slideUp( 800 );
				}	
			}	
		},
		defaultText : function() {
			$(".defaultText").focus(function(srcc)
			{
				if ($(this).val() == $(this)[0].title)
				{
					$(this).removeClass("defaultTextActive");
					$(this).val("");
				}
			});
			
			$(".defaultText").blur(function()
			{
				if ($(this).val() == "")
				{
					$(this).addClass("defaultTextActive");
					$(this).val($(this)[0].title);
				}
			});
			
			$(".defaultText").blur();    	
		},
		gup : function() {

			var vars = [], hash;
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			for(var i = 0; i < hashes.length; i++)
			{
				hash = hashes[i].split('=');
				vars.push(hash[0]);
				vars[hash[0]] = hash[1];
			}
			return vars;
	
		}
	}
}();
$(document).ready(hush.init);
