// font replace
Cufon.replace('.replace', {
	hover: true,
	hoverables: { a: true, button: true }
});


function rollIE(lingua){
 $('button.btn_rosa').live('mouseover', function(){
     $(this).addClass('btnHover').css({'background-image': 'url("img/'+lingua+'/sprite_btn_rosa.png")'
  			});        
     });
     $('button.btn_rosa').live('mouseout', function(){
    $(this).removeClass('btnHover').css({'background-image':'url("img/'+lingua+'/sprite_btn_rosa.png")'       
 			});
     });  
   $('button.btn_stretto').hover(function(){
     $(this).addClass('btnHover').css(
     	{	'background-image': 'url("img/comuni/btn_largo_stretto.png")',
     		'background-position':'-200px -64px'
     		});
     }, function(){
     $(this).removeClass('btnHover').css(
     	{	'background-image': 'url("img/comuni/btn_largo_stretto.png")',
     		'background-position':'0 -64px'
     		});
     });
    $('button.btn_largo').hover(function(){
     $(this).addClass('btnHover').css(
     	{	'background-image': 'url("img/comuni/btn_largo_stretto.png")',
     		'background-position':'-200px 0'
     		});
     }, function(){
     $(this).removeClass('btnHover').css(
     	{	'background-image': 'url("img/comuni/btn_largo_stretto.png")',
     		'background-position':'0 0'
     		});
     });     
};

$(function(){
jQuery.fn.outerHTML = function(s) {
	return (s) ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html();
};
		
	$('#finestra_modale').ajaxSend(function(evt, request, settings){			
			$('#finestra_modale #messaggio').html('<img src="/img/comuni/spinner.gif" />');
		});
		
		$('a.apriModale').live(
			"click", 
			function(el){
				if($(this).attr('href')){
					if($(this).attr('href').length>1 && $(this).attr('href').substr(0, 1) == '#' && !$(this).attr('title')){
						$($(this).attr('href')).show();
						apriModale({message: $($(this).attr('href')).outerHTML(), target_el:this});
						$($(this).attr('href')).hide();
					}else{
						apriModale({url: $(this).attr('href'), target_el:this});
					}
				}else if(this.title){
					apriModale({message: $(this).attr('title'), target_el:this});
				}		
				return false;
			}
		);
	
	// HOME PAGE ////////////////////////////////////////////
		// login hp
		$('#login').ajaxForm({
	    	dataType: 'json',
	    	success: function(data){
	    		if(data.success){
	    			location.href = 'private_page_home.php';
//	    			$('#username_utente_loggato').html(data.response.nickname);
//	    			$('#login').fadeOut();
//	    			$('#utente_loggato').fadeIn();
	    		}else{
	    			apriModale({message:data.response});
	    		}
	    	}
		}); 
		// switch lovatar
		$('#labelz a').click(function(){
			$('#labelz a').removeClass('active');
			$(this).addClass('active');
			var target= $(this).attr('href');
			$('.lovatar').hide();
			$(target).fadeIn();
			return false;
		});		
		// GADGETS vignette /////////////////////////////////////
		function mostraVignetta(data){
			$('#modalBoxOverlay').show();
			var offset =$('#vignettaModale').outerHeight()/2;
			if(typeof document.body.style.maxHeight == "undefined") {
				// IE6
				var viewportheight = document.documentElement.clientHeight //altezza area visibile
				var scrOfY = document.documentElement.scrollTop; //scroll verticale
				var topIe6 =scrOfY+(viewportheight/2)-offset;	//margin-top	
				$('#vignettaModale').css({'top':topIe6+'px'}).fadeIn();
			}else{
			// tutti gli altri
			$('#vignettaModale').css({'top':'50%', 'margin-top':'-'+offset+'px'}).fadeIn();
			};		
		};		
		$('a').live('click', function(event){ //intercetto il click
		if($(this).attr('rel')=='vignetta'){
			var target = $(this).attr('href');			
			$('div#vignettaModale .messaggio').load(target, null, mostraVignetta);	
			event.preventDefault();		
		};
		if($(this).attr('rel')=='chiudiVignetta'){
			$('div#vignettaModale').hide();	
			$('#modalBoxOverlay').hide();
			event.preventDefault();		
		}
	});
	
	// APRI SONDAGGI /////////////////////////////////////
	$('a.apriSondaggio').live('click', function(){
		$.target_url =$(this).attr('href');
		$.get(
				'ajax/login_modale.php',
				null,
				function(data){
					if(data['success']){
						var target=$.target_url;
						var windowLeft = (screen.width - 475) / 2;
				  		var windowTop = (screen.height - 396) / 2;
				      window.open(target, 'popup', 'height=396, width=475, top=' + windowTop + ',left=' + windowLeft + ',scrollbars=no, resizable=0, menubar=0, toolbar=0, location=0, statusbar=0, fullscreen=0');
					}else{
						bloccaInterfaccia({});
						$('#messaggio').fadeIn();
						$('#finestra_modale #messaggio').html(data['response']);
						centraMessaggio();
					}
				},
				'json'
			);				
      return false;
	});
	// APRI TEST ////////////////////////////////////////
		$('a.apriTest').live('click', function(){
		$.target_url =$(this).attr('href');
		$.get(
				'ajax/login_modale.php?chk_login_required='+$.target_url,
				null,
				function(data){
					if(data['success']){
						var target=$.target_url;
						var windowLeft = (screen.width - 580) / 2;
  						var windowTop = (screen.height - 380) / 2;
     					window.open(target, 'test', 'height=380, width=580, top=' + windowTop + ',left=' + windowLeft + ',scrollbars=no, resizable=0, menubar=0, toolbar=0, location=0, statusbar=0, fullscreen=0');
					}else{
						bloccaInterfaccia({});
						$('#messaggio').fadeIn();
						$('#finestra_modale #messaggio').html(data['response']);
						centraMessaggio();
					}
				},
				'json'
			);		
      return false;
	});
	// APRI TOOLS ///////////////////////////////////////
		$('a.apriTool').live('click', function(){
			$.target_url = $(this).attr('href');
			$.get(
				'ajax/login_modale.php?chk_login_required='+$.target_url,
				null,
				function(data){
					if(data['success']){
						var target=$.target_url;
						var windowLeft = (screen.width - 580) / 2;
				  		var windowTop = (screen.height - 380) / 2;
				  		window.open(target, 'tools', 'height=380, width=580, top=' + windowTop + ',left=' + windowLeft + ',scrollbars=no, resizable=0, menubar=0, toolbar=0, location=0, statusbar=0, fullscreen=0');
					}else{
						bloccaInterfaccia({});
						$('#messaggio').fadeIn();
						$('#finestra_modale #messaggio').html(data['response']);
						centraMessaggio();
					}
				},
				'json'
			);
			return false;
		});
	
	// MUSICA IN HOME //////////////////////////////////	
	$('a#toggleMusic').live('click', function(){
		if($(this).hasClass('on')){
			suono.location.href="music_off.php";	
			$(this).removeClass('on').addClass('off');				
			$.get(
				'services/chkSession.php?audio_ctrl=off',
				null,
				function(){}
			);
		}else{
			$.get(
				'services/chkSession.php?audio_ctrl=on',
				null,
				function(){}
			);
			suono.location.href="music_on.php";
			$(this).removeClass('off').addClass('on');	
		}
		
		return false;
	});	
	// TOOLTIPS ////////////////////////////////////////	
	$('.tTips').tooltip({
		track: true, 
    delay: 0, 
    showURL: false, 
    showBody: " - ", 
    fade: 250 
	});
});	

	
	

	