// Wait for DOM to be ready
$(document).ready(function(){

	// Attach mouse over bubbles to Policies in nav
	$('#Policies li 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('<< Hide more brands');
			$('#BrandIcons ul').animate( { height:$('#BrandIcons ul').attr('elemHeight')}, 500 );
		},
		function(e){
			$(this).html('See more brands >>');
			$('#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;
	}
});