$(document).ready(function() {
	//Get our elements for faster access and set overlay width
	var div = $('div.sc_menu'),
		ul = $('ul.sc_menu'),
		ulPadding = 15;
	//Get menu width
	var divWidth = div.width();
	//Remove scrollbars	
	div.css({overflow: 'hidden'});
	//Find last image container
	var lastLi = ul.find('li:last-child');
	//When user move mouse over menu
	div.mousemove(function(e){
		//As images are loaded ul width increases,
		//so we recalculate it each time
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;	
		var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
		div.scrollLeft(left);
	});
	// preload images
	var img = ['cancel.png', 'form_bottom.png', 'form_middle.png', 'form_top.png', 'loading.gif', 'send.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = '../img/' + this;
	});
	
	var contact = {
			message: null,
			open: function (dialog) {
				// add padding to the buttons in firefox/mozilla
				if ($.browser.mozilla) {
					$('#contact-container .contact-button').css({
						'padding-bottom': '2px'
					});
				}
				// input field font size
				if ($.browser.safari) {
					$('#contact-container .contact-input').css({
						'font-size': '.9em'
					});
				}

				// dynamically determine height
				var h = 280;
				if ($('#contact-subject').length) {
					h += 26;
				}
				if ($('#contact-cc').length) {
					h += 22;
				}

				var title = $('#contact-container .contact-title').html();
				$('#contact-container .contact-title').html('Pouring...');
				dialog.overlay.fadeIn(200, function () {
					dialog.container.fadeIn(200, function () {
						dialog.data.fadeIn(200, function () {
							$('#contact-container .contact-content').animate({
								height: h
							}, function () {
								$('#contact-container .contact-title').html(title);
								$('#contact-container form').fadeIn(200, function () {
									$('#contact-container #contact-name').focus();

									$('#contact-container .contact-cc').click(function () {
										var cc = $('#contact-container #contact-cc');
										cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
									});

									// fix png's for IE 6
									if ($.browser.msie && $.browser.version < 7) {
										$('#contact-container .contact-button').each(function () {
											if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
												var src = RegExp.$1;
												$(this).css({
													backgroundImage: 'none',
													filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
												});
											}
										});
									}
								});
							});
						});
					});
				});
			},
			show: function (dialog) {
				$('#contact-container .contact-send').click(function (e) {
					e.preventDefault();
					// validate form
					if (contact.validate()) {
						$('#contact-container .contact-message').fadeOut(function () {
							$('#contact-container .contact-message').removeClass('contact-error').empty();
						});
						$('#contact-container .contact-title').html('Serving...');
						$('#contact-container form').fadeOut(200);
						$('#contact-container .contact-content').animate({
							height: '80px'
						}, function () {
							$('#contact-container .contact-loading').fadeIn(200, function () {
								$.ajax({
									url: 'bin/contact.php',
									data: $('#contact-container form').serialize() + '&action=send',
									type: 'post',
									cache: false,
									dataType: 'html',
									complete: function (xhr) {
										$('#contact-container .contact-loading').fadeOut(200, function () {
											$('#contact-container .contact-title').html('Thank you!');
											$('#contact-container .contact-message').html(xhr.responseText).fadeIn(200);
										});
									},
									error: contact.error
								});
							});
						});
					}
					else {
						if ($('#contact-container .contact-message:visible').length > 0) {
							var msg = $('#contact-container .contact-message div');
							msg.fadeOut(200, function () {
								msg.empty();
								contact.showError();
								msg.fadeIn(200);
							});
						}
						else {
							$('#contact-container .contact-message').animate({
								height: '30px'
							}, contact.showError);
						}
						
					}
				});
			},
			close: function (dialog) {
				$('#contact-container .contact-message').fadeOut();
				$('#contact-container .contact-title').html('All Gone...');
				$('#contact-container form').fadeOut(200);
				$('#contact-container .contact-content').animate({
					height: 40
				}, function () {
					dialog.data.fadeOut(200, function () {
						dialog.container.fadeOut(200, function () {
							dialog.overlay.fadeOut(200, function () {
								$.modal.close();
							});
						});
					});
				});
			},
			error: function (xhr) {
				alert(xhr.statusText);
			},
			validate: function () {
				contact.message = '';
				if (!$('#contact-container #contact-name').val()) {
					contact.message += 'Name is required. ';
				}

				var email = $('#contact-container #contact-email').val();
				if (!email) {
					contact.message += 'Email is required. ';
				}
				else {
					if (!contact.validateEmail(email)) {
						contact.message += 'Email is invalid. ';
					}
				}

				if (!$('#contact-container #contact-message').val()) {
					contact.message += 'Message is required.';
				}

				if (contact.message.length > 0) {
					return false;
				}
				else {
					return true;
				}
			},
			validateEmail: function (email) {
				var at = email.lastIndexOf("@");

				// Make sure the at (@) sybmol exists and  
				// it is not the first or last character
				if (at < 1 || (at + 1) === email.length)
					return false;

				// Make sure there aren't multiple periods together
				if (/(\.{2,})/.test(email))
					return false;

				// Break up the local and domain portions
				var local = email.substring(0, at);
				var domain = email.substring(at + 1);

				// Check lengths
				if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
					return false;

				// Make sure local and domain don't start with or end with a period
				if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
					return false;

				// Check for quoted-string addresses
				// Since almost anything is allowed in a quoted-string address,
				// we're just going to let them go through
				if (!/^"(.+)"$/.test(local)) {
					// It's a dot-string address...check for valid characters
					if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
						return false;
				}

				// Make sure domain contains only valid characters and at least one period
				if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
					return false;	

				return true;
			},
			showError: function () {
				$('#contact-container .contact-message')
					.html($('<div class="contact-error">').append(contact.message))
					.fadeIn(200);
			}
		};
	
	var queryhash = window.location.hash
	switch (queryhash) {
		case "#main":
			document.title = "St Louis Beer Club - Welcome Home";
			initialShowMain();
			break;
		case "#podcast":
			document.title = "St Louis Beer Club - Vidcast / Podcast";
			initialShowPodcast();
			break;
		case "#networks":
			document.title = "St Louis Beer Club - Social Network";
			initialShowNetworks();
			break;
		case "#events":
			document.title = "St Louis Beer Club - Upcoming Events";
			initialShowEvents();
			break;
		case "#sponsors":
			document.title = "St Louis Beer Club - Sponsors";
			initialShowSponsors();
			break;
		case "#about":
			document.title = "St Louis Beer Club - About / Crew";
			initialShowAbout();
			break;
		case "#live":
			document.title = "St Louis Beer Club - LIVE!";
			initialShowLive();
			break;
		default:
			initialShowMain();
			break;
	}
	
	$("h2").hide();
	/*	//When mouse rolls over
	$("li.drop").mouseover(function(){
		$(this).stop().animate({height:'150px'},{queue:false, duration:600, easing: 'easeInOutExpo'})
	});
	//When mouse is removed
	$("li.drop").mouseout(function(){
		$(this).stop().animate({height:'30px'},{queue:false, duration:600, easing: 'easeInOutExpo'})
	});*/
	
	var fflink = $("div#fflink"),
            fflinkimg = $("div#fflink img"),
            follow = $("div#fflink div.follow"),
            firefox = $("div#fflink div.firefox"),
			rssverimg = $("div#rss_wrapper img.rssverify")
			openopt = 700, 
            closeopt = 400,
        	i = 0;
			
	firefox.hide().css({width:"0px", opacity: 0.0});
	
	fflink.show().mouseenter(function(){
		firefox.show().animate({width: "280px", opacity: 1.0}, openopt);
		follow.animate({width: "0px"}, openopt);
		fflinkimg.animate({right: "280px", opacity: 0.0} , openopt);
	}).mouseleave(function(){setTimeout( function(){
		firefox.animate({width: "0px", opacity: 0.0}, closeopt);
		follow.animate({width: "280px"}, closeopt);
		fflinkimg.animate({right: "0px", opacity: 1.0}, closeopt);}, 1000);
	}).click(function(){
		location.href = 'http://www.mozilla.com';
	});
	
	var ielink = $("div#ielink"),
            ielinkimg = $("div#ielink img"),
            iefollow = $("div#ielink div.iefollow"),
            iexplorer = $("div#ielink div.iexplorer"),
			rssverimg = $("div#rss_wrapper img.rssverify")
			
	iexplorer.hide().css({width:"0px", opacity: 0.0});
	
	ielink.show().mouseenter(function(){
		iexplorer.show().animate({width: "280px", opacity: 1.0}, openopt);
		iefollow.animate({width: "0px"}, openopt);
		ielinkimg.animate({right: "280px", opacity: 0.0} , openopt);
	}).mouseleave(function(){setTimeout( function(){
		iexplorer.animate({width: "0px", opacity: 0.0}, closeopt);
		iefollow.animate({width: "280px"}, closeopt);
		ielinkimg.animate({right: "0px", opacity: 1.0}, closeopt);}, 1000);
	}).click(function(){
		location.href = 'http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx';
	});
	
     var rsshi = $("div#rsshi"),
         rsshiimg = $("div#rsshi img"),
         blnk1 = $("div#rsshi div.rsshi"),
         rss1 = $("div#rsshi div.rss1");
			
	rss1.hide().css({height:"0px", opacity: 0.0});
	
	rsshi.stop().mouseenter(function(){
		rss1.show().animate({height: "280px", opacity: 1.0}, openopt);
		blnk1.animate({height: "0px"}, openopt);
		rsshiimg.animate({top: "220px", opacity: 0.0} , openopt);
		rssverimg.fadeOut("medium");
	}).mouseleave(function(){setTimeout( function(){
		rss1.animate({height: "0px", opacity: 0.0}, closeopt);
		blnk1.animate({height: "0px"}, closeopt);
		rsshiimg.animate({top: "0px", opacity: 1.0}, closeopt);
		rssverimg.fadeIn("fast");}, 1000);
	}).click(function(){
		location.href = 'http://beerclubstl.com/rsshi.xml';
	});
	
	 var rsslo = $("div#rsslo"),
         rssloimg = $("div#rsslo img"),
         blnk2 = $("div#rsslo div.rsslo"),
         rss2 = $("div#rsslo div.rss2");
			
	rss2.hide().css({height:"0px", opacity: 0.0});
	
	rsslo.show().mouseenter(function(){
		rss2.show().animate({height: "280px", opacity: 1.0}, openopt);
		blnk2.animate({height: "0px"}, openopt);
		rssloimg.animate({top: "220px", opacity: 0.0} , openopt);
		rssverimg.fadeOut("medium");
	}).mouseleave(function(){setTimeout( function(){
		rss2.animate({height: "0px", opacity: 0.0}, closeopt);
		blnk2.animate({height: "0px"}, closeopt);
		rssloimg.animate({top: "0px", opacity: 1.0}, closeopt);
		rssverimg.fadeIn("fast");}, 1000);
	}).click(function(){
		location.href = 'http://beerclubstl.com/rsslo.xml';
	});
	
	
	 var rssreg = $("div#rssreg"),
         rssregimg = $("div#rssreg img"),
         blnk3 = $("div#rssreg div.rssreg"),
         rss3 = $("div#rssreg div.rss3");
			
	rss3.hide().css({height:"0px", opacity: 0.0});
	
	rssreg.show().mouseenter(function(){
		rss3.show().animate({height: "280px", opacity: 1.0}, openopt);
		blnk3.animate({height: "0px"}, openopt);
		rssregimg.animate({top: "220px", opacity: 0.0} , openopt);
		rssverimg.fadeOut("medium");
	}).mouseleave(function(){setTimeout( function(){
		rss3.animate({height: "0px", opacity: 0.0}, closeopt);
		blnk3.animate({height: "0px"}, closeopt);
		rssregimg.animate({top: "0px", opacity: 1.0}, closeopt);
		rssverimg.fadeIn("fast");}, 1000);
	}).click(function(){
		location.href = 'http://beerclubstl.com/rss.xml';
	});
	
	$("#vcard a").hover(showVcardLabel, hideVcardLabel);
	$("#vcard2 a").hover(showVcard2Label, hideVcard2Label);
	$("#nav-main a").click(showMain);
	$("#nav-podcast a").click(showPodcast);
	$("#nav-networks a").click(showNetworks);
	$("#nav-events a").click(showEvents);
	$("#nav-sponsors a").click(showSponsors);
	$("#nav-about a").click(showAbout);
	$("#nav-live a").click(showLive);
	$("#footad a.chill").click(showPodcast);
	$("textarea#contact-message").autogrow({
		maxHeight: 100,
		minHeight: 40,
		lineHeight: 20
	});
	$('#gallery a').lightBox({
		fixedNavigation: true,
		containerResizeSpeed: 350,
	});
	
	$('#header input.contact, #header a.contact').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("bin/contact.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				position: ["15%",],
				overlayId: 'contact-overlay',
				containerId: 'contact-container',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});
	});
	
	$f("a.player", "bin/flowplayer-3.0.7.swf", {
		// perform custom stuff before default click action
		onBeforeClick: function() {
			// unload previously loaded player
			$f().unload();
			// get wrapper element as jQuery object
			var wrap = $(this.getParent());
			// hide nested play button
			wrap.find("img").fadeOut();
			// start growing animation
			wrap.animate({width:597, height:350}, 500, function() {
			// when animation finishes we will load our player
				$f(this).load();
			});	
			// disable default click behaviour (player loading)
			return false; 
		}, 
		
		// unload action resumes to original state		
		onUnload: function() {
			$(this.getParent()).animate({width:190, height:150}, 500, function()  {
				// make play button visible again
				$(this).find("img").fadeIn();		
			});				
		}, 
	
		// when playback finishes perform our custom unload action
		onFinish: function() {
			this.unload();	
		}
	});});

