/*jslint strict:true, evil: true, onevar: true, nomen: false  */ /*global $, jQuery, console, hbx, window, ndm, document  */

"use strict";

/*
    Function: jQuery.fn.CG_setCookie
    Simple get and set functions for client side cookies

    $(document).CG_setCookie("username","joe_bloggs");
    $(document).CG_getCookie("username");
*/

jQuery.fn.CG_setCookie = function(dataName,dataVal){
    var expDate = new Date(),
        expTime,
        cookieExpire,
        cookieDomain = document.domain,
        cookiePath = "/",
        parentURL = document.location.toString();

    expTime = expDate.getTime() + (365*24*60*60*1000);
    expDate.setTime(expTime);
    cookieExpire = expDate.toGMTString();
    document.cookie = dataName+"="+dataVal+";expires="+cookieExpire+";domain="+cookieDomain+";path="+cookiePath;
};

jQuery.fn.CG_getCookie = function(ckAttr){
    var cookieData = document.cookie,
        cookieDataArray,
        ckAttrDef,
        cookieDataDef,
        cd=0;

    var cookieAttFound = false;
    var cookieAttValue = "";

    cookieDataArray = cookieData.split(";");
    for(cd=0;cd<cookieDataArray.length;cd++){
        cookieDataDef = cookieDataArray[cd];
        if(cookieDataDef.indexOf(ckAttr) !== -1){
            ckAttrDef = cookieDataDef.split("=");
            cookieAttValue = ckAttrDef[1];
            cookieAttFound = true;
        }
    }

    if(cookieAttFound){
        return cookieAttValue;
    }else{
        return false;
    }
};

jQuery.fn.CG_deleteCookie = function(dataName){
    var expDate = new Date(),
        expTime,
        cookieExpire,
        cookieDomain = document.domain,
        cookiePath = "/",
        parentURL = document.location.toString();

    expTime = expDate.getTime() - (365*24*60*60*1000); //Expired 1 year ago
    expDate.setTime(expTime);
    cookieExpire = expDate.toGMTString();
    document.cookie = dataName+"=NULL;expires="+cookieExpire+";domain="+cookieDomain+";path="+cookiePath;
};

//Use this function to take string portions out of an existing cookie value
jQuery.fn.CG_removeCookiePortion = function(ckAttr,ckPortion){
    var curAttValue = $(document).CG_getCookie(ckAttr),
        adjAttValue = "";

    if(curAttValue !== false){
        adjAttValue = curAttValue.split(ckPortion).join("");
        $(document).CG_setCookie(ckAttr,adjAttValue);
    }
};

//Use this function to add string portions to an existing cookie value
jQuery.fn.CG_appendCookiePortion = function(ckAttr,ckPortion){
    var curAttValue = $(document).CG_getCookie(ckAttr),
        adjAttValue = "";

    if(curAttValue !== false){
        adjAttValue = curAttValue+ckPortion;
        $(document).CG_setCookie(ckAttr,adjAttValue);
    }else{
        $(document).CG_setCookie(ckAttr,ckPortion);
    }
};

//These are required in the head
var storedShortlist = new Array();
function setCookie(cookieName,linkObj){
    storedShortlist[storedShortlist.length] = linkObj;
}

function stopPropagation(e){
    e=e||event;
    e.stoppropagation ? e.stoppropagation() : e.cancelBubble=true;
}
