/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2007 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */


function rateItem(itemId, rating, url) {
	
	// MODIF JC : replace item __currentItemToRate__ arg with selected item
	//url = url.replace('__currentItemToRate__', data_imageList[data_view]);
	//alert('url='+url+'\n');
	
    YAHOO.util.Connect.asyncRequest(
	'GET', url, {success: handleRatingResponse, failure: null, scope: null}, null);

    if ((rating == 3) || (rating == 4)) {
	$('#plusOne').show();
	setTimeout("$('#plusOne').fadeOut(2000)",500);
    }
}

function handleRatingResponse(http) {
    var results = http.responseText.split("\n");
    updateItemRating(results);
}

function updateItemRating(results) {
	
    // always update album_id rating div
    //var itemId = 'rateDiv';
    var itemId = results[0];
    var rating = results[1];
    var votes = results[2];
    var userRating = results[3];

    cpt_incitation = 7;
    if (window_vote==true)
    	close_incitation();

    //alert('UR:'+userRating+', R:'+rating);
    
    //updateElementDisplay('rating.rating.' + itemId, rating);
    //updateElementDisplay('rating.votes.' + itemId, votes);
    updateElementDisplay('rating.userRating.' + itemId, userRating);
    //updateAveragePercent(itemId, rating * 100 / 5);

    resetStarDisplay(itemId);
}

function resetStarDisplay(itemId) {
	//alert('reset de'+itemId);
	var userRating = document.getElementById('rating.userRating.' + itemId).innerHTML;
    updateStarDisplay(itemId, userRating);
}

function updateAveragePercent(itemId, averagePercent) {
    var e = document.getElementById('rating.averagePercent.' + itemId);
    if(e != undefined) {
    e.style.width = averagePercent + "%";
    }
}

function updateStarDisplay(itemId, userRating) {
//    var rating = document.getElementById('rating.rating.' + itemId).innerHTML;
    var rating = userRating;
    // just 4 grades
    for (i=1; i<=4; i++) {
		var star = document.getElementById('rating.star.' + itemId + '.' + i);
//		if (rating >= i && rating < i + 1) {
		if (rating > i - 1 && rating <= i) {
			star.className='giRatingFull' + i;
	    } else {
			star.className='giRatingEmpty' + i;
		}
    }
}


function __updateStarDisplay(itemId, userRating) {
    var rating = document.getElementById('rating.rating.' + itemId).innerHTML;
    // just 4 grades
    for (i=1; i<=4; i++) {
	var star = document.getElementById('rating.star.' + itemId + '.' + i);
	if (userRating != 'N/A') {
	    if ((rating >= i) && (userRating >= i)) {
		star.className='giRatingFullUserYes' + i;
	    } else if ((rating >= i) && (userRating < i)) {
		star.className='giRatingFullUserNo' + i;
	    } else if ((rating < i) && (userRating >= i)) {
		if (i - rating < 1) {
		    star.className='giRatingHalfUserYes' + i;
		} else {
		    star.className='giRatingEmptyUserYes' + i;
		}
	    } else if ((rating < i) && (userRating < i)) {
		if (i - rating < 1) {
		    star.className='giRatingHalfUserNo' + i;
		} else {
		    star.className='giRatingEmpty' + i;
		}
	    }
	} else {
	    if (rating >= i) {
		star.className='giRatingFullUserNo' + i;
	    } else if (i - rating < 1) {
		star.className='giRatingHalfUserNo' + i;
	    } else {
		star.className='giRatingEmpty' + i;
	    }
	}
    }
}

function updateElementDisplay(id, str) {
    //alert('id'+id+'str'+str+document.getElementById(id).childNodes[0]);
	document.getElementById(id).replaceChild(
	    document.createTextNode(str),
	    document.getElementById(id).childNodes[0]);
}
