// JavaScript Document

var CURRENT_POS = 0;

$(document).ready(function(){
	initMonkForms();
	if(!hasFlash){
		$('.no_flash').show();
		initNoFlashNav();
	}else{
		initSideNavigation();
		initFlashSlideshow();
	}
	initLightBox();
	initThickboxContent();
	
	$("#category_dropdown a").click(function(e) {
		e.preventDefault();
		$("#category_hidden").attr("value", $(this).attr("rel"));
		$("#category_title").text($(this).text());
		$("#category_dropdown").css("display", "none");

		$("#category_dropdown").animate({borderTop: "0px"}, 500, function() {
			$("#category_dropdown").removeAttr("style");
		});
	});
	
	$(".product_addtocart").each(function() {
		$(this).find("input[type='image']").attr("src", "/images/addtocart2.png").attr("value", "Add to Cart");
	});
	
	var howmany = $("#buckets_inner .bucket").size();
	var width = parseInt(320*howmany);
	$("#buckets_inner").width(width);
	
	$(".featured_navi li a").bind("click", function(e) {
		e.preventDefault();
		$("#buckets_inner").stop();
		$(".featured_navi li a").removeClass("active");
		$(this).addClass("active");
		var which = $(this).attr("rel");
		newmargin = "-"+parseInt((960*which)-960);
		$("#buckets_inner").animate({marginLeft: newmargin}, "slow", function() {
		
		});
	});
});

function initNoFlashNav(){
	var navHTML = $('#navigation').html();
	$('#no_flash_nav').html(navHTML);
	$('#navigation').html('');
	
	// Center-align the rollovers
	var navWidth = 0;
	$('#nav>li').each(function(i,elem){
		var $w = $(elem).outerWidth()/2;
		navWidth += $(elem).outerWidth() + 30; // 15px margin on either side
		try{
			var $ul = $(elem).find('ul');
			var $ulw = $ul.outerWidth()/2
			var $newpos = $w - $ulw;
			$ul.css("margin-left", $newpos);
		}catch(e){}
	});
	
	// Center the entire nav
	var offset = (960 - navWidth - 30)/2; // subtract the margins from the leftmost and rightmost nav items
	$('#nav').css('margin-left', offset);
}

function clicklink() {
	$("#login_btn").trigger("click");
}

function clicklogout() {
	window.location = $("#logout_btn").attr("href");
}

function initNoFlashGallery(){
	$('#jq_gallery').cycle({
		fx: 'fade',
		timeout: 6000
	});
}

function initHomeLinksSlider(){
	var totalItems = $("#gi_links_wrapper ul").length;
	ITEM_WIDTH = 168;
	var containerWidth = ITEM_WIDTH * totalItems;
	$("#gi_links_wrapper").css("width", containerWidth);
	$("#btnPrev").attr("class", "inactive");
	if(totalItems > 2){
		MAX_POS = totalItems - 2;
		$("#btnNext").click(function(){ doSlide("next"); return false; });
	}else{
		$("#btnNext").attr("class", "inactive");
	}
}

function doSlide(direction){
	if(direction == "prev"){
		CURRENT_POS--;
		$("#gi_links_wrapper").animate({"left": "+="+ITEM_WIDTH});
		$("#btnNext").unbind("click");
		$("#btnNext").click(function(){ doSlide("next"); return false; });	
		$("#btnNext").attr("class", "");
		if(CURRENT_POS == 0){
			$("#btnPrev").unbind("click");	
			$("#btnPrev").attr("class", "inactive");
		}
	}else{
		CURRENT_POS++;
		$("#gi_links_wrapper").animate({"left": "-="+ITEM_WIDTH});
		$("#btnPrev").unbind("click");
		$("#btnPrev").click(function(){ doSlide("prev"); return false; });	
		$("#btnPrev").attr("class", "");
		if(CURRENT_POS == MAX_POS){
			$("#btnNext").unbind("click");
			$("#btnNext").attr("class", "inactive");
		}
	}
}

function initSideNavigation(){
	var lis = $('#subnav').children('li');
	var li = lis[0];
	$(li).children('a').wrap('<span id="sIFR-me"></span>');
	sIFR.replace(gotham, {
		selector: '#subnav #sIFR-me',
		css : {
			'.sIFR-root':{'color':'#CC0033','font-size':'15px','text-transform':'uppercase'},
			'a':{'color':'#CC0033','text-decoration':'none'},
			'a:hover':{'color':'#CC0033','text-decoration':'underline'}
		},
		wmode: 'transparent'
	});
}

function initMonkForms(){
	var max_width = 0;  
	$(".monkForm label").each(function(){  
		if ($(this).width() > max_width)  
			max_width = $(this).width();  
			
	});
	$("#formsubmit input").css("marginLeft", parseInt(max_width+30));
	$(".monkForm label").width(max_width);  
	$('form.monkForm div.required label').append(' <span title="Required Field" class="required">*</span>');
	if($('form.monkForm div.checkbox.required').length > 0){
		$('form.monkForm').submit(function(){
			var validated = true;
			$('form.monkForm div.checkbox.required').each(function(i,elem){
				if($(elem).find('input:checked').length == 0){
					validated = false;
					if($(elem).find('p.data label .required').length <= 1){
						$(elem).find('p.data label').append('&nbsp;&nbsp;&nbsp;<span class="required">Required</span>');
					}
				}
			});
			if(!validated){
				alert('Please be sure to fill in all required items before continuing.');
			}
			return validated;
		});
	}
}

function initLightBox(){
	//var imgPrev = (isIE(6)) ? '/images/photo_prev.gif' : '/images/photo_prev.png';
	//var imgNext = (isIE(6)) ? '/images/photo_next.gif' : '/images/photo_next.png';
	var imgPrev = '/images/lightbox-btn-prev.gif';
	var imgNext = '/images/lightbox-btn-next.gif';
	$(function() {
		$("a[rel='lightbox']").lightBox({
			imageLoading: '/images/lightbox-ico-loading.gif',
			imageBtnClose: '/images/lightbox-btn-close.gif',
			imageBtnPrev: imgPrev,
			imageBtnNext: imgNext,
			fixedNavigation: true
		});
	});
}

function initFlashSlideshow(){
	$('.flash_slideshow').each(function(i,elem){
		var slug = $(elem).find('.slug').text();
		if(slug){
			var flvars = {'fileName':slug};
			var params = {'menu':'false','wmode':'opaque'};
			var attrib = {'id':'slideshow'+i,'name':'slideshow'+i};
			swfobject.embedSWF("/swf/LI_insetText.swf", "slideshow"+i, "450", "230", "9", "", flvars, params, attrib);
		}
	});
}

function initThickboxContent(){
	var contentBlocks = $('.thickbox_content');
	if(contentBlocks.length > 0){
		$('a.thickbox').each(function(i,elem){
			if($(elem).attr('href') == '#'){
				if(contentBlocks[i] != null){
					$(contentBlocks[i]).wrap('<div id="myTB'+i+'" class="thickbox_content"></div>');
					$(contentBlocks[i]).attr('class','');
					$(elem).attr('href','#TB_inline?height=300&width=450&inlineId=myTB'+i);
				}else{
					$(elem).attr('class','');
				}
			}
		});
	}
}

function emailPopup_ToggleAnonymous(){
	$('#email_popup .toggle').toggle();
}
		
function emailPopup_Submit(){
	var options = {target:'#ajax_response'};
	$('#emailForm').ajaxForm();
	$("#emailForm").ajaxSubmit(options);
}

function emailPopup_Close(){
	TB_remove();
}

