
var tempX = 0;
var tempY = 0;  


var STAR_CRD_MAX_1 = 18; 
var STAR_CRD_MAX_2 = 37;
var STAR_CRD_MAX_3 = 56;
var STAR_CRD_MAX_4 = 75;
var STAR_CRD_MAX_5 = 94;

var WIDGET_TYPE_MOVIE_STD = 1;
var WIDGET_TYPE_STATIC_STD = 3;

var NOTIM_OFF = STARBAR_IMG_ROOT + "nim_low.gif";;
var NOTI_OFF = STARBAR_IMG_ROOT + "ni_low.gif";;
var NOOP_OFF = STARBAR_IMG_ROOT + "nop_low.gif";
var ZERO_STARS = STARBAR_IMG_ROOT + "stars_0_0.gif";

//var ALT_MOV_1 = "Click to rate the song \"Hated It\"";
var ALT_MOV_1 = "";
//var ALT_MOV_2 = "Click to rate the song \"Didn\'t Like It\"";
var ALT_MOV_2 = "";
//var ALT_MOV_3 = "Click to rate the song \"Liked It\"";
var ALT_MOV_3 = "";
//var ALT_MOV_4 = "Click to rate the song \"Really Liked It\"";
var ALT_MOV_4 = "";
//var ALT_MOV_5 = "Click to rate the song \"Loved It\"";
var ALT_MOV_5 = "";

var ALT_MOV_STATIC_1 = "Hated It";
var ALT_MOV_STATIC_1 = "Didn\'t Like It";
var ALT_MOV_STATIC_1 = "Liked It";
var ALT_MOV_STATIC_1 = "Really Liked It";
var ALT_MOV_STATIC_1 = "Loved It";

var ALT_GENERIC = "Star Rating For Movie";

var CLEAR_RATING = "clear";
var NO_OPINION = "noseen";
var NOT_INTERESTED = "norec";
    
var RATING_1 = 1;
var RATING_2 = 2;
var RATING_3 = 3;
var RATING_4 = 4;
var RATING_5 = 5;

var W_OBJ_PFX = "w";
var SB_OBJ_PFX = "st";
var NO_OBJ_PFX = "no";
var NI_OBJ_PFX = "ni";
var NIM_OBJ_PFX = "nm";
var CR_OBJ_PFX = "cr";

var STD_STARBAR_SRC = STARBAR_IMG_ROOT + "stars_" + WIDGET_TYPE_MOVIE_STD + "_"; //performance optimization

function wR(widgetId, wType, wRating) {  //Widget REGISTER
    
    widget = document.getElementById(W_OBJ_PFX + widgetId);
    if (widget.registered == null) {
        widget.registered = true;
        
        widget.widgetId = widgetId;
        widget.onmouseout = doRestore;
        
        
        starbar = document.getElementById(SB_OBJ_PFX + widgetId);
        noop    = document.getElementById(NO_OBJ_PFX + widgetId);
        noti    = document.getElementById(NI_OBJ_PFX + widgetId);
        notim   = document.getElementById(NIM_OBJ_PFX + widgetId);
        clrr    = document.getElementById(CR_OBJ_PFX + widgetId);
   
        if (starbar) {
            widget.starbar = true;
            starbar.widgetId = widgetId;
            starbar.widgetType = wType;
            starbar.restorePath = starbar.src;
            starbar.onmouseup = doStarCommit;
            starbar.onmousemove = doStarRollover;
            starbar.onmouseout = doMouseout;
            starbar.prediction = wRating;
        }
        
        if (noop) {
            widget.noop = true;
            noop.restorePath = noop.src;
            noop.clickvalue = NO_OPINION;
            noop.widgetId = widgetId;
            noop.onmousemove = doNoOpRollover;
            noop.onmouseout = doMouseout;
            noop.onclick = doButtonCommit;
        }
        
        if (noti) {
            widget.noti = true;
            noti.restorePath = noti.src;
            noti.clickvalue = NOT_INTERESTED;
            noti.widgetId = widgetId;
            noti.onmousemove = doNiRollover;
            noti.onmouseout = doMouseout;
            noti.onclick = doButtonCommit;
        }
        
       if (notim) {
            widget.notim = true;
            notim.restorePath = notim.src;
            notim.clickvalue = NOT_INTERESTED;
            notim.widgetId = widgetId;
            notim.onmousemove = doNimRollover;
            notim.onmouseout = doMouseout;
            notim.onclick = doButtonCommit;
        }
        
        if (clrr) {
            widget.clrr = true;
            clrr.restorePath = clrr.src;
            clrr.clickvalue = CLEAR_RATING;
            clrr.widgetId = widgetId;
            clrr.onmousemove = doCrRollover;
            clrr.onmouseout = doMouseout;
            clrr.onclick = doButtonCommit;
        }
    } 
}


