var CURRENCY;
function initTabs() {
    var urlParam = window.location.href.split('#');
    var selectedTab = "";
    if (urlParam.length > 1) {
        selectedTab = urlParam[1];
    }
    var getId = function(str) {
        var start, end, ret;

        if (typeof str === "string") {
            start = str.indexOf("#");
            if (start > -1) {
                //start += 1;
                ret = str.substr(start, (str.length - start));
            }
        }

        return ret;
    };

    $("ul.tabset").each(function() {

        var parentUL = this;
        $(this).find("li a").each(function(index) {

            if ($(this).hasClass("ajax")) $(this).show();
            var dataID = $(this).attr("rel");
            if (dataID == "" || (dataID != "" && $('#' + dataID).length == 0))
                dataID = getId($(this).attr("href"));
            else
                dataID = "#" + dataID;
            if (dataID != "#") {
                $(this).removeClass("active");
                $(dataID).hide();
                if ((selectedTab == "" && index == 0) || ("#" + selectedTab == dataID)) {
                    $(this).addClass("active");
                    $(dataID).show();
                }
                $(this).click(function(e) {
                    e.preventDefault();
                    var activeLink = $(parentUL).find("li a.active");
                    var hideID = activeLink.attr("rel");
                    if (hideID == "" || (hideID != "" && $('#' + hideID).length == 0)) {
                        hideID = getId(activeLink.attr("href"));
                    }
                    $((hideID.indexOf("#") > -1 ? "" : "#") + hideID).hide();

                    $(parentUL).find("li a").removeClass("active");
                    $(this).addClass("active");

                    $(dataID).show();
                    if ($(dataID).children().length == 0 && $(this).attr("href") != dataID) {
                        jQuery.ajax({
                            url: $(this).attr("href"),
                            cache: true,
                            error: function(XMLHttpRequest, textStatus, errorThrown) {
                                alert("Oops! Our server wasn't able to respond to your request. Please reload the page and try again.");
                            },
                            success: function(data) {
                                $(dataID).append(data);
                                try {
                                    var callback = eval(dataID.replace("#", ""));
                                    if (typeof (callback) == "function") callback();
                                } catch (ex) {
                                }
                                _typeface_js.renderDocument(function(e) { e.style.visibility = 'visible' });
                            }
                        });
                    }
                });
            }
        });
    });
}


function setCurrentPageActiveLinks() {
    $url = document.location.href.toLowerCase();
    var $linkIDArray = { "idurlarray": [
        { "url": "termsconditions", "id": "lnkTerms" },
        { "url": "privacypolicy", "id": "lnkPrivacy" },
        { "url": "/account#order", "id": "lnkOrderStatus" },
        { "url": "/prices", "id": "lnkPrices" },
        { "url": "/shipping", "id": "lnkShipping" },
        { "url": "/offers", "id": "lnkOffers" },
        { "url": "/bulk-order", "id": "lnkBulkOrder" },
        { "url": "/aboutus", "id": "lnkAboutUs" },
        { "url": "/servicepromise", "id": "lnkServicePromise" },
        { "url": "/green", "id": "lnkGreen" },
        { "url": "/careers", "id": "lnkCareers" },
        { "url": "/help", "id": "lnkHelp" },
        { "url": "/help", "id": "lnkHelp1" },
        { "url": "#contact", "id": "lnkContact" },
        { "url": "/photo-prints", "id": "lnkProducts" },
        { "url": "/photo-books", "id": "lnkProducts" },
        { "url": "/photo-mugs", "id": "lnkProducts" },
        { "url": "/tote", "id": "lnkProducts" },
        { "url": "/greeting", "id": "lnkProducts" },
        { "url": "/photo-calendars", "id": "lnkProducts" },
        { "url": "/t-shirt", "id": "lnkProducts" },
        { "url": "/canvas", "id": "lnkProducts" },
        { "url": "/collage", "id": "lnkProducts" },
        { "url": "/bodysuit", "id": "lnkProducts" },
        { "url": "/gifts", "id": "lnkProducts" },
        { "url": "/photo-prints", "id": "lnkPhotoPrints" },
        { "url": "/photo-books", "id": "lnkPhotoBooks" },
        { "url": "/t-shirt", "id": "lnkTShirts" },
        { "url": "/photo-calendars", "id": "lnkCustomCalendars" },
        { "url": "/photo-mugs", "id": "lnkPhotoMugs" },
        { "url": "/canvas", "id": "lnkCanvasPrints" },
        { "url": "/gifts", "id": "lnkPhotoGifts" },
        { "url": "/safe-shopping", "id": "lnkSafeShopping" },
        { "url": "/affiliates", "id": "lnkAffiliates" },
        { "url": "/corporategifts", "id": "lnkCorporateGifts" }
    ]
    };
    for (var i = 0; i < $linkIDArray.idurlarray.length; i++) {
        $currentItem = $linkIDArray.idurlarray[i];
        if ($url.indexOf($currentItem.url) > 0 && document.getElementById($currentItem.id)) {
            $('#' + $currentItem.id).after('<span class="activeLink">' + document.getElementById($currentItem.id).innerHTML + '</span>').remove();
        }
    }
    $("#lnkAhmedabad").parents("li:first").find("ul").hide();$("#lnkMumbai").parents("li:first").find("ul").hide();
    $("#lnkMumbai, #lnkAhmedabad").click(function(e) {
        e.preventDefault();
        $(this).parents("li:first").find("ul").toggle();
    });
}

