//Set submission favorite. Used by submissionview.php.

var starflash = new Array;
starflash[1]='no';
starflash[2]='no';
starflash[3]='no';
var starpulseeffect = new Array;
starpulseeffect[1]=false;
starpulseeffect[2]=false;
starpulseeffect[3]=false;

//Flash star number.
function flashStar(stars) {
	
	tmpstars = stars;
	if (tmpstars == 1) tmpstars = '';

	if (starflash[stars]=='no') {
		starflash[stars]='yes';
		$('fav_block_star'+stars).src=starloc+tmpstars+'star.png';
		minopacity = 0.5;
		//Start at minimum opacity to avoid the first pulsation appearing to jump from full brightness to min opacity on first cycle.
		$('fav_block_star'+stars).setStyle({ opacity: minopacity });
		starpulseeffect[stars] = new Effect.Pulsate('fav_block_star'+stars, { pulses: 100, duration: 60, from: 1.0, to: minopacity });
	} else {
		starflash[stars]='no';
		$('fav_block_star'+stars).src=starloc+tmpstars+'star_30pc_trans.png';
		if (starpulseeffect[stars]) starpulseeffect[stars].cancel();
		$('fav_block_star'+stars).setStyle({ opacity: 1 });
	}
}

function flashStarStart(stars) {
	flashStarStop();
	flashStar(stars);
}

//Stop all flashing stars.
function flashStarStop() {
	starflash[1]='no';
	starflash[2]='no';
	starflash[3]='no';
	if (starpulseeffect[1]) starpulseeffect[1].cancel();
	if (starpulseeffect[2]) starpulseeffect[2].cancel();
	if (starpulseeffect[3]) starpulseeffect[3].cancel();
	$('fav_block_star1').setStyle({ opacity: 1 });
	$('fav_block_star2').setStyle({ opacity: 1 });
	$('fav_block_star3').setStyle({ opacity: 1 });
	$('fav_block_star1').src=starloc+'star_30pc_trans.png';
	$('fav_block_star2').src=starloc+'2star_30pc_trans.png';
	$('fav_block_star3').src=starloc+'3star_30pc_trans.png';
}

//Update interface to show new number of fav stars.
function setStars(stars) {
	
	flashStarStop();
	
	globalstars = stars;
	
	$('fav_block_remove').setStyle({ display: 'none' });
	$('fav_block_help').setStyle({ display: 'none' });
	
	switch (stars) {
		default:
		case '0':
			$('fav_block_help').setStyle({ display: 'inline' });
			$('fav_block_heart').src=favloc+'favorite_30pc_trans.png';
			$('fav_block_title').setStyle({ color: '#cccccc' });
			
			break;
		case '1':
			$('fav_block_remove').setStyle({ display: 'inline' });
			$('fav_block_star1').src=starloc+'star.png';
			$('fav_block_heart').src=favloc+'favorite.png';
			$('fav_block_title').setStyle({ color: 'ffffff' });
			break;
		case '2':
			$('fav_block_remove').setStyle({ display: 'inline' });
			$('fav_block_star2').src=starloc+'2star.png';
			$('fav_block_heart').src=favloc+'favorite.png';
			$('fav_block_title').setStyle({ color: 'ffffff' });
			break;
		case '3':
			$('fav_block_remove').setStyle({ display: 'inline' });
			$('fav_block_star3').src=starloc+'3star.png';
			$('fav_block_heart').src=favloc+'favorite.png';
			$('fav_block_title').setStyle({ color: 'ffffff' });
			break;
	}
}

function favProcess(mode, submission_id) {
	
	//Skip if this many stars is already set.
	if (globalstars == mode) return;
	
	var stars;
	var add = 'true';
	var remove;
	
	//Flash the clicked stars, or if we're removing then flash the current number of stars.
	if (mode != 'remove') { 
		flashStarStart(mode);
	} else {
		flashStarStart(globalstars);
	}
	
	if (mode=='1') {
		stars = '1';
	} else if (mode=='2') {
		stars = '2';
	} else if (mode=='3') {
		stars = '3';
	} else if (mode=='remove') {
		stars = '';
		add = '';
		remove = 'true';
	}
	
	new Ajax.Request('submissionfav_process.php',
	{
		parameters: { token: global_token, stars: stars, add: add, remove: remove, submission_id: submission_id },
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport){
			var response = transport.responseText || "{'error':'no response'}";
			
			if (!transport.responseText) {
				//There was some kind of error. Set stars back to what it was before.
				setStars(globalstars);
			}
			
			var jsondata = response.evalJSON(true);
			if (jsondata.success=='yes') {
				setStars(stars);
			} else if (jsondata.success=='mod') {
				setStars(globalstars);
				alert('You cannot fav this submission. You are not a friend of the submission owner.');
			} else if (!jsondata.success || jsondata.success != 'yes') {
				//There was some kind of error. Set stars back to what it was before.
				setStars(globalstars);
			}
		},
		onFailure: function() { 
			//There was some kind of error. Set stars back to what it was before.
			setStars(globalstars);
		}
	});

	/* GR disabled due to performance
        if (mode != 'remove') {
                //If it wasn't previously favved, show the recommendations div
                if (globalstars == 0) {
                        Effect.BlindDown('recommendationsbox');
                        Effect.Pulsate('loadingrecommendations', { pulses: 10, duration: 10 });
                        recommendationsProcess(submission_id);
                }
	} */
}
