// Wait for DOM to be ready
$(document).ready(function(){

	// Attach mouse over bubbles to Policies in nav
	$('#Policies span span').each(function(){
		$(this).qtip({
			content: $(this).siblings('ul').find('li').html(),
			position: {
			  corner: {
				 target: 'bottomLeft',
				 tooltip: 'topRight'
			  }
		   },
			style: {
				tip: 'topRight',
				name: 'dark',
				'font-size': 11
			},
			show: 'mouseover',
			hide: 'mouseout'
		});
	});
	
	// Events for brand bar
	/*$('#BrandIcons img[sortvalue]').click(function(e){
		$('#CategorySortBy').val($(this).attr('sortvalue'));
		$('#FindMyBrand').removeAttr('disabled');
		$('#frmSort').submit();
	});
	$('#BrandIconToggle').toggle(
		function(e){
			$(this).html('<img src="/shared/images/icon_menu_collapse.png" /> Hide more brands');
			$('#BrandIcons ul').animate( { height:$('#BrandIcons ul').attr('elemHeight')}, 500 );
		},
		function(e){
			$(this).html('See more brands <img src="/shared/images/icon_menu_expand.png" />');
			$('#BrandIcons ul').animate( { height:154}, 500 );
	});*/
	
	// Events for search box
	$('#SearchValue').focus(function(e){
		if($(this).val() == $(this).attr('hint')){
			$(this).val('');
			$(this).css('color','#FFF');
			$(this).css('text-transform','uppercase');
		}
	});
	$('#SearchValue').blur(function(e){
		if($(this).val().length == 0){
			$(this).css('color','#999');
			$(this).css('text-transform','none');
			$(this).val($(this).attr('hint'));
		}
	});
	
	// Events for nav flyouts
	$('#CategoryLinks .menu > a').mouseenter(function(e){
		$(this).parent().mouseenter(function(e){
			$('select').css('visibility','hidden');
			$(this).children('ul').fadeIn('fast');
		});
		$(this).parent().mouseleave(function(e){
			$(this).children('ul').fadeOut('fast');
			$(this).unbind();
			$('select').css('visibility','visible');
		});
		$(this).parent().trigger('mouseenter');
	});
	$('#CategoryLinks .menu .subMenu').mouseenter(function(e){
		$(this).children('ul').fadeIn('fast');
		$(this).addClass('subMenuHover');
	});
	$('#CategoryLinks .menu .subMenu').mouseleave(function(e){
		$(this).children('ul').fadeOut('fast');
		$(this).removeClass('subMenuHover');
	});
	
	// Events for category sorting
	$('.dropMenu:has(#SortingMenu) li[sortvalue]').each(function(e) {
		if(($.getUrlVar('FindMyBrand') === undefined) && ($.getUrlVar('FindMySize') === undefined) && ($(this).attr('sortvalue') == $.urlDecode($.getUrlVar('SortBy')))) {
			$('#SortingMenu').text($(this).attr('title'));
		}
		$(this).click(function(elem) {
			$('#CategorySortBy').val($(this).attr('sortvalue'));
			$('#FindMyBrand').attr('disabled', 'disabled');
			$('#FindMySize').attr('disabled', 'disabled');
			$('#frmSort').submit();
		});
	});
	
	// Events for "Find my brand"
	$('.dropMenu:has(#FindMyBrandMenu) li[sortvalue]').each(function(e) {
		if(($.getUrlVar('FindMyBrand') !== undefined) && ($(this).attr('sortvalue') == $.urlDecode($.getUrlVar('SortBy')))) {
			$('#FindMyBrandMenu').text($(this).attr('title'));
		}
		$(this).click(function(elem) {
			$('#CategorySortBy').val($(this).attr('sortvalue'));
			$('#FindMyBrand').removeAttr('disabled');
			$('#frmSort').submit();
		});
	});
	
	// Events for "Find my size"
	$('.dropMenu:has(#FindMySizeMenu) li[sortvalue]').each(function(e) {
		if((!$.getUrlVar('FindMySize')  !== undefined) && ($(this).attr('sortvalue') == $.urlDecode($.getUrlVar('SortBy')))) {
			$('#FindMySizeMenu').text($(this).attr('title'));
		}
		$(this).click(function(elem) {
			$('#CategorySortBy').val($(this).attr('sortvalue'));
			$('#FindMySize').removeAttr('disabled');
			$('#frmSort').submit();
		});
	});
	
	// Events for items per page
	$('#ItemsPerPage').change(function(e){
		$('#ObjectsPerPage').val($(this).val());
		$('#frmSort').submit();
	});
})

// Extend JQuery to support easy URL vars and URLEncode and Decode
$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
		  hash = hashes[i].split('=');
		  vars.push(hash[0]);
		  vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	},
	urlEncode: function(c){
		var o='';
		var x=0;
		c=c.toString();
		var r=/(^[a-zA-Z0-9_.]*)/;
		while(x<c.length){
			var m=r.exec(c.substr(x));
			if(m!=null && m.length>1 && m[1]!=''){
				o+=m[1];x+=m[1].length;
			}else{
				if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
				o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;
			}
		}
		return o;
	},
	urlDecode: function(s){
		var o=s;
		var binVal,t;
		var r=/(%[^%]{2})/;
		while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){
			b=parseInt(m[1].substr(1),16);
			t=String.fromCharCode(b);o=o.replace(m[1],t);
		}
		return o;
	}
});

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}