function doRestore(evt) {

    var widgetId = this.widgetId;
    setTimeout('restoreWidget("' + widgetId + '", false)', 10);
}

function restoreWidget(widgetId, starDelayActive) {

    widget = document.getElementById(W_OBJ_PFX + widgetId);
    
    if (widget.noop) {
        noop = document.getElementById(NO_OBJ_PFX + widgetId);
        if (noop && !noop.active) {
            noop.src = noop.restorePath;
        }
    }
    if (widget.noti) {

        noti = document.getElementById(NI_OBJ_PFX + widgetId);
        if (noti && !noti.active) {
            
            noti.src = noti.restorePath;
                

            restoreStars = false;
        }
    } else if (widget.notim) {
        notim = document.getElementById(NIM_OBJ_PFX + widgetId);
        if (notim && !notim.active) {
            
            notim.src = notim.restorePath;
                
	
            restoreStars = false;
        }
    }
    if (widget.clrr) {
        clrr = document.getElementById(CR_OBJ_PFX + widgetId);
        if (clrr &&!clrr.active) {
            clrr.src = clrr.restorePath;
            restoreStars = false;
        }
    }
    if (widget.starbar) {
        if (starDelayActive) {
            setTimeout('restoreWidget("' + widgetId + '", ' + !starDelayActive + ')', 100);
            
        } else {
            starbar = document.getElementById(SB_OBJ_PFX +  widgetId);
            if (starbar && !starbar.active ) {
                starbar.src = starbar.restorePath;
                starbar.offsetX = null;
            }
        }
    }

}

function setRating(widgetType, widgetId, value) {

//    window.location.href = STARBAR_SET_PAGE + "?widgetid=" + widgetId + "&value=" + value + (forcePgRefresh == false ? "&ncok=true" : "");
  window.location.href = STARBAR_SET_PAGE + "?songid=" + widgetId + "&value=" + value 

}

function doStarCommit(evt) { 
    this.active = true;
    var starValue = getCurrentStarValue(evt, this);
    this.restorePath = this.src;
    notim = document.getElementById(NIM_OBJ_PFX + widgetId);
	noti = document.getElementById(NI_OBJ_PFX + widgetId);
	clrr = document.getElementById(CR_OBJ_PFX + widgetId);
	if (noti) {
        noti.src = STARBAR_IMG_ROOT + "/ni_low.gif";
        noti.restorePath = noti.src;
    } else if (notim) {
    	notim.src = NOTIM_OFF;
        notim.restorePath = notim.src;
    }
	resetClear(false, this.widgetId);
    setRating(this.widgetType, this.widgetId, starValue);
    
} 

function doButtonCommit(evt) { 

    this.active = true;
    this.restorePath = this.src;
    var widgetId = this.widgetId;

	clickValue = this.clickvalue;
	
    starbar = document.getElementById(SB_OBJ_PFX + widgetId);
    

    if (clickValue == CLEAR_RATING) { 
        starbar.src = STD_STARBAR_SRC + starbar.prediction + ".gif"

        this.restorePath = STARBAR_IMG_ROOT + "clear_unavailable.gif";
		this.cleared = true;
        
    }
	
	if (clickValue != NOT_INTERESTED) {

		noti = document.getElementById(NI_OBJ_PFX + widgetId);
		notim = document.getElementById(NIM_OBJ_PFX + widgetId);
		if (noti) {
	        noti.src = STARBAR_IMG_ROOT + "/ni_low.gif";
	        noti.restorePath = noti.src;
	    } else if (notim) {
    		notim.src = NOTIM_OFF;
        	notim.restorePath = notim.src;
    	}
		resetClear(true, widgetId);
	}
    
	starbar.restorePath = starbar.src;
    
    setRating(this.widgetType, widgetId, clickValue);
    
} 

function resetClear(isCleared, widgetId) {
	 clrr = document.getElementById(CR_OBJ_PFX + widgetId);
	 if (clrr) {
	 	clrr.cleared = isCleared;
		clrr.src = STARBAR_IMG_ROOT + "/clear_low.gif";
        clrr.restorePath = clrr.src;
	 }
}

