
function loadDiv(id, url){
			_gaq.push(['_trackPageview', url]);
        	$('#panel').append('<div id="'+id+'" class="pdiv"><br /><br /><div id="content"><br />Caricamento in corso...</div></div>');
        	$("#"+id).load(url+"?notemplate=true");
			updateSite();
}

function updateSite(){
	//Get the height of the first item
	//$('#mask').css({'height':$('#home').height()});	
	
	//Calculate the total width - sum of all sub-panels width
	//Width is generated according to the width of #mask * total of sub-panels
	$('#panel').width(parseInt($('#mask').width() * $('#panel div').length));
	
	//Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
	//$('#panel div').width($('#mask').width());
}

var lasttag='.pdiv';

function centraBox(tag){
		
		var that = $("#page");
		var wwidth=$(window).width();
		if(wwidth<that.outerWidth(true)) wwidth=900;
		var oset=(wwidth-that.outerWidth(true))/2;
		//alert(that.outerWidth(true)+" - "+$(window).width()+" - "+wwidth);
		var cwit=((that.outerWidth(true)-899)/2)+50;
		$('#mask').scrollTo(tag, 1000, { axis:'x', offset:-oset-cwit });	
}


$(document).ready(function() {	
	if (navigator.appName!="Microsoft Internet Explorer"){
		var that = $(".pdiv");
		var wwidth=$(window).width();
		if(wwidth<that.outerWidth(true)) wwidth=900;
		var oset=(wwidth-that.outerWidth(true))/2;
		$(".pdiv").css("margin-left", -oset+'px');
		

		updateSite();
		}
		
					

$(window).bind('resize', function() {
	centraBox(lasttag);
});
	
	//Get all the links with rel as panel
	$('a').click(function () {
	
		if (navigator.appName=="Microsoft Internet Explorer") return true;

		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).length==0){
        
			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
		$('#mask').animate({'height':panelheight},{queue:false, duration:1000});			
		
		//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		centraBox('#'+tag);
		lasttag='#'+tag;
		
		//var chisiamo_mp3="/wp-content/themes/ocularium/intervista_fulvio.mp3";
		//if(tag=="httpwwwoculariumtorinoitchisiamo"){
		//	$("#jplayer").jPlayer("setFile", chisiamo_mp3).jPlayer("play");
		//}else{
		//	if($("#jplayer").jPlayer("getData","diag.src")==chisiamo_mp3){
		//		$("#jplayer").jPlayer("setFile", default_mp3).jPlayer("play");
		//	}
			
		//}
		
		//Discard the link default behavior
		return false;
	});
	
	var default_mp3="/wp-content/themes/ocularium/chopin_9_1.mp3";
	 $("#jplayer").jPlayer( {
    ready: function () {
      this.element.jPlayer("setFile", default_mp3).jPlayer("play"); // Defines the counterpart mp3 and ogg files
    },
    swfPath: "/wp-content/themes/ocularium/js"
      });

	
});



function popup(){

			var url = this.href;
			alert(url);
			var dialog = $('<div style="display:hidden"></div>').appendTo('body');
			// load remote content
			dialog.load(
				url, 
				{},
				function (responseText, textStatus, XMLHttpRequest) {
					dialog.dialog();
				}
			);
			//prevent the browser to follow the link
			return false;
}
