var ourInterval = new Array, direction;

Array.prototype.indexOf = function(oData) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == oData) {
            return (i);
        }
    }
    return ( - 1);
};

Array.prototype.exists = function(oData) {
    return (this.indexOf(oData) != -1);
};

Array.prototype.remove = function(oData) {
    var i = this.indexOf(oData);
    if (i > -1) this.splice(i, 1);
    return (i > -1)
};

Array.prototype.clear = function() {
    var iLength = this.length;
    for (var i = 0; i < iLength; i++) {
        this.shift();
    }
}; //check one string whether in array or not

function in_array(str, arr) {
    if (arr.length == 0) {
        return false;
    }
    for (i = 0; i < arr.length; i++) {
        if (arr[i] == undefined) {
            continue;
        }
        thisEntry = arr[i].toString();
        if (thisEntry == str) {
            return true;
        }
    }
    return false;
}

function printrObj(obj) {
    for (o in obj) { //document.write(obj[o]);
        alert(obj[o]);
    }
}

/* check one object is undefined or not */
function isObjectOrNot(obj) {
    try {
        if (obj === undefined) return false;
        if (obj === null) return false;
        if (obj == "undefined") return false;
        return true;
    } catch(e) {
        return false;
    }
}
/*---------------------------------------------------*/
shoes = {};

shoes.eventHistory = function() {
    this.eHistory = [];
    for (i = 0; i < eventMan.aActivity.length; i++) {
        this.log("activity_" + eventMan.aActivity[i]);
    }
    for (i = 0; i < eventMan.aBrand.length; i++) {
        this.log("brand_" + eventMan.aBrand[i]);
    }
};

shoes.eventHistory.prototype.getRequest = function(sEvent, oData) {
    sEvent = sEvent.toLowerCase();
    if (sEvent.indexOf("clear") == -1) {
        if (sEvent.indexOf("brand") > -1) {
            value = "brand_" + oData;
            this.log(value);
        }
        if (sEvent.indexOf("activities") > -1) {
            value = "activity_" + oData;
            this.log(value);
        }
    } else {
        if (sEvent.indexOf("brand") > -1) {
            this.clear('brand');
        }
        if (sEvent.indexOf("activities") > -1) {
            this.clear('activities');
        }
    }
    current = this.eHistory.pop();
    this.eHistory.push(current);
    if (current == undefined) {
        return '';
    }
    var style = getQueryVariable('style');
    if (style != false) {
        q = '&style=' + style + "&current=" + current;
    } else {
        q = "&current=" + current;
    }
    var finder = getQueryVariable('finder');
    if (finder == false) {
        finder = 'shoe';
    }
    q = q + '&finder=' + finder;
    // cachebuster
    q = q + '&v=5';
    var cat = getQueryVariable('cat');
    if (cat != false) {
        q = q + '&cat=' + cat;
    }
    return q;
};

shoes.eventHistory.prototype.log = function(value) {
    if (in_array(value, this.eHistory)) {
        this.eHistory.remove(value);
    } else {
        this.eHistory.push(value);
    }
};

shoes.eventHistory.prototype.clear = function(str) {
    if (this.eHistory.length == 0) {
        return;
    }
    temp_arr = [];
    while (this.eHistory.length > 0) {
        temp = this.eHistory.pop();
        if (temp == undefined) {
            continue;
        }
        if (temp.indexOf(str) == 0) {
            continue;
        }
        temp_arr.unshift(temp);
    }
    this.eHistory = temp_arr;
}; ////////////////////////////////////////////////////////