/* START */
function showVcardLabel() {
	$("#vcard a span").show();
	$("#vcard a span").animate({
		top: "-40px",
		opacity: 1
	}, 250 );
}

function hideVcardLabel() {
	$("#vcard a span").animate({ 
		top: "-35px",
		opacity: 0
	}, 250 );
	setTimeout("$('#vcard a span').hide();", 250);
	$("#vcard a span").animate({ 
		top: "-45px",
	}, 250 );
}
/* End  */

/* START */
function showVcard2Label() {
	$("#vcard2 a span").show();
	$("#vcard2 a span").animate({
		top: "-40px",
		opacity: 1
	}, 250 );
}

function hideVcard2Label() {
	$("#vcard2 a span").animate({ 
		top: "-35px",
		opacity: 0
	}, 250 );
	setTimeout("$('#vcard2 a span').hide();", 250);
	$("#vcard2 a span").animate({ 
		top: "-45px",
	}, 250 );
}
/* End  */

/* Function Init Start */
function initialShowMain() {
	$("#panels").hide();
	$("#meybers").removeClass();
	$("#meybers").addClass("main");
	$(".node").hide();
	$("#main").show();
	setTimeout("$('#panels').slideDown('slow');", 1000);
}

function initialShowPodcast() {
	$("#panels").hide();
	$("#meybers").removeClass();
	$("#meybers").addClass("podcast");
	$(".node").hide();
	$("#podcast").show();
	setTimeout("$('#panels').slideDown('slow');", 1000);
}

