PreLoad();
$(PageLoaded);

function PreLoad() {
	if (self.document.location.hash) {
		if(typeof LoadedPage != "undefined")
			window.location = "./" + self.document.location.hash;
		else
			$("<style type='text/css'> #MainFrame { height:0; display:block; } </style>").appendTo("head");
	}
}

function PageLoaded() {
	InitNavigation();
	InitAnalytics();
	
	$("#Logo a, #Links a, #SubLinks a, #Footer a").click(function() {
	  $(this).blur();
	});
	
	$("#SubscribeLink").click(function(e) {
		e.preventDefault();
		$("body").css("overflow","hidden");
		$("#SignupForm").dialog({
			modal:true, 
			title:"HWF Email/MMS Signup",
			height:322,
			width:250,
			close:function() { $("body").css("overflow","auto"); }});
	});
	
	if (self.document.location.hash && self.document.location.hash.length > 1)
		LoadContent(self.document.location.hash.substring(1))
	else {
		$("#MainFrame").css({height:$("#MainFrameLoader").height()});
		FixAjaxContent();
		$("#AjaxLoader").fadeOut('fast');
	}
	
	fo = new FlashObject("music/Playa.swf", "Mp3Playa", "300", "40", "8", "#ffffff");
	fo.addParam("wmode", "transparent");
	fo.write("Mp3Player");
	
	WindowResized();
	
	$("#Mp3Player")
		.hover(PlayerMouseOver, PlayerMouseOut)
		.show();
	TempShowPlayer();
	
	
	$(window).resize( WindowResized );

}

function WindowResized() {
	$("#Mp3Player").css("left",($(window).width() > 915?$(window).width()/2-457:0) + 307 )
}


var PlayerShowing = false;
var PlayerAnimating = false;
function  PlayUrl(Url){
	getFlashMovieObject("Mp3Playa").PlayURL(Url);
}
function  ShowPlayer(){
	if (MouseWatchmanTimer) {
		MouseWatchmanTimer.stop();
		MouseWatchmanTimer = null;
	}
	if (!PlayerShowing && !PlayerAnimating) {
		PlayerAnimating = true;
		$("#Mp3Player").animate(
			{top: 0},
			{duration: 1000,
			easing:Easing,
			complete: function() {PlayerShowing = true;PlayerAnimating = false;}}
		);
	}
}

function HidePlayer() {
	if (MouseWatchmanTimer) {
		MouseWatchmanTimer.stop();
		MouseWatchmanTimer = null;
	}
	if (PlayerShowing && !PlayerAnimating) {
		PlayerAnimating = true;
		$("#Mp3Player").animate(
			{top: "-30px"},
			{duration: 1000,
			easing:Easing,
			complete: function() {PlayerShowing = false;PlayerAnimating = false;}}
		);
	}
}
var MouseWatchmanTimer;
function PlayerMouseOver() {
	ShowPlayer();
}
function TempShowPlayer() {
	ShowPlayer();
	MouseWatchmanTimer = $.timer(10000,HidePlayer)
}
function PlayerMouseOut() {
	if (MouseWatchmanTimer)
		MouseWatchmanTimer.stop();
	MouseWatchmanTimer = $.timer(10000,HidePlayer)
}

function FixAjaxContent() {
	$("#MainFrameLoader a")
		.click(function() {
		  $(this).blur();
		})
		.not(":has(img)").addClass("TextLink");
	$("#SubmitButton").button();
}

function NoFollow() {
	if(window.event) event.returnValue=false; return false;
}

function SetPageTitle(PageTitle) {
	document.title = "Houston Wine Fest - September 24-25th - " + PageTitle;
}

function FormMail(SuccessMessage) {
	$('#SubmitButton').attr("disabled", "disabled");
	$('#SubmitButton').val("Please Wait...");
	var fdata = $("#FormMailer").serialize();
	fdata.t = (new Date()).getTime();
	$.post("scripts/FormMail.php", fdata, function(data) { if (data.indexOf("SUCCESSFUL") == -1) { alert(data); $('#SubmitButton').removeAttr("disabled");$('#SubmitButton').val("Submit"); } else ContentResize(function () {$("#FormMailer").html(SuccessMessage);}); });
}

