/*Global "flyout" menu objects*/
var cartDropdown;
var stateDropdown;
var leftNav;
var regionDropdown;
var priceDropdown;
var brandsDropdown;
var sortDropdown;

$(document).ready(function(){
	//set current shipping state
	var oldState = $('#hdrCartState .state').html();
	
	//check for cookie, if no cookie show age gate (age gate function will set cookie onClose)
	var x = get_cookie ( "tbr-visited" );
	if(!x){
	  $("select:not(#modal select)").hide();
	  openModel=new modal();
	 }
	//when dom ready, assign global objects
	cartDropdown=new hdrCart;
	stateDropdown=new hdrState;
	leftNav=new leftNavMenu;
	regionDropdown=new regionFilter;
	priceDropdown=new priceFilter;
	brandsDropdown=new brandsFilter;
	sortDropdown=new sortFilter;
	
	//google events (might not be used after google search is hooked up)
	$(".google").click(function(){
		$(this).addClass("googleActive");
	});
	$(".google").blur(function(){
		if($(this).val()=="") $(this).removeClass("googleActive")
	});
	
	//shopping cart dropdown events
	$("#hdrCartTxt").click(function(){
			if ($("#cartDropdown").css("display")=="none"){
				//cartDropdown.clearTime=setTimeout("cartDropdown.closeCart()",1500);
				cartDropdown.openCart();
			}
			else cartDropdown.closeCart();
	});
	/*$("#cartDropdown .close").click(function(){
		cartDropdown.closeCart();
	});
	$("#cartDropdown").mouseleave(function(){
		cartDropdown.clearTime=setTimeout("cartDropdown.closeCart()",500);
	});
	$("#cartDropdown").mouseenter(function(){
		clearTimeout(cartDropdown.clearTime);
	});*/
	
	//state dropdown
	$("#hdrCartState").click(function(){
			if ($("#stateDropdown").css("display")=="none"){
				//stateDropdown.clearTime=setTimeout("stateDropdown.closeState()",1500);
				stateDropdown.openState();
			}
			else stateDropdown.closeState();
	});
	$("#stateDropdown .close").click(function(){
		stateDropdown.closeState();
	});
	$("#stateDropdown li.selectStateOn").each(function(){
		$(this).click(function(){
			state=$(this).find("a").html();
			stateDropdown.populateState(state)
			stateDropdown.closeState();
			return false;
		})
	});
	$("#stateDropdown").mouseleave(function(){
		stateDropdown.clearTime=setTimeout("stateDropdown.closeState()",500);
	});
	$("#stateDropdown").mouseenter(function(){
		clearTimeout(stateDropdown.clearTime);
	});
	
	//left nav events
	$("#leftNav .jsMenu").each(function(){
		$(this).mouseover(function(){
			menuID=$(this).attr("id").replace("item","")
			leftNav.showMenu(menuID);
		});
		$(this).mouseleave(function(){
			leftNav.clearTime=setTimeout("leftNav.closeMenu()",500);
		});
		$(this).mouseenter(function(){
			clearTimeout(leftNav.clearTime);
		});
	});
	$("#leftNav .hdr .close").each(function(){
		$(this).click(function(){
			leftNav.closeMenu();
		});
	});
	$("#leftNav .activePage li a").each(function(){
		$(this).mouseenter(function(){
			leftNav.closeMenu();
		});		
	});
	$("#leftNav .noJSMenu").each(function(){
		$(this).mouseenter(function(){
			leftNav.closeMenu();
		})
	});
	$("#leftNav .hdr").each(function(){
		$(this).mouseleave(function(){
			leftNav.clearTime=setTimeout("leftNav.closeMenu()",500);
		});
		$(this).mouseenter(function(){
			clearTimeout(leftNav.clearTime);
		});
	});
	
	//region filter dropdown events
	$("#breadcrumb .opt1").click(function(){
			if ($("#regionFilter").css("display")=="none"){
				//regionDropdown.clearTime=setTimeout("regionDropdown.closeFilter()",1500);
				regionDropdown.openFilter();
			}
			else regionDropdown.closeFilter();
			return false;
	});
	$("#regionFilter .close").click(function(){
		regionDropdown.closeFilter();
	});
	$("#regionFilter").mouseleave(function(){
		regionDropdown.clearTime=setTimeout("regionDropdown.closeFilter()",500);
	});
	$("#regionFilter").mouseenter(function(){
		clearTimeout(regionDropdown.clearTime);
	});
	$("#regionFilter li").each(function(){
		$(this).click(function(){
			regionDropdown.populate($(this).find("a").html());
			$('input[name="wineregionid"]').val($(this).find('a').attr('id'));
			$("#regionFilter li").each(function(){$(this).find("a").css({"color":"#700800","text-decoration":"underline"});});
			$(this).find("a").css({"color":"#8E6A34","text-decoration":"none"});
			return false;
		});
	});

	//price dropdown events
	$("#breadcrumb .opt2").click(function(){
			if ($("#priceFilter").css("display")=="none"){
				//priceDropdown.clearTime=setTimeout("priceDropdown.closeFilter()",1500);
				priceDropdown.openFilter();
			}
			else priceDropdown.closeFilter();
			return false;
	});
	$("#priceFilter .close").click(function(){
		priceDropdown.closeFilter();
	});
	$("#priceFilter").mouseleave(function(){
		priceDropdown.clearTime=setTimeout("priceDropdown.closeFilter()",500);
	});
	$("#priceFilter").mouseenter(function(){
		clearTimeout(priceDropdown.clearTime);
	});
	$("#priceFilter li").each(function(){
		$(this).click(function(){
			priceDropdown.populate($(this).find("a").html());
			$('input[name="lowprice"]').val($(this).find('a').attr('id'));
			$('input[name="highprice"]').val($(this).find('a').attr('class'));
			$("#priceFilter li").each(function(){$(this).find("a").css({"color":"#700800","text-decoration":"underline"});});
			$(this).find("a").css({"color":"#8E6A34","text-decoration":"none"});
			return false;
		});
	});

	//brands dropdown events
	$("#breadcrumb .opt3").click(function(){
			if ($("#brandsFilter").css("display")=="none"){
				//brandsDropdown.clearTime=setTimeout("brandsDropdown.closeFilter()",1500);
				brandsDropdown.openFilter();
			}
			else brandsDropdown.closeFilter();
			return false;
	});
	$("#brandsFilter .close").click(function(){
		brandsDropdown.closeFilter();
	});
	$("#brandsFilter").mouseleave(function(){
		brandsDropdown.clearTime=setTimeout("brandsDropdown.closeFilter()",500);
	});
	$("#brandsFilter").mouseenter(function(){
		clearTimeout(brandsDropdown.clearTime);
	});
	$("#brandsFilter li").each(function(){
		$(this).click(function(){
			brandsDropdown.populate($(this).find("a").html());
			$('input[name="winebrandid"]').val($(this).find('a').attr('id'));
			$("#brandsFilter li").each(function(){$(this).find("a").css({"color":"#700800","text-decoration":"underline"});});
			$(this).find("a").css({"color":"#8E6A34","text-decoration":"none"});
			return false;
		});
	});
	
	//sorting dropdown events
	$("#sortByName").click(function(){
			if ($("#sortFilter").css("display")=="none"){
				sortDropdown.openFilter();
			}
			else sortDropdown.closeFilter();
			return false;
	});
	$("#sortFilter .close").click(function(){
		sortDropdown.closeFilter();
	});
	$("#sortFilter").mouseleave(function(){
		sortDropdown.clearTime=setTimeout("sortDropdown.closeFilter()",500);
	});
	$("#sortFilter").mouseenter(function(){
		clearTimeout(sortDropdown.clearTime);
	});
	$("#sortFilter li").each(function(){
		$(this).click(function(){
			sortDropdown.populate($(this).find("a").html());
			$('input[name="orderby"]').val($(this).find('a').attr('id'));
			$("#sortFilter li").each(function(){$(this).find("a").css({"color":"#700800","text-decoration":"underline"});});
			$(this).find("a").css({"color":"#8E6A34","text-decoration":"none"});
			//$('form[name="filterForm"]').submit();
			submitDropdown();
		});
	});
	
	//submit Dropdown filters
	$('#goButton').click(function(){
		submitDropdown();
	});
	
	//ftr input events, clears text on focus, etc.
	$("#emailOffers .txt").click(function(){
		$(this).val("");
	});
	$("#emailOffers .txt").blur(function(){
		if($(this).val()=="") $(this).val("Enter Your Email Address");
	});
	$("#orderTracking .txt").click(function(){
		$(this).val("");
	});
	$("#orderTracking .txt").blur(function(){
		if($(this).val()=="") $(this).val("Enter Tracking Number(s)");
	});
	
	
	//stateSelector 
	$('.selectStateOn').click(function(){
		if($('input[name="ClearSessionSign"]').val() == 'true'){							   
			var message=confirm("Changing your shipping state will delete the contents of your cart. Would you like to proceed?");
			if (message==true){
				//set shippingstate
				$('input[name="ShippingState"]').val($(this).attr('id'));
				//submit form
				$('form[name="ShippingState"]').submit();
			} else {
				$('#hdrCartState .state').html(oldState);
				return
			}
		} else {
			//set shippingstate
			$('input[name="ShippingState"]').val($(this).attr('id'));
			//submit form
			$('form[name="ShippingState"]').submit();
		}
		
	});
	
	$('#submitAuthentication').click(function(){
		//sets cookie
		var days=1;
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = date.toGMTString();
		cookieString="tbr-visited=true; expires="+expires+"; path=/";
		document.cookie=cookieString;
		$("#modalForm").submit();
	});
	
	//droplogin
	//account login dropdown
	 $("#myAccountLoginBtn,#accountLoginImg").click(function(){
	 	$("#myAccountLogin").show();
	 	$("#accountLoginImg").attr({"src":"/assets/images/global/newImages/accountBoxBtnDown.gif"});
	 });
	 $("#myAccountLoginClose").click(function(){
	  	$("#myAccountLogin").hide();
	  	$("#accountLoginImg").attr({"src":"/assets/images/global/newImages/accountBoxBtn.gif"});
	 });
	
	
});
//Div objects that need to be closed:
//hdrCart
//stateDropdown
//leftNav
//regionFilter
//priceFilter
//brandsFilter
//sortFilter
function submitDropdown(){
	//$('form[name="filterForm"]').submit();
	if($('input[name="marketingurl"]').length > 0){
		//create new link
		var newLink = '/'+$('input[name="marketingurl"]').val()+'?';
		$('form[name="filterForm"] input[type="hidden"]').each(function(i){
			if($(this).attr('name') != 'marketingurl' && $(this).val() != '' && $(this).attr('name') != 'productcategoryid' && $(this).attr('name') != 'method'){
				if(i<($('form[name="filterForm"] input[type="hidden"]').length)-1){
					newLink = newLink+$(this).attr('name')+'='+$(this).val()+'&';
				} else {
					newLink = newLink+$(this).attr('name')+'='+$(this).val();
				}
			}
		});
		window.location = newLink;
	} else {
		$('form[name="filterForm"]').submit();
	}
}

