/*
 * Cufon
 */

Cufon.replace('#splash h2');
Cufon.replace('.font-apex');

/*
 * Animated splash script for Urban Capital
 *
 * Copyright (c) 2009 Mr. Henry
 * Author: brecht@mrhenry.be
 *
 */

$(document).ready(function() {
   $("#splash").splash();
   
   /* Language
   -----------------------------------------------------*/
   $('#language ul').css({ 'display' : 'none' });
   $('#language').mouseenter(function(){
       var list    = $(this).find('ul');
       if (!(list.is(':animated'))) {
           $(this).css({ 'background-image' : 'url(/img/icons/ico-arrow-down.png)' });
           $(this).find('ul').slideDown('fast'); 
           console.log("alo");
       }
   });
   $('#language').mouseleave(function(){
       $(this).css({ 'background-image' : 'url(/img/icons/ico-arrow-right.png)' });
       $(this).find('ul').slideUp('fast');
   });
});


/*
 * extending jQuery
 */
 
// Splash shizzle 
var hovered = false;
var hovered_item = "";

jQuery.fn.splash = function() {
  var grid = $(this);

  grid.find(".thumb").hover(
    function(){
      if(!hovered){
        var item = $(this);
        hovered_item = item.parent().attr("id");
        var f = function() {animate(item,grid); };
        setTimeout(f,250);
      }      
    },
    function(){
      hovered_item = "";
      $(this).parent().children("h2").css("margin-top","0");
    }
  );
};

function animate(item,grid){
  if(hovered_item==item.parent().attr("id")){
    // Hovered state
    hovered=true;
    
    // Variables
    var current_item = item.parent().attr("class").replace("item ","");
    var img = item.css("background-image");
    var description = item.parent().children(".description").html();
    var o_x = item.parent().position().left;
    var o_y = item.parent().position().top+40;
    var o_height = item.height();
    var h_height = item.parent().children("h2").innerHeight();
    var h_diff = 95-o_y;

    // Calculate height difference if item has header on top
    if(item.parent().children("h2").hasClass("top")){
      o_y = o_y + h_height;
      h_diff = h_diff - h_height;
    }
    h_diff = h_diff+'px';

    // Generate overlay
    grid.append("<div id='overlay' class='clearfix'></div>");
    var expanded_html = "<div class='thumb' style='background-image:"+img+"'>";
    expanded_html+="<div class='description'>"+description+"</div></div>";

    // Set properties on overlay 
    // Inline css properties (Safari bugs when using css class)
    $("#overlay").html(expanded_html);
    $("#overlay").addClass("overlay");
    $("#overlay").css("top",o_y+"px");
    $("#overlay").css("left",o_x+"px");
    $("#overlay").css("width","200px");
    $("#overlay").css("height",o_height+"px");
    $("#overlay").css("position","absolute");
    $("#overlay").css("padding","0");
    $("#overlay").css("margin","0");
    $("#overlay").css("overflow","hidden");

    // Animate overlay
    grid.find(".item").each(function(){
      if(!$(this).hasClass(current_item)){
        $(this).fadeTo('fast',0.1, function(){});
      }else{
        $(this).children(".thumb").css("visibility","hidden");
      }
    });

    $("#overlay").animate({top:'95px',left:'0px',width:'865px',height:'405px'},1500);

    // Animate headers
    var h_diff_s = '-'+h_diff.replace("-","");
    item.parent().children("h2").css("margin-top","0");
    item.parent().children("h2").animate({marginTop:h_diff_s},1500,function(){
      if(!hovered){
        $(this).animate({marginTop:0},200);
      }
    });

    // Cufon-ize the descriptions
    Cufon.replace('#overlay .description p');

    // Add hover event to overlay for catching the roll-out state
    grid.find("#overlay").hover(
      function(){},
      function(){
        $("#overlay").remove();

        $("."+current_item).children(".thumb").css("visibility","visible");
        $("."+current_item).children(".thumb").css('opacity',0.1);
        $("."+current_item).children(".thumb").fadeTo('fast',1);

        grid.find(".item").fadeTo('fast',1,function(){
          if(jQuery.browser.msie)
             $(this).get(0).style.removeAttribute('filter');
          hovered=false;
        });

        $("."+current_item).children("h2").animate({marginTop:0},200);
      }
    );
  }
}

/*(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);*/
