/* this thing does what?

- looks for a cookie
- if found it replaces it with a new one
- if not found it places an image or ajax call that logs the event
  AND
  we set a cookie

*/


/* Cookie Functions */

function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
            c_start=c_start + c_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 "";
}

function setCookie(c_name, value) {
    
    var exdate=new Date();
    exdate.setTime(exdate.getTime() + 1000 * 60 * 30); // expire 30 minutes from now
    document.cookie = c_name+"="+escape(value)+";expires="+exdate.toGMTString();
    
    return (getCookie(c_name)>"") ? true : false;
    
}



/* AJAX Functions */

function getXMLHttp() {
    var xmlHttp
    
    try {
        //Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch(e) {
        //Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                //alert("Your browser does not support AJAX!")
                return false;
            }
        }
    }
    return xmlHttp;
}


function makeRequest(pageString, action, mediaItemID, pageID, element) {
    
    var xmlHttp = getXMLHttp();
        
    // the href delay is necessary for any <a> initiated click to ensure our ajax fires first
    if (element) {
        var oldHref = element.href;
        
        xmlHttp.onreadystatechange = function() {
            if(xmlHttp.readyState == 4) {
                HandleResponse(element, oldHref);
            }
        }
    }
    
    var urlString = baseURL+"includes/track_stats.php?name="+cname+"&isBot="+isSearchBot+"&isMobile="+isMobileDevice+"&view="+view+"&pageString="+pageString+"&sname="+sname+"&action="+action;
    if (mediaItemID && pageID) urlString += "&mediaItemID="+mediaItemID+"&pageID="+pageID+"&zone="+zone;
    
    
    xmlHttp.open("GET", urlString, true);
    xmlHttp.send(null);
    
}


function HandleResponse(element, oldHref) {
    window.location.href = oldHref;
}


/* Making it work */

function checkCookie(pageString, mediaItemID, pageID, element) {
    
    cookie = getCookie(cname);
        
    if (cookie != null && cookie != "") { // if cookie is set update the expiration date
        
        setCookie(cname, cvalue);
                
        // however, we still want to track the media item play
        if (mediaItemID && pageID)
            makeRequest(pageString, 'media', mediaItemID, pageID, element);
        
        
    } else { // if cookie is not set attempt to set it
        
        cookieEnabled = setCookie(cname, cvalue);
        
        if (cookieEnabled)
            makeRequest(pageString, 'visit', mediaItemID, pageID, element);
        else
            makeRequest(pageString, 'cookie', mediaItemID, pageID, element);
        
    }
    
}

