$(function() {


	var tempTarget;
	var originalWidth;
	var agegate = "/agegate";  
	var modalCookie;
	
	//set body width and update on window resize

	var currentBodyWidth = $("body").width();
	
	//store default width of hover boxes
	
	var initializeSlideLinks = function() {
	
		$('.title').each(function() {
			$(this).data('defaultWidth', $(this).width());
		 });
		 
	};
	
	// menu expansion
	
	menuExpand = function() {
		$(".primary_menu").stop().animate({width:'160px'},300, function() {
			$(".sub_menu").show("slow");
		});
	}
	
	menuCollapse = function() {
		$(".primary_menu").stop().animate({width:'130px'},300, function() {
			$(".sub_menu").hide("slow");
		});		
	}
	
	//expand menu for touch screens
	tabletMenuExpand = function() {

		$(".primary_menu").stop().animate({width:'160px'},300, function() {
			$(".sub_menu").show("slow");
		});
		
	};
	
	$(".no-touch nav .primary_menu ").mouseenter(function() {
		menuExpand();
	});
	
	$(".no-touch header").mouseleave(function() {
		menuCollapse();
	});	
	
	//expand menus on touch screens
	$(".touch nav .primary_menu a").click(function() {
	
		if (!$(".primary_menu").hasClass("expanded")) {
			$(".primary_menu").addClass("expanded");	
		tabletMenuExpand();			
			return false;
		} else {
			return true;
		}			

	});	
	
	//test for cookie.  if not present, redirect to agegate
	
	var value = $.readCookie('diageoagecheck');
	
	if( value ) {
		var locale = $.readCookie('region')
		$('body').addClass('region-'+locale);
	}
	else {
		$(location).attr('href',agegate);
	}
	
	// run initialization  for slide links on page load
	initializeSlideLinks();
	 
	//build hover states for boxes throughout the site
	var navSlideWidth;
	$(".expando_box").live('mouseenter', function(){
		titleTarget =$(this).children('.title');	
		$(titleTarget).stop().removeAttr('style');	
		$(titleTarget).addClass("expanded");
		$(titleTarget).stop().animate({width:'100%'},300);
	}).live('mouseleave',function(){
			var revertSize = $(this).children('.title').data('defaultWidth');
			titleTarget =$(this).children('.title');
			$(titleTarget).animate({width: revertSize},300);
			$(titleTarget).removeClass("expanded");
	});
	
	$(".expando_box").click(function() {
		var expandoLink = $(this).find("a").attr("href");
		window.location= expandoLink;
	});
	
	/* pager links population and interaction */
	
	populatePagerTips = function(tempTarget) {
		var hoverText = $(tempTarget).attr("title");
		$(tempTarget).data('tempTitle', hoverText);
		$(tempTarget).attr("title","");
		var hoverTargetPos = $(tempTarget).position();
		var hoverPlacementLeft = hoverTargetPos.left - 22 + "px";
		var hoverPlacementtop = hoverTargetPos.top + 25 + "px";
		$(".pager_hover_text").fadeIn(300);
		$(".pager_hover_text").css("left", hoverPlacementLeft);		
		$(".pager_hover_text").css("top", hoverPlacementtop);				
		$(".pager_hover_text #hover_text_content").html(hoverText);
	}
	
	killPagerTips = function(tempTarget) {
	
		$(tempTarget).attr("title", tempTarget.data('tempTitle') );
		$(".pager_hover_text").hide();
	}
	
	$(".link").mouseover(function() {
	 tempTarget = $(this);
	 populatePagerTips(tempTarget);
	});
	
	$(".link").mouseout(function() {
		tempTarget = $(this);	
		killPagerTips(tempTarget);
	});
	
	//vertically center the main wrapper
	
	function CenterItem(vertDiv){
	
		if (navigator.appVersion.indexOf("MSIE 7.") != -1) {
		} else {
			var winHeight=$(window).height();
			var windowMiddle=winHeight/2;
			var tempItemMiddle= $(vertDiv).height()/2;
			var itemMiddle = "-" + tempItemMiddle + "px";
			var theMiddle=windowMiddle-itemMiddle;
		
			if(winHeight>760) { //vertical			
				$(vertDiv).css('top',windowMiddle);		
				$(vertDiv).css('margin-top',itemMiddle);
			} else {
				$(vertDiv).css('top','0');
				$(vertDiv).css('margin-top','0');
			}
		}
	}
	
	CenterItem('#main_wrapper');
	
	//re-center if browser size is modified
	$(window).resize(function() {
		CenterItem('#main_wrapper');
		currentBodyWidth = $("body").width();
	});	
	
	//homepage what's new
	
	$('.collapsed #handle').live('click', function() {
		collapseParent = $(this).parent();
		collapseParent.removeClass("collapsed");
		collapseParent.addClass("expanded");
		collapseParent.animate({left: "0"}, 300);
		return false;
	});

	$('.expanded #handle').live('click', function() {
	  	collapseParent = $(this).parent();
		collapseParent.animate({left: "-297"}, 100);			
		collapseParent.removeClass("expanded");
		collapseParent.addClass("collapsed");
		return false;
	});	
	
	// footer modals 
	
	 $('#accessibility_overlay').jqm({
		ajax: '/static/accessibility.html',
		trigger: '#accessibility_statement_link',
		onShow: function(h) {
			h.w.css('opacity',1.0).fadeIn(); 
		},
		onHide: function(h) {
		h.w.fadeOut("slow",function() { if(h.o) h.o.remove(); }); } 		
	});
	  
	$('#terms_overlay').jqm({
		ajax: '/static/terms.html',
		trigger: '.terms_link',
		onShow: function(h) {
			h.w.css('opacity',1.0).fadeIn(); 
		},
		onHide: function(h) {
		h.w.fadeOut("slow",function() { if(h.o) h.o.remove(); }); } 				
	});  	
	
	$('#privacy_overlay').jqm({
		ajax: '/static/privacy.html',
		trigger: '#privacy_link',
		onShow: function(h) {
			h.w.css('opacity',1.0).fadeIn(); 
		},
		onHide: function(h) {
		h.w.fadeOut("slow",function() { if(h.o) h.o.remove(); }); } 				
	});  	
	
	$('#contact_overlay').jqm({
		ajax: '/static/contact.html',
		trigger: '#contact_link',
		onShow: function(h) {
			h.w.css('opacity',1.0).fadeIn(); 
		},
		onHide: function(h) {
		h.w.fadeOut("slow",function() { if(h.o) h.o.remove(); }); } 				
	});  		
	
	$('#drinkresponsibly_overlay').jqm({
		ajax: '/static/responsible_drinking.html',
		trigger: '#responsible_drinking',
		onShow: function(h) {
			h.w.css('opacity',1.0).fadeIn(); 
		},
		onHide: function(h) {
		h.w.fadeOut("slow",function() { if(h.o) h.o.remove(); }); } 				
	});  			

	$('.modal_close_button').live('click', function() {
		$(this).parent().parent().jqmHide(); 
		return false;
	});
	
	// Homepage flash bottle animation:
	
	// TODO: move me to special script for index page only
	var params = {
		menu: false,
		quality: "high",
		wmode: "opaque",
		allowscriptaccess: "always",
		allownetworking: "all",
		allowfullscreen: "true",
		scale: "noscale"
	};
	
	var attributes = {
		id: 'swf_replace_centerpiece',
		name: 'Centerpiece'
	};
	
	//swfobject.embedSWF("/media/swf/preloader.swf", "swf_replace_centerpiece", "1280", "720", "10.0.0", "", {}, params, attributes);
	
    // TODO: test for html5 support, use video element instead if so
    // swf bg element
    var el = $("#video_replace");
    var swf_url = el
	
	$("#contactinfo").live('submit',function(){
		var errors = 0;
		$('input, textarea').not('input[type="submit"]').each(function(){
			
			if($(this).val() == ""){
				$(this).css('border','1px solid #f00');
				errors += 1;
			}
			else{
				$(this).css('border','none');
			}
			
		});
		if(errors){
			return false;
		}
		
		var cdata = $(this).serialize();
		$.ajax({
			data: cdata,
			dataType: 'html',
			type: 'post',
			url: '/ajax_contact',
			success: function(txt){

				$("#contactinfo").slideUp('fast',function(){
					
					$(this).before(txt);
					
				});
				
				
			}
		});
		
		return false;
	});
	
	

});