//close all open menus:
function closeAll(){
	cartDropdown.closeCart();
	stateDropdown.closeState();
	leftNav.closeMenu();
	regionDropdown.closeFilter();
	priceDropdown.closeFilter();
	brandsDropdown.closeFilter();
	sortDropdown.closeFilter();
}

//dropdown cart object
function hdrCart(){
		this.clearTime;
		this.openCart=function(){
			//close all to reset all open submenus
			closeAll();
			$("#cartDropdown").css({"display":"block"});
			$("#hdrCartTxt").addClass("hdrCartTxtActive");
			$("#hdrCart .checkout").attr({"src":"/assets/images/global/newImages/hdrCartCheckoutBtnActive.gif"})
		}
		this.closeCart=function(){
			$("#cartDropdown").css({"display":"none"});
			$("#hdrCartTxt").removeClass("hdrCartTxtActive");
			$("#hdrCart .checkout").attr({"src":"/assets/images/global/newImages/hdrCartCheckoutBtn.gif"})
		}
}

//dropdown state object
function hdrState(){
		this.clearTime;
		this.openState=function(){
			//close all to reset all open submenus
			closeAll();
			$("#stateDropdown").css({"display":"block"});
			$("#hdrCartState").addClass("hdrCartStateActive");
		}
		this.closeState=function(){
			$("#stateDropdown").css({"display":"none"});
			$("#hdrCartState").removeClass("hdrCartStateActive");
		}
		this.populateState=function(state){
			switch (state){
				case "Alabama":
									$("#hdrCartState .state").html("AL");
									break;
				case "Alaska":
									$("#hdrCartState .state").html("AK");
									break;
				case "Arizona":
									$("#hdrCartState .state").html("AZ");
									break;
				case "Arkansas":
									$("#hdrCartState .state").html("AR");
									break;
				case "California":
									$("#hdrCartState .state").html("CA");
									break;
				case "Colorado":
									$("#hdrCartState .state").html("CO");
									break;
				case "Connecticut":
									$("#hdrCartState .state").html("CT");
									break;
				case "Delaware":
									$("#hdrCartState .state").html("DE");
									break;
				case "Florida":
									$("#hdrCartState .state").html("FL");
									break;
				case "Georgia":
									$("#hdrCartState .state").html("GA");
									break;
				case "Hawaii":
									$("#hdrCartState .state").html("HI");
									break;
				case "Idaho":
									$("#hdrCartState .state").html("ID");
									break;
				case "Illinois":
									$("#hdrCartState .state").html("IL");
									break;
				case "Indiana":
									$("#hdrCartState .state").html("IN");
									break;
				case "Iowa":
									$("#hdrCartState .state").html("IA");
									break;
				case "Kansas":
									$("#hdrCartState .state").html("KS");
									break;
				case "Kentucky":
									$("#hdrCartState .state").html("KY");
									break;
				case "Louisiana":
									$("#hdrCartState .state").html("LA");
									break;
				case "Maine":
									$("#hdrCartState .state").html("ME");
									break;
				case "Maryland":
									$("#hdrCartState .state").html("MD");
									break;
				case "Massachusetts":
									$("#hdrCartState .state").html("MA");
									break;
				case "Michigan":
									$("#hdrCartState .state").html("MI");
									break;
				case "Minnesota":
									$("#hdrCartState .state").html("MN");
									break;
				case "Mississippi":
									$("#hdrCartState .state").html("MS");
									break;
				case "Missouri":
									$("#hdrCartState .state").html("MO");
									break;
				case "Montana":
									$("#hdrCartState .state").html("MT");
									break;
				case "Nebraska":
									$("#hdrCartState .state").html("NE");
									break;
				case "Nevada":
									$("#hdrCartState .state").html("NV");
									break;
				case "New Hampshire":
									$("#hdrCartState .state").html("NH");
									break;
				case "New Jersey":
									$("#hdrCartState .state").html("NJ");
									break;
				case "New Mexico":
									$("#hdrCartState .state").html("NM");
									break;
				case "New York":
									$("#hdrCartState .state").html("NY");
									break;
				case "North Carolina":
									$("#hdrCartState .state").html("NC");
									break;
				case "North Dakota":
									$("#hdrCartState .state").html("ND");
									break;
				case "Ohio":
									$("#hdrCartState .state").html("OH");
									break;
				case "Oklahoma":
									$("#hdrCartState .state").html("OK");
									break;
				case "Oregon":
									$("#hdrCartState .state").html("OR");
									break;
				case "Pennsylvania":
									$("#hdrCartState .state").html("PA");
									break;
				case "Rhode Island":
									$("#hdrCartState .state").html("RI");
									break;
				case "South Carolina":
									$("#hdrCartState .state").html("SC");
									break;
				case "South Dakota":
									$("#hdrCartState .state").html("SD");
									break;
				case "Tennessee":
									$("#hdrCartState .state").html("TN");
									break;
				case "Texas":
									$("#hdrCartState .state").html("TX");
									break;
				case "Utah":
									$("#hdrCartState .state").html("UT");
									break;
				case "Vermont":
									$("#hdrCartState .state").html("VT");
									break;
				case "Virginia":
									$("#hdrCartState .state").html("VA");
									break;
				case "Washington":
									$("#hdrCartState .state").html("WA");
									break;
				case "West Virginia":
									$("#hdrCartState .state").html("WV");
									break;
				case "Wisconsin":
									$("#hdrCartState .state").html("WI");
									break;
				case "Wyoming":
									$("#hdrCartState .state").html("WY");
									break;
			}
		}
}

