$(document).ready(function() {		
	
	//Execute the slideShow
	if($('#httpwwwspazioinprovait').length!=0){
		slideShow();
	}
	
	attivaContent();

});

var galleryAvviata=false;

function slideShow() {

	if(galleryAvviata){return false;}else{galleryAvviata=true;}

	//Set the opacity of all images to 0
	$('#httpwwwspazioinprovait a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#httpwwwspazioinprovait a:first').css({opacity: 1.0});
	
	$('#httpwwwspazioinprovait .caption').css({display: 'block'});
	
	//Set the caption background to semi-transparent
	$('#httpwwwspazioinprovait .caption').css({opacity: 0.7});

	//Resize the width of the caption according to the image width
	$('#httpwwwspazioinprovait .caption').css({width: $('#httpwwwspazioinprovait a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
	$('#httpwwwspazioinprovait .content').html($('#httpwwwspazioinprovait a:first').find('img').attr('rel'))
	.animate({opacity: 0.7}, 400);
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',6000);
	
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#httpwwwspazioinprovait a.show')?  $('#httpwwwspazioinprovait a.show') : $('#httpwwwspazioinprovait a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#httpwwwspazioinprovait a:first') :current.next()) : $('#httpwwwspazioinprovait a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#httpwwwspazioinprovait .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#httpwwwspazioinprovait .caption').animate({opacity: 0.7},100 ).animate({height: '70px'},500 );
	
	//Display the content
	$('#httpwwwspazioinprovait .content').html(caption);
	
}

var tag = window.location+"/";
tag = tag.replace(/[^a-zA-Z0-9\s]/g,"");
tag = tag.toLowerCase();
tag = tag.replace(/\s/g,'-');

//alert(tag);

var attuale=tag;

function loadDiv(id, url){
			var scomparso=false;
			var caricato=false;

			
			if($('#'+id).length==0){
				$('#contenuto').append('<div id="'+id+'" style="display:none"></div>');		
				$("#"+id).load(url+"?notemplate=true", function(){
					caricato=true;
					if(scomparso&&caricato){
							 $("#"+id).show('fold');
							 attuale=id;		
							 attivaContent();	
							 if($('#httpwwwspazioinprovait').length!=0) slideShow();
						}
				});
			
			}else{
				caricato=true;
			}
			$('#contenuto #'+attuale).hide('fold',function(){
				scomparso=true;
					if(scomparso&&caricato){
							 $("#"+id).show('fold');
							 attuale=id;	
							 attivaContent();	
							 if($('#httpwwwspazioinprovait').length!=0) slideShow();
		
						}
			});
        	//$('#contenuto').append('<div id="'+id+'"><br /><br />dsfsd</div>');
        	//$("#"+id).load(url+"?notemplate=true");
        	
}


	function doAjax(form){
	
	$("#"+form).append('<br /><img src="/wp-content/themes/spazioinprova/images/loading.gif" alt="" /> Verifica in corso...');
	//alert($("#"+form).attr('action'));
   			
    var variabili=$("#"+form).serialize();
   	$.ajax({
   	   type: $("#"+form).attr('method'),
	   url: $("#"+form).attr('action'),
	   data: variabili,
	   success: function (data) {
	      $("#"+form).replaceWith($(data).find("#"+form));
	      		attivaContent()

	   },
	   dataType: 'html'
	});		
   			

	return false;
	}


function attivaContent(){
//$(".datepicker").datepicker();
	//$("input:submit, input:button, a.button").button();


//Range date

/*
$('form').submit(function(){


	
	//$("#"+form).append('<br /><img src="/loading.gif" alt="" /> Verifica in corso...');
   			
    var variabili=$(this).serialize();
   	$.ajax({
   	   type: this.method,
	   url: this.action+'?notemplate=true',
	   data: variabili,
	   success: function (data) {
	   alert($(data).find("#"+this.id));
	   $('#'+this.id).html(data);
	      //$(this).replaceWith(data);
	      		//startUI();

	   },
	   dataType: 'html'
	});		
   			

	return false;

});
*/







		var dates = $('#datefrom, #dateto').datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 2,
			onSelect: function(selectedDate) {
				var option = this.id == "datefrom" ? "minDate" : "maxDate";
				var instance = $(this).data("datepicker");
				var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
				dates.not(this).datepicker("option", option, date);
			}
		});



}




var lasttag='.pdiv';


$(document).ready(function() {	
		
	
	//Get all the links with rel as panel
	$('a').click(function () {
	
		var tag=$(this).attr('href');
		if((tag.indexOf('http')!=-1) && (tag.indexOf('ocularium')==-1)){
			$(this).attr('target','_blank');
			return true;

		}
		
		
	    tag = tag.replace(/[^a-zA-Z0-9\s]/g,"");
		tag = tag.toLowerCase();
		tag = tag.replace(/\s/g,'-');
        //alert(tag+$('#'+tag).length);
		if(tag=="") tag="httpwwwspazioinprovait";
		loadDiv(tag, $(this).attr('href'));

	
		//Get the height of the sub-panel
		var panelheight = $('#'+tag).height();
		
		
		
		//Set class for the selected item
		$('li').removeClass('current_page_item');
		$(this).parent().addClass('current_page_item');
		
		//Resize the height
		
		//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		lasttag='#'+tag;
		
		//Discard the link default behavior
		return false;
	});

	
});
