/*
 Author: vladb
*/

// Constants
var MYSTATIONS_COOKIE = "mystations";

// Globals
var __EventRegionCookieFormjs = new Object();
__EventRegionCookieFormjs.instances = new Array();

// MyStations
function MyStations(css)
{
    var text = new Object(); 
    var lang = "en";
    var max = 8;
    
    // stations
    this.stations = null;


    // methods
    this.setLang = function(short)
        { this.lang = short; }

    this.setText = function(name, val)
        { text[name] = val; }

    this.getStations = function()
        {
            var sa = new Array();
            
	        try
	        {
                var c = Cookies.get(MYSTATIONS_COOKIE);

                // cookie doesn't return .isUndefined() compatible value
                if (c == null)
                    return sa;

                var ca = c.split("|");
                for (var ci = 0; ci < ca.length; ci++)
                {
                    var s = new Object();
                    sa[sa.length] = s;

                    var cr = ca[ci];
                    var fa = cr.split(";");
                    for (var fi = 0; fi < fa.length; fi++)
                    {
                        var fr = fa[fi];
                        var vp = fr.split("=");
                        var n = vp[0];
                           var v = vp[1];
                        if (isUndefined(n) || isUndefined(v))
                            continue;
                        s[n] = v;
                    }
                }    
	        }
	        catch (e)
	        {}
        
           return sa;
        }

    this.addStation = function(id, name, sid, fid, info)
        {
            // search for the station
            for (var i = 0; i < this.stations.length; i++)
            {
                var o = this.stations[i];
                if (o.id == id)
                    this.stations.splice(i,1);
            }

            if (this.stations.length >= max)
                this.stations.splice(0, 1);

            var o = new Object();
            o.id = id;
            o.name = name;
            o.sid = sid;
            o.fid = fid;
            o.info = info;
            this.stations[this.stations.length] = o;
        }

    this.saveStationUrl = function(url, name, info)
        {
            try
            {
                var sid = this._queryVar(url, "sid");
                var staid = this._queryVar(url, "staid");
                var fid = this._queryVar(url, "fid");     
                this.saveStation(staid, name, sid, fid, info);
            }
            catch (e)
            { }
        }

    this.saveStation = function(id, name, sid, fid, info)
        {
            this.addStation(id, name, sid, fid, info);
            this.saveStations();
        }

    this.saveStations = function() 
        {
            if (this.stations.length == 0)
            {
                Cookies.kill(MYSTATIONS_COOKIE);
                return;
            }

            var val = "";
            var sep = "";
            // serializes array of simple object beans into 
            // string value for storage as cookie
            for (var i = 0; i < this.stations.length; i++)
            {
                var s = this.stations[i];
                if (eval("s.id") == "undefined")
                    continue;

                val += sep+
                    "id="+s.id+";"+
                    "sid="+s.sid+";"+
                    "fid="+s.fid+";"+
                    "info="+s.info+";"+
                    "name="+s.name;

                sep = "|";
            }

            var today = new Date();
            var expiry = new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
            var ret = Cookies.set(MYSTATIONS_COOKIE, val, expiry);

            this.refresh();

            return val;
        }

    this.refresh = function()
        {
            this.render(this.target);
        }

    this.render = function(target)
        {
            var d = window.document;
            var ds = "<ul class=\"upcoming-display\">";
            
            this.target = target;
            this.stations = this.getStations();
            
            if (this.stations.length == 0)
                ds += this.emptymessage;			 
            else
            {
                for (var i = 0; i < this.stations.length; i++)
                {
                    var s = this.stations[this.stations.length - i - 1];
                    if (i == 4)
                        ds += "</ul><ul class='upcoming-display'>";
                    ds += "<li><a href=\"javascript:popPlayer('/ipradio20/play?staid="+s.id+"&sid="+s.sid+"&=fid="+s.fid+"')\">"+s.name+"</a><br/>"+s.info+"</li>";
                }
            }
            ds += "</ul>";

            if (isUndefined(target))
                d.write(ds);
            else
                printHtml(target,ds);
        }

    this.clear = function(callback)
        {
            this.stations = new Array();
            this.saveStations();

            if (!isUndefined(callback))
                eval(callback+"()");

            this.refresh();
        }

    this._queryVar = function(query, variable)
        {
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++)
            {
                var pair = vars[i].split("=");
                if (pair[0] == variable)
                    return pair[1];
            }
            return null;
        }
        
    // init stations    
    this.stations = this.getStations();
    
}