//left nav object
function leftNavMenu(){
	this.clearTime;
	this.currentMenu;
	this.showMenu=function(id){
		closeAll();
		this.currentMenu=id;
		itemHeight=$("#item"+id).height()
		if(itemHeight<=18)
		$("#item"+id).addClass("activeSub");
		$("#item"+id).parent().addClass("secondLvlActiveJS");
		$("#m"+id).css({"display":"block"});
		
	};
	this.closeMenu=function(){
		$("#m"+this.currentMenu).css({"display":"none"});
		$("#item"+this.currentMenu).removeClass("activeSub");
		$("#item"+this.currentMenu).parent().removeClass("secondLvlActiveJS");
	}
}

//region dropdown object
function regionFilter(){
		this.clearTime;
		this.openFilter=function(){
			//close all to reset all open submenus
			closeAll();
			$("#regionFilter").css({"display":"block"});
		}
		this.closeFilter=function(){
			$("#regionFilter").css({"display":"none"});
		}
		this.populate=function(choice){
			$(".opt1 .region").html(choice);
			$(".opt1 .region").css({
				"color" : "#A90D00",
				"font-size" : "14px",
				"font-weight" : "bold"
			});

			regionDropdown.closeFilter();
		}

}

//price dropdown object
function priceFilter(){
		this.clearTime;
		this.openFilter=function(){
			//close all to reset all open submenus
			closeAll();
			$("#priceFilter").css({"display":"block"});
		}
		this.closeFilter=function(){
			$("#priceFilter").css({"display":"none"});
		}
		this.populate=function(choice){
			$(".opt2 .price").html(choice);
			$(".opt2 .price").css({
				"color" : "#A90D00",
				"font-size" : "14px",
				"font-weight" : "bold"
			});

			priceDropdown.closeFilter();
		}
}

