$().ready(function() {

	if($('#imgcont').length > 0) {
		$('#imgcont').jcarousel();
	}
	
	if($('#commenti_form').size() > 0) {
		 $("#commenti_form").validate({
			 invalidHandler: function(form, validator) {
					var errors = validator.numberOfInvalids();
					if (errors) {
						$("div#error_container").show();
					} else {
						$("div#error_container").hide();
					}
				},
				onfocusout: false,
				onkeyup: false,
				onclick:false,
				meta: "validate",
				errorClass: "invalid",
				errorPlacement: function(error, element) {
					$labelToAppendTo = $('label[for='+element[0].id+']');
					error.appendTo($("div#error_container")).clone().appendTo($labelToAppendTo);
					$('<br />').appendTo($("div#error_container"));
				}
		 });
	}

	if($('.lightbox, .lightbox_inside').size() > 0) {

		$('.lightbox_inside').each(function() {
		 $(this).lightBox({
			imageLoading:			main_host+'images/lightbox/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
			imageBtnPrev:			main_host+'images/lightbox/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
			imageBtnNext:			main_host+'images/lightbox/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
			imageBtnClose:		main_host+'images/lightbox/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
			imageBlank:				main_host+'images/lightbox/lightbox-blank.gif'			// (string) Path and the name of a blank image (one pixel)
			});
		});

		$('.lightbox').lightBox({
			imageLoading:			main_host+'images/lightbox/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
			imageBtnPrev:			main_host+'images/lightbox/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
			imageBtnNext:			main_host+'images/lightbox/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
			imageBtnClose:		main_host+'images/lightbox/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
			imageBlank:				main_host+'images/lightbox/lightbox-blank.gif'			// (string) Path and the name of a blank image (one pixel)
			});

	}

	if($('#recaptcha_div').size() > 0) {
		Recaptcha.create($('#recaptcha_code').val(),
		"recaptcha_div", {
			 theme: "custom"
		});
	}

	// gestione commenti

	$('.switch_vis_commento').bind('click', function() {
		id = $(this).parent().attr('id').replace('commento', '');
		data = 'cmd=DYNAMIC_CONTENTS.SWITCHVISIBILITYCOMMENTO&id='+id;
		$.ajax({
			type: "POST",
			url: main_host+"ajax.php",
			data: data,
			dataType: "json",
			success: function(returned_data) { if(parseInt(returned_data.result) == 1) {
																					 location.href = location.href;
																				 } else {
																				 }
																			 }
		});

	});

	$('.cancella_commento').bind('click', function() {
		id = $(this).parent().attr('id').replace('commento', '');
		data = 'cmd=DYNAMIC_CONTENTS.DELETECOMMENTO&id='+id;
		$.ajax({
			type: "POST",
			url: main_host+"ajax.php",
			data: data,
			dataType: "json",
			success: function(returned_data) { if(parseInt(returned_data.result) == 1) {
																					 $('#commento'+id).parent().slideUp('slow').remove();
																				 } else {
																				 }
																			 }
		});

	});

	$('.edit_commento').bind('click', function() {
		if($('#edit_commento').size() > 0) {
			$('#edit_commento').parent().html($('#mem_commento').html());
		}


		id = $(this).parent().attr('id').replace('commento', '');
		$commento = $('.commento', $(this).parent().parent());

		commento = $.trim($commento.html());

		frm = '<span id="mem_commento" style="display:none">'+commento+'</span>';
		frm += '<form name="edit_commento" id="edit_commento" method="post" action="'+main_host+'ajax.php">';
		frm += '<input type="hidden" name="cmd" value="DYNAMIC_CONTENTS.SAVEPCOMMENTO" />';
		frm += '<input type="hidden" id="id_commento" name="id" value="'+id+'" />';
		frm += '<textarea name="commento" id="commento_textarea">'+commento+'</textarea><br />';
		frm += '<input type="submit" value="'+getLang('base', 'SALVA')+'" />';
		frm += '<input type="button" id="cancel_edit" value="'+getLang('base', 'CANCEL')+'" />';
		frm += '<br class="clear" />';
		frm += '</form>';

		$commento.html(frm);

		$('#cancel_edit').bind('click', function() {
			$commento.html($('#mem_commento').html());
		});

		$('#edit_commento').ajaxForm({
			success: function(ret_data) {
				$commento.html(ret_data.commento);
			},
			dataType: 'json'
		});

	});

	$container = $('.container_docs');

	if($container.size() > 0) {

		$scroller = $('#scroller_docs');
		thumb_docs_height = $('.container_docs #docs img:first').height();
		thumb_docs_width = $('.container_docs #docs img:first').width();
		var passo_docs = thumb_docs_width*2;
		numero_immagini = $('.container_docs #docs img').size();
		lunghezza_contenitore_docs = ((5+thumb_docs_width)*numero_immagini)-5;

		$container.css({'height':thumb_docs_height+'px'});
		if(lunghezza_contenitore_docs <= ($container.width()-66)) {
			scroller_docs_width = $container.width()-66;
		} else {
			$('#prev_doc, #next_doc').css({'display':'block'});
			scroller_docs_width = $container.width()-66;
			$('#docs').css({'position':'relative', 'width':lunghezza_contenitore_docs+'px'});

			$('#prev_doc img').css({'position':'relative', 'top':((thumb_docs_height/2)-8)+'px', 'display':'block', 'left':(($('#prev_doc').width()/2)-12)+'px'});
			$('#next_doc img').css({'position':'relative', 'top':((thumb_docs_height/2)-8)+'px', 'display':'block', 'left':(($('#next_doc').width()/2)-12)+'px'});
			$('#prev_doc, #next_doc').css({'cursor':'pointer', 'height': thumb_docs_height+'px', 'width':'28px', 'border':'1px solid #fff'});

			$('#prev_doc').bind('click', function() {
				posizione_attuale = parseInt($('#docs').css('left').replace('px', ''));
				log(posizione_attuale);
				if(posizione_attuale == 0 || (-1*posizione_attuale) < passo_docs) {
					new_pos = 0;
				} else {
					new_pos = posizione_attuale + passo_docs;
				}
				$('#docs').animate({
						left: new_pos+'px'
				}, 500, 'swing' );
			});

			$('#next_doc').bind('click', function() {
				max_left = -1*(lunghezza_contenitore_docs - scroller_width);
				posizione_attuale = parseInt($('#docs').css('left').replace('px', ''));
				log('Posizione attuale :'+posizione_attuale);
				if(posizione_attuale == max_left || (posizione_attuale-passo) <= max_left) {
					new_pos = max_left;
				} else {
					new_pos = posizione_attuale - passo_docs;
				}
				$('#docs').animate({
						left: new_pos+'px'
				}, 500 , 'swing');
			});

		}

		$scroller.css({'position':'relative',
									 'width':scroller_docs_width+'px',
									 'height':thumb_docs_height,
									 'overflow':'hidden'});
	}

});

function showvideo(title, contenuto) {
	$('<div id="youtubeplayer">&nbsp;</div>').dialog({
		title: title,
		modal : true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		width:'425px',
		height:'388px',
		close:function() {
			$(this).dialog('destroy').remove();
		}
	});

}

var ytplayer;
function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("youtubeplayer");
	playVideo();
}
function playVideo() {
	ytplayer.playVideo();
}
