var indexAccordion = false;

var pageInfo = {
		id: '',
		sezione: '',
		sezionenome: '',
		sezionetitle: ''	
	};

var availableTags = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl"];

jQuery.fn.getInfo = function () {
	if($(this).attr("alt")!= undefined){
  	arrayInfo = $(this).attr("alt").split(':_:');
	pageInfo.id = arrayInfo[0];
	pageInfo.sezione = arrayInfo[1];
	pageInfo.sezionenome = arrayInfo[2];
	pageInfo.sezionetitle = arrayInfo[3];
	$(this).hide();
	}
}

this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = -530;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img width='500' src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

var timerLayout;
function ippMoveNext() {
	var toshow = $(".ippvisible").next('.ipp');
	if(toshow.length<1){
		toshow = $(".ippvisible").parent().children(':first');
	}
				
	$(".ippvisible").hide();
	$(".ippvisible").removeClass('ippvisible');
	toshow.addClass('ippvisible');
	toshow.show();
	timerLayout = setTimeout('ippMoveNext()',3000);
}			



$(document).ready(function(){
	
	//$(document).snowfall({flakeCount : 100, maxSpeed : 5,minSize : 1,maxSize: 5,round:true});


	$('#popupHome').dialog({
		autoOpen: true,
		modal: true,
		resizable: false,
		width: 720,
		height: 620,
		title:"Dedicato alle ONLUS",

		buttons: {}
	});		
	
	
	$('#iscrizionelibro').dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		width: 660,
		height: 400,
		title:"Iscrizione",

		buttons: {
			"Procedere con l'iscrizione": function() {
				//$(this).dialog('close');
				//alert("/tagadd/"+sezioneID+"/"+pageInfo.id+"/"+$("#tag").val());
				document.location = $('#btnIscrizioneLibro').attr("alt")
			},
			'Annulla': function() {
				$(this).dialog('close');
			}
		}
	});		
	$('a.btnIscrizioneLibro').click(function(event) {
  		event.preventDefault();
  		//alert(989);
		$('#iscrizionelibro').dialog('open');
		//alert(99);
		return false;
	});	
		
	$('#requisiti').dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		width: 660,
		height: 400,
		title:"Requisiti Tecnici",

		buttons: {
			'Close': function() {
				$(this).dialog('close');
			}
		}
	});		
	$('a.btnRequisiti').click(function(event) {
  		event.preventDefault();
  		//alert(989);
		$('#requisiti').dialog('open');
		//alert(99);
		return false;
	});	
	
	$('#crediti').dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		width: 680,
		height: 440,
		title:"Crediti ECM",

		buttons: {
			'Close': function() {
				$(this).dialog('close');
			}
		}
	});		
	$('a.btnCrediti').click(function(event) {
  		event.preventDefault();
  		//alert(989);
		$('#crediti').dialog('open');
		//alert(99);
		return false;
	});	
	
	$("button, a.button").button();
	
	imagePreview();
	
	$('#ipphomelink').children(':first').addClass('ippvisible');
	$('#ipphomelink').children(':first').css('display','block');
	
	
	timerLayout = setTimeout('ippMoveNext()',3000);
	$(".ipp").mouseenter(function(event) {
		clearTimeout(timerLayout);
	});
	$(".ipp").mouseleave(function(event) { timerLayout = setTimeout('ippMoveNext()',3000);});
	
	
	$("#ippright").click(function(event){
		event.preventDefault();
		clearTimeout(timerLayout);
		ippMoveNext();
		/*
		var toshow = $(".ippvisible").next('.ipp');
		if(toshow.length<1){
			toshow = $(".ippvisible").parent().children(':first');
		}
				
		$(".ippvisible").hide();
		$(".ippvisible").removeClass('ippvisible');
		toshow.addClass('ippvisible');
		toshow.show();
		*/
//		$(".ippvisible").animate({"left": "+=50px"}, "slow");
	});

	$("#ippleft").click(function(event){
		event.preventDefault();
		clearTimeout(timerLayout);
		var toshow = $(".ippvisible").prev('.ipp');
		//alert(toshow.length);
		if(toshow.length<1){
			toshow = $(".ippvisible").parent().children(':last');
		}
		$(".ippvisible").hide();
		$(".ippvisible").removeClass('ippvisible');
		toshow.addClass('ippvisible');
		toshow.show();
		
		//$(".block").animate({"left": "-=50px"}, "slow");
	});	
	
	


	$("#navcategory").accordion({
		autoHeight: false, header: 'div.acTitle',collapsible: true,navigation: true,active:false
	});	
	
	
	
	
	$('#pageInfo').getInfo();
	



	$("button, a.button").button();
	
	/* GESTIONE ERRORI NEI MODULI*/
	$('div.formErrors').dialog({
		autoOpen: true,
		modal: true,
		resizable: false,
		width: 580,
		height: 300,
		title:"Attenzione Errore!",

		buttons: {
			'Riprova': function() {
				$(this).dialog('close');
				//$("#accordion").accordion( "activate" , indexAccordion );
			}
		}

	});	
	
	
	$('#pagehtml').dialog({autoOpen: false,modal: true,resizable: false,width: 780,height: 400});
		
	$('.htmlPage').hide();
	$('.btnPagehtml').click(function() {
		$('#pagehtml').html($(this).parent().parent().find("div.htmlPage").html());
		$('#pagehtml').dialog('open');
		return false;
	});
		

	$('#pagetag').dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		width: 480,
		height: 200,

		buttons: {
			'Aggiungi': function() {
				$(this).dialog('close');
				//alert("/tagadd/"+sezioneID+"/"+pageInfo.id+"/"+$("#tag").val());
				document.location = '/'+pageInfo.sezionenome+"/tagadd/id/"+pageInfo.id+"/tag/"+$("#tag").val();
			},
			'Annulla': function() {
				$(this).dialog('close');
				//$("#accordion").accordion( "activate" , indexAccordion );
			}
		}
	});
	
	$('.btnPagetag').click(function() {
		pageInfo.id= $(this).attr("href");
		pageInfo.id =pageInfo.id.substr(1,pageInfo.id.length);
		$('#pagetag').dialog('open');
		return false;
	});


	$('#pagecancella').dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		title:"Operazione",

		buttons: {
			'Si sono sicuro': function() {
				$(this).dialog('close');
				document.location = '/'+pageInfo.sezionenome+"/delete/id/"+pageInfo.id;
			},
			'No': function() {
				$(this).dialog('close');
			}
		}

	});
		
	$('.btnPagecancella').click(function() {
		pageInfo.id = $(this).attr("href");
		pageInfo.id = pageInfo.id.substr(1,pageInfo.id.length);
		$('#pagecancella').html(pageInfo.id+$(this).attr("alt"));
		$('#pagecancella').dialog('open');
		return false;
	});
	
	$('div.txtPagecancella').hide();
			
	$(".accordion").accordion({
		navigation: true,
		autoHeight: false,
		active:false,
		collapsible: true
	});
	
	$('.datepicker').datepicker({
		showAnim: 		'fadeIn',
		dateFormat: 'dd-mm-yy'
	});
		
		
	$("#tag").autocomplete({
		source: availableTags
	});		
	


