var DropDown = function(li, dropdown, hover) {
  var ul = li.parent();
  var link = li.find('a');
  
  var stayOpen = false;
  var opening = false;
  
  function fadeIn(e) {
    var offset = link.offset();
    
    hover.left(offset.left - 8);
    hover.top(offset.top - 8);
    hover.width(link.width() + 13);
    
    e.left(offset.left - 8);
    e.top(offset.top + hover.height() - 8);
    
    e.queue(function() {
      hover.fadeIn(200);
      $(this).fadeIn(200, function() { dropdown.find('p.gallery').css('zoom', 1); });
      $(this).dequeue();
    });
  }
  
  function fadeOut(e) {
    e.queue(function() {
      hover.fadeOut(200);
      $(this).fadeOut(200);
      $(this).dequeue();
    });
  }
  
  link.hover(function() {
    opening = true;
    setTimeout(function() {
      if (opening) { fadeIn(dropdown); }
    }, 100);
  }, function() {
    opening = false;
    setTimeout(function() {
      if (!stayOpen) { fadeOut(dropdown); }
    }, 300);
  });
  
  dropdown.add(hover).hover(function() {
    stayOpen = true;
  }, function() {
    stayOpen = false;
    setTimeout(function() {
      if (!stayOpen) { fadeOut(dropdown); }
    }, 300);
  });
};

jQuery.fn.extend({
  dropdown: function(dropdown, hover) {
    new DropDown($(this), $(dropdown), $(hover));
  },
  
  left: function(offset) {
    $(this).css('left', offset + 'px');
  },
  
  top: function(offset) {
    $(this).css('top', offset + 'px');
  }
});

$(function() {
  $('li#products-nav').dropdown('#products-dropdown', '#products-hover');
  $('li#project-types-nav').dropdown('#project-types-dropdown', '#project-types-hover');

  $('ul#video-navigation').each(function() {
    $(this).find('li').each(function() {
      var li  = $(this);
      var swf = li.find('img')[0].src.replace('png', 'swf')

      li
        .mouseover(function() { $(this).addClass('hover') })
        .mouseout(function() { $(this).removeClass('hover') })
        .click(function() { swfobject.embedSWF(swf, 'video-player', '400', '300', '9.0.0'); })
    });
  });
});


var WhatsNew = {
  show: function (number) {
   // $('.whats-new-popup').hide('normal', function() {
      $('#whats-new-popup-' + number).show('normal');
   // });
  },
  
  close: function() {
    $('.whats-new-popup').hide('normal');
  }
}




 $.fn.verticalAlignMiddle = function() {  
 
   
    return this.each(function() {
        var $this = $(this);    
        var imgH = $this.find('img').height();    
        var thisH = $this.height();    
        var pad = Math.ceil((thisH - imgH) / 2);   
        
        var imgW = $this.find('img').width();
        var thisW = $this.width();
        var padW = Math.ceil( (thisW - imgW)  /2 );
        
         
        $this.css({     
         'padding-top': pad,      
         'height': thisH - pad,      
         'min-height': thisH - pad ,
   
         'padding-left': padW,
         'width': thisW - padW,
         'min-height': thisW - padW
           
         });
    });
    
 }

 function centerImages(){  
    try{  
        $(function() { $(".uniformProductPreviewImage").verticalAlignMiddle() }); 
    }
    catch(ee){
    
    }
 }
 
