var urlAjaxHandler='functions/ma_common_helper.php';; // percorso  del contenuto del  dialog
var urlAjaxHandlerFeed=serverPath+'functions/feed_storage.inc.php';
var fill="";



$(document).ready(function(){
	
	$.backstretch("images/"+imgBck, {speed: 150});
	var msgOpts = {
			modal: true,
			autoOpen: false,
			width: 300,
			height:150,
			closeOnEscape:true,
		    draggable: true,
			resizable: false,
			
			overlay: {	
				backgroundColor: '#000',
				opacity: 0.7
			}		
		};
	    $(window).resize(function(){
  			//setDescrizione();
		})
		
		$('a.chiudi').click(function(){
  			setDescrizione();
		})
		$("#contentDescrizione").click(function(){
  			setDescrizioneOn();
		})
		$("#msgBox").dialog(msgOpts);	//end dialog
		//setDescrizione();
		$("#contentDescrizioneBox.dragga").draggable({handle: '#contentDescrizioneBoxHeader' , containment: 'parent' });	
		
		showSocial();
		$("#contentMenuFooter ul li:last").each(function(){
		      $(this).addClass("lastitem");
		});
	//  swap    
		   swapValues = [];
	       $(".swap_value").each(function(i){
		        swapValues[i] = $(this).val();
		        $(this).focus(function(){
		            if ($(this).val() == swapValues[i]) {
		                $(this).val("");
		            }
		        }).blur(function(){
		            if ($.trim($(this).val()) == "") {
		                $(this).val(swapValues[i]);
		            }
		        });
	        });
	
});





/*********************** ajax  and    modal  window  handler *******************/


function  iscivitiNewsletter(){
	    
		msg='';
	  //Email
	     
	     if(msg=='') {
	     showWait();
	     $.post( urlAjaxHandler, 
	  		 {email: $('#email').val(),Firstname:$('#nome').val(),actionType:'newsletter'}
			 
			 ,function(xml){
			 	
	                //location.href='profilo.php'
	            
			   erroreMessage(xml);
		     }
			 
	     );
	  }
	  else erroreMessage(msg);
	}  

function  logout(){
    $.post( urlAjaxHandler, 
  		 {actionType:'logout'}
		 
		 ,function(xml){
		 	if (xml.match('OK')) {
                location.href='http://www.designdirectory.it/'
            }
            else erroreMessage('Si � verificato un Errore');
		   //alert(xml);
	     }
		 
     );
 
}
/******************* start  ajax helper ***************************/
// funzione generica 
function  ma_ajax_helper(nome_form,actionTypeName){
  
  if(nome_form!='')para=$('#'+nome_form).serialize();
  else para="logout:1";
  $.post( urlAjaxHandler, 
  		 $('#'+nome_form).serialize()
		 
		 ,function(xml){
		 	if (xml.match('OK')) {
               erroreMessage(xml); 
            }
            else erroreMessage(xml);
		   //alert(xml);
	     }
		 
     );
  
} 



function  ma_ajax_updater(nome_form,actionTypeName,updateItem){
  showWait()
  if(nome_form!='')para=$('#'+nome_form).serialize();
  $.ajax({
  		 url: urlAjaxHandler+'?actionType='+actionTypeName, 
  		 data:$('#'+nome_form).serialize(),
		 cache: false,
  			success: function(code){
			   	$("#"+updateItem).html(code);
				closeDialog("#msgBox");
  			}

		 }
     );
  
} 
function setDescrizione(){
  var p = $("#contentDescrizione");
  var t = $("#contentDescrizioneBox");
  var h=$(t).height();
  if(isFinito==false)$(t).slideToggle('slow', function(){isFinito=false} );
  isFinito=true
}
  
  /*
  $(t).animate({
       height:bh}, 
	   1000, "linear", function(){alert("all done");} );

}
*/

function showSocial(){

	if($('#social').length>0){
		objSocial=$('#social');
		objSocial.show()
		var t = $(".contenutoSxLong");
		var h=$(t).height();
		var position = $(t).position()
		var hs=$('#social').height();
		posSocial=position.top+(h-hs)+8;
		objSocial.css({ position: "absolute",
              marginLeft: 0, marginTop: 0,
              top: posSocial });
	}
}
function setDescrizioneOn(){
  var p = $("#contentDescrizione");
  var t = $("#contentDescrizioneBox");
  var h=$(t).height();
  if(isFinito==false) $(t).slideDown('slow', function(){isFinito=false} );
}


/***********************************  gestione  messaggi ***********************************/
function erroreMessage(msgContent){
   //$("#loading").remove();

   $('#msgBox').dialog('option', 'title','Wip Metr&ograve;');
   $("#msgBox").html(msgContent);
   $('#msgBox').dialog('open');
}

function showWait(){
   $("#msgBox").html('<div id="loading"></div>');
   $('#msgBox').dialog('option', 'title','....Wait loading.....');
   $('#msgBox').dialog('open');
}

function closeDialog(box){
   $(box).dialog('close');
  
}

/*********************************funzioni ricerca negozi ********************************/
var curNazione
function ricercaNegozi(Obj,Campo,Valore) {
	//scoloro
	$('#'+Obj).parent().parent().children().each(function(index) {
	   $(this).children().css("color","#fff");
	});
	//coloro
	$('#'+Obj).css("color","#FFFF04");
    if(Campo=='IdCountry'){
    	$("#boxListaNegozi").html('');
    	curNazione=Valore;
    	ma_ajax_updater_ricerca(Campo,Valore,'select_citta','boxListaCitta');
 	};
    if(Campo=='City')ma_ajax_updater_ricerca(Campo,Valore,'select_negozi','boxListaNegozi');
    if(Campo=='Id')ma_ajax_updater_ricerca(Campo,Valore,'select_negozio','boxNegozio');
    else $("#boxNegozio").html('');
}

function ma_ajax_updater_ricerca(Campo, Valore, actionTypeName, updateItem) {
	    $("#" + updateItem).html('...loading...');
	    $.ajax( {
		url : urlAjaxHandler + '?actionType=' + actionTypeName+"&Campo="+Campo+"&Valore="+Valore+"&IdCountry="+curNazione,
		cache : false,
		success : function(code) {
			$("#" + updateItem).html(code);
			//closeDialog("#msgBox");
		}

	});

} 