function initialShowNetworks() {
	$("#panels").hide();
	$("#meybers").removeClass();
	$("#meybers").addClass("networks");
	$(".node").hide();
	$("#networks").show();
	setTimeout("$('#panels').slideDown('slow');", 1000);
}

function initialShowEvents() {
	$("#panels").hide();
	$("#meybers").removeClass();
	$("#meybers").addClass("events");
	$(".node").hide();
	$("#events").show();
	setTimeout("$('#panels').slideDown('slow');", 1000);
}

function initialShowSponsors() {
	$("#panels").hide();
	$("#meybers").removeClass();
	$("#meybers").addClass("sponsors");
	$(".node").hide();
	$("#sponsors").show();
	setTimeout("$('#panels').slideDown('slow');", 1000);
}

function initialShowAbout() {
	$("#panels").hide();
	$("#meybers").removeClass();
	$("#meybers").addClass("about");
	$(".node").hide();
	$("#about").show();
	setTimeout("$('#panels').slideDown('slow');", 1000);
}

function initialShowLive() {
	$("#panels").hide();
	$("#meybers").removeClass();
	$("#meybers").addClass("live");
	$(".node").hide();
	$("#live").show();
	setTimeout("$('#panels').slideDown('slow');", 1000);
}
/* Function Init End */