//brands dropdown object
function brandsFilter(){
		this.clearTime;
		this.openFilter=function(){
			//close all to reset all open submenus
			closeAll();
			$("#brandsFilter").css({"display":"block"});
		}
		this.closeFilter=function(){
			$("#brandsFilter").css({"display":"none"});
		}
		this.populate=function(choice){
			//trim choice to fit in small box (15 char)
			if(choice.length>12)
			choice=String(choice).substr(0,12)+"...";
			$(".opt3 .region").html(choice);
			$(".opt3 .region").css({
				"color" : "#A90D00",
				"font-size" : "14px",
				"font-weight" : "bold"
			});
			brandsDropdown.closeFilter();
		}
}

//sort dropdown object
function sortFilter(){
		this.clearTime;
		this.openFilter=function(){
			//close all to reset all open submenus
			closeAll();
			$("#sortFilter").css({"display":"block"});
		}
		this.closeFilter=function(){
			$("#sortFilter").css({"display":"none"});
		}
		this.populate=function(choice){
			//trim choice to fit in small box (15 char)
			if(choice=="Sort by Name") choice="Sort by: Name";
			//else if(choice.length>11)
			//choice=String(choice).substr(0,11)+"...";
			$("#mainBreadcrumb .sortFilter").html(choice);
			$("#mainBreadcrumb .sortFilter").css({
				"color" : "#A90D00",
				"font-size" : "14px",
				"font-weight" : "bold"
			});

			sortDropdown.closeFilter();
		}
}

//function to show popup windows
function showWindow(win){
 window.open(win,'legal','toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=yes,width=650,height=500');
 return false;
 }

function modal(){
	//load modal overlay
	var api = $("#modal").overlay({
		api: true,
		expose: { 
			color: '#ECE5C5', 
			loadSpeed: 200, 
			opacity: 1
		}, 
    	closeOnClick: false,
		closeOnEsc: false 
	});
	api.load();
	api.onClose=function(){
		//sets cookie
		var days=1;
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = date.toGMTString();
		cookieString="tbr-visited=true; expires="+expires+"; path=/";
		document.cookie=cookieString;
		$("#modalForm").submit();
	}
}



//function to get cookies
function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name);
	
  if ( results )
    return ( unescape ( results[1] ) );
  else
    return false;
}
