
 function ProjectWaypoint()
{
    var id;
    var name;
    var lat;
    var lon;
}




function setCookie(c_name, value, expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ";path=/";


}

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 routerCookie()
{
    var projectsCookieName = 'routes_selectedProjects';
    var cookie = getCookie(projectsCookieName);

    var state;

    this.init = function()
    {
        if (!cookie)
        {
            this.state = {idToProjectWayPoint: [], start: {} }
        }
        else
        {
            this.state = jQuery.evalJSON(cookie);
        }
    }

    this.updateCookie = function()
    {
        setCookie(projectsCookieName,  jQuery.toJSON(this.state), 200);
    }

    this.projectCount = function()
    {
        return this.state.idToProjectWayPoint.length;
    }

    this.add = function(pId, projectName, lattitude, longitude)
    {
        var pw = new ProjectWaypoint(pId, projectName, lattitude, longitude);
        pw.id = pId;
        pw.project = projectName;
        pw.lat = lattitude;
        pw.lon = longitude;

        this.state.idToProjectWayPoint.push(pw);
        this.updateCookie()
    }

    this.remove = function(pId)
    {
        var newArray = [];
        var j=0;

        //this.state.idToProjectWayPoint = this.state.idToProjectWayPoint.remove(pId);
        for (i=0; i<this.state.idToProjectWayPoint.length; i++)
        {
            if (this.state.idToProjectWayPoint[i].id != pId)
            {
                newArray[j++] = this.state.idToProjectWayPoint[i];
            }
        }

        this.state.idToProjectWayPoint = newArray;
        this.updateCookie();

    }

    this.removeAll = function()
    {
        this.state.idToProjectWayPoint = [];
        this.updateCookie();
    }

    this.getAllProjectIds = function()
    {
        return this.state.idToProjectWayPoint;
    }

    this.has = function(pId)
    {
        for (i=0; i<this.state.idToProjectWayPoint.length; i++)
        {
            if (this.state.idToProjectWayPoint[i].id == pId)
            {
                return true;
            }
        }
      
        return false;
    }

    this.setStart = function(lat, lng)
    {
        this.state.start['lat'] = lat
        this.state.start['lng'] = lng;
        this.updateCookie();
    }

    this.init();
}


