var yagsheaderimage = {
  
  init: function() {
    
    // Start slideshow if it has more than 1 picture
    if(jQuery('.slideshow').length > 0) {
      jQuery(function() {
          setInterval("yagsheaderimage.slideSwitch()", 5000 );
      });
    }
    
    if(jQuery('#nav').length > 0) {
      jQuery('#nav ul').each(function() {
        jQuery(this).width(jQuery(this).parent().width());
      });
      jQuery("#nav li.firstlevel a:contains('PROD')").attr('href', '#').live('click', function(){ return false; });
    }
    
    if(jQuery('#searchbox').length > 0) {
      jQuery('#searchbox').clearAndRestore();
    }
    
    if(jQuery('.tx-yagspageteaser-pi1 li').length > 0) {
      jQuery('.tx-yagspageteaser-pi1 li').live('mouseover mouseout',function(e){
        if(e.type == 'mouseover')  jQuery(this).find('.headline').addClass('hover');
        if(e.type == 'mouseout') jQuery(this).find('.headline').removeClass('hover');
      });
    }
    
  },
  
  slideSwitch: function() {
    jQuery('.slideshow').each(function(){
      if(jQuery(this).find('div, li').length > 1) {
        var active = jQuery(this).find('div.active, li.active');
        if ( active.length == 0 ) active = jQuery(this).find('div:last, li:last');
        var next =  active.next().length ? active.next() : jQuery(this).find('div:first, li:first');
        active.addClass('last-active');
        active.animate({opacity: 0.0}, 1000);
        next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
          active.removeClass('active last-active');
        });
      }
    });
  },
  
  fixHeight: function(sel) {
    var height = 0;
    jQuery(sel).each(function(){
      var elem_height = jQuery(this).height();
      if(elem_height > height) {
        height = elem_height;
      }
    });
    
    jQuery(sel).each(function(){
      jQuery(this).height(height);
    });
  }
  
}

jQuery(document).ready(function() {
  
  yagsheaderimage.init();
  
});

// clear and restore inputfields
jQuery.fn.clearAndRestore = function() {
 	var obj = jQuery(this);
 	var currVal = obj.val();

 	obj.bind("focus", function() {
 	  if (obj.val() == currVal) {
 	    obj.css('color','black');
 	    obj.val("");
 	  };
 	});
 	obj.bind("blur", function() {
 	  if (obj.val() == "") {
 	    obj.css('color','#999');
 	    obj.val(currVal);
 	  };
 	});
};

jQuery(function(){
  jQuery("ul.dropdown li").hover(function(){
    jQuery(this).addClass("hover");
    jQuery('ul:first',this).css('visibility', 'visible');
  }, function(){
    jQuery(this).removeClass("hover");
    jQuery('ul:first',this).css('visibility', 'hidden');
  });
  jQuery("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
});
