﻿/* --------------------
Frame Digital
    The Mall 

Common javascript functions

-------------------- */

var currentPage = 1;
$(document).ready(function () {
    applySelected();
    correctPNG();

    if ($(".page").length) {
        $(".page_" + currentPage).css("display", "block");
    }

    $(".feeds").affiliateFeed();

});

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
    var arVersion = navigator.appVersion.split("MSIE");
    var version = parseFloat(arVersion[1]);
    if ((version >= 5.5 && version < 7) && (document.body.filters)) {
        for (var i = 0; i < document.images.length; i++) {
            var img = document.images[i];
            var imgName = img.src.toUpperCase();
            if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
                var imgID = (img.id) ? "id='" + img.id + "' " : "";
                var imgClass = (img.className) ? "class='" + img.className + "' " : "";
                var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
                var imgStyle = "display:inline-block;" + img.style.cssText;
                if (img.align == "left") imgStyle = "float:left;" + imgStyle;
                if (img.align == "right") imgStyle = "float:right;" + imgStyle;
                if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
                var strNewHTML = "<span " + imgID + imgClass + imgTitle
                    + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
                img.outerHTML = strNewHTML;
                i = i - 1;
            }
        }
    }
}


// --------------------------------
// set nav selected items
function applySelected() {
    var loc = location.pathname;
    if (loc == "/" || loc == "/default.aspx") {
        $("#home").addClass("selected");
        return true;
    }

    if (loc == "/rewardme.aspx") {
        $("#rewardme").addClass("selected");
        return true;
    }

    if (loc == "/community/spend-me-card.aspx") {
        $("#gift-card").addClass("selected");
        return true;
    }

    var parts = loc.split("/");

    // parts will start with an empty value, so we check position [1]
    if (parts[1] == "my-mall" || loc == "/find-a-mall.aspx") {
        $("#find-a-mall").addClass("selected");
        return true;
    }

    // if we have arrive here, we haven't returned from anything so only the platform pages and business stuff is left.  This bit is easy
    $("#" + parts[1]).addClass("selected"); // see that was easy!
}


/*  AffiliateFeed PLUGIN By COLIN WISEMAN */
(function ($) {
    
    var AffiliateFeed = function (element) {
        
        var elem = $(element);
        var current = 1;

        this.init = function (settings) {
            
            var pages = elem.find(".product-pager li:not(:first)");
            if(pages.length > 0)
            {

                $(pages).each(function (index, obj) {

                    $(this).click(function () {

                        elem.find(".page").css("display", "none");
                        elem.find(".page_" + (index + 1)).fadeIn("fast");
                        currentPage = (index + 1);
                        $(pages).removeClass("selected");
                        $(this).addClass("selected");
                    });

                });

            }
            
        };
    }

    $.fn.affiliateFeed = function (options) {
        var settings = {
            
        };

        return this.each(function () {
            if (options) {
                $.extend(settings, options);
            }

            var element = $(this);

            // Return early if this element already has a plugin instance
            if (element.data('affiliateFeed')) return;

            var affiliateFeed = new AffiliateFeed(this);
            affiliateFeed.init(settings);

            // Store plugin object in this element's data
            element.data('affiliateFeed', affiliateFeed);

        });
    };

})(jQuery);

