if (! ("console" in window) || !("firebug" in console)) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group"
                 , "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0; i <names.length; ++i) window.console[names[i]] = function() {};
}

function css(el, prop) {
    return parseInt($.css(el[0], prop)) || 0;
};

function applyAccordian(accordian, options)
{  

  accordian.css({'float':'left', 'height':options.height+'px', 'overflow': 'hidden'})
  tabs = $(options.tab, accordian)
  contents = $(options.content, accordian)
  contents.css({'float':'left', 'height':options.height+'px', 'width': '0px', 'overflow': 'hidden'})
  tabs.css({'float':'left', 'height':options.height+'px'})

  openIndex = -1
  
  openTab(0)
  
  function getTabIndex(tab){
    tabs.each(function(i,el){
            if(el==tab) index=i;
        });
    return index;
  }
  
  function openTab(tabIndex){

    // first find current open tab, width > half
    curOpenIndex = openIndex;
    for(i=0;i<contents.size();i++){
	if(css($(contents[i]), 'width') >= options.width/2.0){
	    curOpenIndex = i;
	}
    }
     
    // current opened tab is being closed, lets not reopen it again, but next one
    if(tabIndex == curOpenIndex)
    {
        tabIndex = tabIndex + 1;
        if(tabIndex > tabs.size() - 1)
            tabIndex = tabIndex - 2;
    }
    
    openIndex = tabIndex;

    $(contents[openIndex]).animate({
        width: options.width+'px'
      }, {duration: options.timeout, easing: options.easing, queue:false});
      
    //close everything else
    contents.each(function(i,c){
	    if(i==openIndex) return;
	    $(c).animate({
	    width: '0px'
	    }, {duration: options.timeout, easing: options.easing, queue:false});
	});
  }
  
  function moveTab(tabIndex, dx){
  
    //first tab can not be moved anywhere
    if(tabIndex <= 0) return false;
    
     ret = true;
     
    //if all the content before this tab are more then fully opened  and it is moving to tight do not move
    totalContentWidth = 0
    for(i=0;i<tabIndex;i++)
	totalContentWidth += css($(contents[i]), 'width')
    if(totalContentWidth + dx > options.width){
	ret = false;
	dx = options.width - totalContentWidth;
    }

    prevContent = $(contents[tabIndex-1])
    nextContent = $(contents[tabIndex])
    
   
    newWidth = css(prevContent, 'width')+dx
    if(newWidth < 0){
	newWidth = 0
	ret = false;
    }
    
    if(newWidth > options.width) {
	newWidth = options.width
	ret = false;
    }
    prevContent.css( 'width', newWidth+'px')

    newWidth = css(nextContent,'width')-dx
    if(newWidth < 0){
	newWidth = 0
	ret = false;
    }
    
    if(newWidth > options.width) {
	newWidth = options.width
	ret = false;
    }
    
    nextContent.css( 'width', newWidth+'px')
    return ret;
    
  }
  
  
  $(options.tab, accordian).click(function(){
  
    var currentTime = new Date()

    if(options.lastClickTime && (currentTime.getTime() - options.lastClickTime < options.timeout)) return;
    if(options.lastDragTime && (currentTime.getTime() - options.lastDragTime < 200)) return;
    
    options.lastClickTime = currentTime.getTime();
    
    openTab(getTabIndex(this))
    
  });
  
  //mouse movement based opening
  function momentumCallback(disp, tabIndex){
	if(tabIndex < 0){
	    momentum.stop()
	    return
	}
        if(!moveTab(tabIndex, -disp)) momentum.stop()
    }
    
  var vmouse = new VMouse()
  var momentum = new Momentum(momentumCallback, {'braking':0.3, 'timeout':50})
  function clear(){
    leftDown = false;
    lastMouseX = lastMouseY = null;
    dragTabIndex = -1;
  }
  clear()
  
  $(document).mouseup(function(e){
        momentum.start(vmouse.xSpeed, dragTabIndex)
        clear();
	if(!vmouse.recentlyMoved()) return;
    });
    
  $(options.tab, accordian).mousedown(function(e){
        dragTabIndex = getTabIndex(this);
        leftDown = true;
	momentum.stop()
        return false;
    });
    
  accordian.mousemove(function(e){
      if(!leftDown || dragTabIndex==-1) return;
      
    var currentTime = new Date()
    options.lastDragTime = currentTime.getTime();
    vmouse.addPos(new VMousePos(e.pageX, e.pageY))
    
      if(lastMouseX)
        moveTab(dragTabIndex, e.pageX-lastMouseX);
      lastMouseX=e.pageX
      lastMouseY=e.pageY
      return false;
    });
}



(function(jQuery) {

//ad my easing
jQuery.extend( jQuery.easing, {
	myOutBounce: function (x, t, b, c, d) {
		ret=b+c;
		s1 = .9; r1 =(1-s1)/(1-0.75);
		s2 = .96; r2 = (1-s2)/(1-.9375)
		if ((t/=d) < (1/2.75)) {
			ret = c*(7.5625*t*t) + b;
		}else if (t < (2/2.75)) {
			ret = c*(7.5625*r1*(t-=(1.5/2.75))*t + s1) + b;
		}else if (t < (2.5/2.75)) {
			ret = c*(7.5625*r2*(t-=(2.25/2.75))*t + s2) + b;
		} else {
			ret = c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
		
		return ret;
	}
});

jQuery.fn.koolAccordian = function(o) {

o = jQuery.extend({
        timeout: 6000,
        easing : 'myOutBounce',
        width : '500px',
        height : '192px',
        content: ".content",
        tab: ".tab",
        
    }, o || {});

o.width = parseInt(o.width)
o.height = parseInt(o.height)
return this.each(function(){
    applyAccordian($(this), o)
        });


};

})(jQuery);


