


(function($){ 	// ------------------------------------------------------------------------------------------------------------------------------ 	$ closure

	$(document).ready(function(){// -------------------------------------------------------------------------------------------	Document ready
	
		parseCartSummary($('#hidden-cart-sum').html());
	
		setPatternGallery();
		
		preloadMostUsedIMages();
		
	//Set blur and focus events for the search form text input and the submit event for the form
		validateSearchForm();
		
		//setNewProducts();
	
	}); // --------------------------------------------------------------------------------------------------------------------	Document ready

})(jQuery);	// ---------------------------------------------------------------------------------------------------------------------------------- 	$ closure


//-------------------------------------------------------------------------------------------	PUBLIC PROPERTIES

	//var simpleOverlay = null;

//-------------------------------------------------------------------------------------------	PUBLIC METHODS



function validateSearchForm()//Validates the search form
{
	jQuery('#CAT_Search')//blur and focus events for the text input
		.val('Search PiP')
		.focus(function(){
			var e = jQuery(this);
			var sVal = jQuery.trim(e.val());
			if(sVal == 'Search PiP')e.val('');	
		})
		.blur(function(){
			var e = jQuery(this);
			var sVal = jQuery.trim(e.val());
			if(sVal == '')e.val('Search PiP');	
		});
	
	jQuery('#site-search form').submit(function($e){//Submit event for the form
		var e = jQuery(this);
		var sVal = jQuery.trim(e.find('#CAT_Search').val());
		if(sVal.length == 0 || sVal == 'Search PiP')
		{
			$e.preventDefault();
			alert('You must enter a valid search term.');	
		}
	});	
}

function getMonthNum(str)
{
	var months = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'];
	for(var i = 0, x = months.length; i < x; i++)
	{
		if(str.toString().toLowerCase().indexOf(months[i]) != -1)return i;		
	}
	return -1;
}


function setNewProducts()
{
	var products = jQuery('.productItem');
	
	products.each(function(){
		var e = jQuery(this);
		var str = e.find('h2').attr('class');
		if(str)
		{
			str = jQuery.trim(str.replace('rd-','')).split('-');
			var oDate = new Date();
			oDate.setFullYear(parseInt(str[2]),getMonthNum(str[1]),parseInt(str[0]));
			
			var today = new Date();
			
			if(oDate < today)
			{
				var days = Math.round(oDate.getDaysBetween(today));
				if(days < 5000)
				{ 
					jQuery('<img />')
						.attr({
							width: '40',
							height: '36',
							alt: 'New',
							'class': 'new png-fix',
							src: '/assets-img/layout/icon-new.png'	
						})
						.appendTo(e);
				}
			}
		}
	});
	
}



function preloadMostUsedIMages()// ----------------------------------------------------------------------------------
{
	setTimeout(function(){
		
		//Preload loader gif
		var oImg1 = document.createElement('img');
		oImg1.src = '/assets-img/layout/loader.gif';
		
	},1000);
}

function parseCartSummary(str)// ----------------------------------------------------------------------------------
{
	
	if(typeof jQuery.BCCartSum != 'function' || typeof str != 'string' || jQuery.trim(str).length == 0)
	{
		jQuery('#cart-summary').find('.with-items').html('<strong>0</strong> item(s)').css('visibility','visible');
		return;
	}
	var c = jQuery.BCCartSum(str);
	
	
	if(c.ok)
	{
		jQuery('#cart-summary').find('.with-items').html('<strong>' + c.quantity + '</strong> item(s) Total: <strong>$' + parseFloat(c.total).toFixed(2) + '</strong><br /><a href="' + c.url + '">Checkout</a>').css('visibility','visible');
	}
}




function setPatternGallery()// ----------------------------------------------------------------------------------
{
	
	//Reformat patterns
	jQuery('#patterns li')
			.find('.title')
			.css({
				height: 'auto'
			})
			.find('span')
			.css({
				width: 'auto'
			})
			.parent()
			.find('img')
			.remove()
			.end();
							
	
	var patterns = jQuery('#patterns li');
	
	//Loop through patterns and resize each cell	
	for(var i = 0, x = patterns.length; i < x; i += 4)
	{
		var nHeight = 0;
		var n = ((i + 4) > x) ? x : (i + 4);
		for(var y = i; y < n; y++)
		{
			var e = jQuery(patterns[y]);
			var tempHeight = e.find('.img').innerHeight() + e.find('.title').innerHeight();
			if(tempHeight > nHeight)nHeight = tempHeight;
		}
		
		for(var y = i; y < n; y++)
		{
			jQuery(patterns[y]).find('a').css('height',nHeight);
		}
	}
	
	
	
	var patternLargeImg = null;
	var patternCaption = null;
	var patternOverlay = jQuery('<div></div>').attr({'class':'overlay','id':'overlay-patterns'}).appendTo(document.body);
	
    if(typeof jQuery.fn.SimpleOverlay != 'function' || patternOverlay.length != 1)return;
	
	
	jQuery('#patterns a').not('#webapp6713pagination a').Gallery({
		largeImgwidth: Math.round(jQuery(document.body).width() * 0.8),
		overlayRef: '#overlay-patterns',
		overlayTop: 40,
		loaderSrc: '/assets-img/layout/ajax-loader.gif',
		frameEffect: 'easeInOutExpo',
		overlayEffect: 'easeInOutExpo',
		overlayEffectDuration: 800
	});
	
}


