$(document).ready(function() {	
	$('#lower-tabs').tabs();
	$('#tabs').tabs();
	
	
	$('a.group').fancybox({
		'hideOnContentClick': false
	});
	
	$('.image-link').each(function() {
		$(this).after($(this).html()).html('');
	});
	
	$('.swatch-inactive').addClass('swatch-hide').removeClass('swatch-inactive');
	
	$('.product-thumbnail').click(function() {
		$(this).children('img').setMainImage();
	});
	
	$('.swatch').click(function() {
		if($('#select-color option[value='+$(this).attr("buy_id")+']').length)
			$('#select-color').val($(this).attr("buy_id"));
		
		$(this).setActiveColor();
	});
	
	$('#select-color').click(function() {
		$('.swatch[buy_id="'+$(this).val()+'"]').setActiveColor();
	});
	
	$('.temp-hide').removeClass('temp-hide');
	
	
	//Slideshow

	$("#lifestyle-images").cycle({
		next: '.prev',
		prev: '.next'
	});
	
	$(".prev").fadeTo(2500, 0);
	
	$(".prev").hover(function(){
	$(this).fadeTo("fast", 1);
	},function(){
	$(this).fadeTo("fast", 0); 
	});
	
	$(".next").fadeTo(2500, 0);
	
	$(".next").hover(function(){
	$(this).fadeTo("fast", 1);
	},function(){
	$(this).fadeTo("fast", 0); 
	});
	
});

// Sets the main image (and surrounding link) to the 
// image of the function's caller
jQuery.fn.setMainImage = function() {
	
	$('a[rel="group_skip"]').attr("rel", $('#main-product-image').attr("rel"));	
	
	var img_src = $(this).attr("src");
	$('#main-product-image').attr("href", img_src.replace('/60/', '/700/'))
		.attr("rel", $(this).prev().attr("rel"))
		.children('img').attr("src", img_src.replace('/60/', '/375/'));
	
	$(this).prev().attr("rel", "group_skip");
	
	return $(this);
}

jQuery.fn.setActiveColor = function() {
	if($('.swatch').length) {
		var color_id = $(this).attr("color_id");

		$('.sku').html("SKU#"+$('img.swatch[color_id="'+color_id+'"]').attr("sku"));
		
		if($('.swatch-show').attr("color_id") != color_id) {
			$('.swatch-show').removeClass('swatch-show').addClass('swatch-hide');
			$('.product-thumbnail[color_id='+color_id+']').removeClass('swatch-hide').addClass('swatch-show');
			$('.swatch-show').children('img:first').setMainImage();
		}
	}
	
	return $(this);
}

function validateSignup() {
	var colors = document.email_signup['colors[]'];
	var colorchecked = false;
	
	if(colors.length == undefined) {
		colorchecked = colors.checked;
	}
	else {
		for(i = 0; i < colors.length; i++) {
			if(colors[i].checked) {
				colorchecked = true;
			}
		}
	}
	
	if(colorchecked) return true;
	else {
		alert("Please select a color to be notified when it comes in stock.");
		return false;
	}
}