/**
* 	jQuery Crash (http://mktgdept.com/jquery-crash) +
*		
*		then_do (http://mosne.it) 
*		preload all images, delay, then_do!
*
**/

(function($){
$.crash=function(x){for(x in document.open);};
$.fn.then_do = function(options) {

 var defaults = {
  delay : 0,
  before : function(){},
  after : function(){}
 };
 var settings = $.extend({}, defaults, options);
 this.each(function(){
 
  var current = $(this);
  settings.before.apply(current); 
  
  var counter = 0;
  var allimg = $(this).find("img");
  var totant = $(allimg).length;
  
  $(allimg).each(function(){
		var mysrc = $(this).attr("src");
		var img = new Image();					
		$(img).load(function () {
		  counter++;
	    if(counter>=totant){
    	setTimeout(function () {
	   		settings.after.apply(current); 
      }, settings.delay);
	   };
	  }).attr("src",mysrc);	
   });

 return true;
 });


};
})(jQuery);  



//domerady
$(function(){

  $wall = $('section.category');
  var speed = 550; 
  
  $wall.masonry({
    columnWidth: 18,
    itemSelector: ".box",
		animate: true,
    animationOptions: {
  	  duration: speed,
    	easing: 'easeInOutQuad',
    	queue: false
	  }
		
  });
  /*
  
  $wall.infinitescroll({
    debug           : false,
    nextSelector    : "#wp_page_numbers a:last",
    loadingImg      : "http://www.alizarina.net/wp-content/themes/alizarina/images/aj3.gif",
    donetext        : "<em>This is the end!</em>",
    navSelector     : "#wp_page_numbers",
    itemSelector    : ".box"
    },function(newElements){ 
						setTimeout(function(){
						$wall.masonry({ appendedContent: $(newElements) }); 
  					},900);
  	});

*/
  

  $('section.category').css({
    width: "990px"
  });

	  $("section.category").then_do({after:function(){
	  	$wall.masonry();
      $("#cat .current-cat a").trigger("click");
	  
	  }});

  $(".mini").live("mouseenter",function () {

    $(this).find(".anteprima").css({
      visibility: "hidden"
    });
    $(this).find(".texto").css({
      visibility: "visible"
    });

  }).live("mouseleave",function () {

    $(this).find(".anteprima").css({
      visibility: "visible"
    });
    $(this).find(".texto").css({
      visibility: "hidden"
    });

  });

  $(".cat-3 a.go").live("click",function (event) {
    event.preventDefault();
    var who = $(this).parent().parent();
    var link = $(this).attr("href")+"?call=ajax";
    $(who).addClass("loading").find(".news-out").show().load(link, function () {
      $(who).removeClass("loading");    
      
      var n_next = $(who).find("nav a.next");
      var n_prev = $(who).find("nav a.prev");
      
      $(who).find(".pics").cycle({
                    
            fx: 'none', 
    				speed:  speed, 
    				timeout: 0, 
    				next:   this,
    				next:   n_next, 
    				prev:   n_prev
					
					});

      $(who).find(".pics a").click(function(){
        $(who).find("a.next").trigger("click");
      });  
      
     	$wall.masonry();
     
     
      setTimeout(function(){
      $.scrollTo( who, 900, { offset:-21, easing:'easeOutQuint' });
      },speed);
      
    });
    $(who).find(".mini").hide();
    $(who).addClass("col3");
    
  });
    
  
  $(".cat-3 a.close").live("click",function() {
    
    var who = $(this).parent().parent().parent().parent();
    $(who).find(".news-out").hide().empty();
    $(who).removeClass("col3");
    $(who).find(".mini").show();
    /*setTimeout(function(){
      $(window).trigger("smartresize");
    },200);*/
      setTimeout(function(){
     	$wall.masonry();
     },speed);
     
    setTimeout(function(){
      $.scrollTo( who, 900, { offset:-21, easing:'easeOutQuint' });
    },speed);
    
  });

  
  $(".tendina").click(function (event) {
   event.preventDefault();
  });
  
  
   $(".flt_441 .oembed").oembed(null, { 
			embedMethod: "replace", 
			vimeo: {autoplay:false, color: "AA372F", portrait:false, width: 441, title:false, byline:false}});

   $(".flt_639 .oembed").oembed(null, { 
			embedMethod: "replace", 
			vimeo: {autoplay:false, color: "AA372F", portrait:false, width: 639, title:false, byline:false}});
 
      
   $('.news_gallery,.prj_gallery').then_do({after:function(){
   		
   		
   		var n_who  = $(this);
      var nn_next = $(n_who).find("nav a.next");
      var nn_prev = $(n_who).find("nav a.prev");
      
      $(n_who).find(".pics").cycle({
                    
                    fx: 'none', 
    				speed:  900,
    				pause:           1, 
    				timeout: 6000, 
    				next:   this,
    				next:   nn_next, 
    				prev:   nn_prev
					
					});

      $(n_who).find(".pics a").click(function(){
        $(n_who).find("a.next").trigger("click");
      }); 
      
    
    
    }
    
    });
    
    $("a.screen").then_do({delay:300,after:function(){
         var c_a = $(this);
         var a_nh = $(c_a).find("img").height();
         var a_or = $(c_a).height();
         if (a_or>a_nh){ 
            var a_or = a_nh;
            $(c_a).height(a_nh);
         }
         $(c_a).data("sizes", {or:a_or,nh:a_nh});
         
    		
				$(c_a).toggle(function () {
         $(this).animate({height:$(this).data("sizes").nh},speed,"easeInOutQuad");
    		},function () {
         $(c_a).animate({height:$(this).data("sizes").or},speed,"easeInOutQuad");
		    });
    
    }
    });
    
    
  $(".sliders h2 a").toggle(function(){
  	$(this).parent().parent().find(".slidedown").slideDown(speed,"easeInOutQuad");
  },function(){
  	$(this).parent().parent().find(".slidedown").slideUp(speed,"easeInOutQuad");
  });  
    

  
  //switcher     
  if ($('#switcher').length) {

    //init
    var m_delay = 80;
    var m_speed = 550;
    var m_easing = 'easeInOutQuad';

    $("#cat .cat-item-3 a,#cat .cat-item-4 a,#cat .cat-item-20 a").attr({
      "href": "javascript:;"
    });

    //events
    $("section.category").bind("close_news", function () {

      $(".cat-4").find('.tendina').each(function (i) {
        $(this).delay(Math.random() * i * m_delay).hide("blind", {
          direction: "left",
          easing: m_easing
        }, m_speed, function () {
          $(this).remove();
        });
      });
    });

    $("section.category").bind("close_prj", function () {

      $(".cat-3").find('.tendina').each(function (i) {
        $(this).delay(Math.random() * i * m_delay).hide("blind", {
          direction: "left",
          easing: m_easing
        }, m_speed, function () {
          $(this).remove();
        });
      });
    });

    $("section.category").bind("open_news", function () {

      $('<div class="tendina"/>').appendTo(".cat-4").each(function (i) {
        $(this).delay(Math.random() * i * m_delay).show("blind", {
          direction: "left",
          easing: m_easing
        }, m_speed);
      });
    });

    $("section.category").bind("open_prj", function () {

      $('<div class="tendina"/>').appendTo(".cat-3").each(function (i) {
        $(this).delay(Math.random() * i * m_delay).show("blind", {
          direction: "left",
          easing: m_easing
        }, m_speed);
      });
    });

    $("section.category").data("mstatus", {
      op: "none"
    });

    // switcher clicks

    //news
    $("#cat .cat-item-3 a").click(function () {

      $("#cat li").removeClass("current-cat");
      
      if ($("section.category").data("mstatus").op == "none") {
        $("section.category").trigger("open_news");
        $(this).parent().addClass("current-cat");
        $("section.category").data("mstatus", {
          op: "news"
        });
      } else if ($("section.category").data("mstatus").op == "prj") {
				$(this).parent().addClass("current-cat");
        $("section.category").trigger("open_news");
        $("section.category").trigger("close_prj");
        $("section.category").data("mstatus", {
          op: "news"
        });
      } else {
        $("section.category").trigger("close_news");
        $("section.category").data("mstatus", {
          op: "none"
        });

      }
    });

    //prj
    $("#cat .cat-item-4 a").click(function () {

      $("#cat li").removeClass("current-cat");
    
      if ($("section.category").data("mstatus").op == "none") {
        $("section.category").trigger("open_prj");
        $(this).parent().addClass("current-cat");
        $("section.category").data("mstatus", {
          op: "prj"
        });
      } else if ($("section.category").data("mstatus").op == "news") {
			  $(this).parent().addClass("current-cat");
        $("section.category").trigger("open_prj");
        $("section.category").trigger("close_news");
        $("section.category").data("mstatus", {
          op: "prj"
        });
      } else {
        $("section.category").trigger("close_prj");
        $("section.category").data("mstatus", {
          op: "none"
        });

      }

    });

    //gallery (show-all)
    $("#cat .cat-item-20 a").click(function () {


      $("#cat li").removeClass("current-cat");
      $(this).parent().addClass("current-cat");

      $("section.category").trigger("close_news");
      $("section.category").trigger("close_prj");
      $("section.category").data("mstatus", {
        op: "none"
      });

    });


  } //end of switcher



  if ($.browser.msie) {

    if (parseInt($.browser.version) <= 6) {
			
			if(confirm('Do you want update your browser now?')){
      	location = "http://www.browserchoice.eu/";
			}else{
      	$.crash();
      }
      

    } //if ie6
  } //if ie*





}); //scope
