/**
 * jQuery pixoStage plugin
 *
 * @require jquery.serialScoll 
 * @support jquery.cluetip, Cufon
 * @author Pixopat <alex@pixopat.com>
 * © 2009 Pixopat
 *
 **/
;(function($) {

  /**
   * plugin definition
   */
  $.fn.pixoStage = function(options) {
    
    //;;; _debug(this, 'definition');
    var main_options = $.extend({}, $.fn.pixoStage.defaults, options);

    var actions = {
       
       init: function(e) {
	     //;;; _debug(e, 'init');
         this.e = e;
         this.items = e.find(this.settings.item_element);
         this.stage = e.find(this.settings.stage_element);
         this.headings = e.find(this.settings.headings_element);
         this.headings_elements = this.headings.find('li');
         this.headings_elements_anchors = this.headings_elements.find('a');
         this.previous_buttons = e.find('.prev');
         this.next_buttons = e.find('.next ');
         ;;; _debug(this, 'init this');
         this._current = this.settings.item_starting;
         this._size = this.items.size();
         this.tooltips = this.items.find('a.cluetip');
         this.is_first = true;
         if (this._size > 1) this.initSerialScroll();
         else this.headings.hide();
         this.initCluetip();
         this.initCufon();
   	   },

       initSerialScroll: function() {
	     //;;; _debug(this, 'initSerialScroll');
	     if ($.serialScroll) {
		   this.stage.serialScroll({
		     items: 'li',
		     duration: 1000,
		     lock: false,
		     stop: true,
		     cycle: true,
		     force: true,
		     queue: false,
		     constant: false,
		     axis: 'x',
		     easing: 'easeOutCirc',
		     lazy: false,
		     interval: 5000,
		     step: 1,
		     onBefore: this.onSceneMove,
		     onAfter: this.onSceneStop
		   });
		   this.initStage();
		   this.initHeadings();
	     }
       },

       initStage: function() {
	     //;;; _debug(this, 'initStage');
		 this.stage.hover(
		   function(){$(this).trigger('stop');}, 
		   function(){$(this).delay(5000).trigger('stop').trigger('start');}
		 );
		 this._current = this.settings.item_starting;
		 this.stage.trigger('goto', this._current);
		 if (this._current == 0) this.setActiveHeadings();
       },

	   initHeadings: function() {
	     //;;; _debug(this, 'initHeadings');
	     this.previous_buttons.bind('click', function(){
		   actions.stage.trigger('stop').trigger('prev'); 
		   return false;
		 });
		 this.next_buttons.bind('click', function(){
		   actions.stage.trigger('stop').trigger('next');
		   return false;
		 });
		 this.headings_elements_anchors.bind('click', function(){
		   var i = actions.headings_elements.index($(this).parent());
		   actions.stage.trigger('stop').trigger('goto', [i]);
		   return false;
		 });
		 this.headings_elements_anchors.cluetip({
           topOffset: 5, 
           leftOffset: 5, 
           local: true,
           attribute:'rev',
           showTitle: false,
           cluetipClass: 'heading-thumb',
           cursor: 'pointer',
           dropShadow: false,
           dropShadowSteps: 0,
           positionBy: 'mouse',
           tracking: true,
           sticky: false,
           mouseOutClose: true,
           fx: {             
             open: 'fadeIn',
             openSpeed: 'fast'
		   }
		 });
       },
       
       initCluetip: function() {
	     //;;; _debug(this, 'initCluetip');
	     if ($.cluetip && this.tooltips.size() > 0) {
		   $(this.tooltips)
		  	.cluetip({
			  attribute:'rel',
		  	  local: true,
		  	  cursor: 'pointer',
		  	  cluetipClass: 'product_scene',
		  	  dropShadow: false,
		  	  dropShadowSteps: 0,
		  	  showTitle: false,
		      positionBy: 'mouse',
		  	  tracking: true,
		  	  sticky: false,
		  	  mouseOutClose: true,
		  	  arrows: true,   
		  	  //closeText: i18n_scene_close,
		  	  fx: {             
		  	    open: 'fadeIn',
		  	    openSpeed: 'fast'
		  	  },
		  	  onShow: this.refreshCufon
		  	})
		  	.css('opacity', 1);
	     }
       },
       
       initCufon: function() {
	     if (Cufon)
	       Cufon.set('fontFamily', this.settings.cufon_font).replace(this.headings_elements_anchors); 
       },
       
       refreshCufon: function(ct, c) {
	     ;;; _debug(this, 'refreshCufon');
	     if (Cufon) {
	       var wrap = ct.find('.cufon');
	       var text;
	       wrap.each(function(i){
	         text = $(this).text();
	         $(this).replaceWith(text);
	       });
	       Cufon.refresh();
	     }
       },

	   setActiveHeadings: function() {
		 //;;; _debug(this, 'setActiveHeadings');
	     this.headings_elements.removeClass('active');
	     this.headings.find('li:eq('+this._current+')').addClass('active');
	   },
       
	   onSceneMove: function(o, t, c, p) {
		 //;;; _debug(this, 'onSceneMove');
	     actions._current = o.data;
	     if (actions._current == p.length - 1) actions.next_buttons.fadeOut('fast');
	     else actions.e.find('.next:hidden').fadeIn('fast');
	     if (actions._current == 0) actions.previous_buttons.fadeOut('fast');
	     else actions.e.find('.prev:hidden').fadeIn('fast');
	   },
       
	   onSceneStop: function(o, t) {
		 //;;; _debug(this, 'onSceneStop this'); 
	     actions.setActiveHeadings();
	   }
    
    };

    return this.each(function() {
      //;;; _debug($(this), 'returned element');
      var $this = $(this);
      actions.settings = $.meta ? $.extend({}, main_options, $this.data()) : main_options;
      actions.init($this);
    });
  };
  
  /**
   * private function for debugging
   */ 
  function _debug(o, str) {
    if (window.console && window.console.log)
      window.console.log('pixoStage debug '+str+' (%o)', o);
  };

  /**
   * plugin defaults
   */
  $.fn.pixoStage.defaults = {
    item_element: 'li',
    item_starting: 0,
    stage_element: 'ul:first',
    headings_element: 'ul:last',
    cufon_font: 'Joy'
  };

})(jQuery);