/* GESTIONE PULSANTI MINISITO */

$('#dlgAbstract').dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		width: 660,
		height: 400,
		title:"Gestione Abstract",

		buttons: {
			'Close': function() {
				$(this).dialog('close');
			}
		}
	});		
	$('a.btnAbstract').click(function(event) {
  		event.preventDefault();
		$('#dlgAbstract').dialog('open');
		return false;
	});



	if(jpInpage){
		$('.jpInpage').each(function(){
			var opt = jQuery.parseJSON($(this).find('div.options').html());
			$(this).find('div.options').remove();
			//alert(opt.width);
			
		$("#jquery_jplayer_1").jPlayer({
        	ready: function () {
          		$(this).jPlayer("setMedia", {
            	m4v: opt.m4v,
            	ogv: opt.ogv,
            	poster: "http://www.jplayer.org/video/poster/Big_Buck_Bunny_Trailer_480x270.png"
          		}).jPlayer("play");
			},
			size: {width: opt.width,height: opt.height,cssClass:opt.cssClass},
			loop: opt.loop,
			muted: true,
        	swfPath: "/js",
        	supplied: "ogv,m4v"
      	});
      
		});
	}


	if(mediaInpage){
		//alert(jQuery.parseJSON('{"width":"300","height":"200"}'));
		//var obj = eval(jsonString);
		//$('.media').media(eval($(this).attr("alt")));
		$('.media').each(function(){
			var opt = jQuery.parseJSON($(this).attr("alt").replace(/\'/g, "\""));
			$(this).media(opt);
		});
	}



	

});

$(window).resize(function(){
});



var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

$(document).ready(function()
{  $('#jsddm > li').bind('mouseover', jsddm_open)
   $('#jsddm > li').bind('mouseout',  jsddm_timer)});

document.onclick = jsddm_close;