function doNoOpRollover() {
    this.active = true;
    this.alt, this.title = "No Opinion";
    this.src = STARBAR_IMG_ROOT + "nop_high.gif";
    starbar = document.getElementById(SB_OBJ_PFX + this.widgetId);
    starbar.active = true;
    starbar.src = ZERO_STARS;
}

function doNiRollover() {   
    this.active = true;
    this.alt, this.title = "Not Interested";
    this.src = STARBAR_IMG_ROOT + "ni_high.gif";
    starbar = document.getElementById(SB_OBJ_PFX + this.widgetId);
    starbar.active = true;
    starbar.src = ZERO_STARS;
}

function doNimRollover() {   
    this.active = true;
    this.alt, this.title = "Not Interested";
    this.src = STARBAR_IMG_ROOT + "nim_high.gif";
    starbar = document.getElementById(SB_OBJ_PFX + this.widgetId);
    starbar.active = true;
    starbar.src = ZERO_STARS;
}

function doCrRollover() {
    if (!this.cleared) {
		this.active = true;
	    this.alt, this.title = "Clear Rating";
	    this.src = STARBAR_IMG_ROOT + "clear_high.gif";
	    starbar = document.getElementById(SB_OBJ_PFX + this.widgetId);
	    starbar.active = true;
	    starbar.src = STD_STARBAR_SRC + starbar.prediction + ".gif"   
	}
}

function doMouseout() {
    this.active=false;
    var widgetId = this.widgetId;
    starbar = document.getElementById(SB_OBJ_PFX + widgetId);    
    noop    = document.getElementById(NO_OBJ_PFX + widgetId);
    noti    = document.getElementById(NI_OBJ_PFX + widgetId);
    notim   = document.getElementById(NIM_OBJ_PFX + widgetId);
    clrr    = document.getElementById(CR_OBJ_PFX + widgetId);
    
    if (noop)
        noop.active = false;
    if (noti)
        noti.active = false;
    if (notim)
        notim.active = false;
    if (clrr)
        clrr.active = false;
    starbar.active = false;
}

function getCurrentStarValue(evt, curEmt) {
    var xC = -1;  
    if (!evt) var evt = window.event;
    
    if (evt.offsetX) {
        xC = evt.offsetX;
    } else if (curEmt.offsetX != null) {
        eXC = curEmt.offsetX;
        xC = evt.layerX - eXC;
    } else if (evt.layerX) {
        eXC = getElementOffsetX(curEmt);
        xC = evt.layerX - eXC;
        curEmt.offsetX = eXC;
    }
    
    if (xC <= STAR_CRD_MAX_1) return 1;   
    else if (xC <= STAR_CRD_MAX_2) return 2;
    else if (xC <= STAR_CRD_MAX_3) return 3;
    else if (xC <= STAR_CRD_MAX_4) return 4;
    else if (xC <= STAR_CRD_MAX_5) return 5;
    else return 0;
}

function doStarRollover(evt) {
   
    this.active = true;
    widgetId = this.widgetId;
    noti = document.getElementById(NI_OBJ_PFX + widgetId);
    notim = document.getElementById(NIM_OBJ_PFX + widgetId);
    noop = document.getElementById(NO_OBJ_PFX + widgetId);
    
    if (noti) {
        noti.active = true;
        noti.src = NOTI_OFF;
    } else if (notim) {
    	notim.active = true;
        notim.src = NOTIM_OFF;
    }
    if (noop) noop.src = NOOP_OFF;
    restoreWidget(this.widgetId, false);
    var starValue = getCurrentStarValue(evt, this);

    this.src    =  STARBAR_IMG_ROOT + "stars_2_" + starValue + "0.gif";
    setAltText(this, starValue);
    
    

}  

function setAltText(starbar, value) {
    var widgetType = starbar.widgetType;
    var starValue = value;
    
    switch (widgetType) {
        default:
            switch (value) {
                case 1: starbar.alt, starbar.title = ALT_MOV_1; break;
                case 2: starbar.alt, starbar.title = ALT_MOV_2; break;
                case 3: starbar.alt, starbar.title = ALT_MOV_3; break;
                case 4: starbar.alt, starbar.title = ALT_MOV_4; break;
                case 5: starbar.alt, starbar.title = ALT_MOV_5; break;
            
                default: starbar.alt, starbar.title = ALT_GENERIC; break;
            }       
            break;
    }
    
}

function StarbarFillInAll() {}