/**
 * @author Viames Marino
 */

// id dell'oggetto da votare
var itemRef;
var itemRef;
var sessionId;
// voto predefinito
var voto = 0;

//NB: LA VARIABILE VA SETTTATA NELLA PAGINA CHIAMANTE POPUP-VOTA.JSP
//var giudizi	= new Array(
//	'così così',
//	'niente di speciale',
//	'degno di nota',
//	'bello',
//	'strepitoso');

var starOff = '';


jQuery(document).ready(function(){

	/* pagina invio-skin */
	
	jQuery('.valued').focus(function() {
		var search_text = jQuery(this).attr('value');
		if ('Scrivi qui l\'autore'==search_text || 'Scrivi qui la tua email'==search_text) {
			jQuery(this).attr('value','');
		}
	});
	
	/* pagina gallery */
	
	// valorizza l'item da votare
	jQuery('.open-popup').click(function() {
		href = jQuery(this).attr('href');
		itemRef = href.substring(href.indexOf('?m=')+3);
		//alert("itemRef="+itemRef);
	});

	/* popup-vota */
	
	jQuery('img.stars').mouseover(function() {
		if (''==starOff || ''==starOn) {
			starOff	= jQuery('img.stars:first').attr('src');
			starOn	= starOff.substring(0,starOff.indexOf('off.png')) + 'on.png';
		}
		voto = jQuery('img.stars').index(this) + 1;
		jQuery('#giudizio').text(giudizi[voto-1]);
		for (i=0;i<5;i++) {
			//item = jQuery('img-stars').eq(i); IE bug
			if (i<voto) {
				jQuery('#star' + (i+1)).attr('src',starOn);
			} else {
				jQuery('#star' + (i+1)).attr('src',starOff);
			}
		}
	});
	
	// invia il risultato con AJAX
	jQuery('#btn_vote').click(function() {
		
		// ha cliccato prima di scegliere il voto
		if (0 == voto) {
		
			alert("È necessario indicare il voto per l'elemento scelto!");

		} else {
			
			// invio dati con ajax
			itemRef = jQuery("#idTexture").val();
			//alert("ID macchina = " + itemRef + "\n\nVoto = " + voto + " (" + giudizi[voto-1] + ")");
			//jQuery.ajax({});
			vote("AMM_TEXTURE_"+itemRef, voto);
			
			// conferma di registrazione voto
			jQuery('#popup').load('skin-close-popup.htm');
	
			// per una verifica del funzionamento
			//alert("ID macchina = " + itemRef + "\n\nVoto = " + voto + " (" + giudizi[voto-1] + ")");

		}
		
	});
	
	// trigger per il click di chiusura
	jQuery('#btn_close').click(function() {
		jQuery('#fancy_close').trigger('click');
		location.reload(); 
	});
	
});

/* COOKIE FUNCTION */

function setCookie(key,value) {
	//Cookie.set(key, value, {duration: 10000}); // save this for 10000 day
	jQuery.cookie(key, value, {expires: 365});
}

function getCookie(key) {
//	return Cookie.get(key);
	return jQuery.cookie(key);
}


function vote(uri, voto) {
    if(getCookie(uri)!='true'){
        //actualURI = uri;
        //alert("uri="+uri+"\nvote="+vote);
    	//alert("ID macchina = " + uri + "\n\nVoto = " + voto + " (" + giudizi[voto-1] + ")");    
        aggregatorAjaxService.saveVote(uri,voto, handleAfterUserVote);
    	//sessionId = jQuery('#sessId').val()
    	//jQuery.post("/it-it/voteContribute.htm", { uri: uri, voto: voto} );
        //aggregatorAjaxService.getCountOfVote(uri,handleGetCountOfVote);
        //aggregatorAjaxService.getVoteStat(uri,handleGetVoteStat);
        setCookie(uri,'true');
    }
    
}

function getIdFromUriVote(uri) {
	return uri.replace("AMM_TEXTURE_","");
}

function handleAfterUserVote(data) {
	if(data!=null){
    
        }else{
              alert('Internal Server Error');
	}
}