var AnimatingContent = false;
var MenHeight = 0;
var MenDuration = 1500;
var Easing = 'quadEaseOut';
function LoadContent(Page) {
	if (AnimatingContent)
		return NoFollow();
	if(typeof LoadedPage != "undefined") {
		if (Page == null)
			window.location = "./";
		else
			window.location = "./#" + Page;
	}
	AnimatingContent = true;
	
	if(typeof DeInit != "undefined" && DeInit) {
		DeInit();
		DeInit = null;
	}
	
	var Params = null;
	if (Page == null) {
		Page = "index";
	}
	if (Page.indexOf('?') != -1) {
		Params = Page.substr(Page.indexOf('?')+1);
		Params += (Params == ""?"":"&") + "t=" + (new Date()).getTime();
		Page = Page.substr(0,Page.indexOf('?'));
	}
	$("#MainFrameLoader").fadeOut('fast', function() {
		$("#AjaxLoader:hidden").fadeIn('fast');
		$('#MainFrameLoader').load('pages/' + Page + '.php', Params, function(Resp, Status) {
			ShowContent(Math.max(237,$("#MainFrameLoader").height()));
	
			if (Status != 'error') {
				FixAjaxContent();
				if (Page != "index") 
					self.document.location.hash = Page;
				else
					self.document.location.hash = "";
			}
		});
	});
	
	return NoFollow();
}

function ContentResize(ChangeDataCallback) {
	$("#MainFrameLoader").fadeOut('fast', function() {
		$("#AjaxLoader:hidden").fadeIn('fast');
		ChangeDataCallback();
		FixAjaxContent()
		$("#MainFrameLoader").css("height","");
		ShowContent(Math.max(237,$("#MainFrameLoader").height()));
	});
}

function ShowContent(ContentHeight) {
	$("#MainFrame").animate({height:ContentHeight},
			{duration: MenDuration,
			easing:Easing,
			complete: function() {AnimatingContent = false;$("#AjaxLoader").fadeOut('fast');$("#MainFrameLoader").fadeIn('fast', RunPostLoad)}});
}

function RunPostLoad() {
	if(typeof PostLoad != "undefined" && PostLoad) {
		$(PostLoad);
		PostLoad = null;
	}
}


function Over(dis) {(dis.src?dis:this).src = (dis.src?dis:this).src.replace(".png","H.png");}
function Out(dis) {(dis.src?dis:this).src = (dis.src?dis:this).src.replace("H.png",".png");}

function InitNavigation() {
	$("div#TopLinks a img, div#Links a img, div#SubLinks div div a img").hover(Over, Out);
	
	//$("#PurchaseButton").hover(ShowPurchaseMenu, HidePurchaseMenu);
	//$("#PurchaseSlideOut").hover(ShowPurchaseMenu, HidePurchaseMenu);
	$("#EntertainmentButton").hover(ShowEntMenu, HideEntMenu);
	$("#EntSlideOut").hover(ShowEntMenu, HideEntMenu);
	$("#AboutUsButton").hover(ShowAboutMenu, HideAboutMenu);
	$("#AboutSlideOut").hover(ShowAboutMenu, HideAboutMenu);	
}
function ShowPurchaseMenu() {
	$("#PurchaseSlideOuter").show().css('z-index',12);
	$("#PurchaseSlideOut").stop().animate({ 'top': 0}, 400, 'bounceEaseOut');
}
function HidePurchaseMenu() {
	$("#PurchaseSlideOuter").css('z-index',11);
	$("#PurchaseSlideOut").stop().animate({ 'top': "-52px"}, 400, 'cubicEaseIn', function() { $("#PurchaseSlideOuter").hide();});
}
function ShowEntMenu() {
	$("#EntSlideOuter").show().css('z-index',12);
	$("#EntSlideOut").stop().animate({ 'top': 0}, 400, 'bounceEaseOut');
}
function HideEntMenu() {
	$("#EntSlideOuter").css('z-index',11);
	$("#EntSlideOut").stop().animate({ 'top': "-76px"}, 400, 'cubicEaseIn', function() { $("#EntSlideOuter").hide();});
}
function ShowAboutMenu() {
	$("#AboutSlideOuter").show().css('z-index',12);
	$("#AboutSlideOut").stop().animate({ 'top': 0}, 600, 'bounceEaseOut');
}
function HideAboutMenu() {
	$("#AboutSlideOuter").css('z-index',11);
	$("#AboutSlideOut").stop().animate({'top': "-148px"}, 600, 'cubicEaseIn', function() { $("#AboutSlideOuter").hide();});
}

var _gaq = _gaq || [];
function InitAnalytics() {
  _gaq.push(['_setAccount', 'UA-17237971-4']);
  
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
}

function TrackPage(Url) {
	if (Url)
		_gaq.push(['_trackPageview', Url]);
	else
  		_gaq.push(['_trackPageview']);	
}

oldAlert = alert;

function alert(Message,Title) {

	if (!$("#jAlert").length)
		$("body").append('<div id="jAlert"></div>');

	var jAlert = $("#jAlert");

	jAlert.html(Message);

	$("body").css("overflow","hidden");
	if (!jAlert.hasClass('ui-dialog-content')) {
		jAlert.dialog({
			title: (Title?Title:'Alert!'),
			autoOpen: false,
			modal: true,
			resizable: false,
			draggable: false,
			buttons: {"OK": function () { $(this).dialog("close"); }},
			close:function() { $("body").css("overflow","auto"); }
		});
	}

	jAlert.dialog('open');

	return undefined;
}
