﻿/**
 * Cookie plugin
 *
*/
jQuery.getCookie = function(name, key) {
    var cookie;
    var cookies;
    var cookieValues = '';
    var keys;
    if (document.cookie && document.cookie != '') {
        cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1).toLowerCase() == (name.toLowerCase() + '=')) {
                cookieValues = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }

        if (typeof (key) != 'undefined') {

            if (cookieValues != '') {
                keys = cookieValues.split('&');

                for (var i = 0; i < keys.length; i++) {
                    if (jQuery.trim(keys[i]).substring(0, key.length + 1).toLowerCase() == (key.toLowerCase() + '=')) {
                        return jQuery.trim(decodeURIComponent(jQuery.trim(keys[i]).substring(key.length + 1)));
                    }
                }
            }
        }

        return cookieValues

    }
    else
        return '';
};

jQuery.setCookie = function(cookieName, key, value, daysToExpire) {
    if (arguments.length == 2) {
        //add cookie with no key.
        cookieName = arguments[0];
        value = arguments[1];
        document.cookie = cookieName + "=" + value;
    }
    else if (arguments.length >= 3) {
        //Check Type of third argument to see if we have a key or daysToExpire
        var testValue = arguments[2];
        if (typeof (testValue) == "number") {
            cookieName = arguments[0];
            value = arguments[1];
            daysToExpire = testValue;
            var date = new Date();
            date.setDate(date.getDate() + daysToExpire);
            document.cookie = cookieName + "=" + value + ";expires=" + date;
        }
        else {
            cookieName = arguments[0];
            key = arguments[1];
            value = arguments[2];
            var expire = "";
            var currentCookieValues = jQuery.getCookie(cookieName);
            var newCookieValues = "";

            if (currentCookieValues.length > 0 && currentCookieValues.indexOf("=") < 0) {
                currentCookieValues = cookieName + "=" + currentCookieValues
            }

            if (currentCookieValues.length > 0) {
                var array = currentCookieValues.split("&");

                jQuery.each(array, function() {
                    var testValue = this.split("=");
                    if (key.length > 0 && testValue[0].toLowerCase() != key.toLowerCase()) {
                        newCookieValues += "&" + this;
                    }
                });
            }

            newCookieValues += "&" + key + "=" + value
            newCookieValues = newCookieValues.substring(1);

            //see if we should set expiration
            if (arguments.length == 4) {
                var testValue = arguments[3];
                if (typeof (testValue) == "number") {
                    daysToExpire = arguments[3]
                    var date = new Date();
                    date.setDate(date.getDate() + daysToExpire);
                    expire = ";expires=" + date;
                }
            }

            document.cookie = cookieName + "=" + newCookieValues + expire;

        }
    }

};