/* Cookie format   id-views:id-views:id-views:
 * 
 */

var cookieName = "sponorsWagoner";
var exDays = 1;
var sponsorViews = new Array();

//Gets the cookie
/*
function getSponsorCookie() {
    //Stolen from w3 school
    var ARRcookies = document.cookie.split(";");
    
    var i, x, y;
    //Find the cookie!
    for( i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|$/g, "");
        
        if(x == cookieName) {
            return unescape(y);
        }
        else {
            return null;
        }
    }
}

//Sets the cookie
function setSponsorCookie(data) {
    var exDate = new Date();
    exDate.setDate(exDate.getDate() + exDays);
    
    //If the cookie exists, than delete it
    if(getSponsorCookie != null) {
        deleteCookie();
    }
    
    var cookieString = escape(cookieName + "=" + data + "; expires=" + exDate.toUTCString() + "; path=" + "/;");
    document.cookie = cookieString;
    
}

//Deletes a cookie
function deleteCookie() {
    var date = new Date();
    date.setTime( date.getTime() - 30);
    document.cookie = cookieName + "=; expires=" + date.toUTCString();
}

//Parse cookie data
//Returns an array of all cookie values
function parseCookieData(data) {
    
}
*/
function getSponsors(serverHandle) {
    var content;
    if(serverHandle) {
        $.ajax({
            url: 'javascript/dbaccess.php?action=getSponsors&random=true',
            type: 'get',
            dataType: 'json',
            async: true,
            success: function(data) {
                $(data).each(function() {
                    sponsorAdd(this.url, this.logo);
                });
            }
        });
    }
    else {
        $.get('javascript/dbaccess.php', {action: 'getSponsors'}, function(data) {
            return data;
        });
    }
    return content;
}

function sponsorAdd(url, logo) {
    var html = '<div class="sponsorDiv">';
    
    if(url != null) {
        var check = url.split(":");

        if(check[0] != 'http') {
            url = "http://" + url;
        }
        html = html + '<a href="' + url + '" target="_blank">';
    }
    html = html + '<img class="sponsorLogoImg" src="images/sponsors/' + logo + '" />';
    if(url != null) {
        html = html + '</a>';
    }
    html = html + '</div><br />';
    $('#sponsorSideBarDiv').append(html);
}
/*
//Most inefficant search in history
//Loops through the sponsors and keeps the lowest shown sponsors
function findSponsor(data) {
    
}

//Cookie Sponor class
function cookieSponsor(id, views) {
    this.id = id;
    this.views = views;
}
*/
