/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	File: 				main.js
/*	Description:		Contains all master javascript code.
/*	Target browsers:	IE6+, Mozilla/Firefox, Safari, Opera
/*	Author(s):			J. Jackson | jjackson@jplcreative.com
/*	Created: 			06.08.2010
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/

/* +++++ custom scripts +++++ */
/* +++++ require jquery 1.4.2 +++++++++++++++++++++++++++++++++++++++++ */
$(function(){
	
	// hover state for search button
	$('.search-btn').hover(
		function() { $(this).addClass('hover') },
		function() { $(this).removeClass('hover') }
	);

/*	
	// hover/click states for brand navigation
	$('#brand-nav li > a').click(function(event){ 
		event.preventDefault();
	});
*/
	
	// hover/click states for brand navigation
	$('#brand-nav li:not(.active)').hoverIntent(
		function() { 
			$(this).addClass('hover');
			var parent_w = $(this).children('a').width()/2;
			var div_w = 232/2;
			var calc_w = '-' + (div_w - parent_w) + 'px';
			$(this).children('div').css('margin-left',calc_w).show('fast');
		},
		function() {
			$(this).removeClass('hover');
			$(this).children('div').hide(100);
		}
	);
	
	// hover/click states for main navigation
	$('.nav li').hoverIntent(
		function() { 
			$(this).addClass('hover');
			$(this).children('ul').slideDown('fast');
		},
		function() {
			$(this).removeClass('hover');
			$(this).children('ul').slideUp(100);
		}
	);
	
	// click event for image gallery on product pages
	$('div.gallery li.thumbs a').click(function(event) {
		event.preventDefault();
		swapThumb(this);
	});
	// click event for enlarging images on product pages (lightbox)
	$('div.gallery li.enlarge a').lightBox();
	
	// sync column heights for product detail pages
	//if(!$('#main').hasClass('home')) { syncHeights() };
	
	/*
	// use js rounded corner solution for IE browsers
	if($.browser.msie){
		DD_roundies.addRule('.rounded-top','5px 5px 0px 0px');
		DD_roundies.addRule('.rounded','5px');
		DD_roundies.addRule('.rounded-bottom','0px 0px 5px 5px');
		DD_roundies.addRule('.rounded-small-top','4px 4px 0px 0px');
		DD_roundies.addRule('.rounded-small','4px');
		DD_roundies.addRule('.rounded-small-bottom','0px 0px 4px 4px');
	}
	*/

	//hover popup for items
	$("div.product-hover img").thumbPopup({
		imgSmallFlag: "_P.JPG&w=160&h=140",
		imgLargeFlag: "_L_01.JPG&w=360&h=315",
		popupCSS: {'border': '1px solid #000000', 'background': '#FFFFFF', 'padding': '15px'},
		loadingHtml: "<span style='padding: 5px;'>Image Loading</span>"
	});

});

// image gallery for product pages
function swapThumb(element) {
	//remove and assign 'active' class
	$('div.gallery li.thumbs a').removeClass('active');
	$(element).addClass('active');
	// swap src for large image
	var new_src = $(element).attr('href');
	$('div.gallery img.large').css('visibility','hidden');
	// load new image
	// if ie 
	if($.browser.msie){ 
		$('div.gallery img.large').attr('src',new_src);
		$('div.gallery img.large').css('visibility','visible');
	}
	else {
		$('div.gallery img.large').load(new_src, function() {
  			// when load completes, swap img src and show
			$(this).attr('src',new_src);
			$(this).css('visibility','visible');
		});
	}
	// swap enlarge button
	var thumb_i =  $(element).index('div.gallery li.thumbs a')
	$('div.gallery li.enlarge a').removeClass('active');
	$('div.gallery li.enlarge a:eq(' + thumb_i + ')').addClass('active');
}

// sync column heights for product detail pages
function syncHeights() {
	if($('#left-faceted').height() < $('.interior').height()) {
		$('#left-faceted').css('height', ($('.interior').height() - 75) + 'px');
		$('img').load(function() {
			$('#left-faceted').css('height', ($('.interior').height() - 75) + 'px');
		});
	}
	else {
		$('.interior').css('height', ($('#left-faceted').height() - 75) + 'px');
		$('img').load(function() {
			$('.interior').css('height', ($('#left-faceted').height() - 75) + 'px');
		});
	}
}
