/**
***************************************************************************
*       @developed by 	Mediafamily SRL,
* 						web: http://www.mediafamily.ro
* 						email: office@mediafamily.ro
* 						address:	str Tudor Vladimirescu nr. 63/14, 540014, Targu Mures, Romania
*
*		This script make uses of prototype and scriptaculous JS libraries.
***************************************************************************
*/

var mPosX, mPoxY;

function showOkWindow(){

	// display the ajax return message at the cursord position
	msgContainer = $('ajax_result_box');
	msgContainer.style.display =  'block';
	msgContainer.innerHTML = '<img src="/images/ajax_ok.gif" width="16" height="16">';
	Effect.Fade('ajax_result_box');
}


function hideMsgWindow(){

	// show a X image
	msgContainer = $('ajax_result_box');
	msgContainer.innerHTML = '<img src="/images/ajax_error.gif" width="16" height="16">';
	Effect.Fade('ajax_result_box');
}


function showSpinnerWindow(){

	// show a X image
	msgContainer = $('ajax_result_box');
	msgContainer.style.display =  'block';
	msgContainer.innerHTML = '<img src="/images/spinner.gif" width="16" height="16">';
}

function getMousePosition(e) {

	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

	mPosX = posx+15;
	mPosY = posy;

	// set the position of the message window
	msgContainer = $('ajax_result_box');
	msgContainer.style.left = mPosX  + 'px';
	msgContainer.style.top = mPosY  + 'px';
}


var ratings = new Array;
// cache the images used for rollover
if (document.images)
{
  pic1= new Image(7,14);
  pic1.src="/images/star_rating/star_e1.jpg";
  pic2= new Image(7,14);
  pic2.src="/images/star_rating/star_e0.jpg";
  pic3= new Image(7,14);
  pic3.src="/images/star_rating/star_f1.jpg";
  pic4= new Image(7,14);
  pic4.src="/images/star_rating/star_f0.jpg";
  pic5= new Image(14,15);
  pic5.src="/images/star_rating/hate1.jpg";
  pic6= new Image(14,15);
  pic6.src="/images/star_rating/hate0.jpg";
  pic7= new Image(14,15);
  pic7.src="/images/star_rating/love0.jpg";
  pic8= new Image(14,15);
  pic8.src="/images/star_rating/love1.jpg";
}


function showStarRateingResponse(response)
{
		var s = response.responseText;
		var res = s.split(",");
		var group = res[0];
		var id = res[1];
		var index = res[2];
		var message = res[3];

		ratings['star_'+group+'_'+id] = index;

		showOkWindow();



}


function votio_vote(group, id, index, e)
{

	// save the event position;
	getMousePosition(e);

	// show the waiting message window
	showSpinnerWindow();

	var url = "/index.php?rate_article=" + id + "&vote=" + index;
	var myAjax = new Ajax.Request(
			url,
			{
				method: 'get',
				onComplete: showStarRateingResponse,
				onFailure: hideMsgWindow,
				onException: hideMsgWindow
			});
}

function votio_hatelove(group, id, option, e)
{

	// save the event position;
	getMousePosition(e);

	// show the waiting message window
	showSpinnerWindow();
	var url;
	i = $('star_'+group+'_'+id+'_hate');
	j = $('star_'+group+'_'+id+'_love');

	if (option=='hate') {
		if (i.src.indexOf('hate1.jpg')>0) {
			useih = '/images/star_rating/hate0.jpg';
		}else{
			useih = '/images/star_rating/hate1.jpg';
		}
		useil = '/images/star_rating/love0.jpg';
		url = "/direct-radiobutton_ajax-save_radiobutton-"+group+"Fav-" + id + "-hate.php";
	}else{
		if (j.src.indexOf('love1.jpg')>0) {
			useil = '/images/star_rating/love0.jpg';
		}else{
			useil = '/images/star_rating/love1.jpg';
		}
		useih = '/images/star_rating/hate0.jpg';
		url = "/direct-radiobutton_ajax-save_radiobutton-"+group+"Fav-" + id + "-fav.php";
	}


	i.setAttribute("src", useih);
	j.setAttribute("src", useil);

	var myAjax = new Ajax.Request(
			url,
			{
				method: 'get',
				onComplete: showOkWindow,
				onFailure: hideMsgWindow,
				onException: hideMsgWindow
			});
}

function votio_hover(id, index, rating)
{

	var i;

	if (ratings[id] == null){
		ratings[id] = rating;
	}

	/*
	txt = $("ratings_text_" + id);
	var msg = ['Poor, I would not recommend','Below Average','Averange','Above Average','Excellent, I would recomment this'];
	txt.innerHTML = msg[index-1];
	txt.style.display = 'block';
	*/


	for (z=1; z<=10; z++) {
		i = $(id + "_" + z);

		image_part = (z%2)?'0':'1';
		on_off = (z<=index)?'f':'e';
		on_off = on_off+image_part;

		usei = "/images/star_rating/star_"+on_off+".jpg";
		i.setAttribute("src", usei);
	}
}

function votio_unhover(id, index)
{
	var i;
	for (z=1; z<=10; z++) {
		i = $(id + "_" + z);

		image_part = (z%2)?'0':'1';
		on_off = (z<=ratings[id])?'f':'e';
		if (ratings[id] == 0) {
					// no vote yet
			on_off = 'n'+on_off;
		}
		on_off = on_off+image_part;

		usei = "/images/star_rating/star_"+on_off+".jpg";

		i.setAttribute("src", usei);
	}
	// txt.style.display = 'none';
}

