/**
  Switcher Plugin
  - swaps between 'item' passed in using fade effect
**/
(function($) {

	$.fn.switcher=function(options){
		return this.each(function(){			
			if(this._sw) return; //if already a switcher return
			else this._sw = plugin_counter; //otherwise add the index
      var params = $.extend(true, {}, $.fn.switcher.defaults, options);
			H[plugin_counter] = {container:this, params:params,item_switch_countdown:false, items:[],active:false};
			$.switcher.setup(plugin_counter);
      $.switcher.setup_rotate(plugin_counter);
			if(H[plugin_counter].params.pagination) $.switcher.pagination_trigger(plugin_counter);			
			plugin_counter ++;
		});
	};
	
	//the default config vars
	$.fn.switcher.defaults = {after_function:false, auto_rotate:false, rotate_time:6000, item:'.item', pagination:false, pagination_active_class:'active' ,speed:"slow", swap_container_class:false};
	//the over riden stuff
	$.switcher = {
		hash:{}, //the hash used to store all the configs & targets
		/*
		 loop over all items in the container and add new class and also push them to the internal array - this 
		 is then used for rotate / pagination etc
		*/
		setup:function(index){
		  var counter = 0;
      jQ(H[index].container).find(H[index].params.item).each(function(){
        H[index].items.push(this);
        jQ(this).addClass('switcher').addClass('switcher_'+counter);
        if(counter>0) jQ(this).hide();
        counter++;        
      });
      H[index].active = 0;
	  },
	  setup_rotate:function(index){
	    if(H[index].params.auto_rotate && H[index].params.rotate_time){	      
	      H[index].item_switch_countdown = setTimeout("$.switcher.rotate("+index+")", H[index].params.rotate_time);
      }
	  },
	  rotate:function(index){
	    var outbound = 0;
      if(!H[index].active) H[index].active = outbound = 0;
      else outbound = H[index].active;      
      var inbound=outbound +1;     
      if(inbound > H[index].items.length-1) inbound = 0; //so doesnt rotate beyond limits!
      $.switcher.hide_show(index,outbound, inbound);
      $.switcher.setup_rotate(index);       
	  },
	  hide_show:function(index,outbound,inbound){
	    jQ(H[index].items[outbound]).fadeOut(H[index].params.speed, function(){	      
        jQ(H[index].items[inbound]).fadeIn(H[index].params.speed, function(){
          if(H[index].params.after_function) H[index].params.after_function();
        });
        if(H[index].params.swap_container_class){
          var class_name = jQ(H[index].items[inbound]).find("h2 a").attr('rel');
          jQ(H[index].container).attr('class', class_name);
        }        
        if(H[index].params.pagination){
          var target_href = '#'+jQ(H[index].items[inbound]).attr('id');
          jQ(H[index].container).find('.'+H[index].params.pagination_active_class).removeClass(H[index].params.pagination_active_class);
          jQ(H[index].container).find('a[href='+target_href+']').parent().addClass(H[index].params.pagination_active_class);
        }
        if(typeof(H[index].item_switch_countdown) != "undefined") clearTimeout(H[index].item_switch_countdown);
        $.switcher.setup_rotate(index);       
      });
      H[index].active = inbound;
	  },
	  pagination_trigger:function(index){
	    var search_for = '#'+H[index].container.id+' '+H[index].params.pagination+' a';
	    jQ(search_for).hover(function(){
	      //clear timers
	      if(typeof(H[index].item_switch_countdown) != "undefined") clearTimeout(H[index].item_switch_countdown);
	      var match_to = this.rel.replace('#', ''), inbound = 0;
        for(var i in H[index].items){
          if(jQ(H[index].items[i]).attr('id') == match_to) inbound = i;
        }
	      $.switcher.hide_show(index, H[index].active, inbound);
	      $.switcher.setup_rotate(index);
	      return false;
	    },function(){
				if(typeof(H[index].item_switch_countdown) != "undefined") clearTimeout(H[index].item_switch_countdown);
				return false;
			});
			jQ(search_for).click(function(){return false;});
	  }
	};
	var H=$.switcher.hash,	jQ = jQuery; ie6=$.browser.msie&&($.browser.version == "6.0"), plugin_counter=0;
})(jQuery);
