
$(document).ready(function () { 
	initializeHome(); 
});

function initializeHome() {

	$(function() {
		$( ".column" ).sortable( {
		  //grid: [50, 20],
		  distance     : 10,
		  opacity      : 0.8,
		  cursor       : "move",
			connectWith  : ".column",
			handle       : ".dd-portlet-header",
			stop         : saveOrder
		});

		$( ".dd-portlet" ).addClass("") // "ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" )
			.find( ".dd-portlet-header" )
				.addClass("pointer") //"ui-widget-header ui-corner-all pointer" )
				.end()
			.find( ".dd-portlet-content" );
    
    $( ".dd-portlet-command" ).prepend( "<a href='javascript:void(0);' class='ui-icon ui-icon-minusthick'></a>" );
    
		$( ".dd-portlet-header .ui-icon" ).click(function() {
		  // Da mettere le icone come nell'esempio di jquery ui-icon minus add
		  //if ( $( this ).text() == '+' ) $( this ).text('-');
		  //else $( this ).text('+');
			$( this ).toggleClass( "ui-icon-minusthick" ).toggleClass( "ui-icon-plusthick" );
			$( this ).parents( ".dd-portlet:first" ).find( ".dd-portlet-content-back" ).slideToggle("fast");
		});

		$( ".column" ).disableSelection();
	});				            
}

function saveOrder (event, ui) {
	var str =""; 
  
	//$(".column").each( function(index){ str+= ( $(this).sortable('serialize').replace(/box/g,'col'+index) + '&'); } );
	$(".column").each( function(index){ str+= ( $(this).sortable('serialize',{key:'col'+index+'[]'}) + '&'); } );
  
	// send ajax call
	$.ajax({
	    url : baseSite + '/index/savehome/',
	    data : str,  
	    dataType : 'html',
	    success: function (data, stato) {
			//alert(data);
	    },
	    error : function (richiesta, stato, errori) {
	        //alert("E' evvenuto un errore. Stato della chiamata: " + errori);	        
	    }
	});
}

$(document).ready(function () { 
		
	$('#slideshow').cycle({ 
	    fx:     'fade', 
	    speed:  2000, 
	    timeout: 8000, 
	    pager:  '#nav', 
	    startingSlide: 0,
	    cleartype: true,
	    cleartypeNoBg: true,
	    fit: 0,
	    pagerAnchorBuilder: function(idx, slide) { 
	        // return selector string for existing anchor 
	        return '#nav li:eq(' + idx + ') a'; 
	    } 
	});
		
	$('#slideshow2').cycle({ 
	    fx:     'fade', 
	    speed:  2000, 
	    timeout: 8000, 
	    pager:  '#nav2', 
	    startingSlide: 0,
	    cleartype: true,
	    cleartypeNoBg: true,
	    fit: 0,
	    pagerAnchorBuilder: function(idx, slide) { 
	        // return selector string for existing anchor 
	        return '#nav2 li:eq(' + idx + ') a'; 
	    } 
	});

	
	$('#undoHome').click( function(){
		
		// send ajax call
		$.ajax({
		    url : baseSite + '/index/deletehome/',		   
		    dataType : 'html',
		    success: function (data, stato) {
				//alert(data);
				location.href = location.href;
		    },
		    error : function (richiesta, stato, errori) {
		        //alert("E' evvenuto un errore. Stato della chiamata: " + errori);	        
		    }
		});
		
	});
	
});
