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");
	});			
};


$(document).ready(function(){
	
	
	/*Gesione Elenco Soci */
	$('#infoSocio').dialog({autoOpen: false,modal: true,resizable: false,width: 380,height: 190});
		
	$('.infoSocio').hide();
	$('.btnIfosocio').click(function(event) {
		event.preventDefault();
		$('#infoSocio').html($(this).parent().find("div.infoSocio").html());
		$('#infoSocio').dialog('open');
		return false;
	});	
	
	imagePreview();
	
	$('#ipphomelink').children(':first').addClass('ippvisible');
	$('#ipphomelink').children(':first').css('display','block');
	
	
	$("#ippright").click(function(event){
		event.preventDefault();
		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();
		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
	});		
	
	

});

$(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;



