// Variables
// var ratings = new sack(ratings_ajax_url);
var post_id = 0;
var post_rating = 0;
var rate_fadein_opacity = 0;
var rate_fadeout_opacity = 100;
var is_ie = (document.all && document.getElementById);
var is_moz = (!document.all && document.getElementById);
var is_opera = (navigator.userAgent.indexOf("Opera") > -1);
var is_being_rated = false;
var ratings_custom = false;
var ratings_max = 5;
var ratings_plugin_url = '';
var ratings_text_wait = 'Operatiune in progres...';
var ratings_show_loading = true;
var ratings_response = '';
var rate_pro = '';
var rate_cons = '';
var comment_id = 0;
var comment_edit = false;
var ratings_show_fading = false;

// Post Ratings Fade In Text
function vote_fadein_text(val) {
	ratings_response = val;
	vote_fadein_text1();
}

function vote_fadein_text1() {
	if(rate_fadein_opacity < 100) {
		rate_fadein_opacity += 10;
		if(is_opera)  {
			rate_fadein_opacity = 100;
		} else	 if(is_ie) {
			if(ratings_show_fading) {
				document.getElementById('post-ratings-' + post_id).style.filter = 'alpha(opacity=' + rate_fadein_opacity + ')';
			} else {
				rate_fadein_opacity = 100;
			}
		} else	 if(is_moz) {
			if(ratings_show_fading) {
				document.getElementById('post-ratings-' + post_id).style.MozOpacity = (rate_fadein_opacity/100);
			} else {
				rate_fadein_opacity = 100;
			}
		}
		setTimeout("vote_fadein_text1()", 100); 
	} else {
		rate_fadein_opacity = 100;
		rate_unloading_text();
		if(ratings_response == '') {
			alert('Parerea si evaluarea ta a fost trimisa. Va fi moderata in cel mai scurt timp. Iti multumim!');
			is_being_rated = false;
			rate_pro = '';
			rate_cons = '';
		}
		else {
			alert(ratings_response);
		}
		ratings_response = '';
	}
}

function vote_current(id, rating, rating_text) {
	if(!is_being_rated) {
		post_id = id;
		post_rating = rating;
		if(ratings_custom && ratings_max == 2) {
			document.images['rating_' + post_id + '_' + rating].src = eval("ratings_" + rating + "_mouseover_image.src");
		} else {
			for(i = 1; i <= rating; i++) {
				if(ratings_custom) {
					document.images['rating_' + post_id + '_' + i].src = eval("ratings_" + i + "_mouseover_image.src");
				} else {
					document.images['rating_' + post_id + '_' + i].src = '/images/ratings/rating_on.gif';
				}
			}
		}
		if(document.getElementById('ratings_' + post_id + '_text')) {
			document.getElementById('ratings_' + post_id + '_text').style.display = 'inline';
			document.getElementById('ratings_' + post_id + '_text').innerHTML = rating_text;
		}
	}
}

function vote_login() {
	alert('Trebuie sa fii inregistrata si logata ca sa poti lasa o parere despre acest aliment!');
}

function vote_off(rating_score, insert_half) {
	if(!is_being_rated) {
		for(i = 1; i <= ratings_max; i++) {
			if(i <= rating_score) {
				if(ratings_custom) {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/ratings/' + 'rating_' + i + '_on.gif';
				} else {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/ratings/' + 'rating_on.gif';
				}
			} else if(i == insert_half) {
				if(ratings_custom) {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/ratings/' + 'rating_' + i + '_half.gif';
				} else {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/ratings/' + 'rating_half.gif';
				}
			} else {
				if(ratings_custom) {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/ratings/' + 'rating_' + i + '_off.gif';
				} else {
					document.images['rating_' + post_id + '_' + i].src = ratings_plugin_url + '/images/ratings/' + 'rating_off.gif';
				}
			}
		}
		if(document.getElementById('ratings_' + post_id + '_text')) {
			document.getElementById('ratings_' + post_id + '_text').style.display = 'none';
			document.getElementById('ratings_' + post_id + '_text').innerHTML = '';
		}
	}
}


// Post Ratings Loading Text
function rate_loading_text() {
	if(ratings_show_loading) {
		document.getElementById('post-ratings-' + post_id + '-loading').style.display = 'inline';
	}
}


