/*  SINOVAC JavaScript FRW
 *  Version 1.0.0
 *  (c) Serv. Inf. NOVAC, Sdad. Coop. And.
 *
 *   Yes, this code is waste.
/*--------------------------------------------------------------------------*/

var rated = false ;
var image_id = '' ;
var user_id = '' ;
var root_url = '/'
var action_url = '/rateme/' ;

function startObserves() {

	Event.observe('star_1', 'mouseover', function(e){ changeState(1, 'on') });
	Event.observe('star_2', 'mouseover', function(e){ changeState(2, 'on') });
	Event.observe('star_3', 'mouseover', function(e){ changeState(3, 'on') });
	Event.observe('star_4', 'mouseover', function(e){ changeState(4, 'on') });
	Event.observe('star_5', 'mouseover', function(e){ changeState(5, 'on') });
	Event.observe('star_6', 'mouseover', function(e){ changeState(6, 'on') });
	Event.observe('star_7', 'mouseover', function(e){ changeState(7, 'on') });
	Event.observe('star_8', 'mouseover', function(e){ changeState(8, 'on') });
	Event.observe('star_9', 'mouseover', function(e){ changeState(9, 'on') });
	Event.observe('star_10', 'mouseover', function(e){ changeState(10, 'on') });

	Event.observe('star_1', 'click', function(e){ ratePhoto(1) });
	Event.observe('star_2', 'click', function(e){ ratePhoto(2) });
	Event.observe('star_3', 'click', function(e){ ratePhoto(3) });
	Event.observe('star_4', 'click', function(e){ ratePhoto(4) });
	Event.observe('star_5', 'click', function(e){ ratePhoto(5) });
	Event.observe('star_6', 'click', function(e){ ratePhoto(6) });
	Event.observe('star_7', 'click', function(e){ ratePhoto(7) });
	Event.observe('star_8', 'click', function(e){ ratePhoto(8) });
	Event.observe('star_9', 'click', function(e){ ratePhoto(9) });
	Event.observe('star_10', 'click', function(e){ ratePhoto(10) });
	
	Event.observe('rate_image', 'mouseout', function(e){ changeState(10, 'off') });
}


function changeState(id, state) {
	var block_id = '' ;

	if (rated) return true ;

	if (state == 'on') {
		for (i=id; i>0; i--) {
			block_id = 'star_' + i ;
			$(block_id).src = root_url + 'include/images/rate_full-02.gif' ;
		}
		$('rated').innerHTML = id ;
	} else if (state == 'off') {
		for (i=id; i>0; i--) {
			block_id = 'star_' + i ;
			$(block_id).src = root_url + 'include/images/rate_empty-02.gif' ;
		}
		$('rated').innerHTML = '&nbsp;' ;
	}
}

function ratePhoto(id) {
	if (rated) return true ;
	changeState(id, 'on') ;
	setRated() ;

	var success = function(t){	};
	var url = action_url + 'ajax.requestdata.php' ;
	var pars = 'action=rate&id=' + image_id + '&value=' + id + '&user_id=' + user_id ;
	var block = 'rated';
	var myAjax = new Ajax.Updater(block, url, { method: 'get', parameters: pars, onComplete: success });
}

function setRated() {
	rated = true ;
}

function setUserId(id) {
	user_id = id;
}

// ????
function centerImage() {
	var pwidth = $('photo').width ;
	pwidth = (500 - pwidth)/2 ;
	$('photo').setStyle({padding: '0 0 0 ' + pwidth + 'px'});
}

function getImageInfo(id) {
	var success = function(t){
		//centerImage() ;
		startObserves();
	};
	image_id = id ;
	var url = action_url + 'ajax.requestdata.php' ;
	var pars = 'action=getimage&id=' + id ;
	var block = 'loader' ;
	//alert(url + '?' + pars) ;
	var myAjax = new Ajax.Updater( block, url, { method: 'get', parameters: pars, evalScripts: true, onComplete: success });
}