var urlAjaxHandler=serverPath+'functions/ma_common_helper.php';; // percorso  del contenuto del  dialog
var  curLetter=''; //usato per  tenere in memoria le letter
$(document).ready(function(){
  	$.ui.dialog.defaults.bgiframe = true;

	 var dialogOpts = {
			modal: true,
			autoOpen: false,
			width: 600,
			position:[10,10],
			closeOnEscape:true,
		    draggable: true,
			bgiframe: true,
			resizable: false,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			open: function() {
			    //display correct dialog content
				//$("#dialogBox").load(url);
			},
			close: function() {
				$("#dialogBox").html('');
			}
			
		};
	
	   $("#dialogBox").dialog(dialogOpts);
		
	   var dialogLongOpts = {
			modal: true,
			autoOpen: false,
			width: 650,
			position:[10,10],
			closeOnEscape:true,
		    draggable: true,
			resizable: false,
			height:500,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.9
			},
      
			open: function() {
			    //display correct dialog content
				//$("#dialogLongBox").load(url);
			},
			close: function() {
			
			$("#dialogLongBox").html('');
			//display correct dialog content
				//$("#dialogBox").dialog( 'destroy' );
			}
			
		};
		$("#dialogLongBox").dialog(dialogLongOpts);	//end dialog
		
		
		var msgOpts = {
			modal: true,
			autoOpen: false,
			width: 300,
			height:150,
			closeOnEscape:true,
		    draggable: true,
			resizable: false,
			
			overlay: {
				backgroundColor: '#000',
				opacity: 0.7
			}		
		};
	
		$("#msgBox").dialog(msgOpts);	//end dialog
		
		// Dialog Link metodo
		$('#dialog_link').click(function(){
		    var dialogTitle=this.title
		    url=this.href;
			$("#dialogBox").dialog(dialogOpts);	//end dialog
			
			$("#dialogBox").load(url);
			$("#dialogBox").dialog('open');
			return false;
		});
		
		
		// Dialog Link footer
		$('a.pop_link').click(function(){
		    var dialogTitle=this.title
		    url=this.href;
			$("#dialogBox").dialog(dialogOpts);	//end dialog
			$("#dialogBox").load(url);
			$("#dialogBox").dialog('open');
			return false;
		});
		// Dialog Link footer lungo
		$('a.pop_long').click(function(){
		    url=this.href;
			var dialogTitle=this.title
			$("#dialogLongBox").load(url);
			$("#dialogLongBox").dialog('option', 'height', 600);
			//$('#dialogLongBox').dialog('option', 'title', dialogTitle);
			$('#dialogLongBox').dialog('open');
			return false;
		});
		
		
		$("#loginDimenticato a").click(function(){
	  			$("#loginDimenticatoForm").slideToggle("slow");
	  			$(this).toggleClass("active");
		});

		
		//rollover
		$(".rolla").rollover(true);// call the function

				
});


// non in jquwry  style  but :(((
function getHTML(obj,pars){
 	$('.radioFirst a').css("color","black");
	$('.radio a').css("color","black")
    if(curLetter!=pars){
		$(obj).css("color","white");
		$('input#tipolettera').val(pars)
		curLetter=pars
	}
	else {
	    curLetter='';
	    $('input#tipolettera').val('lettere')
	}
	;
}


/***********************************  gestione   login  , pwd  etc ***********************************/
function  sendPwd(){
  
  if($('#email').val()!='') {
  	
		 curEmail=$('#email').val();
		 if(checkEmail(curEmail)==true){
			var url = serverPath+'functions/ma_common_helper.php';
			$.post( urlAjaxHandler, 
			 		 {email: curEmail,actionType:'sendPwd'}
				 ,function(xml){
				 	erroreMessage(xml);
				 	
				  }
				 
			  );
		  }
		  else erroreMessage('Email non valido')
  }
  else   erroreMessage('Inserire email')
} 


function  auth(){
  if($('#username').val()!='' && $('#password').val()!='') {
     erroreMessage('Attendere prego');
     $.post( urlAjaxHandler, 
  		 {username: $('#username').val(),password:$('#password').val() ,actionType:'auth'}
		 
		 ,function(xml){
		 	  
			  if(xml.length>25)erroreMessage(xml)
			  else  location.href='profilo.php'
            
		      
	     }
		 
     );
  }
  else erroreMessage('Prego Inserire username e password');
} 

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

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

		 }
     );
  
} 

// aggiorna immagine
function updateImg(obj,curImg){
   
   
   $(obj).attr('src', curImg); 
}

//aggiorna  sia  provincie  che citta
function  aggiornaLista() {
	ma_ajax_updater('adv_search','select_provincie','provincia')
	ma_ajax_updater('adv_search','select_citta','citta');
}
/***********************************  gestione  messaggi ***********************************/
function erroreMessage(msgContent){
   closeDialog("#msgBox");
   //$("#loading").remove();
   $('#msgBox').dialog('option', 'title','Attenzione !!!!');
   $("#msgBox").html(msgContent);
   $('#msgBox').dialog('open');
}

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

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