// Post Ratings Finish Loading Text
function rate_unloading_text() {
	if(ratings_show_loading) {
		document.getElementById('post-ratings-' + post_id + '-loading').style.display = 'none';
	}
}

function vote_edit(cid) {
	if(!is_being_rated) {
		var rating = $('mark-' + cid).value;
		var mark = '';
		comment_id = cid;
		for(i = 1; i <= ratings_max; i++) {
			if(i <= rating) 
				mark = mark + '<img src="/images/ratings/rating_on.gif" />';
			else
				mark = mark + '<img src="/images/ratings/rating_off.gif" />';
		}
		$('evMark').innerHTML = mark + ' (' + rating + ')'; 
		$('evPro').innerHTML = $('pro-' + cid).innerHTML; 
		$('evCons').innerHTML = $('cons-' + cid).innerHTML; 
		$('evVote').value = ' Modifică '; 
		comment_edit = true;
		Modalbox.show($('evaluateProd').innerHTML, {title: "Modifica parerea despre aliment",  width:540 });
	} else {		
		alert(ratings_text_wait);
	}	
}

//Process Post Ratings
function vote_post() {	
	if(!is_being_rated) {
		var mark = '';
		for(i = 1; i <= ratings_max; i++) {
			if(i <= post_rating) 
				mark = mark + '<img src="/images/ratings/rating_on.gif" />';
			else
				mark = mark + '<img src="/images/ratings/rating_off.gif" />';
		}
		$('evMark').innerHTML = mark + ' (' + post_rating + ')'; 
		comment_edit = false;
		Modalbox.show($('evaluateProd').innerHTML, {title: "Parerea ta despre aliment",  width:540 });
	} else {		
		alert(ratings_text_wait);
	}
}

function vote_edit_cb(val) {
	if(val == '') {
		if(comment_id > 0) {
			$('pro-' + comment_id).innerHTML = rate_pro;
			$('cons-' + comment_id).innerHTML = rate_cons;
			rate_pro = '';
			rate_cons = '';
			comment_id = 0;
		}
	}
	else {
		alert(val);
	}
	comment_edit = false;
}

function vote_post_process(isEdit) {	
	if(!is_being_rated) {
		var pro = trim($('evPro').value);
		if(pro == '') {
			$('evMsg').innerHTML = 'Nu ai completat ce iti place la acest aliment!';
			$('evPro').focus();
			return;
		}
		if(pro.length <= 10) {
			$('evMsg').innerHTML = 'Textul pentru ce iti place la acest aliment este prea mic!';
			$('evPro').focus();
			return;
		}
		
		var cons = trim($('evCons').value);
		if(cons == '') {
			$('evMsg').innerHTML = 'Nu ai completat ce nu iti place la acest aliment!';
			$('evCons').focus();
			return;
		}
		if(cons.length <= 10) {
			$('evMsg').innerHTML = 'Textul pentru ce nu iti place la acest aliment este prea mic!';
			$('evCons').focus();
			return;
		}		
		rate_pro = pro;
		rate_cons = cons;
		Modalbox.hide();
		if(comment_edit) {
			if(comment_id != 0)
				x_commentEdit(comment_id, post_rating, rate_pro, rate_cons, vote_edit_cb);
			else 
				alert('Eroare: Comentariu incorect!');
		}
		else {
			is_being_rated = true;
			rate_loading_text();
			rate_process();
		}
	} else {		
		alert(ratings_text_wait);
	}
}


// Process Post Ratings
function rate_process() {
	if(rate_fadeout_opacity > 0) {
		rate_fadeout_opacity -= 10;
		if(is_opera) {
			rate_fadein_opacity = 0;
		} else if(is_ie) {
			if(ratings_show_fading) {
				document.getElementById('post-ratings-' + post_id).style.filter = 'alpha(opacity=' + rate_fadeout_opacity + ')';
			} else {
				rate_fadein_opacity = 0;
			}
		} else if(is_moz) {
			if(ratings_show_fading) {
				document.getElementById('post-ratings-' + post_id).style.MozOpacity = (rate_fadeout_opacity/100);
			} else {
				rate_fadein_opacity = 0;
			}
		}
		setTimeout("rate_process()", 100); 
	} else {
		rate_fadeout_opacity = 0;
		x_voteProduct(post_id, post_rating, rate_pro, rate_cons, vote_fadein_text);
		rate_fadein_opacity = 0;
		rate_fadeout_opacity = 100;
	}
}