function initCartDropDown() {
    var hide = false;
    $cartdrop = $('.cart-drop');
    $cartdrop.hide();
    if ($.browser.msie) {
        $cartdrop.bgiframe();
        var hide = false;
        $("#cart-qty a").click(function(event) {
            if ($('.cart-drop:visible').length) {
                clearTimeout(hide);
                $cartdrop.hide();
            }
            else {
                $cartdrop.show();
                hide = setTimeout(function() {
                    $cartdrop.slideUp('slow');
                }, 10000);
            }
            event.preventDefault();
        });
    }
    else {
        $("#cart-qty a, .cart-drop").hover(function() {
            if ($("#cart-qty *:animated").length == 0) {
                if (hide) clearTimeout(hide);
                $cartdrop.slideDown("fast");
            }
        }, function() {
            if ($("#cart-qty *:animated").length == 0) {
                hide = setTimeout(function() {
                    $cartdrop.slideUp("fast");
                }, 250);
            }
        });
    }
}

$(function() {
    initTabs();
    initCartDropDown();
    setCurrentPageActiveLinks();
    BrowserDetect.init();
    screenshotPreview();

    // Add an onClick behavior to this link
    /*
    var creationURL = ZOOMIN.util.Util.gup("creation");
    if (creationURL != '') {
    var ahref = document.createElement("a");
    ahref.setAttribute("href", creationURL);
    ZOOMIN.Popup.openPopup(null, ahref);
    }
    // Add an onClick behavior to this link

    $('.lnkPopup').click(function(event) {
    event.preventDefault();
    //Check if user is logged in. If not send them to the sign in page.
    if (false) {
    var returnUrl = encodeURI(window.location.pathname + "?creation=" + this.href.replace(window.location.pathname, ''));
    window.location = "/Signin?ReturnUrl=" + returnUrl;
    }
    else {
    ZOOMIN.Popup.openPopup(event, this);
    }

    });
    */

    /** select boxes **/
    $('.select-currency #cart-currency-1').select({ width: 80 }); // Currency drop down at the bottm of the page
    $('.select-currency #cart-currency-2').select({ width: 80 }); // Currency drop down at the bottm of the page
    $('.sortdropdown select').select({ width: 220 });

    var countryDropDown = $('.select-currency #cart-currency-2');
    if (countryDropDown.length && countryDropDown.val() == "INR") {
        showRupee();
    }
    else {
        showDollar();
    }
    countryDropDown.change(function() {
        currency = countryDropDown.find('option').filter(':selected').text();
        var url = "/Currency/UpdateCountryCookie/" + currency + "";
        jQuery.ajax({
            url: url,
            dataType: "json",
            cache: false,
            success: function(data) {
                if (currency == "INR") {
                    //India
                    showRupee();
                } else {
                    //US
                    showDollar();
                }
                $(CURRENCY).trigger('currencyChange', data);
            }
        });

    });
});
function showRupee() {
    $(".rupee").show();
    $(".dollar").hide();
}
function showDollar() {
    $(".rupee").hide();
    $(".dollar").show();
}
//CurrencyChange is a public event triggered after the countrychages and can be extended in every page where ever necessary
$(CURRENCY).bind('currencyChange', function(args, data) {
    //location.reload();
});
BindFrameEvents = function() {
    $(".thumb").click(function(e) {
        e.preventDefault();
        var imageurl = $(this).find("img").attr("src").replace("_thumb", "");
        console.log(imageurl);
        $(this).parents("div").eq(0).find(".main").attr("src", imageurl);
    });
    $(".btn-addframeboxy").click(function(e) {
        e.preventDefault();
        PremiumTypeCodeID = $("#typeid").val();
        var redirectToCheckout = $("#redirectToCheckout").val();
        window.location = "/Cart/AddFrameToCart?frameId=" + PremiumTypeCodeID + "&redirectToCheckout=" + redirectToCheckout;
    });
};

