/**
 * @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: 1, scope: 'banner', queueLimit: null };
      var tabsHandler = new Tabs( 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 );
      
      // observe click event
      tabs.links.each(function (item) {
        item.observe('click', function(event) {tabsHandler.onLinkClick(item, event);});
      });
      
      tabsHandler.tabs = tabs;
      var tabsTimer = new TabsTimer( tabs, tabsHandler, { interval: 16000 } );
    //}
  //);
}

initProjectTabs = function() {
  //document.observe('dom:loaded',function()
    //{
      /*var options     = { duration: 0.5, scope: 'project', queueLimit: 3 };
      var tabsHandler = new Tabs( options );*/
      
      options = {};
      options.hover        = true;
      /*options.showFunction = tabsHandler.showFunction.bind( tabsHandler );
      options.hideFunction = tabsHandler.hideFunction.bind( tabsHandler );
      options.beforeChange = tabsHandler.beforeChange.bind( tabsHandler );*/

      var tabs = new Control.Tabs( 'projectTabs', options );
      /*tabsHandler.tabs = tabs; 
      new HoverTabs( tabs );*/
    //}
  //);
}

HoverTabs = Class.create({
  initialize: function(tabs,options){
    this.tabs = tabs;
    tabs.links.each(function(link,i){
			link.observe( 'mouseover', this.over.bindAsEventListener( this ) );
			link.observe( 'mouseout' , this.out.bindAsEventListener( this ) );
		}.bind(this));
  },
  over: function( event ) {
    var elem = Event.element( event );
    new Effect.Morph( elem, { duration: 0.1, style: 'border-width: 1px;margin: 0px;width: 67px; height: 65px;', queue: { limit: 1, position: 'end', scope: elem.parentNode.getAttribute( 'href' ) } } );
    if( !elem.timeout ) {
      elem.timeout = window.setTimeout( this.out.bind(this, event, true), 4000 );     
    }
  },
  out: function( event, slow ) {
    var elem = Event.element( event );
    if( elem.timeout ) {
      window.clearTimeout( elem.timeout );
      elem.timeout = false;
    }
    new Effect.Morph( elem, { duration: slow === true ? 0.7 : 0.4, style: 'border-width: 0px;margin: 3px 3px 0 3px;width: 63px; height: 61px;', queue: { position: 'end', scope: elem.parentNode.getAttribute( 'href' ) } } );
  }
}
)

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() {
		
  },
  onLinkClick: function(item, event) {
    GATrackEvent('Banners', 'Change', item.title);
  }
}
)

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) {}
	}
}
);

// abstracted function for better debugging
function GATrackEvent(category, action, label)
{
  try
  {
    pageTracker._trackEvent(category, action, label);
    //alert(category + ": " + action + " (" + label + ")");
  }
  catch (e) {};
}