//pragma compressprefix __m

// help
L_H_APP='MSN Entertainment';
//H_URL_BASE='http://help.msn.com/EN_US';
H_BURL='/help/frameset.htm';
H_CONFIG='entertainmentv2.ini';


// login token
var LIT = "loginHereNow=1";
var SLIT = "secureHereNow=1";

var maxStaHist = 10;


function Cookie(document, name, hours, path, domain, secure)
{
    this.$document = document;
    this.$name = name;
    
    if( hours ) {
        this.$expiration = new Date((new Date()).getTime() + hours * 3600000);
    } else {
        this.$expiration = new Date(2049, 1, 1, 1, 1, 1, 1);
    }
    
    if( path ) { this.$path = path; } else { this.$path = "/";  }
    if( domain ) { this.$domain = domain; } else { this.$domain = domain; }
    if( secure ) { this.$secure = true; } else { this.$secure = false; }
}
function _Cookie_store()
{
    var cookieval = "";
    for( var prop in this ) {
        if( (prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') )
            continue;
        if( cookieval != "" )
            cookieval += '&';
        cookieval += prop + '=' + escape(this[prop]);
    }
    var cookie = this.$name + '=' + cookieval;
    if( this.$expiration ) cookie += '; expires=' + this.$expiration.toGMTString();
    if( this.$path ) cookie += '; path=' + this.$path;
    if( this.$domain ) cookie += '; domain=' + this.$domain;
    if( this.$secure ) cookie += '; secure';
    this.$document.cookie = cookie;
}
function _Cookie_load()
{
    var allcookies = this.$document.cookie;
    if( allcookies == "" )
        return false;
    var start = allcookies.indexOf(this.$name + '=');
    if( start == -1 )
        return false;
    start += this.$name.length + 1;
    var end = allcookies.indexOf(';', start);
    if( end == -1 )
        end = allcookies.length;
    var cookieval = allcookies.substring(start, end);
    var a = cookieval.split('&');
    var i = a.length;
    while( i-- )
    {
        if(a[i].indexOf(':') != -1)
            a[i] = a[i].replace(':', '=');
            
        a[i] = a[i].split('=');
    }
    i = a.length;
    while( i-- )
        this[a[i][0]] = unescape(a[i][1]);
    return true;
}
function _Cookie_remove()
{
    var cookie;
    cookie = this.$name + '=';
    if( this.$path ) cookie += '; path=' + this.$path;
    if( this.$domain ) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
    this.$document.cookie = cookie;
}

new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

function GetCkVal(sName)
{
    var aCookie = document.cookie.split("; ");
    
    for (var i=0; i < aCookie.length; i++)
    {
        var aCrumb = aCookie[i].split("=");
        
        if (sName == aCrumb[0]) 
            return unescape(aCrumb[1]);
    }
    
    return null;
}

// relies on a value set in each page; is not fully accurate because the user can timeout during
// the period after the page is rendered.
function IndefIsLoggedIn()
{
    return (indefli != null && indefli);
}


function Trim(str) {
    var out = str;
    
    while( out.charAt(0) == ' ' ) out = out.substring(1);
    while( out.charAt(out.length - 1) == ' ' ) out = out.substring(0, out.length - 2);
    
    return out;
}

// Ratings
var ratingsDom;


var sdd = true;
    
// Safe window.onload function
var gsl = new Array(); 

function SOL(f) 
{ 
    if(window.onload) 
    { 
        if(window.onload != SOLF) 
        { 
            gsl[0] = window.onload; 
            window.onload = SOLF; 
        } 
        
        gsl[gsl.length] = f;        
    } 
    else 
    { 
        window.onload = f; 
    } 
} 

function SOLF() 
{ 
    for(var i=0; i < gsl.length; i++) 
        gsl[i](); 
}

// instantiate images (with sizes!) for mouseover performance
var ruimgs = new Array(5);
ruimgs[0] = new Image(64, 12); ruimgs[0].src = imgPre + "/i/r_su_1.gif";
ruimgs[1] = new Image(64, 12); ruimgs[1].src = imgPre + "/i/r_su_2.gif";
ruimgs[2] = new Image(64, 12); ruimgs[2].src = imgPre + "/i/r_su_3.gif";
ruimgs[3] = new Image(64, 12); ruimgs[3].src = imgPre + "/i/r_su_4.gif";
ruimgs[4] = new Image(64, 12); ruimgs[4].src = imgPre + "/i/r_su_5.gif";
var rctls = new Array();
var RatingTimers = new Array();
var PrevMousedRating = -1;

// Rating object
function Rating(span, type, id)
{
    this.Span = span;
    this.Type = type;
    this.TypeId = id;
    this.Rating = -1;
    this.Image = 0;
    this.PartImage = -1;
}

Rating.prototype.toString = function() { return this.Type +":" + this.TypeId; };

function SetupRatings()
{
    var spans = document.getElementsByTagName("SPAN");
    var x = spans.length - 1;
    do
    {
        var itm = spans[x];
        var rtp = itm.getAttribute("RTP");
        var rid = itm.getAttribute("RID");
        var avg = itm.getAttribute("AVG");
        var node;
        if(rtp != null && rid != null && avg != null) {
            node = new Rating(itm, rtp, rid);
            var avgs = avg.split('_');
            if (avgs[0] != null)
                node.Rating = avgs[0];
            if (avgs[1] != null)
                node.PartImage = avgs[1];

            rctls[rtp + ":" + rid] = node;
        }
    } 
    while (x--);

    window.setTimeout( "InitFillRatings()", 1 );
    if (IndefIsLoggedIn() || window.inNowPlaying == true)
        __LoadRatings();
}
function __LoadRatings()
{
    var request = 'http://' +  document.location.hostname + "/ratings/loadratings.aspx?i=";
    var sep = null;
    var cnt = 0;
    
    for(var prop in rctls)
    {
        if(sep == null)
            sep = ",";
        else
            request += sep;

        request += prop;            
        cnt++;
    }
    
    if(cnt == 0)
        return;
        
    if(window.inNowPlaying == true && window.nppih && window.nppil)
    {
        request += '&pih=' + nppih + '&pil=' + nppil;       
    }
    
    try 
    {
        ratingsDom = new ActiveXObject("Microsoft.XMLDOM");
        ratingsDom.onreadystatechange = __ParseRatings;
        ratingsDom.load(request);
    } 
    catch(e) 
    {
        ratingsDom = null;
        return;
    }
}
function __ParseRatings()
{
    if (ratingsDom == null || ratingsDom.readyState != 4 || ratingsDom.documentElement == null)
        return;

    if(ratingsDom.selectSingleNode("//USER") != null) 
    {
        var nodeUser = ratingsDom.selectSingleNode("//USER");

        if(nodeUser != null && nodeUser.getAttribute("LI") != null) 
            logged_in = 1;
        else
            logged_in = 0;
    }

    var ratings = ratingsDom.selectNodes("//RATINGS/RATING");

    for(var i = 0, len = ratings.length; i < len; i++) 
    {
        // [] is 2x faster than iten()
        var node = ratings[i];
        
        if(node == null || node.hasChildNodes() == false)
            continue;
            
        if(node.nodeName == "RATING") 
        {
            var type = node.selectSingleNode('T').text;
            var typeid = node.selectSingleNode('ID').text;
            var rating = rctls[type + ":" + typeid];
            //var rtv = -1;
            //var rtp = 0;
            
            if(node.selectSingleNode("USR") != null)
            {
                rating.Rating = parseInt(node.selectSingleNode("USR").text);
                rating.Image = 1;
                rating.PartImage = -1;
            }
                
            /*
            if(rtv == -1 || isNaN(rtv))
            {
                rtv = parseInt(node.selectSingleNode("AVG").text);

                if(rtv == -1 || isNaN(rtv))
                {
                    rtv = 0;
                }
                else
                {
                    var fvl = parseFloat(node.selectSingleNode("AVG").text);
                    
                    if(!isNaN(fvl))
                    {
                        var dif = fvl - rtv;
                        
                        if(dif >= 0.75)
                            rctls[key].PartImage = 75;
                        else if(dif >= 0.50)
                            rctls[key].PartImage = 50;
                        else if(dif >= 0.25)
                            rctls[key].PartImage = 25;
                    }
                }
                
                rtp = 0;
            }
            */
        }
    }
    
    ratingsDom = null;
    window.setTimeout( "FillRatings()", 1 );
}
function InitFillRatings()
{
    for(var prop in rctls)
        __DrawRating(rctls[prop]);
    
    var rpid = GetCkVal("srpid");
    var newR = GetCkVal("srnvl");

    if(rpid != null && parseInt(rpid) > 0 && newR != null && parseInt(newR) > 0)
    {
        document.cookie = "srpid=" + "; domain=" + __getDocDomain();
        document.cookie = "srnvl=" + "; domain=" + __getDocDomain();
        
        PPSaveRating(rpid, newR);
    }
}
function FillRatings()
{
    for(var prop in rctls)
        __DrawRatingImage(rctls[prop]);
}
function __DrawRating(node)
{
    node.Span.innerHTML = __RatingHTML(node);
}
function __RatingHTML(node)
{
    var html = "<map id='rating_"+node.Type+"_"+node.TypeId+"'>"+
        "<area shape=\"rect\" coords=\"0,0,12,13\"  onclick=\"rtclk(this, 1);\" onmouseover=\"rtmovr(this, 1);\" onmouseout=\"rtmout(this);\">"+
        "<area shape=\"rect\" coords=\"13,0,24,13\" onclick=\"rtclk(this, 2);\" onmouseover=\"rtmovr(this, 2);\" onmouseout=\"rtmout(this);\">"+
        "<area shape=\"rect\" coords=\"25,0,37,13\" onclick=\"rtclk(this, 3);\" onmouseover=\"rtmovr(this, 3);\" onmouseout=\"rtmout(this);\">"+
        "<area shape=\"rect\" coords=\"38,0,50,13\" onclick=\"rtclk(this, 4);\" onmouseover=\"rtmovr(this, 4);\" onmouseout=\"rtmout(this);\">"+
        "<area shape=\"rect\" coords=\"51,0,65,13\" onclick=\"rtclk(this, 5);\" onmouseover=\"rtmovr(this, 5);\" onmouseout=\"rtmout(this);\"></map>"+
        "<img id=\"ratingimg_"+node.Type+"_"+node.TypeId+"\" src=\""+ __RatingImage(node) +"\" usemap=\"#rating_"+node.Type+"_"+node.TypeId+"\" width=\"64\" height=\"12\" border=\"0\">";
    return html;
}
function __RatingImage(node)
{
    if (node.Rating < 1)
        return imgPre + "/i/r_s_0.gif";

    var imageName;
    if(node.Image == 0) { 
        imageName = "sc"; 
    } else {
        imageName = "su";
    }

    return imgPre +"/i/r_"+ imageName +"_"+ node.Rating + ((node.PartImage != -1) ? "_"+node.PartImage : "") + ".gif";
}
function __DrawRatingImage(node)
{
   node.Span.lastChild.src = __RatingImage(node);
}

// rating mouse over
function rtmovr(star, num)
{
    var span = star.parentNode.parentNode;
    var rtstr = span.getAttribute("RTP") +":"+ span.getAttribute("RID");
        
    // cancel a previous revert timer on this rating control
    if (RatingTimers[rtstr] != 0) {
        window.clearTimeout(RatingTimers[rtstr]);
        RatingTimers[rtstr] = 0;
    }

    // cancel the restore timer for a previous control (eliminates vertical ghosting)
    if (PrevMousedRating != -1 && PrevMousedRating != rtstr)
    {
        __RevertRating(PrevMousedRating);
    }
    PrevMousedRating = rtstr;

    // inline this rating draw for max perf
    span.lastChild.src = ruimgs[num - 1].src;
}

// rating mouse out
function rtmout(star)
{
    var span = star.parentNode.parentNode;
    var rtstr = span.getAttribute("RTP") +":"+ span.getAttribute("RID");
    
    if (! RatingTimers[rtstr]) {
        RatingTimers[rtstr] = window.setTimeout("__RevertRating(\""+ rtstr +"\")", 120);
    }
}
function __RevertRating(rtstr)
{
    var node = rctls[rtstr];
    
    if(node == null)
        return; 

    RatingTimers[rtstr] = 0;
    __DrawRatingImage(node);
}

// rating click
function rtclk(star, num)
{
    var span = star.parentNode.parentNode;
    var rtp = span.getAttribute("RTP");
    var rid = span.getAttribute("RID");
    var node = rctls[rtp + ":" + rid];

    // alert(node + ":" + num);

    if(node == null)
        return; 
        
    node.Rating = num; 
    node.PartImage = -1;
    node.Image = 1;
    
    __DrawRatingImage(node);
    
    //if(!IsLoggedIn() && !(window.inNowPlaying == true))
    //{ 
    //    TrigLogIn('', false, rtp + ":" + rid, node.Rating); 
    //    return; 
    //}

    try 
    {
        //var dom = new ActiveXObject("MSXML");
        
        //dom.async = false;
        
        //var url = 'http://' + document.location.hostname + '/Ratings/SaveRatings.aspx?tp=' +
       //     node.Type + '&i=' + node.TypeId + '&r='+ node.Rating;

        //if(window.inNowPlaying == true && window.nppih && window.nppil)
        //{
        //    url += '&pih=' + nppih + '&pil=' + nppil;
       // }
        
       // dom.url = url;
       // dom = null;
       var oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
      	
      // Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
      var sURL = "saverating.aspx?p=" + rid + "&q=" + rtp + "&w=" + num;
      oXMLHTTP.open( "POST", sURL, false );

      // Execute the request
      oXMLHTTP.send();

      //if (oXMLHTTP.responseText == "exists") 
      //alert("Sorry - the User ID " + suserid + " already exists.");

      //document.body.style.cursor='auto';

      alert("Thank you for voting!");

    } 
    catch(e) 
    {
    }

    window.status = "";
    return false;
}