function N2EventManager() {
    this.aEvents = {};
    this.aColor = new Array();
    this.aSize = new Array();
    this.aWidth = new Array();
    this.aBrand = new Array();
    this.aActivity = new Array();
    this.aGender = new Array();
    /*this.aFinder = new Array();*/
    // 1) Register a widget to be notified when any event in the
    // given list is published.
    // ARGS: oWidget a ref to a widget
    //       aMessages a ref to an array of events to subscribe to
    this.subscribe = function(oWidget, aEvents) {
        var i;
        for (i = 0; i < aEvents.length; i++) {
            var sEvent = aEvents[i];
            var aWidgets = this.aEvents[sEvent];
            if (!aWidgets) {
                aWidgets = this.aEvents[sEvent] = [];
            } // Push widget onto array
            aWidgets.push(oWidget);
        }
    }; // 3) Unregister a widget to be notified...
    // ARGS: oWidget a ref to a widget
    this.unsubscribe = function(oWidget) {
        for (var i in this.aEvents) {
            var aWidgets = this.aEvents[i];
            for (a = 0; a < aWidgets.length; a++) {
                if (aWidgets[a] === oWidget) {
                    aWidgets.splice(a, 1);
                }
            }
        }
    }; // 2) When an event comes in notify all the widgets that have
    // subscribed to the event
    this.publish = function(oSrcWidget, sEvent, oData) {
        var aWidgets = this.aEvents[sEvent];
        if (aWidgets) {
            var i;
            for (i = 0; i < aWidgets.length; i++) {
                var oWidget = aWidgets[i];
                oWidget.onEvent(oSrcWidget, sEvent, oData);
                this.genAjaxRequest(oData, sEvent);
            }
        }
    };
    this.setRequestDate = function(oRequest, oE) {
        var oR = oRequest.split("|");
        for (i = 0; i < oR.length; i++) {
            if (oE.indexOf("color") > -1) {
                this.aColor.push(oR[i]);
            } else if (oE.indexOf("size") > -1) {
                this.aSize.push(oR[i]);
            } else if (oE.indexOf("brand") > -1) {
                this.aBrand.push(oR[i]);
            } else if (oE.indexOf("activity") > -1) {
                this.aActivity.push(oR[i]);
            } else if (oE.indexOf("width") > -1) {
                this.aWidth.push(oR[i]);
            } else if (oE.indexOf("gender") > -1) {
                this.aGender.push(oR[i]);
                /*} else if (oE.indexOf("finder") > -1) {
        this.aFinder.push(oR[i]);*/
            }
        }
    };
    this.genAjaxRequest = function(oData, sEvent) { //alert('genAjaxRequest oData->'+oData+' Sevent->'+sEvent);
        for (var i = 0; i <= ourInterval.length - 1; i++) {
            clearInterval(ourInterval[i]);
        }
        sEvent = sEvent.toLowerCase();
        request = ''; //  request = eventhistory.getRequest(sEvent, oData);
        //alert('request->'+request);
        if (sEvent.indexOf("clear") == -1) {
            if (sEvent.indexOf("color") > -1) {
                if (this.aColor.exists(oData)) {
                    this.aColor.remove(oData);
                } else {
                    this.aColor.push(oData);
                }
            } else if (sEvent.indexOf("size") > -1) {
                if (this.aSize.exists(oData)) {
                    this.aSize.remove(oData);
                } else {
                    this.aSize.push(oData);
                }
            } else if (sEvent.indexOf("width") > -1) {
                if (this.aWidth.exists(oData)) {
                    this.aWidth.remove(oData);
                    if (oData == "D" && this.aGender == "Men") {
                        this.aWidth.remove('M');
                    }
                    if (oData == "M" && this.aGender == "Men") {
                        this.aWidth.remove('D');
                    }
                    if (oData == "B" && this.aGender == "Women") {
                        this.aWidth.remove('M');
                    }
                    if (oData == "M" && this.aGender == "Women") {
                        this.aWidth.remove('B');
                    }
                } else {
                    if (oData == "D" && this.aGender == "Men") {
                        this.aWidth.remove('M');
                        this.aWidth.push('M');
                    }
                    if (oData == "M" && this.aGender == "Men") {
                        this.aWidth.remove('D');
                        this.aWidth.push('D');
                    }
                    if (oData == "B" && this.aGender == "Women") {
                        this.aWidth.remove('M');
                        this.aWidth.push('M');
                    }
                    if (oData == "M" && this.aGender == "Women") {
                        this.aWidth.remove('B');
                        this.aWidth.push('B');
                    }
                    this.aWidth.push(oData);
                }
            } else if (sEvent.indexOf("brand") > -1) {
                if (this.aBrand.exists(oData)) {
                    this.aBrand.remove(oData);
                } else {
                    this.aBrand.push(oData);
                }
            } else if (sEvent.indexOf("activities") > -1) {
                if (this.aActivity.exists(oData)) { //alert(oData);
                    this.aActivity.remove(oData);
                } else {
                    this.aActivity.push(oData);
                }
            } else if (sEvent.indexOf("gender") > -1) {
                if (this.aGender.exists(oData)) {
                    this.aGender.remove(oData);
                } else {
                    this.aGender.push(oData);
                }
                /*}  else if (sEvent.indexOf("finder") > -1) {
        if (this.aFinder.exists(oData))
        {
          this.aFinder.remove(oData);
        } else {
          this.aFinder.push(oData);
        }*/
            }
        } else {
            if (sEvent.indexOf("color") > -1) this.aColor.clear();
            if (sEvent.indexOf("size") > -1) this.aSize.clear();
            if (sEvent.indexOf("width") > -1) this.aWidth.clear();
            if (sEvent.indexOf("brand") > -1) this.aBrand.clear();
            if (sEvent.indexOf("activitys") > -1) this.aActivity.clear();
            if (sEvent.indexOf("activity") > -1) this.aActivity.clear();
            if (sEvent.indexOf("activities") > -1) this.aActivity.clear();
            if (sEvent.indexOf("gender") > -1) this.aGender.clear();
            /*if (sEvent.indexOf("finder") > -1) this.aFinder.clear();*/
        }
        var q = "";
        q = this.generateQ('color', q, this.aColor);
        q = this.generateQ('size', q, this.aSize);
        q = this.generateQ('width', q, this.aWidth);
        q = this.generateQ('brand', q, this.aBrand);
        q = this.generateQ('activity', q, this.aActivity);
        q = this.generateQ('gender', q, this.aGender);
        var style = getQueryVariable('style');
        if (style != false) {
            q = q + '&style=' + style;
        }
        var finder = getQueryVariable('finder');
        if (finder == false) {
            finder = 'shoe';
        }
        q = q + '&finder=' + finder;
        var cat = getQueryVariable('cat');
        if (cat != false) {
            q = q + '&cat=' + cat;
        }
        var dir = getQueryVariableHash('dir');
        if (dir != false) {
            q = 'dir=' + dir + '&' + q;
        }
        var limit = getQueryVariableHash('limit');
        if (limit != false) {
            q = 'limit=' + limit + '&' + q;
        }
        var order = getQueryVariableHash('order');
        if (order != false) {
            q = 'order=' + order + '&' + q;
        }
        var page = getQueryVariableHash('page');
        if (page != false) {
            q = 'page=' + page + '&' + q;
        }
        q = q.replace('?', ''); //add internal parmas to direction here direction = intParams + q;
        // cachebuster
        q = q + '&v=5';
        direction = q;
        ourInterval.push(window.setInterval("ajaxHistoryCall(\"" + q + "\")", 1500)); //alert(ourInterval);
        // ajaxHistoryCall(q);
    };
    this.generateQ = function(oE, oQ, oArr) {
        if (oQ.indexOf("=") > -1) {
            oQ = oQ + "&";
        }
        oQ = oQ + oE + "=";
        for (var i = 0; i < oArr.length; i++) {
            if (i == 0) {
                oQ += oArr[i];
            } else {
                oQ += "|" + oArr[i];
            }
        }
        return oQ;
    };
}

