// Objects
if (!Nets) var Nets = {};

// Vars
var IE7 = (navigator.appVersion.indexOf("MSIE 7.") == -1) ? false : true;

// Private functions
Nets.DynamicStyle = function() {
    // same height columns
    var tempHeight = jQuery(".guide3steps .illustration").height();
    jQuery(".guide3steps .illustration").each(function() {
        if (jQuery(this).height() > tempHeight)
            tempHeight = jQuery(this).height();
    });
    jQuery(".guide3steps .illustration").height(tempHeight);

    tempHeight = jQuery(".products3rows dt").height();
    jQuery(".products3rows dt").each(function() {
        if (jQuery(this).height() > tempHeight)
            tempHeight = jQuery(this).height();
    });
    jQuery(".products3rows dt").height(tempHeight);

    tempHeight = jQuery(".products3rows dt + dd").height();
    jQuery(".products3rows dt + dd").each(function() {
        if (jQuery(this).height() > tempHeight)
            tempHeight = jQuery(this).height();
    });
    jQuery(".products3rows dt + dd").height(tempHeight);

    // hide "one-item" breadcrumb
    if (jQuery("#breadcrumb li").length <= 1)
        jQuery("#breadcrumb").hide();

    // :first-child, >, + CSS rules
    $(".leftNav + .content").addClass("contentAfterLeftNav");
    $(".aside + .content").addClass("contentAfterAside");

    // disabled overlay
    jQuery(".formFields .disabledOverlay").css("opacity", "0.6");

    /* data grid same width columns
    jQuery(".dataGridBody table.dataGrid tr:first-child td").each(function() {
        var item = jQuery(this);
        var itemWidth = item.width();
        //if (IE7) itemWidth -= 20;
        var itemNum = jQuery(".dataGridBody table.dataGrid tr:first-child td").index(this);

        jQuery("table.dataGrid").each(function() {
            jQuery(this).find("tr:first-child td:eq(" + itemNum + ")").width(itemWidth);
        });
    }); */

    // steps selection
    jQuery(".stepsView a.selected, .stepsView1 a.selected").prev("a").addClass("preselected");
}

Nets.Tabs = function(selector) {
    var obj = jQuery(selector);

    obj.find("a").click(function(e) {
        e.preventDefault();
        var item = jQuery(this);
        var tabNum = item.parents(".tabs").find("a").index(this);

        item.parent().siblings().removeClass("selected");
        item.parent().addClass("selected");
        
        if (item.parents(".tabsV1").length) {
            item.parent().siblings().find("a").css("paddingRight", "20px");
            item.parent().prev("li").find("a").css("paddingRight", "34px");
        }

        item.parents(".tabs").next().find(".tabCont").hide();
        item.parents(".tabs").next().find(".tabCont:eq(" + tabNum + ")").show();

        // Set row-heights
        new Nets.ProductInfoGrid(item.parents(".tabs").next().find(".tabCont:eq(" + tabNum + ")"));
    });
}

Nets.SetSelectedTab = function (tabSelect, tabNum) {
    var tabObj = jQuery(tabSelect);
    var tabLink = jQuery(tabObj.find("a").get(tabNum));

    tabLink.parent().siblings().removeClass("selected");
    tabLink.parent().addClass("selected");

    if (tabLink.parents(".tabsV1").length) {
        tabLink.parent().siblings().find("a").css("paddingRight", "20px");
        tabLink.parent().prev("li").find("a").css("paddingRight", "34px");
    }

    tabLink.parents(".tabs").next().find(".tabCont").hide();
    tabLink.parents(".tabs").next().find(".tabCont:eq(" + tabNum + ")").show();

    // Set row-heights
    new Nets.ProductInfoGrid(tabLink.parents(".tabs").next().find(".tabCont:eq(" + tabNum + ")"));
}

Nets.ProductView = function(selector) {
    var obj = jQuery(selector);
    var item = obj.find("img");
    var objScroll = "<a class='selected' />";

    for (var i = 1; i < item.length; i++) {
        objScroll += "<a />";
    }

    obj.find(".productScroll").append(objScroll);

    obj.find(".productScroll a").click(function(e) {
        e.preventDefault();
        jQuery(".productScroll a").removeClass("selected");
        jQuery(this).addClass("selected");
        var itemNum = jQuery(".productScroll a").index(this);
        obj.find("p:visible").fadeOut("fast", function() {
            obj.find("p:eq(" + itemNum + ")").fadeIn("fast");
            obj.find(".productZoom a").attr({ "href": obj.find("p:eq(" + itemNum + ") img").attr("src"), "title": obj.find("p:eq(" + itemNum + ") img").attr("alt") });
        });
    });
}

Nets.ProductInfoGrid = function (selector) {
    var obj = jQuery(selector);
    var temp;

    obj.find(".column:first-child table tr").each(function () {
        var item = jQuery(this);
        var itemHeight = item.height();
        if (IE7) itemHeight -= 20;
        var itemNum = obj.find(".column:first-child table tr").index(this);

        obj.find(".column:gt(0) table").each(function () {
            jQuery(this).find("tr:eq(" + itemNum + ")").height(itemHeight);
        });
    });

    temp = 20;
    obj.find(".columnHead").each(function () {
        if (jQuery(this).height() > temp) temp = jQuery(this).height();
    });
    obj.find(".columnHead").height(temp);
    temp = 20;
    obj.find(".columnFoot").each(function () {
        if (jQuery(this).height() > temp) temp = jQuery(this).height();
    });
    obj.find(".columnFoot").height(temp);
}

Nets.ExpandCollapse = function (selector) {
    jQuery(selector).toggle(function () {
        jQuery(this).addClass("expandCollapseActive");
        jQuery(this).parent().find(".dataGridBody").slideUp();
    }, function () {
        jQuery(this).removeClass("expandCollapseActive");
        jQuery(this).parent().find(".dataGridBody").slideDown();
    });
}
Nets.CollapseExpand = function (selector) {
    jQuery(selector).toggle(function () {
        jQuery(this).addClass("collapseExpandActive");
        jQuery(this).parent().find(".dataGridBody").slideDown();
    }, function () {
        jQuery(this).removeClass("collapseExpandActive");
        jQuery(this).parent().find(".dataGridBody").slideUp();
    });
}

Nets.Cufon = function () {
    // Cufon
    Cufon.replace("h1, h2, h4, .splash .amount, .products3rows .amount, .lejKob strong", { fontFamily: "VerdanaBold" });
    Cufon.replace(".banner h2, .splash .from, .splash .period", { fontFamily: "MyriadRegular" });
    Cufon.replace("h3:not(.productDetails h3)", { fontFamily: "MyriadRegular" });
}

Nets.Init = function () {
    // Public functions
    if (jQuery("a[rel^='prettyPhoto']").length) {
        jQuery("a[rel^='prettyPhoto']").prettyPhoto({
            theme: 'facebook'
        });
    }
}

// DOM ready
jQuery(function () {
    new Nets.Cufon();
    new Nets.Init();
    new Nets.DynamicStyle();
    new Nets.Tabs(".tabs");
    new Nets.ProductView(".productView .product");
    new Nets.ProductInfoGrid(".productDetails");
    new Nets.ExpandCollapse(".expandCollapse");
    new Nets.CollapseExpand(".collapseExpand");
});

