(function ($) {
$(document).ready(function () {
	$('.decorate ul li, .decorate td, .decorate tr').decorate();
	// wraps hr in divs so that they can be more creatively styled
	$("hr").decorate();
	
	if ($("#catalogues-list .catalogueItemNotFound").length == 0) { $("#catalogues-list").removeClass("hide") }
	if ($("#products-list .productItemNotFound").length == 0) { $("#products-list").removeClass("hide") }
	
	// changes the search label on click
	(function searchlabel() {
		var searchb = $('#CAT_Search');
		var defaultText = "search";
		searchb.onblur = function () { 
			if (this.value == '') { this.value = defaultText; }
		}
		searchb.onfocus = function () {
			if (this.value == defaultText) { this.value = ''; }
		}
		if (searchb.value == "") {
			searchb.value = defaultText;
		}
	}) ();
	
	var urlSplit = window.location.href.split("brand");
	if (urlSplit.length > 1) {
		$("#brandContainer #catalogContent").append($("<div class='innerBrandContainer' />"));
		$("#brandContainer #catalogContent .innerBrandContainer").load("/brand-content"+urlSplit.pop(), function () { $("#catalogContent .innerBrandContainer link").remove(); });
		
	}
});

// remove bc base mouse events
$(window).load(function () {
	$('.decorate ul li').each(function () {
		this.onmouseover = null;
		this.onmouseout  = null;
		
		var ob = $(this);
	
		ob.hover(
			function () { ob.addClass('hover') },
			function () { ob.removeClass('hover') }
		);
	});
	
	if ($("#midsection")) {
		$("#midsection-insertion").css('height',$("#midsection").height());
	}
	
	// $("#midsection").css($("#midsection-insertion").offset());
	// $("#midsection").css('left',0);
});
})(jQuery);

function imageResizer() {
	var ns = jQuery("textarea[class*=imgresize]");
	ns.each(function () {
		var nshtml = this.value
		var attrs = this.className.replace(/.*imgresize\[([^\]]*)\].*/g, "$1").replace(/ /g,"&amp;");
		var imghtml = nshtml.replace(/(.*)<img.*src=\"([^\"]+)\"[^\/]*\/>(.*)/, '$1<img src="/Utilities/ShowThumbnail.aspx?USM=1'+attrs+'&amp;R=1&amp;Img=$2" />$3');
		this.parentNode.innerHTML = imghtml;	  
	});
	
	var ns = jQuery("img[class*=imgresize]");
	ns.each(function () {
		var attrs = "&amp;W="+this.width+"&amp;H="+this.height;
		this.src = '/Utilities/ShowThumbnail.aspx?USM=1'+attrs+'&amp;R=1&amp;Img=$2'
	});
}

var ddoc = ddoc || {};
ddoc.write = function (text, rep) {
	var scripts = document.getElementsByTagName("script");

	for (var i=0, l=scripts.length; i<l; ++i ) {
	  if (typeof scripts[i] != 'undefined' && scripts[i].innerHTML.indexOf(text) != -1 ) {
		  if (rep.indexOf("<") !== -1) {
			  jQuery(scripts[i]).replaceWith(jQuery(rep));
		  } else {
			scripts[i].parentNode.replaceChild(document.createTextNode(rep),scripts[i]);
		  }
	  }
	}
}
ddoc.imgResize = function (img, width, height) {
	width = width || 40; height = height || 40;
	ddoc.write(img, img.replace(/(.*)<img.*src=\"([^\"]+)\"[^\/]*\/>(.*)/, '$1<img src="/Utilities/ShowThumbnail.aspx?USM=1&amp;width='+width+'&amp;height='+height+'&amp;R=1&amp;Img=$2" />$3'));
}
