/**
 * @author Jan Cinert <jan.cinert@gmail.com>
 * @require prototype.js, scriptaculous.js, livepipe.js, tabs.js
 */

initTabs = function() {
  document.observe('dom:loaded',function()
    {
      var options = { duration: 0.5, scope: 'banner', queueLimit: null };
      var tabsHandler = new TabsAnimated( options );
      
      options = {};
      options.showFunction = tabsHandler.showFunction.bind( tabsHandler );
      options.hideFunction = tabsHandler.hideFunction.bind( tabsHandler );
      options.beforeChange = tabsHandler.beforeChange.bind( tabsHandler );
      options.setClassOnContainer = true;
      
     

      var tabs = new Control.Tabs( 'tabs', options );
      tabsHandler.tabs = tabs;
      

      tabs.next = function() {
      	try {
		  	  this.links.each(function(link,i){
		  			if(this.activeLink == link && this.links[i + 1]){
		  				this.setActiveTab(this.links[i + 1]);
		  				throw new Exception;
		  			}
		  		}.bind(this));
		  		this.first();
			  }
		    catch(e) {}
      }
      
      tabs.previous = function() {
      	try {
		  	  this.links.each(function(link,i){
		  			if(this.activeLink == link && this.links[i - 1]){
		  				this.setActiveTab(this.links[i - 1]);
		  				throw new Exception;
		  			}
		  		}.bind(this));
		  		this.last();
			  }
		    catch(e) {}
      }
      
      
      
      $('hp_banner_prev_button').observe( 'click', tabs.previous.bind( tabs ) );
      $('hp_banner_next_button').observe( 'click', tabs.next.bind( tabs ) );
      
      var tabsTimer = new TabsTimer( tabs, tabsHandler, {} );
    }
  );
}


TabsBase = Class.create({
	initialize: function(options){
	  this.options = {
		};
		Object.extend(this.options,options || {});
	},
	showFunction: function( item ) {
		try {
			var movie = this.getMovie( item );
			movie.StopPlay();
			movie.Rewind();
			movie.Play();
		}
		catch(e) {}
  },
  hideFunction: function( item ) {
    try {
			var movie = this.getMovie( item );
			movie.StopPlay();
		}
		catch(e) {}
  },
  getMovie: function( item ) {
  	return $('mymovie' + item.id.replace('tab', ''));
  },
  beforeChange: function( arg1, arg2 ) {
		if( arg1.id == arg2.id ) {
      throw $break;
    }
  },
  effectAfterFinish: function() {

  }
}
)

Tabs = Class.create( TabsBase, {
	initialize: function(options){
	  this.options = {
		};
		Object.extend(this.options,options || {});
	},
	showFunction: function( $super, item ) {
	  $super( item );
		Element.show( item );
  },
  hideFunction: function( $super, item ) {
  	$super( item );
    Element.hide( item );
  },
  beforeChange: function( $super, arg1, arg2 ) {
		$super( arg1, arg2 );
  },
  effectAfterFinish: function() {
		
  }
}
)

TabsAnimated = Class.create( TabsBase, {
  initialize: function(options){
	  this.options = {
			duration: 1,
			scope:    '',
			slow:     false,
			queueLimit: 9999
		};
		Object.extend(this.options,options || {});
	},
	showFunction: function( $super, item ) {
		$super( item );
    new Effect.Appear( item, { duration: this.getDuration(), afterFinish: this.effectAfterFinish.bind( this ), queue: { position: 'end', scope: item.id } } )
  },
  hideFunction: function( $super, item ) {
    new Effect.Fade( item, { duration: this.getDuration(), queue: { position: 'end', scope: item.id } } )
    $super( item );
  },
  beforeChange: function( arg1, arg2 ) {
    if( this.options.queueLimit == null ) {
      if( arg1.id == arg2.id ) {
        throw $break;
      }
      if( ( Effect.Queues.get( arg1.id ) && Effect.Queues.get( arg1.id ).size() > 0 ) || ( Effect.Queues.get( arg2.id ) && Effect.Queues.get( arg2.id ).size() > 0 ) ) {
        Effect.Queues.get( arg1.id ).each( function( effect ){ effect.cancel(); effect.element.show() } );
        Effect.Queues.get( arg2.id ).each( function( effect ){ effect.cancel();  effect.element.hide() } );
      }
    }
    else {
      if( arg1.id == arg2.id || ( Effect.Queues.get( arg1.id ) && Effect.Queues.get( arg1.id ).size() > this.options.queueLimit ) || ( Effect.Queues.get( arg2.id ) && Effect.Queues.get( arg2.id ).size() > this.options.queueLimit ) ){
        throw $break;
      }
    }
  },
  effectAfterFinish: function() {

  },
  getDuration: function() {
    return this.options.slow ? this.options.duration * 2 : this.options.duration;
  }
}
)

TabsTimer = Class.create({
	initialize: function( tabs, tabsHandler, options){
	  this.tabs = tabs;
	  this.tabsHandler = tabsHandler;
	  this.options = {
			interval: 8000
		};
		Object.extend(this.options,options || {});
		
		this.tabs.observe( 'afterChange', this.schedule.bind( this ) );
		this.schedule();
	},
	schedule: function() {
	  this.reset();
	  this.timeout = window.setTimeout( this.next.bind( this ), this.options.interval );
	},
	next: function() {
	  this.tabsHandler.options.slow = true;
	  try {
  	  this.tabs.links.each(function(link,i){
  			if(this.activeLink == link && this.links[i + 1]){
  				this.setActiveTab(this.links[i + 1]);
  				throw new Exception;
  			}
  		}.bind(this.tabs));
  		this.tabs.first();
	  }
    catch(e) {}
    
	  this.tabsHandler.options.slow = false;
	},
	reset: function() {
	  try {
	    window.clearTimeout( this.timeout );
	  } catch(e) {}
	}
}
);

function openLink(link) {
	document.location.href = link;
}

