(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
	var h = $(this).height();
	var oh = $(this).outerHeight();
      
       if(h<$(window).height()){
      	var mt = (h + (oh - h)) / 2;
	$(this).css("margin-top", "-" + mt + "px");
	$(this).css("top", "50%");
	$(this).css("position", "absolute");
        }
    });

};
})(jQuery);


(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);


(function ($) {
$.fn.hAlign = function() {
	return this.each(function(i){
	var w = $(this).width();
	var ow = $(this).outerWidth();
       
	var ml = (w + (ow - w)) / 2;
	$(this).css("margin-left", "-" + ml + "px");
	$(this).css("left", "50%");
	$(this).css("position", "absolute");
	});
};
})(jQuery);


(function ($) {

$.fn.RMover=function(){
        $(this).mouseenter(function(){


            var parwidth= $(this).width();

          
                         if (jQuery.browser.msie) {
                          $(this).find('.overlay').css('filter',"progid:DXImageTransform.Microsoft.Fade(duration=2, Percent=30)");
                        $(this).find('p').fadeTo(0, 0.3);
                         $(this).find('div:not(.overlay)').fadeTo(0, 0.3);
                         }
                         else
                             {
                                 
                                  $(this).find('.overlay').css('width',parwidth).fadeTo(200, 0.7);

                                    $(this).find('p').fadeTo(200, 0.3);
                                    $(this).find('div:not(.overlay)').fadeTo(200, 0.3);


                             }

                    $(this).find('.hr_a').animate({width:'65px'},  {duration: 200,
                                                                     easing: 'easeInOutExpo'});


                 

});


$(this).mouseleave(function(){
                            
                          if (jQuery.browser.msie) {
                          $(this).find('.overlay').css('filter',"progid:DXImageTransform.Microsoft.Fade(duration=2, Percent=100)");
                           $(this).find('p').css('filter',"progid:DXImageTransform.Microsoft.Fade(duration=2, Percent=0)");
                            $(this).find('div:not(.overlay)').css('filter',"progid:DXImageTransform.Microsoft.Fade(duration=2, Percent=0)");
                         }
                         else
                             {

                                  $(this).find('.overlay').fadeTo(1, 0);
                                  $(this).find('p').fadeTo(0, 1);
                                   $(this).find('div:not(.overlay)').fadeTo(0, 1);
                             }
                           $(this).find('.hr_a').stop().css('width','62px');
                        


          });





  


}



})(jQuery);

/* ************************************************************************* */






