var prevHeight = 760;
var prevHash = '';
var currentHash = '';
var content = '';
var fmail = '';
var agent = navigator.userAgent.toLowerCase();
var subHash = '';

function getURl(hash) {
	//$("header").append(subHash);
	if(!hash || hash == '' || hash == '#home.php') {
		hash = '#home.php';
	}
	
	if(!hash || hash == '' || hash == '#home.php') {
		hash = '#home.php';
	}
	
	if(currentHash != hash) {
	
	$('#blackfade').css({'filter':'alpha(opacity=60)'});
	$('#blackfade').fadeIn(250);
	
	$("#preloader").show(function() {
		$(this).fadeTo(250, 1);	
	});
	
	if(prevHash == '#upcoming_event_list.php' || prevHash == '#concept_event_list.php' || prevHash == '#past_event_list.php' || subHash == '#past_event_detail.php') {
		$("img#bg").fadeOut(100, function() {
			$(this).attr("src", "images/full-bg/bg1.jpg");
		}).fadeIn(100);
	}
	
	$("#main-content .innerpContainer").fadeTo(250, 0, function() {
				
		if(hash == '#upcoming_event_list.php') {
			$(this).wrap("<div class='innerpEvents' style='opacity:0;' />");
			$(".innerpEvents").fadeTo(500, 1);
		}
		$("body").removeClass().addClass(hash.substring(1).replace(".php", ""));
	
		$(this).load(hash.substring(1), function() {
			
			if(currentHash == '#upcoming_event_list.php' || currentHash == '#concept_event_list.php' || currentHash == '#past_event_list.php' || subHash == '#past_event_detail.php') {
				$("img#bg").fadeOut(100, function() {
					$(this).attr("src", "images/full-bg/bg2.jpg");
				}).fadeIn(100);
			}
			
			$(this).addClass("clearfix");
			
			prevHeight = $(this).height();
			
			$("#preloader").fadeTo(250, 0, function() {
				$(this).hide();
				if(currentHash != '#home.php' || currentHash != '#mobile.php') {
					$(".innerpContainer").height($(".innerpContainer .pages").outerHeight());
				}
				$('#blackfade').fadeOut(250);
					
				$("#main-content .innerpContainer").fadeTo(500, 1, function() {
					if(hash == '#home.php' || hash == '#mobile.php') {
						$('#blackfade').css({'filter':'alpha(opacity=0)'});
						$('#blackfade').fadeOut(250);
					}
				});
			
			});
			// ALL JA FOR INNER PAGES SHOULD GO HERE
			prevHash = hash;
			if(currentHash == '#home.php') {
				$("#flash_wrapper").height($(window).width() / 2.41);
			}
			if(currentHash == '#newsletter_form.php') {
				$("#newsletterform input#emailAddress").val(fmail);
			}
			document.title = "BUSHIDO RESTAURANT AND LOUNGE | JAPANESE RESTAURANT | SUSHI BAR | KINGDOM OF BAHRAIN";
			
		}); // END OF THIS LOAD
	});
	currentHash = hash;
	}
}

$(function() {
	
	currURL = window.location.href;
	if(!window.location.hash && currURL.substr(currURL.lastIndexOf(".")) == ".php") {
		var redirect = currURL.substr(currURL.lastIndexOf("/")+1);
		var newURL = currURL.replace(redirect, "#"+redirect);
		window.location = newURL;
	}
	
	$("#preloader").width($(window).width());
	
	
	///////////////////////
	// HASH HANDLER
	///////////////////////
	
	$(window).hashchange(function() {
		var newHash = location.hash;
		var pos = newHash.lastIndexOf(".php");
		subHash = newHash.substr(0, pos+4);
		getURl(newHash);
	});
	$(window).hashchange();
	

	///////////////////////
	// BACKGROUND IMAGE
	///////////////////////
	var theWindow        = $(window),
		$bg              = $("img#bg"),
		aspectRatio      = $bg.width() / $bg.height();
	function resizeBg() {
		if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
			$bg.removeClass().addClass('bgheight');
		} else {
			$bg.removeClass().addClass('bgwidth');
		}
	}
	theWindow.resize(function() {
		resizeBg();
	}).trigger("resize");
	
	
	///////////////////////
	//Footer
	///////////////////////
	$('#minimize_footer').click(function()
	{
		if($('#showhidfooter').is(':hidden'))
		{
			$('#minimize_footer').attr('src','images/footer-close.png');
			scrollTopVal= $(window).scrollTop();
			$('#showhidfooter').slideDown(function(){  });
			//alert(scrollTopVal);
			$('html,body').animate({scrollTop: scrollTopVal+220 }, 300);
		   
		}
		else
		{
			$('#minimize_footer').attr('src','images/footer-open.png');
			$('#showhidfooter').slideUp();
		}
	   
	});
	
	$('ul.sf-menu').superfish({ animation: {height:'show'}});
});

function checkFMail(footmail) {
	var fmailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(!footmail) {
		alert('Please Enter your Email ID');
		return false;
	}
	if(!fmailReg.test(footmail)) {
		alert('Please Enter a Correct Email Address');
		return false;
	}
	else {
		return true;
	}
}
// FOOTER FORM
function passMail() {
	if(checkFMail($("input#textfield").fieldValue())) {
		fmail = $("input#textfield").fieldValue();
		window.location = "#newsletter_form.php";
		scrollPageUp();
	}
}

function changeYear() {
	var year = $("#event_selector #year").val();
	window.location = "#past_event_list.php?year="+year;
}

function changeEvent() {
	var event_id = $("#event_selector #event").val();
	var year = $("#event_selector #year").val();
	window.location = "#past_event_detail.php?selEventID="+event_id+"&year="+year;
}

function scrollPageUp() {
	$('html,body').animate({
	scrollTop: $("#page-wrap").offset().top
	}, 750);
}

$(document).ready(function() {
	$("nav.menu ul ul a").click(function() {
		$(this).parent().parent().hide();
	});
});


