jQuery.fn.center = function () {
    this.css("position","absolute");
    //this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("top", "10px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

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



var indexAccordion = false;
var sezioneID =0;
var paginaID =0;



function hideshowtipo(){
	$('#urlItem').hide();
	$('#allegatoItem').hide();
	$('#htmlItem').hide();

    if($('input:radio[name=tipo]:checked').val()==2){
		$('#urlItem').show();
	}else if($('input:radio[name=tipo]:checked').val()==3){
		$('#allegatoItem').show();
	}else if($('input:radio[name=tipo]:checked').val()==4){
		$('#htmlItem').show();
	}else{
		
		
	}
	
}

function hideshowsezione(){
	if($('input:hidden[name=sezione]').val()==1){
		$('input:radio[name=tipo]').val(4);
		$('#grptipo').hide();
		$('#grpsummary').hide();
		$('#subtitleItem').hide();
		$('label[for=html]').hide();
	}else if($('input:hidden[name=sezione]').val()==14){
		/*Links Utili*/
		$('input:radio[name=tipo]').val(2);
		$('#grptipo').hide();
		$('#subtitleItem').hide();
	}
}	

function doFading() {
    jQuery("#mydiv").animate({opacity: 1.0}, {duration: 3000})
        .animate({opacity: 0}, {duration: 3000})
        .animate({opacity: 0}, {duration: 3000})
        .animate({opacity: 1.0}, {duration: 3000, complete: foo})
}
    
var forever = 5 * 24 * 60 * 60 * 1000; // 5 days! (Which is forever in Internet time)
$(document).ready(function(){
	/*
	$('.galleria').galleria({
		show_info: true,
		image_crop: 'height',
		height: 500
	});
	*/	
	if(mediaInpage){
		$('.media').media();
	}
	
	
//	$("a.novita").each(function() {$(this).animate({opacity: 0.2}, 5000,  'linear');alert(forever);});
$("a.novita").each(function() {
 	$(this) .everyTime ( 1000, function (){
	$(this) .animate ({opacity:0.2 }, 1400 ).animate ({opacity:1.0 }, 800 );
	});
}); 
$(".blink").each(function() {
 	$(this) .everyTime ( 1400, function (){
	$(this) .animate ({opacity:0 }, 400 ).animate ({opacity:1.0 }, 800 );
	});
}); 
	/*GESTIONE REGIONALI*/
	$('#soci').hide();
	$('#attivita').hide();
	$('.show').click(function() {
			$('#organigramma').hide();
			$('#soci').hide();
			$('#attivita').hide();
			$('#'+$(this).attr("alt")).show();
			return false;
		});
	
	
	$('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 );
				}
			}

		});


 	
	
	
/* GESTIONE EDIT IN BASE AL TIPO*/	
hideshowsezione();
hideshowtipo();$("input:radio[name=tipo]").change(function(){hideshowtipo();}); 	
	
sezioneID = $('#sezioneID').attr("alt");
$('#sezioneID').hide();



/* GESTIONE LABEL */
//$('div.itemRow label').prepend('» ');

$("#tipoRadio").prepend($('label input:radio'));
$(function() {$("#tipoRadio").buttonset();});


$('#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+"/"+paginaID+"/"+$("#tag").val());
					document.location = "/tagadd/"+sezioneID+"/"+paginaID+"/"+$("#tag").val();
				},
				'Annulla': function() {
					$(this).dialog('close');
					//$("#accordion").accordion( "activate" , indexAccordion );
				}
			}

		});

$('#pagenoaccess').dialog({
			autoOpen: false,
			modal: true,
			resizable: false,
			width: 480,
			height: 200,
			title: "Accesso riservato ai soli soci",

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

		});
		
$('.btnPageNoaccess').click(function() {
			$('#pagenoaccess').dialog('open');
			return false;
		});
$('#pagenoaccess').hide();

$('.btnPagetag').click(function() {
			//$('#pagecancella').html($(this).parent().find("div.txtPagecancella").html());
			
			
			paginaID = $(this).attr("href");
			paginaID =paginaID.substr(1,paginaID.length);
			

			$('#pagetag').dialog('open');
			
			
			//indexAccordion = 1;
			//$("#accordion").accordion( "activate" , false );
			return false;
		});






$('.paginaNew').attr("href","/edit/"+sezioneID+"/");
$('.showAll').attr("href","/show/"+$('#sezioneID').html()+"/"+sezioneID+"/");



//$('.paginaNew').click(function() {});
//alert(document.location.hash.substr(1,document.location.hash.length));
    	
$('#pagecancella').dialog({
			autoOpen: false,
			modal: true,
			resizable: false,
			title:"Operazione",

			buttons: {
				'Si sono sicuro': function() {
					$(this).dialog('close');
					document.location = "/delete/"+sezioneID+"/"+paginaID;
				},
				'No': function() {
					$(this).dialog('close');
					//$("#accordion").accordion( "activate" , indexAccordion );
				}
			}

		});
		
		$('.btnPagecancella').click(function() {
			//$('#pagecancella').html($(this).parent().find("div.txtPagecancella").html());
			
			
			paginaID = $(this).attr("href");
			paginaID =paginaID.substr(1,paginaID.length);
			$('#pagecancella').html(paginaID+$(this).attr("alt"));

			$('#pagecancella').dialog('open');
			
			
			//indexAccordion = 1;
			//$("#accordion").accordion( "activate" , false );
			return false;
		});
	
		$('div.txtPagecancella').hide();
			

	
	$(".accordion").accordion({
			navigation: true,
			autoHeight: false,
			active:false,
			collapsible: true
		});
		
		
		//$("#accordion").accordion( "activate" , false );

	$("button, a.button").button();

	
	$('.datepicker').datepicker({
		showAnim: 		'fadeIn',
		dateFormat: 'dd-mm-yy'
		});
		
		
