/*
 * Template.js
 *
 * All JavaScript in this document is 'Progressive enhancement'.
 * If javascript is disabled on the client then the deisgn will still function.
 */


/*
 * Scope JQuery to avoid conflict with lightbox
 */
var $j = jQuery;

/*
 * Category Menu
 */
$j(function(){
	$j('#siteCategories>ul>li:has(ul)').each(function(){
		$j(this).addClass('open')
	});
	
	$j('#siteCategories>ul').HEMenu();
});

/*
 * Select Brand combo box
 */
$j(function(){
	$j('#siteSelectBrandOverlay').css({
		'position':'absolute',
		'left':'0',
		'top':'0',
		'z-index':'5',
		'display':'block',
		'height':'19px',
		'width':'110px',
		'paddingTop':'2px',
		'paddingLeft':'4px',
		'color':'#666',
		'overflow':'hidden'
	});
	
	$j('#siteSelectBrandSelect>select').css('opacity', 0);
	
	function getSelectText(){
		var t = $j([
			'#siteSelectBrandSelect>select>option[value=',
			$j('#siteSelectBrandSelect>select').val(),
			']'
		].join('')).text();
		$j('#siteSelectBrandOverlay').text(t);
	}
	
	$j('#siteSelectBrandSelect>select').bind('change', getSelectText);
	
	getSelectText();
	
	$j('#siteSelectBrandSelect>select').bind('change', function(){
		var val = $j(this).val();
		if (val != 'empty' ) window.location.href = val;
	});
});

/*
 * Content Footer
 */
$j(function(){
	$j('#siteBodyCenterBottom').css({
		'position':'absolute',
		'left':'175px',
		'bottom':'0',
		'width':'600px',
		'height': $j('#siteBodyCenterBottom').height() + 'px'
	});
	
	$j('#siteContent').css('paddingBottom',  $j('#siteBodyCenterBottom').height() + 10);
});

/*
 * Minicart remove buttons
 */
$j(function(){
	var RE = /(\d+)-p.asp/;
	$j('#siteMiniCart ul>li').each(function(i){
		var href = $j(this).children('a').attr('href');
		var id = RE.exec(href)[1];
		
		$j(this).prepend([
			'<a href="index.asp?function=CART&mode=REMOVE&productid=',
			id,
			'&cartid=',
			i + 1,
			'" title="Remove" class="remove-from-minicart">Remove</a>'
		].join(''));
	});
});

/*
 * Product Listing
 * Makeing the height even across a row
 */
$j(function(){
	$j('.product-row').each(function(){
		var name = 0;
		var desc = 0;
		
		$j(this).children('.product').each(function(){
			var n = $j(this).children('.product-name').height();
			var d = $j(this).children('.product-desc').height();
			name = (name < n)? n : name;
			desc = (desc < d)? d : desc;
		});
		
		$j(this).find('.product-name').each(function(){
			$j(this).height(name);
		});
		
		$j(this).find('.product-desc').each(function(){
			$j(this).height(desc);
		});
		
	});
});