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 applyAnimation(el, options, index)
{
    el.hide()
    setTimeout( function(){
        finalCSS = {}
        for(k in options.initialCSS){
            finalCSS[k] = css(el, k)+'px'
        }
	
        el.css(options.initialCSS)
        el.show()
        el.animate(finalCSS, {duration: options.timeout+index*options.timeout_factor, easing: options.easing, queue: false} ); 
        }, 
    50 ) //start a bit delat so all do not start at once, somehow overwhelms the animation/cpu?
          
    //setTimeout( function(){ applyAnimation(el, options) }, options.repeatDelay)
}


(function(jQuery) {

jQuery.fn.divAnimation = function(o) {

o = jQuery.extend({
        timeout: 800,
	timeout_factor: 500,
        repeatDelay: 3000,
        easing: 'linear',
	flying_piece: '.flying_piece',
        initialCSS: {'marginLeft': '50%'}
    }, o || {});

this.show()

return this.each(function(){

    $(this).find(o.flying_piece).each(function(i,el){
        applyAnimation($(el), o, i)
        });
    });

};

function applyMakeTabs(el, options)
{

    options.totalTabs = el.children().size()
    
    showTab(options.curIndex)

    function showWithAnimate(e){
    
	$(e).divAnimation(options.animation_options);
    }
    
    function showTab(index, timed){
    
	if(timed && options.lastTime){
	
	    var currentTime = new Date()
	    if(currentTime.getTime() - options.lastTime < options.repeatDelay -5) return;
	
	}
	
    	if(index >= options.totalTabs) index = 0
	if(index < 0) index = options.totalTabs-1;
	
	var currentTime = new Date()
	options.lastTime = currentTime.getTime();
	
	options.curIndex = index;
	el.children().each(function(i, e){
		if(i==index) {
		    $(options.tab_name).html($(e).attr('title'))
		    showWithAnimate(e)
		}
		else $(e).hide();
	 });
	 
	$(options.dots).children().each(function(i,el){
	    if(i==index){
		$(el).addClass(options.selectedDotClass)
		$(el).removeClass(options.unselectedDotClass)
	    }else{
		$(el).removeClass(options.selectedDotClass)
		$(el).addClass(options.unselectedDotClass)
	    }
	});
	
	setTimeout(function(){ showTab(options.curIndex+1, 1) } , options.repeatDelay);
    }
    
    function clickEvent(event){
	data = event.data
	if(data.step){
	    index = options.curIndex + data.step
	}else{
	    index = data.to
	}
	
	showTab(index);
	return false;
    }
    
    // on click of dot move to correct pos
    $(options.dots).children().each(function(i,el){
	$(el).bind('click', {'to':i}, clickEvent)
    });

    // on click of dot move to correct pos
    $(options.prev).children().each(function(i,el){
	$(el).bind('click', {'step':-1}, clickEvent)
    });
    
    // on click of dot move to correct pos
    $(options.next).children().each(function(i,el){
	$(el).bind('click', {'step':1}, clickEvent)
    });
}

jQuery.fn.makeTabs = function(o) {



o = jQuery.extend({
        'repeatDelay': 5000,
        'dots': '#dots',
        'next': '#next',
	'prev': '#prev',
	'selectedDotClass':'selectedDotClass',
	'unselectedDotClass':'unselectedDotClass',
        'curIndex': 0,
	'tab_name':'#tab_name',
	'animation_options':{}
    }, o || {});

if (o.startupcontent)
{
	var thisObj = this;
	var theTiemeout = setTimeout(function(){ 
				$(thisObj).css('display','');
				$(o.startupcontent).remove();
				clearTimeout(theTiemeout);
				return thisObj.each(function(){
				    applyMakeTabs($(thisObj), o)
			    });									  
	} , 4000);
	
	
}
else
{

		return this.each(function(){
			applyMakeTabs($(this), o)
			});
}
    
};

})(jQuery);


