/*
    This utility helps keep track of the cookies to track map open or closed per brand.
 */

if ((typeof YAHOO !== "undefined") && (YAHOO.util) && (YAHOO.util.Event)) {
	YAHOO.util.Event.throwErrors = true;
}

SW.maps.Cookie = {
    getCookie:function() {
        return SW.tools.Cookie.get("MSVEMapStatus");
    },
    setCookie:function(cookieValue) {
        SW.tools.Cookie.set("MSVEMapStatus", cookieValue);
    },

    getCookieValueObj:function() {
        var value = this.getCookie();
        if (value == "") {
            value = '{}';
        }

        var valueObj = YAHOO.lang.JSON.parse(value);
        return valueObj;
    },

    /* returns "" if no cookie, else "open" or "closed" */
    getMapStatus:function(skinCode) {
        var value = this.getCookie();
        if (value == "") {
            return "";
        }
        var valueArray = YAHOO.lang.JSON.parse(value);

        if ((typeof valueArray[skinCode] == undefined)) {
            return "";
        } else {
            return valueArray[skinCode];
        }

    },

    setMapOpen:function(skinCode) {
        var valueArray = this.getCookieValueObj();
        valueArray[skinCode] = "open";
        var value = YAHOO.lang.JSON.stringify(valueArray);
        this.setCookie(value);
    },

    setMapClosed:function(skinCode) {
        var valueArray = this.getCookieValueObj();
        valueArray[skinCode] = "closed";
        var value = YAHOO.lang.JSON.stringify(valueArray);
        this.setCookie(value);
    }
};

SW.maps.SiteConfigFramwork = {
    //this expects something like "item=map&value=close" as a parameter
    updateOmniture:function(urlParams) {
        var url = "/ajax/siteConfigExclude.html?type=search&" + urlParams;
        var callback = {
            success:function(response) {
                var result = YAHOO.lang.JSON.parse(response.responseText);
                var brandCode= SW.local.skinCode;
                var layoutNumber = "Exclude:" + result.data.exclude;
                var mvtPageName =  brandCode + "_" + "SearchResults";
                var localeCode = result.data.localeCode;

                if (typeof s != 'undefined') {
                    // Omniture call
                    s.eVar46 = s.prop46 = brandCode + ":SearchResults:" + layoutNumber;

                    var mapSectionDiv = document.getElementById("mapSection");
                    var mapValue = (mapSectionDiv.style.display == "block") ? "open" : "close";
                    if (mapValue) {
                        // We shall replace the below clickedItem passed into the pageName with an equivalent success event in the next release.
                        // For now lets clean up the pageName to restore tracking in the original bucket.
                        setOmniVars(s.charSet, s.server, s.channel, s.prop2, s.prop3, s.prop1, "", "Search", "Results","Map", mapValue);
                        s.t();
                    }
                    else if (sortByValue) {
                        // We shall replace the below clickedItem passed into the pageName with an equivalent success event in the next release.
                        // For now lets clean up the pageName to restore tracking in the original bucket.
                        setOmniVars(s.charSet, s.server, s.channel, s.prop2, s.prop3, s.prop1, "",  "Search", "Results", "sort drop down", sortByValue);
                        s.t();
                    }
                    else {
                        // Do nothing
                    }
                    // Offermatica call
                    if (typeof mboxUpdate == 'function') mboxUpdate(mvtPageName, 'localeCode=' + localeCode , 'layoutNumber=' + layoutNumber);
                }
            },
            failure:function() {
                //failed, but nothing to do about it
            }
        };
        var transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback, null);

    }
};

