﻿// *******************************************************************
// RATINGS FUNCTIONALITY
// Created by Chris Gregg, Copyright 2009
// *******************************************************************



// -------------------------------------------------------------------
// Writes rating bars to screen, and sets mouse event handlers
// -------------------------------------------------------------------

function generateRatingsBar(uniqueBarID) {

	// Write rating bars to screen
	document.write("<div class=\"ratingBox\">");
	for (x=1; x<=5; x++) {
		document.write("<img src=\"/images/ratingBar_off.gif\" alt=\"\" id=\""+uniqueBarID+x+"\" name=\""+uniqueBarID+"\" />");
	}
	document.write("<span id=\""+uniqueBarID+"Desc\" class=\"barDesc\"></span>");
	document.write("</div>");
	
	// Event handlers for bar images
	for (x=1; x <=5; x++ ) {
		document.getElementById(uniqueBarID+x).onmouseover = rollover; // set rollover image
		document.getElementById(uniqueBarID+x).onmouseout = resetBars; // reset bars to original state
		document.getElementById(uniqueBarID+x).onclick = clicked;      // set bars to selected rating
	}
    
    loadRating(uniqueBarID); // Pre-sets rating bar to value from hidden field

}


// -------------------------------------------------------------------
// Writes rating bars to screen with value (read only) - not able to change rating
// -------------------------------------------------------------------

function displayRatingsBar(uniqueBarID, ratingValue) {

	// Write rating bars to screen
	document.write("<div class=\"ratingBox\">");
	for (x=1; x<=5; x++) {
		document.write("<img src=\"/images/ratingBar_off.gif\" alt=\"\" id=\""+uniqueBarID+x+"\" name=\""+uniqueBarID+"\" />");
	}
	document.write("</div>");
    
    
    if (ratingValue > 0) {	    
	    for (x=1; x <= 5; x++) {
		    if (x <= ratingValue) {
			    document.getElementById(uniqueBarID+x).src = document.getElementById(uniqueBarID+x).src.replace(/_off/,"_on");
		    }
	    }	
    }
            

}



// -------------------------------------------------------------------
// Supporting functions for ratings box
// -------------------------------------------------------------------

// Changes bar to rollover status to "over"
function rollover() { 
	var group = this.name; // gets the group name for this ratings bar
	var selected = this.id.replace(group,""); // extract just the number from ID
	for (x=1; x <= 5; x++) {
		if (x <= selected) {
			document.getElementById(group+x).src = document.getElementById(group+x).src.replace(/_off/,"_over");
		} 
	}
	
    getDifficultyDescription(group, selected);	
		
}

// Resets bar images to original state
function resetBars() {
	var group = this.name; // gets the group name for this ratings bar	
	for (x=1; x <=5; x++) {
		document.getElementById(group+x).src = document.getElementById(group+x).src.replace(/_over/,"_off");
	}
	
	removeDescription(group);
}


// Sets all bar images to "off" state
function clearBars(group) {
	for (x=1; x <=5; x++) {
		document.getElementById(group+x).src = "/images/ratingBar_off.gif";
	}
}


// Select all bars leading up to and including the selected one
function clicked() {
	var group = this.name; // gets the group name for this ratings bar 	
	var selected = this.id.replace(group,"");
	clearBars(group); // reset all bar images to "off" first
	storeValue(group, selected); // store selected rating to hidden field
	for (x=1; x <= 5; x++) {
		if (x <= selected) {
			document.getElementById(group+x).src = document.getElementById(group+x).src.replace(/_off/,"_on");
		}
	}	
		
}

// Get value from hidden field, and display as rating
function loadRating(group) {

    var selected = document.getElementById(group+"Rating").value; // get value from hidden field
    
    if (selected > 0) {
	    clearBars(group); // reset all bar images to "off" first
	    for (x=1; x <= 5; x++) {
		    if (x <= selected) {
			    document.getElementById(group+x).src = document.getElementById(group+x).src.replace(/_off/,"_on");
		    }
	    }	
    }
    
}


// Store selected rating value to hidden field
function storeValue(group, value) {
	document.getElementById(group+"Rating").value = value;
	//alert(document.getElementById(group+"Value").value);
}


// Used to show text description for each rating bar
function getDifficultyDescription(group, value) {

    if (group == "difficulty") {

        if (value == 1) {
            desc = "Simple";
        }
        else if (value == 2) {
            desc = "Easy";
        }
        else if (value == 3) {
            desc = "Moderate";
        }
        else if (value == 4) {
            desc = "Advanced";
        }
        else if (value == 5) {
            desc = "Expert";
        } 
    } 
    else if (group == "health") {
    
        if (value == 1) {
            desc = "Indulge";
        }
        else if (value == 2) {
            desc = "Splurge";
        }
        else if (value == 3) {
            desc = "Balanced";
        }
        else if (value == 4) {
            desc = "Healthy";
        }
        else if (value == 5) {
            desc = "Very Healthy";
        }     
    }
    
        document.getElementById(group+"Desc").innerHTML = desc;
    	    

}


// On mouse out, need to hide description
function removeDescription(group) {
        document.getElementById(group+"Desc").innerHTML = "";
}



// Returns star rating for HTML
function getStarRatingHTML(rating) {

    outputRatingAsHTML = "<div class=\"starRating\"><strong>User Rating:</strong> ";
    
    for (x = 1; x <= 5; x++) {
        // write "on-state" stars
        if (x <= rating) {
            outputRatingAsHTML += "<img src=\"/images/star_ratingSmall_on.gif\" align=\"absmiddle\" >";
        }
        // write "off-state" stars
        else {
            outputRatingAsHTML += "<img src=\"/images/star_ratingSmall_off.gif\" align=\"absmiddle\" >";
        }
    }

    outputRatingAsHTML += "</div>";

    return outputRatingAsHTML;

}


