﻿function SetARcookie(defaultUrl1) {
    var cookieExpdate = new Date();
    var strArLastVisitedLink = window.location.href;
    var strARVisitedCookie;
    var strLastVisitedArtLink;
    var defaultUrl = defaultUrl1;

    cookieExpdate.setDate(cookieExpdate.getDate() + 730);

    strArLastVisitedLink = replaceAllString(strArLastVisitedLink, "=", "|");
    strArLastVisitedLink = replaceAllString(strArLastVisitedLink, "&", "~");

    SetCookieDictionary('ap', 'ARlastVisited', strArLastVisitedLink, cookieExpdate, '/', GetCookieDomain('art'));

    var redirectUrl = GetCookieDictionary('ap', 'ARTlastVisited');
    if ((redirectUrl == null) || (redirectUrl == "")) {
        window.location = defaultUrl; 
    }
    else {
        redirectUrl = replaceAllString(redirectUrl, "|", "=");
        redirectUrl = replaceAllString(redirectUrl, "~", "&");     
        window.location = redirectUrl;
    }
}

function pageHeaderShowHide() {
    var getARvisitedCookieValue;
    getARvisitedCookieValue = GetCookieDictionary('ap', 'ARvisited');
    if ($('#persistentHeader').length > 0) {
        var persistentHeaderDiv = document.getElementById('persistentHeader');
        if (getARvisitedCookieValue == 1) {
            persistentHeaderDiv.style.display = "block";
        }
        else {
            persistentHeaderDiv.style.display = "none";
        }
    }
}

function replaceAllString1(str, strToBeReplaced, newStr) {
    var reg, result

    if (str != null) {
        reg = new RegExp(strToBeReplaced, "g");
        result = str.replace(reg, newStr);
    }
    return result;
}

function replaceAllString(str, strToBeReplaced, newStr) {
    while (str.indexOf(strToBeReplaced) != -1)
        str = str.replace(strToBeReplaced, newStr);
    return str;
}
//SECTION - COOKIES - All the cookie related functions.
GetCookieDomain = function (brand) {
    //brand will be passed as "art" or "allposters".
    var cookieDomain = "." + brand + ".com";
    var hostName;
    hostName = location.host;
    var aPos = hostName.indexOf("." + brand);
    if (aPos > 0) {
        cookieDomain = hostName.substring(aPos);
    }
    return cookieDomain;
};

GetCookieDictionary = function (dictName, dictKey) {
    var retval = "";
    var cookieBase = String(this.GetCookieBase(dictName));
    var arrCookies;
    var cookieVal;
    if (cookieBase != null) {
        arrCookies = cookieBase.split("&");
        for (var i = 0; i < arrCookies.length; i++) {
            cookieVal = arrCookies[i].split("=");
            if (cookieVal[0] == dictKey) {
                retval = cookieVal[1];
                return retval;
            }
        }
    }
    return retval;
};
GetCookieBase = function (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return this.GetCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) {
            break;
        }
    }
    return null;
};

GetCookieVal = function (offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
};

SetCookieDictionary = function (dictName, dictKey, dictValue, expires, path, domain) {
    var Dictionary = String(this.GetCookieBase(dictName));
    var newValue = new String();
    if (Dictionary != "null") {
        //Then this key exists...but is it a dictionary?
        if (Dictionary.indexOf("=") > 0) {
            //This value has an equal sign in it, so most likely a dictionary
            //Now see if this value has the key in that we want
            if (Dictionary.indexOf(dictKey + "=") >= 0) {
                var arParts = Dictionary.split("&");
                var arVals = new Array();
                var indexOfEqual;
                for (var i = 0; i < arParts.length; i++) {
                    indexOfEqual = String(arParts[i]).indexOf("=");
                    arVals[0] = String(arParts[i]).substring(0, indexOfEqual);
                    arVals[1] = String(arParts[i]).substring(indexOfEqual + 1);
                    if (newValue.length != 0) {
                        newValue += "&";
                    }
                    if (arVals[0] == dictKey) {
                        newValue += arVals[0] + "=" + dictValue;
                    } else {

                        newValue += arVals[0] + "=" + arVals[1];
                    }
                }

                // newValue = replaceString(replaceString(newValue, "=", "__"), "&", "|");
                newValue = Dictionary + "&" + dictKey + "=" + dictValue;
                var cookieValue = dictName + "=" + newValue + "; path=/; domain=.art.com";
                document.cookie = cookieValue;
                //this.setCookie(dictName, newValue, expires, path, domain);
                //alert("in loop if GetCookiebase " + String(this.GetCookieBase('ap')));
            } else {
                //No, this key does not exist so add it    
                newValue = Dictionary + "&" + dictKey + "=" + dictValue;
                var cookieValue = dictName + "=" + newValue + "; path=/; domain=.art.com";
                document.cookie = cookieValue;
                //alert("in loop else 2 GetCookiebase " + String(this.GetCookieBase('ap')));
            }
        } else {
            //This value is not a dictionary...how to handle?
        }

    } else {
        //This Dictionary has never been created, so create it
        //newValue = dictKey + "=" + dictValue;
        //newValue = replaceString(replaceString(newValue, "=", "__"), "&", "|");
        newValue = Dictionary + "&" + dictKey + "=" + dictValue;
        var cookieValue = dictName + "=" + newValue + "; path=/; domain=.art.com";
        document.cookie = cookieValue;
        this.setCookie(dictName, newValue, expires, path, domain, secure);
        
    }
};

setCookie = function (name, value, expires, path, domain) {
    var cookieValue = name + "=" + (value) +
    // ((expires) ? "; expires=" + expires.toGMTString() : "") +
	   ((path) ? "; path=" + path : "") +
	   ((domain) ? "; domain=" + domain : "");
    //((secure) ? "; secure" : "");
    document.cookie = cookieValue;
};

getCookie = function (name) {
    var c_start, c_end;
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(name + "=");
        if (c_start != -1) {
            c_start = c_start + name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
};

$(document).ready(function () {
    pageHeaderShowHide();
});


////End of cookie related functions.
