//(c) 2008 Michael Manning 
jQuery.parseQuery=function(A,B){var C=(typeof A==="string"?A:window.location.search),E={f:function(F){return unescape(F).replace(/\+/g," ")}},B=(typeof A==="object"&&typeof B==="undefined")?A:B,E=jQuery.extend({},E,B),D={};jQuery.each(C.match(/^\??(.*)$/)[1].split("&"),function(F,G){G=G.split("=");G[1]=E.f(G[1]);D[G[0]]=D[G[0]]?((D[G[0]] instanceof Array)?(D[G[0]].push(G[1]),D[G[0]]):[D[G[0]],G[1]]):G[1]});return D};

function checkSendToFriend() {
	var email = $('#emailAmico').val();
	var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	  if (!(email_filter.test(email))) {
	  alert("Inserisci un'email valida!");
	  $('#emailAmico').focus();
	  return false;
	} else {
	  var msg = $('#msgAmico').val();
	  var id = $('#id').val();
				var data = {
		email: email,
		msg: msg,
		id: id
	  };          
	  $.post('./magazine_ajax_sendToFriend.php',data,function (data) {
		if (data == "OK") {
		  $('#sendToFriendContainer').html('Articolo inviato, grazie.');
		} else {
		  alert ('Si è verificato un errore durante l\'invio del messaggio.');
		  $('#emailAmico').focus();
		  return false;
		}
	  });
	}
}

function hideSendToFriend() {
		$('#sendToFriend').hide();
}

function showSendToFriend() {
		var pos = $('#sendToEmail').position();
var hei = $('#sendToFriend').height();

$('#sendToFriend').css('position','absolute');
$('#sendToFriend').css('top',pos.top - hei);
$('#sendToFriend').css('left',pos.left);

$('#sendToFriend').show();
$('#inputSendToFriend').click(checkSendToFriend);
}

function enableStars() {    	
		$('a.starvote').hover(
			function() {
				var $title = $(this).attr('title');
				for (var i=1;i<6;i++) {
					if (i <= $title) 
						$('#s' + i + " img").attr('src', 'img/star_on.gif');
					else 
						$('#s' + i + " img").attr('src', 'img/star.gif');
				}	
				switch ($title) {
					case "1":
						$('#votedescription').html('Scarso');
					break;
					case "2":
						$('#votedescription').html('Niente di speciale');
					break;
					case "3":
						$('#votedescription').html('Degno di nota');
					break;
					case "4":
						$('#votedescription').html('Bello');
					break;
					case "5":
						$('#votedescription').html('Strepitoso!');
					break;
					default:
						$('#votedescription').html('???');
					break;
				}
			},
			function () {
				void(0);
			}
		);
		$('a.starvote').click(function() {
			disableStars();
			// alert ("Disabilito");
			var $querystring = $.parseQuery();
			var $id = $querystring.id;
			// alert ("Leggo l'id " + $id);
			var $vote = $(this).attr('title');
			// alert ("Leggo il voto " + $vote);							
			var $data = {
				id : $id,
				vote : $vote
			};	
			// alert ("Creo l'oggetto data");				
			$('#votedescription').load('./magazine_ajax_sendVote.php',$data);
			// alert ("Invio il voto");
		});
	}
	
	function disableStars() {
		$('.starvote').unbind('hover');
		if ($('#votedescription').html() != 'Il tuo voto è stato salvato, grazie!' && $('#votedescription').html() != 'Hai già votato questo articolo.' && $('#votedescription').html() != 'Impossibile salvare il voto.') {
			$('#votedescription').html('');
		}
	}
	
	function setupVotes() {
		var votestatus = $('#votearea').html();
		$('#votearea').hover(
			function() {
				enableStars();		
			},
			function() {
				disableStars();
				$('#votearea').html(votestatus);
			}
		);
	}
	
	function setSondaggioForm() {
		$('textarea').focus(function() {
			var $id = $(this).attr('id');
			var $ref = $id.replace('t_','r_');
			if (!$('#' + $ref).is(':checked') ) {
				$('#' + $ref).attr("checked", "checked"); 
			}
		});
		$('textarea').blur(function() {
			var $id = $(this).attr('id');
			var $ref = $id.replace('t_','r_');
			if ($(this).val() == '' || $(this).val() == undefined) {
				$('#' + $ref).removeAttr("checked", "checked"); 
			}
		});
		$('input[type=checkbox]').click(function() {
			var $id = $(this).attr('id');
			var $ref = $id.replace('r_','t_');
			if ($(this).is(":checked")) {
				if ($('#' + $ref).length > 0) {
					$('#' + $ref).focus();
				}
			} else {
				$('#' + $ref).val('');
			}
		});
	}
	
	function checkSondaggioForm() {
		var $ok = 1;
		var $elem = $('fieldset input[name^="r_"]');
		var $tot = $('fieldset input[name^="r_"]').length;
		var $blocks = new Array();
		var $j = 0;
		for ($i=0;$i<$tot;$i++) {
			if ($blocks.indexOf($($elem[$i]).attr('name')) == -1) {
				$blocks[$j] = $($elem[$i]).attr('name');
				$j++;
			}
		}
		for ($i=0;$i<$blocks.length;$i++) {					
			if ($('input[name="' + $blocks[$i] + '"]:checked').length < 1) {
				$ok = 0;
			}
		}
		if ($ok == 1)
			$('#formSondaggio').submit();
		else
			alert ("Devi rispondere a tutte le domande per poter proseguire");
	}

$(document).ready(function() {
		setLinkTarget();
		if ($('#art_content').length > 0) {
			$('#art_content').show();
		}
		if ($('#sendToEmail').length > 0) {
			$('#sendToEmail').click(showSendToFriend);
		}
		if ($('#votearea').length > 0) {
					setupVotes();
		}
		if ($('#printContent').length > 0) {
			$('#printContent').click(function() {
				var $url = $(this).attr('href');
				window.open($url,'print','width=600,height=400,scrollbars=1');
				return false;
			});
		}
		if ($('#sondaggioRispondi').length > 0) {
			$('#sondaggioRispondi').click(function() {
				$('#formSondaggi').submit();
			});
		}
		if ($('#sondaggioRisultati').length > 0) {
			$('#sondaggioRisultati').click(function() {
				var $id = $('#idsondaggio').val();
				document.location.href = 'magazine_sondaggi_risultati.php?id=' + $id + '&action=view'; 
			});
		}
		if ($('#sndSubmit').length > 0) {
			setSondaggioForm();
			$('#sndSubmit').click(checkSondaggioForm);
		}
});