$("#tag").autocomplete({
			source: availableTags
		});
		
	
$('textarea.tinymce').tinymce({
	script_url : '/js/tiny_mce/tiny_mce.js',
	theme : "advanced",
	theme_advanced_toolbar_location : "top",
	entity_encoding : "numeric",

	content_css : "/css/editor.css",
	relative_urls : false,

	convert_fonts_to_spans : true,

	plugins : "pagebreak,style,advimage,advlink,iespell,inlinepopups,searchreplace,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",
	
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,fontselect,fontsizeselect,|,forecolor,backcolor,|,justifyleft,justifycenter,justifyright,justifyfull,|,sub,sup",
			theme_advanced_buttons2 : "styleselect,|,link,unlink,anchor,image,|,charmap,hr,bullist,numlist,|,search,replace,|,undo,redo,fullscreen,removeformat,cleanup,code",
theme_advanced_buttons3 : ""
		
});

/*


			theme_advanced_buttons3 : "cut,copy,paste,pastetext,pasteword,|,|,outdent,indent,blockquote,|,insertdate,inserttime,preview,|,tablecontrols,visualaid,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|",
			theme_advanced_buttons4 : "formatselect,save,newdocument,|,insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
	

	plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",


tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		editor_selector : "mooFormEditor",
		plugins : "style,liststyle,advlink,advimage",
		entity_encoding : "numeric",
		inline_styles : true,
		trim_span_elements : true,
		cleanup : true,
		content_css : "http://dev.sip.it/css/main.css",
		relative_urls : false,
		convert_fonts_to_spans : true,
		theme_advanced_buttons1 : "undo,redo,separator,styleselect,fontsizeselect,forecolor,backcolor,bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,separator,sub,sup,separator,bullist,liststyle,numlist,separator,image,link,unlink,charmap,cleanup,removeformat,code",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_path_location : "bottom",
		extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
		});






$('textarea.tinymce').tinymce({
			// Location of TinyMCE script
			script_url : '/js/tiny_mce/tiny_mce.js',

			// General options
			theme : "advanced",
			plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

			// Theme options
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Example content CSS (should be your site CSS)
			content_css : "css/content.css",

			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",

			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}
		});



	
	makePage();
	
	$("#tabs").tabs();

	$("#backbutton").css("margin-top",($("#dMain").height()-120)+"px");
	
	
	$("td.toselect").click(function() {
			$(this).toggleClass('selClass');
			$("div[id='tt_"+ $(this).attr("id")+"']").toggle();//Class('selClass');
			return false;
		});
*/

     });

$(window).resize(function(){
  //makePage();
  //showMenu();
});

function makePage(){
	$("div.eventoone").hide();
	/*
	
	$("#menu").hide(); 
	$("#logo").hide(); 
	$("#backspot").hide(); 

	//$("#ppp").center();
	//$("#ppp").corner("26px top");
	
	
	var offset = $("#container").offset();
	$("#testa").html("left: " + offset.left + ", top: " + offset.top);

	
	var goffset = $("#menu").offset();
	$("#coda").html("cleft: " + goffset.left + ", top: " + goffset.top);
		
	var pos = $("#ppp").offset(); 
	$("#menu").show(); 
	$("#menu").corner("20px top");
	$("#menu").css( { "left": (pos.left+8 ) + "px", "top":(pos.top+8 )+ "px" } );
	//$("#menu").offset({ top: 0, left: 0 });
	$("#menu").show(); 
	
	$("#backspot").css( { "left": (pos.left+8 ) + "px", "top":(pos.top+148 )+ "px" } );
	//$("#backspot").offset({ top: 140, left: 0 });
	$("#backspot").corner("10px right");
	$("#backspot").show(); 
	$("#backspot").fadeTo(250, 0.8);
	
	//$("#logo").offset({ top: 5, left: 18 });
	$("#logo").css( { "left": (pos.left+26 ) + "px", "top":(pos.top+13 )+ "px" } );
	$("#logo").show(); 
	*/
	//$("#homeFondo table td").corner("4px");
	$('#date').datepicker();
	
	
		
	$("div.pagecont").accordion({
			autoHeight: false, header: 'div.acTitle',collapsible: true,navigation: true,active:false
		});


}
   /*
   jQuery(document).ready(function(){
	$('.pagecont .acTitle').click(function() {
		$(this).next().toggle('slow');
		return false;
	}).next().hide();
});
*/

var showMenu = function(ev) {
  //get the position of the placeholder element
  var pos = $("#ppp").offset();  
  var width = $("#ppp").width();
  var height = $("#ppp").height();
  //show the menu directly over the placeholder
  //$("#logo").css( { "left": (pos.left ) + "px", "top":(pos.top )+ "px" } );
  $("#logo").offset({ top: 5, left: 5 });  
  $("#logo").show();
}



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;



