/**
 * @author Jesse Chrestler
 * 07/21/2008
 * menu.js
 */
binding.add({
	".watches, .clocks":{
  	selectedClass: 'selectedMenuItem',
		before: [
			{target:".selectedMenuItem + ul", type:"fadeOut", speed:"slow"},
  	],
		after: [
		
			{target:".selectedMenuItem + ul", type:"fadeIn", speed:"slow", beforeAnim:function(){
				$('.dealer-locator-panel').slideUp();
				if($('.btn-dealer-locator').html() != null)
						$('.btn-dealer-locator').removeClass('selected-button');
				if ($(".selectedMenuItem + ul > li a.selectedSubMenuItem").html() == null) {
					if(typeof brand == "undefined"){
						//$(".selectedMenuItem + ul > li:first a").addClass("selectedSubMenuItem");
					}
					else{
						$("."+brand).addClass("selectedMenuItem");
					}
				}
			}}
  	],
  	afterSelect:function(el){
			if(el.attr('href') != "#")
      	window.location = el.attr('href');
    },
		afterSelectAgain:function(el){
			if(el.attr('href') != "#")
      	window.location = el.attr('href');
    }
	},
	".menuBtn":{
  	selectedClass: 'selectedMenuItem',
		autoStart:false,
		before: [
			{target:".selectedMenuItem + ul", type:"fadeOut", speed:"slow"},
  	],
		after: [
		
			{target:".selectedMenuItem + ul", type:"fadeIn", speed:"slow", beforeAnim:function(){
				$('.dealer-locator-panel').slideUp();
				if($('.btn-dealer-locator').html() != null)
						$('.btn-dealer-locator').removeClass('selected-button');
				if ($(".selectedMenuItem + ul > li a.selectedSubMenuItem").html() == null) {
					if(typeof brand == "undefined"){
						$(".selectedMenuItem + ul > li:first a").addClass("selectedSubMenuItem");
					}
					else{
						$("."+brand).addClass("selectedMenuItem");
					}
				}
			}}
  	],
  	afterSelect:function(el){
			if(el.attr('href') != "#")
      	window.location = el.attr('href');
    },
		afterSelectAgain: function(el){
			if (el.attr('href') != "#") 
				window.location = el.attr('href');
		}
	},
	".submenu-button":{
		autoStart:false,
      selectedClass:'selectedSubMenuItem',
      afterSelect:function(el){
        window.location = el.attr('href');
		  }
	  },
      /**
      * Dealer Locator find button
      */
	".find-button":{
		afterSelect: function(el){
			var zip = $('.find-field').val();
			var param = "";
			if(typeof searchResult != "undefined"){
				if(searchResult.type == "product"){
					type = "collection";
				}
				if($(".btn-adv-search").hasClass("selected-button")){
					type = "advancedSearch"
				}
				if(searchResult.type == "product")
					param = "&collection_id=" + searchResult.id;
				else 
					param = "&brand_id=" + searchResult.id;
			}else{
				param = "&product_id=" + $(document).getUrlParam("id") + "&audit=true";
			}
			var win = window.open("/brands/finder.aspx?txtRet=" + zip + param, '_blank');
			win.focus();
			//window.location = "/brands/finder.aspx?txtRet=" + zip + param
		}
	},
	'.dealer-locator':{
		afterSelect:function(){
			$('.btn-dealer-locator').addClass('selected-button');
		},
		before: [
				{target:'.selectedMenuItem + ul', type:'fadeOut', speed:'slow'},
		],
		beforeSelect:function(){
				$('.dealer-locator-panel').removeAttr("style");
		},
		selectedClass:'selectedMenuItem',
		after:[{target:'.dealer-locator-panel', type:'slideDown'}]},
	'.close-button':{
	 		after:[
				{target:'.dealer-locator-panel', type:'slideUp', afterAnim:function(){
					$('.selectedMenuItem').removeClass('selectedMenuItem');
					if($('.btn-dealer-locator').html() != null)
						$('.btn-dealer-locator').removeClass('selected-button');
				}}
			]
	},
	'a.scroller-ajax-link':{
		selectedClass:'selected-ajax-link',
		autoStart:false,
		afterSelect:function(el){
			//console.log('clicked');
			var h = el.attr("href").replace(/#/,'').replace(/\|/,'?');
			$("#pane01").load(h, '', function(){
				$('#pane01').jScrollPane({showArrows:true});
				binding.bind();
				if(typeof $.facebox ==  "function")
	 				$('.jScrollPaneContainer a').facebox();
			});
		},	
		defaultAction:true,
		trackHistory:true
	},
	'a.scroll-to-element':{
		autoStart:false,
		afterSelect:function(el){
			var loc = el.attr("rel");
			$("#pane01")[0].scrollTo(loc);
			window.location = el.attr("href");
			return false;
		}
	},
	'.eyewear-popup':{
		/*
		selectedClass: 'selectedMenuItem',
		before:[{target:".selectedMenuItem + ul", type:"fadeOut", speed:"slow"}],*/
		afterSelect:function(el){
			$.jqURL.loc(el.attr("href"),{w:666,h:472,wintype:'_blank'});
		}
	},
	'.win-a-watch-popup':{
		afterSelect: function(el){
			$.jqURL.loc('guest.aspx',{w:590,h:682,wintype:'_blank'});
		}
	},
	'.commercials':{
		afterSelect: function(el){
			$.jqURL.loc(el.attr("href"),{w:745,h:430,wintype:'_blank'});
		}
	}

});
var debug = function(msg){
	if(window.console && window.console.log)console.log(msg);
}
var css = "";
if(typeof $(document).getUrlParam == "function"){
	var brand = $(document).getUrlParam("brand");
	brand = (brand == null) ? "bulova" : brand;
	css = (brand.search("bulova|accutron|caravelle|wittnauer|harley") == -1) ? "c1c399" : brand;
	css = '<link rel="stylesheet" href="css/'+ css +'-colors.css" type="text/css" media="screen, projection" />';
}
var lastHash = "";
function pageload(h) {
		//lets try to find the hash element
		var element = $("[href=#" + h + "]");
		//console.log("start:"+h+" lastHash:" + lastHash + " " + " hash:"+ window.location.hash);
		//we are searching
		if(h == "#")return false;
		if(h == unescape(lastHash)){
			//alert("hashes are the same - abort:"+h);
			return false;
		}
		if (h.length < 2 && lastHash != "") {
			//alert("redirect:"+h);
			window.location = window.location.href.replace(/#/,'');
		}
		else if(h.length < 2 && lastHash == "") {
			//alert("failed:"+h);
			return false;
		}
		lastHash = h;
		//var lh = window.location.hash.replace(/#/,"");
		//if(h != lh)h = lh;
		//alert("before check:"+h);

		if (h.indexOf("=") > -1 && h.search(/\.aspx/i) == -1) {
			clearAdvancedSearch();
			$(".btn-view-all").css({display:'block'}).removeClass('selected-button');
			var collectionName = "";
			var isAdvancedSearch = true;
			var part = h.split("&");
			var params = {};
			for (var i = 0; i < part.length; i++) {
				var pair = part[i].split("=");
				//var ckbox = pair[1].split(",");
				if(pair[0] != "coll_name"){
					params[pair[0]] = pair[1];
					/*
					for (var j = 0; j < ckbox.length; j++) {
						var index = ckbox[j] - 1;
						$('.' + pair[0] + ' + div > ul > li input:eq(' + index + ')').attr({
							checked: true
						})
					}
					*/
				}else{
					isAdvancedSearch = false;
					collectionName = pair[1];
				}
			}
			if(isAdvancedSearch){
				$(".header-top h2").empty().prepend("Search Results");
				$(".btn-adv-search").click();
			}else{
				var gender = ""
				if(params.gender == 1) gender = "Men's " 
				else if (params.gender == 2)gender = "Ladies' ";
				var aname = collectionName.split(" ");
				name = "";
				for(var i = 0; i < aname.length; i++)name += aname[i].substring(0,1).toUpperCase() + aname[i].substring(1, aname[i].length).toLowerCase() + " ";
				
				$(".header-top h2").empty().prepend( gender + unescape(name) + "Collection");

			}
			for(var obj in params){
				var ckbox = params[obj].split(",");
				for (var j = 0; j < ckbox.length; j++) {
					var v = ckbox[j];
					$('.' + obj + ' + div > ul >  li input[value=' + v + ']').attr({checked: true});
				}
			}
			carousel.load({
				params: params
			});
		}else if (element.length > 0){
			//alert('click from history');
			element.click();
		}
	//alert("end:"+h);
		
	}
	
$(document).ready(function(){
	//update all scroller links
	$('a.scroller-ajax-link').each(function(){
		var href = $(this).attr("href").replace(/\?/, "|");
		$(this).attr({href:"#"+href});
	})
	//if(css != "")
		//$("head").append(css);
	if(typeof $('#pane01').jScrollPane == "function"){
		$('#pane01').jScrollPane({showArrows:true});
	}
	if(typeof brand != "undefined"){
		$('.'+brand).parents(".subMenu").prev().addClass('selectedMenuItem')
	}
	$('.search-field').bind('focus',function(){if($(this).val() == "Enter a Style Number")$(this).val("")})
	$('.search-field').bind('blur', function(){if($(this).val() == "")$(this).val('Enter a Style Number');});
	$('a').bind('focus',function(){$(this).blur()});
	if(typeof $.historyInit == "function" && document.location.hash.indexOf("=") == -1) // must not be searching..
		$.historyInit(pageload);
	binding.bind();
 });