(function($) {

	function debug(mixed)
	{
		if(window.console&&window.console.debug)window.console.debug(mixed);
	}
	
	function log(string)
	{
		if(window.console&&window.console.log)window.console.log(string);
	}
	
	$.fn.bkTabs = function(opts)
	{
		return this.each(function() { 
			new bkTabs(this, $.extend({},bkTabs.fn.defOpts, opts)); 
			}
		);
	}
	
	var bkTabs = function(el,opts)
	{
		this.id=this.uid();
		var t=window[this.id]=this;
		this.$el= $(el);
		this.opts = opts;
		this.init();
	}
	
	bkTabs.prototype = bkTabs.fn = {
		name: 'bkTabs',
		version: 0.1,
		id:0,
		opts: {},
		current: null,
		uid:function()
		{
			return this.name+(new Date().getTime())+''+(Math.round(Math.random()*1000));
		},
		init:function()
		{
			var tabs = this.tabs = this.$el.children('ul:first').find('a[rel]');
			if(!tabs.length)return;
			var t=this;
			tabs.click(function(){
				t.activate($(this));
			});
			if(this.opts.tabContentsContainer)
				$(this.opts.tabContentsContainer).children('div').hide();
			else
				this.$el.children('div:first').children('div').hide();
			this.current = tabs.eq(0);
			$('#'+tabs.eq(0).addClass(this.opts.activeClass).attr('rel')).show();
		},
		activate:function(target)
		{
			var content = $('#'+target.attr('rel'));
			if(!content.length || this.current[0] == target[0])return;
			
			this.tabs.removeClass(this.opts.activeClass);
			target.addClass(this.opts.activeClass);      
			

			
			var t=this;
			
			if(this.current !== null)
			{
				$('#'+this.current.attr('rel')).stop().animate({opacity:0},t.opts.speedOut,t.opts.easeOut,function(){
					$(this).hide();
					content.css('opacity',0).show();
					content.animate({opacity:1},t.opts.speedIn,t.opts.easeIn);
				});
			}else{
				content.show(); 
				content.animate({opacity:1},t.opts.speedIn,t.opts.easeIn);
			}
			this.current = target;
			
			/* Raphael Zschorsch - Hide sidebar if checkbox in page properties is checked */
			
			if(this.current.hasClass('hide_sidebar_1')) {
                            $("div.cp_catalog_sidebar").fadeOut("fast", function() {
                                $("div.cp_catalog_serien_info").css("float","none");
                            });
			}
			else {
                            $("div.cp_catalog_sidebar").hide().fadeOut("slow").fadeIn("slow");
                            $("div.cp_catalog_serien_info").css("float","right");
                        }	
      
                        /* Raphael Zschorsch - Hide sidebar if checkbox in page properties is checked */		
			
		}
	};
	
	bkTabs.fn.defOpts = {
		activeClass: 'active',
		speedIn: 300,
		speedOut: 150,
		easeIn: 'easeInQuad',
		easeOut: 'easeOutQuad',
		tabContentsContainer: null
	};
	
})(jQuery);
