var currentProdList = 0;
var prodLists = {};

function toggleLayer(id) {
	if (document.getElementById) {
		var elm = document.getElementById(id);
		if (elm) {
			if (elm.style.display == "" || elm.style.display == "block") {
				elm.style.display = "none";
			} else {
				elm.style.display = "";
			}
		}
	}
}

function enlargePhoto(id, label) {
	_gaq.push(['_trackEvent', 'Products', 'Enlarge Photos', label]);
	var enlargeWin = window.open('/products/enlarge.php?imageId='+id,'GardnerWhitePhoto','width=630, height=490, scrollbars=no');
	if (enlargeWin.focus) enlargeWin.focus();
}

function sendFriend(type,id,parentId) {
	if (type && id) var label = type.substr(0,1).toUpperCase() + type.substr(1) + ": " + id;
	_gaq.push(['_trackEvent', 'Products', 'Send to a Friend', label]);
	var friendWin = window.open('/products/friend.php?type='+type+'&id='+id+'&parentCategoryId='+parentId,'GardnerWhiteFriend','width=530, height=300, scrollbars=no');
	if (friendWin.focus) friendWin.focus();
}

function weeklyAd() {
	var weeklyWin = window.open('/weekly/frameset.php','GardnerWhiteWeeklyAd','width=765, height=700, scrollbars=yes');
	if (weeklyWin.focus) weeklyWin.focus();
}

var Tooltip = {
	showTimeouts: new Array(),
	hideTimeouts: new Array(),
	
	show: function(id) {
		if (document.getElementById) {
			if (Tooltip.hideTimeouts[id]) clearTimeout(Tooltip.hideTimeouts[id]);
			Tooltip.hideTimeouts[id] = null;
			Tooltip.showTimeouts[id] = setTimeout('Tooltip.showNow('+id+')', 1500);
		}
	},
	hide: function(id) {
		if (document.getElementById) {
			if (Tooltip.showTimeouts[id]) clearTimeout(Tooltip.showTimeouts[id]);
			Tooltip.showTimeouts[id] = null;
			Tooltip.hideTimeouts[id] = setTimeout('Tooltip.hideNow('+id+')', 100);
		}
	},
	showNow: function(id) {
		var e = document.getElementById('tooltip'+id);
		if (e) e.style.display = "block";
	},
	hideNow: function(id) {
		var e = document.getElementById('tooltip'+id);
		if (e) e.style.display = "none";
	}
}

function itemLoadCallbackFunction (carousel, state) {
	var list = prodLists[currentProdList];
	
	for (var i = carousel.first; i <= carousel.last; i++) {
        // Check if the item already exists        
        if (carousel.has(i)) continue;
        
        if (i > $(list).find('li').length || i <= 0) break;
        else {
            // Add the item
            var item = $(list).find('li:nth-child('+i+')')[0];
            if (item) carousel.add(i, item.innerHTML);
        }
    }
}

$(document).ready(function(){
	
	$('.productGroups').css('display','block').find('ul.groupNav').css('visibility','visible').each(function(){
		var width = 0;
		$(this).find('li').each(function(){
			width += $(this).width();
			$(this).append('<em class="indicator"><!--!--></em>');
		});
		
		$(this).css('width',width+5);
	});
	
	if ($('.productGroups.scroll').length) {		
		prodLists = $('.productGroups ul.products');
		
		var carouselDefaults = {
			size: $(prodLists[currentProdList]).find('li').length,
			itemLoadCallback: itemLoadCallbackFunction
		};
		
		$('<ul id="productCarousel" class="products jcarousel-skin-products"></ul>').appendTo('div.productGroups').jcarousel(carouselDefaults);
		
		$('.productGroups .groupNav a').click(function(){
			$(this).parents('ul').find('.selected').removeClass('selected');
			currentProdList = $(this).parents('li').addClass('selected').prevAll().length;
			
			$('#productCarousel').jcarousel('reset');
			
			return false;
		});
	} else if ($('.productGroups').length) {
		$('.productGroups .groupNav a').click(function(){
			var section = $(this).parents('.productGroups');
			
			$(section).find('.groupNav li.selected').removeClass('selected');
			
			var group = $(this).parents('li').addClass('selected').attr('id').replace('_group','');
			
			$(section).find('.products').hide();
			$('#'+ group +'_products').fadeIn(1000);
			
			return false;
		});
	}
	
	$('input.revert').focus(function(){
		if ($(this).val() == $(this)[0].defaultValue) $(this).val('');
	}).blur(function(){
		if (!$(this).val().length) $(this).val($(this)[0].defaultValue);
	});
	
	$('#deliveryCheck input[type=text]').focus(function(){
		$(this).css('color','#000000');
	}).blur(function(){
		$(this).css('color','#999999');
	});
	
});