function openFBSharer(url) {
	window.open('http://www.facebook.com/share.php?u=' + encodeURIComponent(url),'fb_sharer', 'toolbar=0, status=0, width=626, height=436, top=50');
	return false;
}
function openLinkedInSharer(url) {
	window.open('http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(url),'li_sharer', 'toolbar=0, status=0, width=626, height=436, top=50');
	return false;
}

$(function(){

	$('#modal_form_comments').dialog({
		autoOpen: false,
		width: 400,
		modal: true,
		resizable: false,
		draggable: false,
		buttons: false,
		close: function() {
			$.validationEngine.closePrompt('.formError',true);
			$('#div_video_id').css('display','');
		}
		
	});
	
	$('.ins_commento_link').click(function() {
		$('#modal_form_comments').dialog('open');
		try {
			$('#div_video_id').css('display','none');
		} catch (e) {}
		return false;
	});

	$('.dettaglioTabs .tabsHeader a').click(function() {
		$('.dettaglioTabs .tabsHeader a').removeClass('selected');
		$(this).addClass('selected');
		$('.dettaglioTabs .tabsBody .content_tabs').removeClass('selected');
		$('#'+ $(this).attr('id') +'_div').addClass('selected');
		return false;
	});

});

$(document).ready(function() {
	
	$('#btn_cart').click(function() {
		window.location.href = ROOT_URL + '/carrello/add_prodotto.php?id=' + id_libro;
	});

	$('#id_form_commento').submit(function() {

		if ($('#id_form_commento').validationEngine(
										{returnIsValid:true,
										inlineValidation: false}
										)) {
			ajax_salvaCommento(); 
		}
		return false;
	
	})

	function ajax_salvaCommento() {
		var _data_serialized = $("#id_form_commento").serialize();
		$.post(
				ROOT_URL + "/ajax/commento_salva.php", 
				_data_serialized,
				function(data) {
					switch(data.stato) {
						case -1:
							// dati mancanti
							$('#msg_errore_commento').html('Dati mancanti');
							$('#msg_errore_commento').css('display','block');
							//alert(data.msg);
							break;
						case -2:
							// Captcha errato
							$('#msg_errore_commento').html('Il numero inserito non corrisponde');
							$('#msg_errore_commento').css('display','block');
							$('#commento_captcha').css('background-color','#ff0');
							//alert(data.msg);
							break;
						case -3:
							// Libro non valido
							$('#msg_errore_commento').html('Libro non valido');
							$('#msg_errore_commento').css('display','block');
							//alert(data.msg);
							break;
						case 1:
							$("#modal_form_comments").html( $("#commento_inserito").html() );
							if (data.html_commento) {
								
								var class_commento_inserito = 'odd';
								if ($("#container_commenti .commenti_lettori:first-child").hasClass('odd')) {
									class_commento_inserito = 'even';
								}
								
								$(data.html_commento).prependTo($('#container_commenti'));
								
								if (class_commento_inserito == 'odd') {
									$('#commento_' + data.id_commento).removeClass('even');
									$('#commento_' + data.id_commento).addClass('odd');
								} else {
									$('#commento_' + data.id_commento).removeClass('odd');
									$('#commento_' + data.id_commento).addClass('even');
								}
								
							}
							break;
					}
					
				},
		'json'
		);
	
		return false;
	}

});