$(document).ready(function() {

                var alert=$('#alert h2').text();

                if(alert!=""){
                   $('#alert').show();
                   $('#alert_overlay').fadeTo(300,0.7);
                }

                $('#alert').click(function(){$('#alert').hide();})

                $('#invia_form').click(function(){$('form').submit();});



                $('#contatti_container').find('.hr_a').mouseenter(function(){
            $(this).animate({width:'45px'},  {duration: 200,
                                                                     easing: 'easeInOutExpo'});

})
$('#contatti_container').find('.hr_a').mouseleave(function(){
            $(this).animate({width:'34px'},  {duration: 0,
                                                                     easing: 'easeInOutExpo'});

})


                $.address.change(function(event) {
                    if(event.value!="/")
                        {
                             var href= $.address.value().replace(/^\//, '');
                            $('#popup_overlay').height($(document).height());
                            $('#popup').load(href, function(){$('#popup').fadeIn('fast');});

                   
                        }

                });

    
                $('.affitti_col a').click(function(event) {
                    event.preventDefault(); 
                    $.address.value($(this).attr('href'));
                  
                   

                });


                



















	$("#container").vAlign();
	$("#container").hAlign();
        $("#container_annunci").vAlign();
        $("#container_annunci").hAlign();
        $('#container').fadeIn("slow");       
        $('#overlay').fadeTo("slow", 0).css('z-index',-1);

        //console.log($('#container_annunci').height(),$(window).height() );

        $('#search').click(function(){
                
                
                $('#search_hidden').toggle();

        })


$(window).resize(function() {

 
       if($('#container_annunci').height()>$(window).height())
           {$('#container_annunci').css("margin-top","0px");$('#container_annunci').css("top", "0px");$('#container_annunci').css("position", "absolute");}
       else{$("#container_annunci").vAlign();$("#container_annunci").hAlign();}


        if($('#container').height()>$(window).height())
           {$('#container').css("margin-top","0px");$('#container').css("top", "0px");$('#container').css("position", "absolute");}
       else{$("#container").vAlign();$("#container").hAlign();}


  })








   






















       $("#home_footer_nav ul li h2 a").click(
                                                            function(event){
                                                                             event.preventDefault();

                                                                             $("#home_gallery").append('<img class="active" src="'+$(this).attr('href')+'" />');

                                                                              //$('#home_gallery img').attr('src',$(this).attr('href'));
                                                                             $('.active').load(function(){
                                                                             $('.lastactive').fadeOut(1000, function(){$('.lastactive').remove();});
                                                                               
                                                                             $('.active').fadeIn(1000, function(){$('.active').addClass('lastactive').removeClass('active');});
                                                                                 
                                                                                 
                                                                             })



                                                                            }

                                                            );

           $("#single_navigation ul li  a").click(
                                                            function(event){
                                                                             event.preventDefault();

                                                                             $("#home_gallery").append('<img class="active" src="'+$(this).attr('href')+'" />');

                                                                              //$('#home_gallery img').attr('src',$(this).attr('href'));
                                                                             $('.active').load(function(){
                                                                             $('.lastactive').fadeOut(1000, function(){$('.lastactive').remove();});

                                                                             $('.active').fadeIn(1000, function(){$('.active').addClass('lastactive').removeClass('active');});


                                                                             })



                                                                            }

                                                            );



 







        



          $('.home_blocchi_dx').RMover();
          $('#home_foto_caption').RMover();
          $('#home_footer_nav a').RMover();
          $('#home_navigation h2').RMover();
          $('.single_blocchi_dx').RMover();
          $('.single_blocchi_dx').RMover();
         $('.category_item').RMover();
        

         
       $('#richiedi_info').mouseenter(
                                        function()
                                                {$(this).animate({width:'83px'},  {duration: 200,
                                                                                        easing: 'easeInOutExpo'})}
                                    );
        $('#richiedi_info').mouseleave(
                                        function()
                                                {$(this).stop().css('width','80px')}
                                    );

        

//$('#home_gallery img').css('opacity',0).delay(2000).animate({opacity:1},300);

 /*
 $('.affitti_item').mouseenter(function(){

                            $(this).css('border','1px solid #000');

   });

*/

var parwidth=  $('.on').width();

       if (jQuery.browser.msie)
                                             {
                                               $('.on').find('.overlay').css('filter',"progid:DXImageTransform.Microsoft.Fade(duration=2, Percent=30)");


                                            }
                                             else
                                             {
                                              $('.on').find('.overlay').css('width',parwidth).fadeTo(200, 0.7);

                                             }







  var $active_anchor=$('.on').find('a');
                                       $("#home_gallery").append('<img class="active" src="'+$active_anchor.attr('href')+'" />');
                                       $('.active').load(function(){
                                       $('.lastactive').fadeOut(1000, function(){$('.lastactive').remove();});
                                       $('.active').fadeIn(1000, function(){$('.active').addClass('lastactive').removeClass('active');});


                                       });



$('#affitti_nav a').click(function(){
                                        $('#affitti_nav a').css('border-bottom','none')
                                         $(this).css('padding-bottom','3px');
                                       $(this).css('border-bottom','1px solid #999999');

                                       var constant=960;

                                       var link=parseInt($(this).text());

                                      var nextpos=-constant*(link-1);

                                       $('#affitti_container_interno').stop().animate({'left':nextpos+'px'},
                                                                                        {duration: 1000,
                                                                                        easing: 'easeInOutExpo'});


                                    });

$('.affitti_item').mouseenter(function(){


                                    $this=$(this);
                                     var myimg=$this.find('img')
                                     if(myimg.size()>0)
                                         {
                                               myimg.hide();
                                               var imglink=$(this).find('.hiddenimg').attr('href');
                                               myimg.attr('src',imglink).show();
                                                myimg.attr('src',imglink).show();
         
                                         }




                                    $this.css('border','1px solid #ccc');
                                    $this.find('p').hide();
                                    $this.find('.extra_info').show();
                                  
                                    //console.log(imglink);
                                   
                                    //myimg.load(function(){$(this).fadeIn()});

                                    $this.find('.hr_a').animate({width:'65px'},  {duration: 200,
                                                                     easing: 'easeInOutExpo'});
})

$('.affitti_item').mouseleave(function(){
                                    $this=$(this);
                                    $this.css('border','1px solid #fff');
                                    $this.find('p').show();
                                    $this.find('.extra_info').hide();
                                    $this.find('.hr_a').stop().css('width','62px');

})



                           
                          
          });



function RMswitch(){
           

                            var $active_link=$('#home_footer_nav LI.on');


                                     if ( $active_link.length == 0 ) $active_link = $('#home_footer_nav li:last');
                                   
                                     var $next_link =  $active_link.next().length ? $active_link.next()
                                                : $('#home_footer_nav li:first');


                                        var $active_anchor=$next_link.find('a');
                                       $("#home_gallery").append('<img class="active" src="'+$active_anchor.attr('href')+'" />');
                                       $('.active').load(function(){
                                       $('.lastactive').fadeOut(1000, function(){$('.lastactive').remove();});
                                        $('#home_foto_caption').find('p').fadeOut('300', function(){

                                                         $('#home_foto_caption').find('p').html($('.on .hidden_caption').text()).fadeIn('300');
                                                         $('#home_foto_caption h2').text($('.on .hidden_title').text());
                                                         Cufon.replace('h2');
                                        });

                                       


                                       $('.active').fadeIn(1000, function(){$('.active').addClass('lastactive').removeClass('active');});



                                            var parwidth= $next_link.width();


                                             if (jQuery.browser.msie)
                                             {
                                              $next_link.find('.overlay').css('filter',"progid:DXImageTransform.Microsoft.Fade(duration=2, Percent=30)");
                                              $active_link.find('.overlay').css('filter',"progid:DXImageTransform.Microsoft.Fade(duration=2, Percent=100)");

                                            }
                                             else
                                             {
                                              $next_link.find('.overlay').css('width',parwidth).fadeTo(200, 0.7);
                                              $active_link.find('.overlay').fadeTo(1, 0);
                                             }









                                             $next_link.addClass('on');
                                             $active_link.removeClass('on last-on');


                                       });



                            $active_link.addClass('last-on');
                          
                            /*
                            $next_link.css({opacity: 0.0})
                                .addClass('on')
                                .animate({opacity: 1.0}, 1000, function() {
                                    $active_link.removeClass('on last-on');
                                });
*/

                             
}

$(function() {
  setInterval( "RMswitch()",5000 );
});


