// Ratings
var ratingsDom;

// 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;
var reviewWritten = false;

// 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 );
        __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;
            }
        }
    }
    
    ratingsDom = null;
    window.setTimeout( "FillRatings()", 1 );
}
function InitFillRatings()
{
    for(var prop in rctls)
        __DrawRating(rctls[prop]);
}
function FillRatings()
{
    for(var prop in rctls)
        __DrawRatingImage(rctls[prop]);
}
function __DrawRating(node)
{
	if (document.getElementsByTagName("SPAN")[0].getAttribute("RTP")!=null)
    document.getElementsByTagName("SPAN")[0].innerHTML = __RatingHTML(node);
  else 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;
    //document.rate.rating.value = num + " *";
    if ((span.getAttribute("STT")=="index")||(span.getAttribute("STT")=="post")) {
    	document.getElementById('rate-text-'+span.getAttribute("RID")).innerHTML = num + ' stars';
    } else document.getElementById('rate-text').innerHTML = num + ' stars';
}

// 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);
    }
    if ((span.getAttribute("STT")=="index")||(span.getAttribute("STT")=="post")) {
    	document.getElementById('rate-text-'+span.getAttribute("RID")).innerHTML = document.getElementById('rate-text-'+span.getAttribute("RID")).RD;
    } else document.getElementById('rate-text').innerHTML = document.getElementById('rate-text').RD;
}
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 bid = span.getAttribute("BID");
    var stt = span.getAttribute("STT");
    var node = rctls[rtp + ":" + rid];
    
    if(node == null)
        return; 
        
    node.Rating = num; 
    node.PartImage = -1;
    node.Image = 1;
    
    __DrawRatingImage(node);

    try 
    {
        
        //var url = 'module.php?sub=listen&' + 'soid=' + node.Type + '&i=' + node.TypeId + '&r='+ node.Rating;
        var url = uloc + node.Rating;

        //if(window.inNowPlaying == true && window.nppih && window.nppil)
        //{
        //    url += '&pih=' + nppih + '&pil=' + nppil;
        //}
				
				document.location = url;

    } 
    catch(e) 
    {
    }

    //window.status = "Thanks for vote !";
    return true;
}

// Default PPCB - login type for Passport due to Buy button
var emailRegex = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;