$(document).ready(function () {
    $('#lower').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 ($(this).hasClass('soldOut'))
            return;

        if ($('#select-color option[value=' + $(this).attr("buy_id") + ']').length)
            $('#select-color').val($(this).attr("buy_id"));

        if ($(this).parent().parent().parent().attr('class') == "swatches") {
            $('#select-color2-' + $(this).parent().parent().parent().attr('parent_color')).val($(this).attr("buy_id"));
            setSecondColor($(this).attr("buy_id"));
        }
        else {
            $(this).setActiveColor();
        }
    });

    $('#select-color').click(function () {
        $('.swatch[buy_id="' + $(this).val() + '"]').setActiveColor();
    }).trigger('click');

    $('#select-color').change(function () {
        $('.swatch[buy_id="' + $(this).val() + '"]').setActiveColor();
    });

    $('select[id^=select-color2]').click(function () {
        setSecondColor($(this).val());
    });

    $('select[id^=select-color2]').change(function () {
        setSecondColor($(this).val());
    });

    $('select[id^=select-color1]').click(function () {
        var before = $('.secondColor select:visible option:selected').text();

        $('.secondColor').hide();
        $('#color_' + $(this).val()).show();

        $('.secondColor select:visible option').each(function () {
            if (before == $(this).text()) {
                $('.secondColor select:visible').val($(this).attr('value'));
            }
        });

        setSecondColor($('#select-color2-' + $(this).val()).val());

        $('.swatches').hide();
        $('#swatches_' + $(this).val()).show();
    }).trigger('click');

    $('select[id^=select-color1]').change(function () {
        var before = $('.secondColor select:visible option:selected').text();

        $('.secondColor').hide();
        $('#color_' + $(this).val()).show();

        $('.secondColor select:visible option').each(function () {
            if (before == $(this).text()) {
                $('.secondColor select:visible').val($(this).attr('value'));
            }
        });

        setSecondColor($('#select-color2-' + $(this).val()).val());

        $('.swatches').hide();
        $('#swatches_' + $(this).val()).show();
    });

    $('.topSwatch').click(function () {
        if ($(this).hasClass('soldOut'))
            return;

        $('#select-color1').val($(this).attr('top_color_id'));

        var before = $('.secondColor select:visible option:selected').text();

        $('.secondColor').hide();
        $('#color_' + $(this).attr('top_color_id')).show();

        $('.secondColor select:visible option').each(function () {
            if (before == $(this).text()) {
                $('.secondColor select:visible').val($(this).attr('value'));
            }
        });

        setSecondColor($('#select-color2-' + $(this).attr('top_color_id')).val());

        $('.swatches').hide();
        $('#swatches_' + $(this).attr('top_color_id')).show();
    });

    $('.temp-hide').removeClass('temp-hide');

    $('#coming_soon_signup').submit(function () {
        if ($("#coming_soon_signup input:checked").length > 0) {
            return true;
        } else {
            alert("Please select a color.");
            return false;
        }
    });

    $('#time_learn_more').click(function () {
        var a = function () {
            $('#fancy_close').hide();
            $('#fancy_overlay').fadeOut('fast');
            $('#fancy_close').hide();
            $('#fancy_outer').fadeOut('fast', function () {
                if (!($.browser.msie && $.browser.version == "6.0"))
                    $('#fancy_outer').css('position', 'absolute');

                $('#fancy_outer').css('margin-left', '0px').css('margin-top', '0px');
            });

            return false;
        };

        $('#fancy_overlay').click(a);
        $('#fancy_overlay').css('opacity', '0.3').fadeIn('fast', function () {
            $('#fancy_close').show();
        });

        $('#fancy_close').click(a);

        $('#fancy_outer').css('left', '50%').css('margin-left', '-270px').css('top', '50%').css('margin-top', '-75px').css('width', '540px').css('height', '150px').fadeIn('fast');
        if (!($.browser.msie && $.browser.version == "6.0"))
            $('#fancy_outer').css('position', 'fixed');

        $('#fancy_content').css('top', '10px').css('right', '10px').css('bottom', '10px').css('left', '10px').css('width', 'auto').css('height', 'auto');
        $('#fancy_content').html('<h1>Shark Eye</h1>');

        return false;
    });

    if (document.cookie != '') {
        $.each(document.cookie.split(';'), function (index, value) {
            var parts = value.split('=');
            $.each(parts, function (index2, value2) {
                if (jQuery.trim(value2) == 'swatchcookie') {
                    if ($('.swatch[buy_id="' + parts[1] + '"]').length > 0) {
                        $('#select-color').val(parts[1]);
                        $('.swatch[buy_id="' + parts[1] + '"]').setActiveColor();
                    }
                }
            });
        });
    }
});

function setSecondColor(buy_id) {
    $('.swatch[buy_id="' + buy_id + '"]').setActiveColor();
    $('#buyid').val(buy_id);
}

// 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");
		changeimage(color_id);
	}
	
	return $(this);
}

function changeimage(color_id){

		$('p#sku').html("<strong>SKU:</strong> "+$('img.swatch[color_id="'+color_id+'"]').attr("sku"));
		$("#optn0x0").val(color_id);
		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();
		}
}

function validateSignup(thisform) {
	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;
	}

	if(validateForm(thisform) == false)
	{
		return false;
	}
}
