var config = {};

function showSwf(settings) {
    var swf = $("#" + config.movieID)[0];

    if (jQuery.isFunction(swf.callShowMedia))
		swf.callShowMedia(settings.url, settings.type);
}

function setCurrentTab(value) {
	$("#product-tabs").data("_href", value);
}

function getCurrentTab() {
	return $("#product-tabs").data("_href");
}

$(document).ready(function() {

    var isIE6 = ($.browser.msie && $.browser.version.major < 7);

    // Take care of transparency issues for IE6
    if (isIE6) {
        var div = $("#product-bar");
        var bgIMG = div.css('background-image');
        if (bgIMG.indexOf(".png") != -1) {
            var iebg = bgIMG.split('url("')[1].split('")')[0];
            div.css('background-image', 'url(/bvmodules/themes/bvc5/images/product-bg-ie6.gif)');
            //div.get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
        }
    }

    // Load the default swf
    var settings = { url: config.imageUrl, type: "image" };
    var params = { allowScriptAccess: "always", wmode: "transparent" };
    swfobject.embedSWF("/swf/product.swf", config.movieID, "789", "767", "8.0.0", "/swf/expressInstall.swf", settings, params);

    // Set up the tab event handlers
    $("#product-tabs a").each(function(i, el) {
        var a = $(el);

        var div = $(a.attr("href"));
        if (div.length == 1) {
            div.addClass("tab-content");

            // Hide all tabs, except the first
            if (i > 0) div.hide();
        }

        a.click(function() {

            var href = a.attr("href");

            if (href != "#product-photos")
                $("#product-tabs a").removeClass("selected");

            if (href != getCurrentTab()) {
                a.addClass("selected");

                switch (href) {

                    case "#product-360":
                        showSwf({ url: config.vrXmlUrl, type: "vr" });
                        break;

                    case "#product-photos":
                        a.removeClass("selected");
                        showSwf({ url: config.imageUrl, type: "image" });
                        return !window.open(config.popupUrl, 'product', 'width=870,height=700');

                    case "#product-video":
                        showSwf({ url: config.videoUrl, type: "video" });
                        break;

                    case "#product-details":
                    case "#product-email":
                        $(".tab-content").hide();
                        $(href).show();
                        showSwf({ url: config.imageUrl, type: "image" });
                        break;
                        
                    case "#product-review":
                        $(".tab-content").hide();

						
						// For some strange reason, if we use the normal
						// jQuery.show() method here, the 'write a review'
						// form will be visible underneath the review text
						// in IE7. I have no idea why this is the case.
						// The following method of showing the content works.
						                        
                        $(href).show(1, function() {
							$("#product-review-form").hide();
                        });
                        
                        showSwf({ url: config.imageUrl, type: "image" });
                        break;
                }

                setCurrentTab(href);
            }
            else {
                setCurrentTab("");
            }
            return false;
        });
    });

    // Set up Product Review stuff
    setupProductReviews();

    // Set the first link to be "selected"
    var firstTab = $("#product-tabs a:first");
    firstTab.addClass("selected");
    setCurrentTab(firstTab.attr("href"))


});

function setupProductReviews() 
{
    var form = $("#product-review-form");
    form.hide();
    
    $("#write-a-review a").toggle(function() {
        form.show();
    }, function() {
        form.hide();
    });
}