﻿var itemLineNumber = 3;

function SetUpFaceBook() {
        var i = 0;
        var fbPanel = $(".facebookContainer");
        fbPanel.empty();
        if (fbPanel.length !== 0 && fbPanel[0] !== undefined) {
            for (i = 0; i < fbPanel.length; i++) {
                var l = document.createElement('fb:like');
                l.setAttribute('Width', '200');
                $(fbPanel[i]).append(l);
            }
            window.fbAsyncInit = function () { FBLoaded(); };
            var e = document.createElement('script');
            e.async = true;
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
            $(fbPanel[0]).parent().append(e);
        }
}

$(document).ready(function () {
   SetUpFaceBook();
});


function FBLoaded() {
    FB.init({ status: true, cookie: true, xfbml: true }); 
}
function SetHeights(ele, eleArray) {
    var children = ele.children();
    var length = children.length;
    var remainder = length % itemLineNumber;
    var i = 0;
    var j = 0;
    while (i < children.length) {
        if ((i + itemLineNumber) <= children.length) {
            for (j = 0; j < eleArray.length; j++) {
                SetClassHeights(children, i, itemLineNumber, eleArray[j]);
            }
            i += itemLineNumber;
        }
        else {
            for (j = 0; j < eleArray.length; j++) {
                SetClassHeights(children, i, remainder, eleArray[j]);
            }
            i += remainder;
        }
    }
}

function SetClassHeights(eles, index, number, className) {
    var maxHeight = 0;
    var i = 0;
    for (i = 0; i < number; i++) {
        if ($(className, eles[(index + i)]).height() > maxHeight) {
            maxHeight = $(className, eles[(index + i)]).height();
        }
    }
    for (i = 0; i < number; i++) {
        $(className, eles[(index + i)]).height(maxHeight);
    }
}


var featuredcontentslider = {

    //3 variables below you can customize if desired:
    loadingmsg: '<div style="margin: 20px 0 0 20px"><img src="loading.gif" /> Fetching slider Contents. Please wait...</div>',
    leftButton: '<div id="leftButton" style="width:50px;height:50px;background-color:Red;"><</div>',
    rightButton: '<div id="rightButton" style="width:50px;height:50px;background-color:Blue;">></div>',
    maxLeft: 0,
    maxRight: undefined,
    currentIndex: undefined,
    init: function (variables) {
        //this.registerScrollSetup(variables);
        //this.registerScrollButton(variables);
        this.imagePreviewClicked(variables);
    },
    registerScrollSetup: function (variables) {
        var width = $(variables.imagePreviewParent).width();
        var images = $(variables.imagePreviewContainer, variables.imagePreviewParent);
        var imageWidth = images.width();
        var imageHeight = images.height();
        variables.imageWidth = imageWidth;
        variables.imageHeight = imageHeight;
        var visibleNumber = Math.floor(width / imageWidth);
        this.maxRight = images.length - visibleNumber;
        this.currentIndex = 0;
        if (this.maxRight < 0) {
            this.maxRight = 0;
        }
        var imagePreviewParent = $(variables.imagePreviewParent);
        if (images.length > 0) {
            imagePreviewParent.css({});
            imagePreviewParent.parent().css({ "height": imageHeight, "width": width, "overflow": "hidden" });
        }
    },
    registerScrollButton: function (variables) {
        $(variables.leftButton).unbind("click").click(function () {
            if (featuredcontentslider.currentIndex !== 0) {
                var mL = $(variables.imagePreviewParent).css("margin-left");
                mL = mL.substring(0, mL.length - 2);
                mL *= 1;
                mL += variables.imageWidth * 1;
                $(variables.imagePreviewParent).css({ "margin-left": mL });
                featuredcontentslider.currentIndex--;
            }
        });
        $(variables.rightButton).unbind("click").click(function () {
            if (featuredcontentslider.currentIndex !== featuredcontentslider.maxRight) {
                var mL = $(variables.imagePreviewParent).css("margin-left");
                mL = mL.substring(0, mL.length - 2);
                mL *= 1;
                mL -= variables.imageWidth * 1;
                $(variables.imagePreviewParent).css({ "margin-left": mL });
                featuredcontentslider.currentIndex++;
            }
        });
    },
    imagePreviewClicked: function (variables) {
        $(variables.imagePreviewContainer).unbind("click").click(function () {
            var src2 = $("img", this).attr("src");
            if (src2 !== undefined && src2 !== null) {

                src2 = src2.replace(/width=50/gi, variables.mainWidth);
                src2 = src2.replace(/height=50/gi, variables.mainHeight);
                src2 = src2.replace(/H50/gi, "H190");
                src2 = src2.replace(/W50/gi, "W190");

                $("img", variables.mainImage).attr("src", src2);
            }
        });
    }
};

