$(document).ready(function(){
	// Swap body class
	$('body').addClass('js').removeClass("nojs");
	
	//search box text
	$("#searchStr")
		.bind('focus',function(){
			$(this).siblings("span").hide();	//on focus, hide label
			if($(this).val() == $(this).siblings("span").html())$(this).val('');	//on focus, remove value if default
		})
		.bind('blur',function(){
			if($(this).val()=="")$(this).siblings("span").show();	//on blur, show label if blank
		});
	if($("#searchStr").val()=="")$("#searchStr").blur();	//default
	
	//discount box text
	$("#coupon")
		.bind('focus',function(){
			//$(this).siblings("span").hide();	//on focus, hide label
			if($(this).val() == $(this).attr('title'))$(this).val('');	//on focus, remove value if default
		})
		.bind('blur',function(){
			if($(this).val()=="")$(this).val($(this).attr('title'));	//on blur, show label if blank
		});
	if($("#coupon").val()=="")$("#coupon").blur();	//default
	
	//ie6.fix();
	// nav
	$('#nav ul li.active').prev().addClass('nobg');
	$('#nav ul li, .pag ul li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	banner.init();
	searchfilter.init();
	if($('div#product-suggestions-top div ul.tabs li a').length>0) {
		swoptabs.init();
	}
	if($('div#info ul.nav li a').length>0) {
		swopprodtabs.init();
	}
	$("#header #cart-added #close-dialog").click(function(){$(this).parent().removeClass("visible")});
	$("#header #cart-added #continue-shopping").click(function(){$(this).parent().removeClass("visible")});
	
	//LightBox
	if($('#images div a').length>0) {
		$('#images div a').lightBox();
	}
	
	/**
	* @section Gallery Images
	* @uses crossfade() (optionally)
	* --------------------------------------------------------------------------------------------------
	*/
	$gallery			= $('#images');
	$galleryImage		= $('div a img', $gallery);
	$galleryZoom		= $('div a', $gallery);
	galleryImageFading	= false;
	
	$gallery.bind('click.gallery', {active: 'active'}, function(event){
		var $$, $a, src;
		
		$$		= $(event.target).closest('li');
		$a		= $('a', $$);
		src		= $a.attr('href');
		
		if($$.is(':not(.' + event.data.active + ' , .no-image)')) {
		
			$$.addClass(event.data.active).siblings().removeClass(event.data.active);
			
			if($.fn.crossfade && galleryImageFading === false) {
				galleryImageFading = true;
				$('div a img', $gallery).crossfade({
					target:		src,
					animated:	true,
					duration:	1250,
					callback:	function() {
						galleryImageFading = false;
					}
				});
				//$galleryZoom.attr('href', src);
			}
			else {
				$galleryImage.attr('src', src);
				//$galleryZoom.attr('href', src);
			}
			
			src = src.replace('&w=350&h=350&zc=1&q=75','&w=936&zc=0&q=75');
			$galleryZoom.attr('href', src);

		}
		event.preventDefault();
		$a.blur();
		
	});
	
	/**
	* @section Carousel
	* @uses carousel()
	* @uses jcarousellite()
	* @see http://www.gmarwaha.com/jquery/jcarousellite/
	* --------------------------------------------------------------------------------------------------
	*/
	if($.fn.carousel && $.fn.jCarouselLite) {
		$('.scroll ul').carousel({
			'auto':			false, // 3000
			'circular':		true,
			'speed':		500,
			'visible':		6,
			'scroll':		1,
			'btnPrev':		'#sLeft',
			'btnNext':		'#sRight'
		});
	}
		
		
	
});
/*
ie6={
	fix:function(){
		if($.browser.msie && $.browser.version<7){ 
			ie6.ie6hover();	//fix ie6 hover
			ie6.cssplus();	//fix ie6 + selector
			ie6.iframemagic();	//fix ie with iframe
		}
	},
	ie6hover:function(){	
		$("#nav ul li.nav-1").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});	
	},
	cssplus:function(){
		$("#nav ul li.nav-1-active + li a").css({
			'border-left':'0px'
		});
	},
	iframemagic:function(){
		$("body").append("<iframe id='jam' frameborder='0' allowtransparency='true' src='jam.html'></iframe>");
		$("#header #nav ul li.nav-1 div").hover(function(){alert('iframemagic 2');
			$("#jam").show();
		},function(){
			$("#jam").hide();
		});
	}
}*/

banner={
	options:{
		autorotate:true,	//make banner cycle through automatically: true|false
		pauseonhover:true,	//pause cycling on mouseover: true|false (no effect if autorotate is false)
		stoponclick:true,	//stop cycling automatically if the user uses the picker: true|false (no effect if autorotate is false)
		showfor:3000,		//how long should each banner be shown for: number of milliseconds
		animatefor:1000		//how long should the animation take? number of milliseconds (must be less than showfor [above], else weirdness will ensure)
	},
	current:null,
	ticker:null,
	buildcontrols:function(){
		//controls should be built with js, since they have no function if js is not enabled (in which case, the first banner image will be shown)
		$("#banner").prepend('<div class="corner topleft"></div><div class="corner topright"></div><div class="corner bottomleft"></div><div class="corner bottomright"></div>');
		$("#banner").append('<ul id="picker"></ul>');
		$("#banner #images li").each(function(){
			$("#banner #picker").append('<li><a href="#">' + (($(this).index()*1)+1) + '</a></li>');
		});
		$("#banner #images li a").click(function(){
			$(window.location).attr('href', $(this).attr('href'));
		});
		$("#banner #picker li").eq(0).attr('id','first-picker');
		$("#banner #picker li").eq(-1).attr('id','last-picker');
		$("#banner #picker li").eq($("#banner #images li.active").index()).addClass("active");
		$("#banner #picker li a").click(function(){
			banner.jumpto($(this).parent().index());
			return false;
		});
	},
	init:function(){
		banner.buildcontrols();
		if(banner.options.autorotate){
			banner.ticker=setInterval("banner.next()",banner.options.showfor);
			if(banner.options.pauseonhover){
				$("#banner").hover(function(){
					if(banner.ticker!="disabled")clearInterval(banner.ticker);
				},function(){
					if(banner.ticker!="disabled")banner.ticker=setInterval("banner.next()",banner.options.showfor);
				});
			}
		}
	},
	next:function(){
		banner.current=$("#banner #images li.active").index();
		if(banner.current==$("#banner #images li").length-1)banner.current=-1;
		$("#banner #images li").eq(banner.current+1).fadeIn(banner.options.animatefor,function(){$(this).addClass("active")});
		$("#banner #images li").eq(banner.current).fadeOut(banner.options.animatefor,function(){$(this).removeClass("active")});
		$("#banner #picker li").eq(banner.current+1).addClass("active");
		$("#banner #picker li").eq(banner.current).removeClass("active");
	},
	jumpto:function(n){
		banner.current=$("#banner #images li.active").index();
		$("#banner #picker li.active").removeClass("active");	//patch preventing multiple active banners
		$("#banner #images li.active").removeClass("active").hide();
		$("#banner #images li").eq(banner.current).fadeOut(banner.options.animatefor,function(){$(this).removeClass("active")});
		$("#banner #images li").eq(n).fadeIn(banner.options.animatefor,function(){$(this).addClass("active")});
		$("#banner #picker li").eq(banner.current).removeClass("active");
		$("#banner #picker li").eq(n).addClass("active");
		if(banner.options.stoponclick && banner.ticker){
			clearInterval(banner.ticker);
			banner.ticker="disabled";
		}
	}
}

searchfilter={
	init:function(){
		$("#site-search-dropdown").append("<div id='search-dropdown-replace'><div class='current'>Loading...</div><ul></ul></div>");
		$("#site-search-dropdown option").each(function(){
			$("#search-dropdown-replace ul").append("<li><a href='#'>" + $(this).html() + "</a></li>");
		});
		$("#search-dropdown-replace").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
		$("#site-search-dropdown .current").html($("#header #site-search-dropdown option:selected").html());
		
		$("#search-dropdown-replace ul li a").click(function(event){
			$("#site-search-dropdown .current").html($(this).html());
			
			var currentHTML = $("#site-search-dropdown .current").html();
			$("#site-search-dropdown #search-department option").each(function(){
				if($(this).html() == currentHTML){
					var optValue = $(this).val();
					$('#site-search-dropdown #search-department option[value='+optValue+']').attr("selected","selected"); 
				}
			});
			//console.log($("#site-search-dropdown option:selected").html());
			event.preventDefault();
		});
		
		$("#site-search-dropdown select").bind('change keyup',function(){
			$("#site-search-dropdown .current").html($("#site-search-dropdown option:selected").html());		
		});
		
		$("#search-department").hide();
	}
}

/*
swoptabs={
	init:function(){
		//var tabContainers = $('div#product-suggestions-products div.products');
		var tabContainers = $('div#product-suggestions-products div.products');
		console.log(tabContainers);

		$('div#product-suggestions-top div ul.tabs li').click(function () {
				$('div#product-suggestions-top div ul.tabs li').removeClass('active');
				$(this).addClass('active');
				var link = $(this).children("a:first").attr("href");
				//alert(link);
				console.log(link);
				tabContainers.fadeIn(1000,function() {
									console.log('fadeOut');
									//console.log(tabContainers.filter(link));
													//tabContainers.filter(link).fadeOut(1000);
												  });
				return false;
		});
	}
}
*/
swoptabs={
	init:function(){
		$('div#product-suggestions-top div ul.tabs li a').click(function() {
			//console.log($(this));
			var link = $(this).attr("href");
			//console.log(link);
			$(this).parent().parent().children('.active').removeClass('active');
			$(this).parent().addClass('active');
			
			//console.log($('div#product-suggestions-products'));
			$('div#product-suggestions-products').children('.products').fadeOut('slow',function() {
				//console.log($(this));
				$(this).removeClass('active');
			});
			
			$('div#product-suggestions-products').children('#'+link+'').fadeIn(1000,function() {
				$(this).addClass('active');
			});
			
			return false;
		});
	}
}

swopprodtabs={
	init:function(){
		$('div#info ul.nav li a').click(function() {
			//console.log('greg');
			//console.log($(this));
			var link = $(this).attr("href");
			//console.log(link);
			$(this).parent().parent().children('.active').removeClass('active');
			$(this).parent().addClass('active');
			
			//console.log($('div#product-suggestions-products'));
			$('div#info').children('.info').fadeOut('fast',function() {
				//console.log($(this));
				$(this).removeClass('active');
			});
			
			$('div#info').children('#'+link+'').fadeIn('normal',function() {
				$(this).addClass('active');
			});
			
			return false;
		});
		
		$('div#upsales ul.nav li a').click(function() {
			//console.log($(this));
			var link = $(this).attr("href");
			//console.log(link);
			$(this).parent().parent().children('.active').removeClass('active');
			$(this).parent().addClass('active');
			
			//console.log($('div#product-suggestions-products'));
			$('div#upsales').children('.products').fadeOut('fast',function() {
				//console.log($(this));
				$(this).removeClass('active');
			});
			
			$('div#upsales').children('#'+link+'').fadeIn('normal',function() {
				$(this).addClass('active');
			});
			
			return false;
		});
	}
}

function showProdTab(elm){
	//console.log(elm);
	var link = $(elm).attr("href");
	//console.log(link);
	
	if($('div#info').length>0) {
		$('div#info ul.nav li.active').removeClass('active');
		$('div#info ul.nav li a').each(function(i, element){
			var $$ = $(this);
			if($$.attr('href') == link){
				$$.parent().addClass('active');
			}
			
		});
		
		$('div#info').children('.info').fadeOut('fast',function() {
			//console.log($(this));
			$(this).removeClass('active');
		});
		
		$('div#info').children('#'+link+'').fadeIn('normal',function() {
			$(this).addClass('active');
		});
			
	}
	
	return false;
}

function incBasket(elm, basket) {
	if(jQuery(elm)) {
		var qty;
		qty = parseInt(jQuery(elm).val());
		
		jQuery(elm).val(qty + 1);
		
		if(basket==true) {
			submitDoc('cart');
		}
	}
}

function decBasket(elm, basket, limit) {
	if(jQuery(elm)) {
		var qty;
		qty = parseInt(jQuery(elm).val());
		
		if(qty>limit) {
			jQuery(elm).val(qty - 1);
		}
		if(basket==true) {
			submitDoc('cart');
		}
	}
}