if (!Control) var Control = {};
Control.Picker = Class.create();
Control.Picker.prototype = {
    initialize: function(inDiv, sInEvent, sClearEvt, sOutEvt, sHiStyle, sLoStyle, sGreyStyle, sQParam) {
        this.homeBase = inDiv;
        this.homeBaseSuffix = "--" + this.homeBase;
        this.picks = {};
        this.oname = sQParam;
        this.pickEvent = sInEvent;
        this.clearEvent = sClearEvt;
        this.outEvent = sOutEvt;
        this.hiStyle = sHiStyle;
        this.lowStyle = sLoStyle;
        this.greyStyle = sGreyStyle;
        this.qParamName = sQParam;
        this.allSize = $('ctr_picker_size').value.split(",");
        this.allColor = $('ctr_picker_color').value.split(",");
        this.allWidth = $('ctr_picker_width').value.split(",");
        this.allBrand = $('ctr_picker_brand').value.split(",");
        this.allActivity = $('ctr_picker_activity').value.split(",");
        this.allGender = $('ctr_picker_gender').value.split(","); //alert(this.qParamName);
        var myrows = new Array();
        if (this.oname == "brands") {
            myrows = $(this.homeBase).getElementsByTagName("li");
        } else if (this.oname == "activities") {
            myrows = $(this.homeBase).getElementsByTagName("li");
        } else if (this.oname == "genders") {
            myrows = $(this.homeBase).getElementsByTagName("li");
        } else {
            myrows = $(this.homeBase).getElementsByTagName("div");
        }
        for (var i = 0; i < myrows.length; i++) {
            myNode = myrows[i];
            var re = new RegExp(this.homeBaseSuffix);
            var myID = myNode.id.replace(re, "");
            this.picks[myID] = {};
            this.picks[myID].selected = false;
            myNode.homeBaseSuffix = this.homeBaseSuffix;
            myNode.pickEvent = this.pickEvent;
            myNode.onclick = function() {
                var re = new RegExp(this.homeBaseSuffix);
                var myID = this.id.replace(re, "");
                eventMan.publish(this, this.pickEvent, myID);
            };
        }
    },
    onEvent: function(oSrcWidget, sEvent, oData) {
        switch (sEvent) {
        case this.pickEvent:
            this.updatePicker(oSrcWidget, oData);
            eventMan.publish(this, this.outEvent, null);
            break;
        case this.clearEvent:
            this.clearPicks();
            eventMan.publish(this, this.outEvent, null);
            break;
        case "newSearchResults":
            this.greyOut(oData);
            break;
        }
    },
    //update the brand picker
    updatePicker: function(oSrcWidget, oData) {
        this.picks[oData].selected = !this.picks[oData].selected;
        if (this.picks[oData].selected) {
            oSrcWidget.className = this.hiStyle;
            Element.show('clearPicker-' + this.homeBase);
        } else {
            oSrcWidget.className = this.lowStyle;
            if (this.picksCleared()) {
                Element.hide('clearPicker-' + this.homeBase);
            }
        }
        this.kidCheck();
    },
    picksCleared: function() {
        var cleared = true;
        for (var i in this.picks) {
            if (this.picks[i].selected) {
                cleared = false;
                break;
            }
        }
        return cleared;
    },
    startUp: function(oData) {
        if (oData == "") return;
        var initData = oData.split("|");
        for (var i = 0; i < initData.length; i++) {
            var temp = this.picks[initData[i]];
            if (typeof temp != 'undefined') {
                temp.selected = true;
                $(initData[i] + this.homeBaseSuffix).className = this.hiStyle;
                Element.show('clearPicker-' + this.homeBase);
            }
        }
    },
    clearPicks: function() {
        for (var i in this.picks) {
            this.picks[i].selected = false;
            if (!this.picks[i].grey) {
                $(i + this.homeBaseSuffix).className = this.lowStyle;
            }
        }
        Element.hide('clearPicker-' + this.homeBase);
    },
    clearKid: function() {
        if (this.homeBase != "kidShoeSizePicker") return;
        var kidSize = ["12.5 Youth", "13 Youth", "13.5 Youth", "1 Youth", "1.5 Youth", "2 Youth", "2.5 Youth", "3 Youth", "3.5 Youth", "4 Youth", "4.5 Youth", "5 Youth", "5.5 Youth", "6 Youth", "6.5 Youth", "7 Youth", "7.5 Youth", "8 Youth", "8.5 Youth", "9 Youth"];
        for (var i = 0; i < kidSize.length; i++) {
            if (this.picks[kidSize[i]].selected == false) {
                if (!this.picks[kidSize[i]].grey) {
                    $(kidSize[i] + this.homeBaseSuffix).className = this.lowStyle;
                    $(kidSize[i] + this.homeBaseSuffix).onclick = function() {
                        var re = new RegExp(this.homeBaseSuffix);
                        var myID = this.id.replace(re, "");
                        eventMan.publish(this, this.pickEvent, myID);
                    };
                }
            }
        }
    },
    clearBaby: function() {
        if (this.homeBase != "kidShoeSizePicker") return;
        var babySize = ["0 Infant", "1 Infant", "2 Infant", "3 Infant", "3.5 Infant", "4 T", "4.5 T", "5 T", "5.5 T", "6 T", "6.5 T", "7 T", "7.5 T", "8 T", "8.5 T"];
        for (var i = 0; i < babySize.length; i++) {
            if (this.picks[babySize[i]].selected == false) {
                if (!this.picks[babySize[i]].grey) {
                    $(babySize[i] + this.homeBaseSuffix).className = this.lowStyle;
                    $(babySize[i] + this.homeBaseSuffix).onclick = function() {
                        var re = new RegExp(this.homeBaseSuffix);
                        var myID = this.id.replace(re, "");
                        eventMan.publish(this, this.pickEvent, myID);
                    };
                }
            }
        }
    },
    //disable events.
    greyDisableOnclick: function(oBins) {
        var objAttrs;
        switch (this.homeBase) {
        case "colorPicker":
            objAttrs = this.allColor;
            objChose = eventMan.aColor;
            break;
        case "brandPicker":
            objAttrs = this.allBrand;
            objChose = eventMan.aBrand;
            break;
        case "sizePicker":
            objAttrs = this.allSize;
            objChose = eventMan.aSize;
            break;
        case "widthPicker":
            objAttrs = this.allWidth;
            objChose = eventMan.aWidth;
            break;
        case "activitiesPicker":
            objAttrs = this.allActivity;
            objChose = eventMan.aActivity;
            break;
        case "genderPicker":
            objAttrs = this.allGender;
            objChose = eventMan.aGender;
            break;
        }
        try {
            for (var i = 0; i < objAttrs.length; i++) {
                if (in_array(objAttrs[i], oBins) && oBins != "") {
                    Element.show(objAttrs[i] + this.homeBaseSuffix);
                    $(objAttrs[i] + this.homeBaseSuffix).title = this.getDisplayName(objAttrs[i]);
                    $(objAttrs[i] + this.homeBaseSuffix).onclick = function() {
                        var re = new RegExp(this.homeBaseSuffix);
                        var myID = this.id.replace(re, "");
                        eventMan.publish(this, this.pickEvent, myID);
                    };
                    if (in_array(objAttrs[i], objChose)) {
                        this.picks[objAttrs[i]].selected = true;
                        this.picks[objAttrs[i]].grey = false;
                        $(objAttrs[i] + this.homeBaseSuffix).className = this.hiStyle;
                        Element.show('clearPicker-' + this.homeBase);
                    } else {
                        this.picks[objAttrs[i]].selected = false;
                        this.picks[objAttrs[i]].grey = false;
                        $(objAttrs[i] + this.homeBaseSuffix).className = this.lowStyle;
                    }
                } else {
                    this.picks[objAttrs[i]].selected = false;
                    this.picks[objAttrs[i]].grey = true;
                    Element.show(objAttrs[i] + this.homeBaseSuffix);
                    $(objAttrs[i] + this.homeBaseSuffix).className = this.greyStyle;
                    $(objAttrs[i] + this.homeBaseSuffix).onclick = null;
                    $(objAttrs[i] + this.homeBaseSuffix).title = this.getDisplayName(objAttrs[i]) + " not available";
                }
            }
            if (this.picksCleared()) {
                Element.hide('clearPicker-' + this.homeBase);
            }
        } catch(e) {}
    },
    greyOut: function(oData) {
        try {
            var myrows = new Array();
            if (this.oname == "brands") {
                myrows = $(this.homeBase).getElementsByTagName("li");
            } else if (this.oname == "activities") {
                myrows = $(this.homeBase).getElementsByTagName("li");
            } else if (this.oname == "genders") {
                myrows = $(this.homeBase).getElementsByTagName("li");
            } else {
                myrows = $(this.homeBase).getElementsByTagName("div");
            }
            for (var i = 0; i < myrows.length; i++) {
                myNode = myrows[i];
                Element.hide(myNode);
            }
            var myBins = oData[this.qParamName];
            for (var i = 0; i < myBins.length; i++) {
                if (myBins[i].size < 1 && !myBins[i].chosen) {
                    this.picks[myBins[i].name].selected = false;
                    this.picks[myBins[i].name].grey = true;
                    Element.show(myBins[i].name + this.homeBaseSuffix);
                    $(myBins[i].name + this.homeBaseSuffix).className = this.greyStyle;
                    $(myBins[i].name + this.homeBaseSuffix).onclick = null;
                    $(myBins[i].name + this.homeBaseSuffix).title = this.getDisplayName(myBins[i].name) + " not available";
                } else {
                    Element.show(myBins[i].name + this.homeBaseSuffix);
                    $(myBins[i].name + this.homeBaseSuffix).onclick = function() {
                        var re = new RegExp(this.homeBaseSuffix);
                        var myID = this.id.replace(re, "");
                        eventMan.publish(this, this.pickEvent, myID);
                    };
                    $(myBins[i].name + this.homeBaseSuffix).title = this.getDisplayName(myBins[i].name);
                    if (myBins[i].chosen) {
                        this.picks[myBins[i].name].selected = true;
                        $(myBins[i].name + this.homeBaseSuffix).className = this.hiStyle;
                        Element.show('clearPicker-' + this.homeBase);
                    } else {
                        this.picks[myBins[i].name].selected = false;
                        $(myBins[i].name + this.homeBaseSuffix).className = this.lowStyle;
                    }
                }
            }
            this.kidCheck();
            if (this.picksCleared()) {
                Element.hide('clearPicker-' + this.homeBase);
            }
            if (this.oname == "brands" || this.oname == "activities" || this.oname == "genders") {
                var avail = false;
                for (var i = 0; i < myBins.length; i++) {
                    if (myBins[i].size > 0) {
                        avail = true;
                    }
                }
                for (var i in this.picks) {
                    if (this.picks[i].selected == true) {
                        avail = true;
                    }
                }
                if (avail == false || oData[this.qParamName].length == 0) {
                    Element.hide(this.homeBase);
                } else {
                    Element.show(this.homeBase);
                }
            }
        } catch(sizeEx) {}
    },
    serialize: function() {
        var retString = this.qParamName + "=";
        var r = 0;
        var q = 0;
        for (var i in this.picks) {
            if (this.picks[i].selected) {
                if (q != 0) {
                    retString += "|";
                }
                retString += escape(i.replace(/\"/g, "")).replace(/&/g, "%26");
                q++;
            }
            r++;
        }
        return retString;
    },
    kidCheck: function() {
        var babySize = ["0 Infant", "1 Infant", "2 Infant", "3 Infant", "3.5 Infant", "4 T", "4.5 T", "5 T", "5.5 T", "6 T", "6.5 T", "7 T", "7.5 T", "8 T", "8.5 T"];
        var kidSize = ["12.5 Youth", "13 Youth", "13.5 Youth", "1 Youth", "1.5 Youth", "2 Youth", "2.5 Youth", "3 Youth", "3.5 Youth", "4 Youth", "4.5 Youth", "5 Youth", "5.5 Youth", "6 Youth", "6.5 Youth", "7 Youth", "7.5 Youth", "8 Youth", "8.5 Youth", "9 Youth"];
        var kidsOnly = false;
        var babyOnly = false;
        for (var i in this.picks) {
            if (this.picks[i].selected) {
                for (var j = 0; j < babySize.length; j++) {
                    if (babySize[j] == i) {
                        babyOnly = true;
                    }
                }
                for (var j = 0; j < kidSize.length; j++) {
                    if (kidSize[j] == i) {
                        kidsOnly = true;
                    }
                }
            }
        }
        if (babyOnly == true) {
            for (var i = 0; i < kidSize.length; i++) {
                $(kidSize[i] + this.homeBaseSuffix).className = this.greyStyle;
                $(kidSize[i] + this.homeBaseSuffix).onclick = null;
                $(kidSize[i] + this.homeBaseSuffix).title = this.getDisplayName(kidSize[i]) + " not available";
            }
        } else {
            this.clearKid();
        }
        if (kidsOnly == true) {
            for (var i = 0; i < babySize.length; i++) {
                $(babySize[i] + this.homeBaseSuffix).className = this.greyStyle;
                $(babySize[i] + this.homeBaseSuffix).onclick = null;
                $(babySize[i] + this.homeBaseSuffix).title = this.getDisplayName(babySize[i]) + " not available";
            }
        } else {
            this.clearBaby();
        }
    },
    getDisplayName: function(elementName) {
        var displayName = unescape(elementName);
        if (typeof $(elementName + this.homeBaseSuffix).getAttribute('name') != "undefined" && $(elementName + this.homeBaseSuffix).getAttribute('name') != null) {
            displayName = $(elementName + this.homeBaseSuffix).getAttribute('name');
        }
        return displayName;
    }
}; //this founction for toggle the some div by effect

function changeEffectBlind() {
    this.elementEB = {};
    this.recordEBHistory = function(oElem, eCat) {
        if (oElem.indexOf(eCat) !== -1) { //alert(this.elementEB['color']);
            if (this.elementEB[eCat] == 'expand') {
                this.elementEB[eCat] = 'collapse'; //new Effect.BlindUp(oElem,{});
                Element.hide(oElem);
            } else {
                this.elementEB[eCat] = 'expand'; //new Effect.BlindDown(oElem,{});
                Element.show(oElem);
            }
        }
    }
    this.getEBLastTime = function(oElem, eCat) {
        if (!isObjectOrNot(this.elementEB[eCat])) {
            this.elementEB[eCat] = "expand";
        }
        return this.elementEB[eCat];
    }
} ////##############################################

function serializeElements(elements) {
    if (typeof(elements) == 'string') {
        elements = [elements];
    }
    var queryComponents = new Array();
    for (var i = 0; i < elements.length; i++) {
        var queryComponent = Form.Element.serialize(elements[i]);
        if (queryComponent) queryComponents.push(queryComponent);
    } //alert('serializeElements('+elements+') -> return ->'+queryComponents.join('&'));
    return queryComponents.join('&');
}

function ajaxAction(url) {
    var div = arguments[1] || '';
    var options = arguments[2] || {};
    var ajaxmethod = arguments[3] || '';
    if (ajaxmethod != 'post') {
        ajaxmethod = 'get';
    }
    loading();
    options = Object.extend({
        method: ajaxmethod,
        onComplete: ajaxdone,
        onFailure: reportError,
        onSuccess: function() {
            ajaxdone();
            checkEnable();
        },
        evalScripts: true
    }, options);
    var d = new Date();
    /*  if (url.indexOf('?') == -1) {
        url = url + '?__tmp=' + (d.getTime());
    } else {
        url = url + '&__tmp=' + (d.getTime());
    } */
    if (url.indexOf('finder=') == -1) {
        var finder = getQueryVariable('finder');
        if (finder == false) {
            finder = 'shoe';
        }
        url = url + '&finder=' + finder;
    }
    /*  if (url.indexOf('discount_off_retail=') == -1) {
        var discount_off_retail = getQueryVariable('discount_off_retail');
        if(discount_off_retail==false) {
            discount_off_retail = '1';
        }
        url = url + '&discount_off_retail=' + discount_off_retail;
    } */
    var cat = getQueryVariable('cat');
    if (cat != false) {
        url = url + '&cat=' + cat;
    }
    // cachebuster
    url = url + '&v=5';
    if (div == '') {
        new Ajax.Request(url, options);
    } else {
        new Ajax.Updater(div, url, options);
    }
}

function loading() {
    jQuery('#ajaxloading').show();
}

function ajaxdone(url) {
    jQuery('#ajaxloading').hide();
}

function reportError(request) {
    ajaxdone();
    alert('Sorry. There was an error.');
}

function ajaxSubmit(url, frm, div, ajaxmethod) {
    if (ajaxmethod != 'post') {
        ajaxmethod = 'get';
    }
    var params = Form.serialize(frm);
    var options = arguments[4] || {};
    options.parameters = params;
    ajaxAction(url, div, options, ajaxmethod);
    return false;
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return false;
}

function getQueryVariableHash(variable) {
    var query = window.location.hash.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return false;
}

function ajaxHistoryCall(q) {
    q = direction; //alert('direction ->'+direction +' \n           q ->'+q);
    for (var i = 0; i <= ourInterval.length - 1; i++) {
        clearInterval(ourInterval[i]);
    }
    var internalParam = internalParameters('/catalogsearch/advanced/search4all/' + reverseParameters(q));
    ajaxAction('/catalogsearch/advanced/search4all/' + reverseParameters(internalParam.join('&') + '&' + q) + request, 'product_search4all');
    addHistoryEvent('/catalogsearch/advanced/search4all/' + reverseParameters(internalParam.join('&') + '&' + q) + request, 'product_search4all');
};
