﻿
var http_request = new Array();
var whatFunction = new Array();

/**
* Function for making an XMLHTTP Request
*
* url	Url for the request
* args Arguments passed to url and used in document location for bookmarking
* myFunction Function to execute upon successful return of request
*
* Returns a true if required version or greater is installed, otherwise false.
*/
function makeRequest(url, args, myFunction) {
    nextReq = http_request.length;
    http_request[nextReq] = false;
    whatFunction[nextReq] = myFunction;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request[nextReq] = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        try {
            http_request[nextReq] = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            alert(e);
            try {
                http_request[nextReq] = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert(e);
            }
        }
    }
    if (!http_request[nextReq]) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
    }
    try {
        http_request[nextReq].onreadystatechange = checkStatus;
        http_request[nextReq].open('GET', url + "?" + args, true);
        http_request[nextReq].send(null);
    } catch (e) {
        alert(e);
    }
}

function makePOSTRequest(url, parameters, myFunction) {
    nextReq = http_request.length;
    http_request[nextReq] = false;
    whatFunction[nextReq] = myFunction;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request[nextReq] = new XMLHttpRequest();
        if (http_request[nextReq].overrideMimeType) {
            // set type accordingly to anticipated content type
             http_request[nextReq].overrideMimeType('text/html');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request[nextReq] = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request[nextReq] = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }
    if (!http_request[nextReq]) {
        alert('Cannot create XMLHTTP instance');
        return false;
    }

    http_request[nextReq].onreadystatechange = checkStatus;
    http_request[nextReq].open('POST', url, true);
    http_request[nextReq].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request[nextReq].setRequestHeader("Content-length", parameters.length);
    http_request[nextReq].setRequestHeader("Connection", "close");
    http_request[nextReq].send(parameters);
}

/**
* Function for checking status of XMLHTTP Request
*
* Evaluates whatFunction upon successful request return.
*/
function checkStatus() {
    for (var i = 0; i < http_request.length; i++) {
        if (http_request[i] != null) {
            if (http_request[i].readyState == 4) {
                if (http_request[i].status == 200) {
                    if (whatFunction[i] != null) {
                        var tempHolder = whatFunction[i];
                        whatFunction[i] = null;
                        try {
                            eval(tempHolder);
                        }
                        catch (e) {
                            alert(e)
                        }
                        //Prevent return
                        http_request[i] = null;
                    }

                } 
            }
        }
    }
}

/***
** Like Functionality for KC
**
** This set of functions allows for AJAX implementation of the Like functionality
** The checkLiked(objectId) function takes a string and checks the likes cookie for already liked content
**
** REQUIREMENTS:
** The like functionality requires that jQuery and cookie.utils.js be loaded in the page
**
** USAGE:
** Include inline in a page like the following or add to an onLoad handler
** <script>checkLiked("article7","article7LikeDiv");</script>
***/

var setLikeURL = "/apps/likes/set";
var getLikeURL = "/apps/likes/get";
var likeQueryParam = "id";

//Used on page load to check and display liked state of an object
function checkLiked(objectId, containerId) {
    var likeString = jimAuld.utils.cookies.get('likes');
    var likeArray = new Array();
    if (null != likeString) {
        likeArray = likeString.split(",");
    }

    if ($.inArray(objectId, likeArray) != -1) { //Already liked
        makeRequest(getLikeURL, likeQueryParam + "=" + objectId, "showLiked(http_request[i].responseText, '" + containerId + "')");
    } else { //Not yet liked
        makeRequest(getLikeURL, likeQueryParam + "=" + objectId, "showUnLiked(http_request[i].responseText, '" + objectId + "', '" + containerId + "')");
    }
}

//Set object as liked
function like(objectId, containerId) {
    var likeString = jimAuld.utils.cookies.get('likes');
    var likeArray = new Array();
    if (null != likeString) {
        likeArray = likeString.split(",");
    }
    likeArray.push(objectId);
    jimAuld.utils.cookies.set('likes', likeArray.toString(), 99999999999, '');

    makeRequest(setLikeURL, likeQueryParam + "=" + objectId, "showLiked(http_request[i].responseText, '" + containerId + "')");
}

//Show liked state
function showLiked(likeCount, containerId) {
    document.getElementById(containerId).innerHTML = "You and " + (likeCount -1) + " other "+((likeCount==2)?"person":"people")+" liked this.";
}

//Show unliked state
function showUnLiked(likeCount, objectId, containerId) {
    document.getElementById(containerId).innerHTML = "<a href='javascript:void(0)' title='Like this?' onclick='like(\"" + objectId + "\",\"" + containerId + "\"); return false;' style='text-decoration:none !important'><img src='/img/icon_thumbs-up_like-this.gif' width='21' height='23' alt='Like This?' align='absmiddle'  style='padding-bottom: 8px;'/> </a><a href='javascript:void(0)' title='Like this?' onclick='like(\"" + objectId + "\",\"" + containerId + "\"); return false;'>Like this?</a> |  " + likeCount + ((likeCount==1)?" person":" people")+" like this.";
}


