// Tabs code
var MGFX = MGFX || {};

MGFX.Tabs = new Class({
	
	Extends: MGFX.Rotater,
	
	options: {
		autoplay: false,
		onShowSlide: function(slideIndex) {
			this.tabs.removeClass('active');
			this.tabs[slideIndex].addClass('active');
		}
	},
	
	initialize: function(tabs, slides, options){
		this.setOptions(options);
		this.tabs = $$(tabs);
		this.createTabs();
		if(this.options.hash && window.location.hash) {
			var hash = window.location.hash.substring(1);
			this.tabs.each(function(el, index) {
				if(el.get('id') == hash) {
					options.startIndex = index;
				}
			});
		}
		return this.parent(slides,options);
	},
	
	createTabs: function () {
		this.tabs.each(function(tab,index){
			tab.addEvent('click', function(event){ 
				event.preventDefault();
				this.showSlide(index);
				this.stop(true);
			}.bind(this));
		}.bind(this));
	}.protect()
	
});

window.addEvent('domready',function(){

  if ( document.id('slideHome') ) {
    this.tabs4 = new MGFX.Tabs('#slideHome ul .btn','.img',{
      autoplay: true,
      transitionDuration:500,
      slideInterval:4000,
      hover:false
    });
  }


  /* Image slider */
  var showDuration = 3500;
  var container = $('partnersLogo');
  var images = container.getElements('li');
  var currentIndex = 0;
  var interval;
  /* opacity and fade */
  images.each(function(img,i){ 
    if(i > 0) {
      img.set('opacity',0);
    }
  });
  /* worker */
  var show = function() {
    images[currentIndex].fade('out');
    images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].fade('in');
  };
  /* start once the page is finished loading */
  window.addEvent('load',function(){
    interval = show.periodical(showDuration);
  });

  var menushow;
  var menuhide;
  var timermenushow;
  var timermenuhide;

  menushow = function(list, elem) {
    list.setStyle('visibility', 'visible');
    elem.addClass('hover');
  }

  menuhide = function(list, elem) {
    list.setStyle('visibility', 'hidden');
    elem.removeClass('hover');
  }

  $('drop_down_menu').getElements('li.menu').each( function( elem ){
    var list = elem.getElement('ul.links');
    if(!list || list == null) return;
    list.setStyle('visibility', 'hidden');
    elem.addEvents({
      'mouseenter' : function(){
        timermenushow = menushow.delay(200, this, [list, elem] );
      },
      'mouseleave' : function(){
        $clear(timermenushow);
        timermenuhide = menuhide.delay(200, this, [list, elem] );
      }
    });
  })

});