(function ($) {

    function init(variables) {
        resize(variables);
        setHeights({"heightObjects":[".itemName", ".itemAdd"]});
    }

    function setHeights(variables) {
        var width = GetWidthOfContainers();
        if(width !== undefined) {
            var contentWidth = $(".contentParent_RH", "#contentParent").width();
            var totalNumber = Math.floor(contentWidth / width);
            var remainingWidth = contentWidth - (width * totalNumber);
            var margin = Math.floor(remainingWidth / totalNumber) / 2;
            if(margin < 5)
            {
                totalNumber--;
            }
        }
        var i = 0;
        var j = 0;
        var k = 0;
        var p = 0;
        var maxHeight = 0;
        var tHeight = 0;
        var term = 0;
        for(p = 0; p < variables.heightObjects.length; p++) {
            for(i = 0; i < defaults.containers.length; i++) {
                var ele = $("li", defaults.containers[i]);
                for(j = 0; j < ele.length; j+=totalNumber) {
                    term = j+totalNumber;
                    if(term > ele.length) {
                        term = ele.length;
                    }
                    for(k = j; k < term; k++) {
                        tHeight = $(variables.heightObjects[p], $(ele[k])).height();
                        if(tHeight > maxHeight) {
                            maxHeight = tHeight;
                        }
                    }
                    for(k = j; k < term; k++) {
                        $(variables.heightObjects[p], $(ele[k])).height(maxHeight);
                    }
                    maxHeight = 0;
                }
            }
        }
    }

    function GetWidthOfContainers() {
        var tWidth = undefined;
        var i = 0;
        for(i = 0; i < defaults.containers.length; i++)
        {
            tWidth = $("li:first", defaults.containers[i]).width();
            if(tWidth !== undefined && tWidth !== null && !isNaN(tWidth * 1))
            {
                return tWidth;
            }
        }
        return tWidth;
    }

    function resize(variables)
    {
        var i = 0;
        var j = 0;
        var k = 0;
        var width = GetWidthOfContainers();
        if(width !== undefined && width !== null)
        {
        var contentWidth = $(".contentParent_RH", "#contentParent").width();
        var totalNumber = Math.floor(contentWidth / width);
        var remainingWidth = contentWidth - (width * totalNumber);
        var margin = Math.floor(remainingWidth / totalNumber) / 2;
        if(margin < 5)
        {
            totalNumber--;
            remainingWidth = contentWidth - (width * (totalNumber));
            margin = Math.floor(remainingWidth / totalNumber) / 2;
        }
        if(totalNumber < 1)
        {
            totalNumber = 1;
        }
        if(defaults.lastMargin !== margin)
        {
            defaults.lastMargin = margin;
            for(j = 0; j < defaults.containers.length; j++) {
                $(defaults.containers[j], "#contentParent").each(function() {
                    var li = $("li", this);
                    li.css({"margin-left":margin+"px", "margin-right":margin+"px"});
                    for(i = 0; i < li.length; i+=totalNumber)
                    {
                        $(li[i]).css({"margin-left":0+"px"});
                    }
                });
            }
        }
        }
    }

    var errors = [];

    var methods = {
        init: function (variables) { init(variables); },
        resize:function(variables){resize(variables);}
    };
    var defaults = {
        lastMargin:0,
        containers:[".ul_categoryRecent1", ".ul_categoryView2", ".ul_categoryView1"]
    };

$.fn.master = function (method) {
    if (methods[method]) {
        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method) {
        return methods.init.apply(this, arguments);
    } else {
        errors.push({ "errorMessage": method + " - Method not found in STS_master.js" });
    }
    //$.fn.errorPanel("errorCheck", { "errorArray": errors });
    errors = [];
}
})(jQuery);






