// $cmignore - collage

// Resig's color plugin
(function(jQuery){
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}
			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});
	function getRGB(color) {
		var result;
		if ( color && color.constructor == Array && color.length == 3 )
			return color;
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];
		return colors[jQuery.trim(color).toLowerCase()];
	}
	function getColor(elem, attr) {
		var color;
		do {
			color = jQuery.curCSS(elem, attr);
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 
			attr = "backgroundColor";
		} while ( elem = elem.parentNode );
		return getRGB(color);
	};
	var colors = "";	
})(jQuery);

// make the 'contains' function case insensitive
jQuery.expr[':'].Contains = function(a,i,m){
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};

$(document).ready(function(){
// <------------ first, global actions ------------>
$('body').addClass('jsEnabled');

$("a[rel='external']").click(function() {
	window.open(this.getAttribute("href"), "popup", "");
	return false;
});

$('#siteSearch input').click(function() {
	if ($(this).val() == "SEARCH SITE") {
		$(this).val("");
	}
});

//site nav hover animation
$('#nav a').hover(function() {
	$(this).stop().animate({color: '#f2eee9'}, 200).addClass('active');
},
function () {
	$(this).stop().animate({color: '#232015'}, 200).removeClass('active');
});

// <------------ second, page specific stuff ------------>
if ($('body').attr('id')=="home") 
{	

// preload big images for better perfromance
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
$.preloadImages("../images/creamBckgMiddle.png", "../images/creamBckgMiddle.png", "../images/creamBckgMiddleLeftNav.png", "../images/bannerDesigIntPaints.jpg", "../images/faqBanner.jpg", "../images/creamBckgMiddleLeftNavFAQ.png");	

$('.fullRight ul').hover(function() {
	$(this).stop().animate({bottom: '0px'}, 400);
},
function () {
	$(this).stop().animate({bottom: '-115px'}, 250);
});

	
}
else if ($('body').attr('id')=="criteria")  // page specific branching
{

var mainCats = $('#prodNav .header');
var defLists = $('.section');
var selectCat = "";
var tempText = "";
var startString = "";

//hides current category definition, then displays just selected one
function selectDefList (selectCat) {
	$('.displayedDefList').hide(150);
	$('body').animate({margin: '0'}, 300,
		function () {
			$('#' +  selectCat).show(350).addClass('displayedDefList');
			$('html, body').animate( { scrollTop: 0 }, 0 );
		}
	);
}

//look for local anchor in URL and if found select the target category definition, otherwise, select the first category definition
function initPage () {
	//counter IE opacity anim bug
	if (jQuery.browser.msie) {
		if (jQuery.browser.version<9) {
			$('dt').css({'filter':'alpha(opacity=100)' , 'background':'#f2efea'});
			if (jQuery.browser.version<9) {
				$('dd').css({'filter':'alpha(opacity=100)' , 'background':'#f2efea'});
			}
		}
	}	
	//logic to open correct link based on query string or go with default first otherwise:
	var tempText = document.location.href;
	if (tempText.indexOf("#") == -1) {
		selectCat = $('#prodNav > li:first').addClass('activeLiArrow').children('ul').css('display', 'block').show(0).children('li').eq(0).addClass('activeLi').find('a').attr('href').substring(1);
		selectDefList (selectCat);
		
	} else {
		var startString = tempText.indexOf("#") + 1;
		selectCat = tempText.substring(startString);
		selectDefList (selectCat);
		$(mainCats + "a[href='#" + selectCat +"']").parent().addClass('activeLi').parent().css('display', 'block').show(0).parent().addClass('activeLiArrow');
	}
}
	
mainCats.each(function () {
	$(this).next('ul').hide(0);
});
defLists.hide(0);
initPage ();

//respond to clicking category headers by expanding collapsing their children
mainCats.click(function() {
	if ($(this).parent().hasClass('activeLiArrow')) 
	{	
	//	$(this).next('ul').css('display', 'block').stop().hide(250).end().parent().removeClass('activeLiArrow');
		$(this).next('ul').stop().hide(250).end().parent().removeClass('activeLiArrow');
	}
	else
	{	
		//this is only branched to handle an animation bug in IE8
		if (jQuery.browser.msie) {
			if (jQuery.browser.version==8) {
				$(this).next('ul').css('display','block').show(250).end().parent().addClass('activeLiArrow');
			}
			else
			{
				$(this).next('ul').show(250).end().parent().addClass('activeLiArrow');
			}
		}
		else
		{
			$(this).next('ul').show(250).end().parent().addClass('activeLiArrow');	
		}

		//if there is only one child in the current category go ahead and display it as the new category definition
		if ($(this).next('ul').children('li').length == 1) {
			$('.activeLi').removeClass('activeLi');
			selectCat = $(this).next('ul').find('a').eq(0).parent().addClass('activeLi').end().attr('href').substring(1);
			selectDefList (selectCat);
		}
	}
});


//respond to clicking category children by display a new category definition
$('#prodNav a').click(function(event) {
	event.preventDefault();
	$('.activeLi').removeClass('activeLi');
	selectCat = $(this).parent().addClass('activeLi').end().attr('href').substring(1);
	selectDefList (selectCat);	
});
	
}	
else if ($('body').attr('id')=="products")  // page specific branching
{
		
var searchString = "";
var brandsTab = $('#brandsTab');
var catTab = $('#catTab');
var selectLabel = $('#productFilter label');
var selectCategory = $('#selectCategory');
var selectBrand = $('#selectBrand');
var prodList = $('.prodList li');
var tempText = "";
var startString = "";
var searchType = "";

//fade all products out then display the ones that have a class matching the function argument
function displayProducts (_searchString) {
	prodList.animate({opacity: '0'}, 200,
		function () {
			//
			if ($('#productFilter select:visible').val() == "viewAll") {
				prodList.css('display', 'block');
				prodList.animate({opacity: '1'}, 250);
			}
			else
			{
				prodList.css('display', 'none');
				prodList.each(function () {
					if ($(this).hasClass(_searchString)) $(this).css('display', 'block').animate({opacity: '1'}, 250);
				});
				$('#productFilter select:visible option:eq(0)').text("View all products");
			}
			$('.firstEl').removeClass('firstEl');
			$('.prodList li:visible').eq(0).addClass('firstEl');
		}
	);
}

//alter the select list being displayed, hide all the products, and call displayProducts()
function updateSelect(newTab, oldTab, newSelect, oldSelect, initVal, initTab) {
	//specific action if this is the page load and the URL includes a local anchor  
	if (!(initVal == "")) {
		displayProducts(initVal);
		oldSelect.css('display','none');
		newSelect.css('display','block');
		$(initTab + ' option[value="' + initVal + '"]').attr('selected', 'selected');
	}	
	else {	
		newSelect.val(0);
		oldSelect.animate({opacity: '0'}, 100,
			function () {
				oldSelect.animate({opacity: '0'}, 200,
					function () {
						oldSelect.css('display','none');
						newSelect.css('display','block').animate({opacity: '1'}, 150,
							function () {
								prodList.animate({opacity: '0'}, 100,
									function () {
										prodList.css('display','block').animate({opacity: '1'}, 150);
										$('html, body').animate( { scrollTop: 0 }, 0 );
										//am I sure that I need this:
										$('.prodList li:visible').eq(0).addClass('firstEl');
									}
								)
							}
						);
					}
				);
			}
		);
	}
	
	// if (newTab.attr('id') == 'catTab') {
	// 	newTab.addClass('active').next('select:visible option:eq(0)').text("Please Select a Brand");
	// }	
	// else {
	// 	newTab.addClass('active').next('select:visible option:eq(0)').text("Please Select a Category");
	// }
	
	//Move active class to new tab and slide old one down
	newTab.addClass('active')	
	oldTab.stop().animate({height: '25px',paddingTop: '10px'}, 200,
		function () {
			oldTab.removeClass('active');
		}
	);
}

function initProdPage () {
	//counter IE opacity anim bug
	if (jQuery.browser.msie) {
		if (jQuery.browser.version<9) {
			$('.prodList li').css({'filter':'alpha(opacity=100)' , 'background':'#F6F5F2'});
		}
	}
	//if the URL includes a local anchor grab the value and call updateSelect() with the arguments for the search string and search type,
	// otherwise call updateSelect() with an empty search string and initial tab settings
	var tempText = document.location.href;
	if (tempText.indexOf("#-") == -1) {
		updateSelect(brandsTab, catTab, selectBrand, selectCategory,"");
	} else {
		startString = tempText.indexOf("#") + 1;
		searchString = tempText.substring(startString);
		searchType = tempText.slice(startString, startString + 3);
		if (searchType == '-BD') {
			updateSelect(brandsTab, catTab, selectBrand, selectCategory, searchString, "#selectBrand");
		}
		else if (searchType == '-CA') {
			updateSelect(catTab, brandsTab, selectCategory, selectBrand, searchString, "#selectCategory");
			selectLabel.text('choose category:');
		}
	}
}

initProdPage();

$('#productFilter select').change(function() {
	displayProducts($(this).val());
});

$('#brandsTab, #catTab').hover(function() {
	if ( !$(this).hasClass('active') ) {
		$(this).stop().animate({height: '29px',paddingTop: '14px'}, 200);
	}
},
function () {
	if ( !$(this).hasClass('active') ) {
		$(this).stop().animate({height: '23px',paddingTop: '10px'}, 200);
	}	
});

brandsTab.click(function() {
	if ( !$(this).hasClass('active') ) {
		updateSelect(brandsTab, catTab, selectBrand, selectCategory, "", "");
		selectLabel.text('choose a brand:');
		//$('#productFilter option:(0)').text("View all products")
		// 
		selectBrand.find('option').eq(0).addClass('test').text("All Brands");
	}
});

catTab.click(function() {
	if ( !$(this).hasClass('active') ) {
		updateSelect(catTab, brandsTab, selectCategory, selectBrand, "", "");
		selectLabel.text('choose category:');
		selectCategory.find('option').eq(0).addClass('test').text("All Categories");
		//
	}
});

}	
else if ($('body').attr('id')=="faq") // page specific branching
{

$('dd').hide(0);
$('dd').eq(0).show(1000).prev('dt').addClass('activeItem');
$('dt').click(function () {
	if (!$(this).hasClass('activeItem')) {
			$('.activeItem').removeClass('activeItem').next('dd').hide(400);
			$(this).addClass('activeItem').next('dd').show(550);
	}
});


} // end page specific code branching
}); //close doc ready

// $/cmignore