/* Function Show Start */

function showMain() {
	if ($("#meybers").hasClass("main")){ }
	else {
		document.title = "St Louis Beer Club - Welcome Home";
		$("#panels").slideUp(500);
		$(".node").fadeOut(500);
		$("#footad").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#main').show();", 500);
		$("#footad").fadeIn(500);
		$("#panels").slideDown(500);
		$("#meybers").removeClass();
		$("#meybers").addClass("main");
	}
}

function showPodcast() {
	if ($("#meybers").hasClass("podcast")){ }
	else {
		document.title = "St Louis Beer Club - Vidcast / Podcast";
		$("#panels").slideUp(500);
		$(".node").fadeOut(500);
		$("#footad").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#podcast').show();", 500);
		$("#footad").fadeIn(500);
		$("#panels").slideDown(500);
		$("#meybers").removeClass();
		$("#meybers").addClass("podcast");
	}
}

function showNetworks() {
	if ($("#meybers").hasClass("networks")){ }
	else {
		document.title = "St Louis Beer Club - Social Network";
		$("#panels").slideUp(500);
		$(".node").fadeOut(500);
		$("#footad").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#networks').show();", 500);
		$("#footad").fadeIn(500);
		$("#panels").slideDown(500);
		$("#meybers").removeClass();
		$("#meybers").addClass("networks");
	}
}

function showEvents() {
	if ($("#meybers").hasClass("events")){ }
	else {
		document.title = "St Louis Beer Club - Upcoming Events";
		$("#panels").slideUp(500);
		$(".node").fadeOut(500);
		$("#footad").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#events').show();", 500);
		$("#footad").fadeIn(500);
		$("#panels").slideDown(500);
		$("#meybers").removeClass();
		$("#meybers").addClass("events");
	}
}

function showSponsors() {
	if ($("#meybers").hasClass("sponsors")){ }
	else {
		$("#panels").slideUp(500);
		$(".node").fadeOut(500);
		$("#footad").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#sponsors').show();", 500);
		$("#footad").fadeIn(500);
		$("#panels").slideDown(500);
		$("#meybers").removeClass();
		$("#meybers").addClass("sponsors");
		document.title = "St Louis Beer Club - Sponsors";
	}
}

function showAbout() {
	if ($("#meybers").hasClass("about")){ }
	else {
		$("#panels").slideUp(500);
		$(".node").fadeOut(500);
		$("#footad").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#about').show();", 500);
		$("#footad").fadeIn(500);
		$("#panels").slideDown(500);
		$("#meybers").removeClass();
		$("#meybers").addClass("about");
		document.title = "St Louis Beer Club - About / Crew";
	}
}

function showLive() {
	if ($("#meybers").hasClass("live")){ }
	else {
		$("#panels").slideUp(500);
		$(".node").fadeOut(500);
		$("#footad").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#live').show();", 500);
		$("#footad").fadeIn(500);
		$("#panels").slideDown(500);
		$("#meybers").removeClass();
		$("#meybers").addClass("live");
		document.title = "St Louis Beer Club - LIVE!";
	}
}
/* Function Show End */