(function($){
	
    $.sliderBox = function(el, options)
    {
    	// To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;
        
        base.getButtonCoords = function(button)
		{
			var pos = button.position();
			return {
				left: pos.left,
				top: pos.top,
				width: button.outerWidth(true),
				height: button.outerHeight(true)
			};
		}
        
		base.ieRemoveFilter = function(els)
		{
			els.css('filter', '');
		}
		
		base.init = function()
		{
			base.options = $.extend({}, $.sliderBox.defaults, options);
			
			base.tabs = $(base.options.tab_class);
			base.tabs_cnt = base.tabs.length;

			base.tab_size = {};
			base.tab_size.x = $(base.tabs[0]).outerWidth(true) + base.options.tab_offset.width;
			base.tab_size.y = $(base.tabs[0]).outerHeight(true) + base.options.tab_offset.height;
			
			base.buttons = $(base.options.button_class);
			base.buttons_cnt = base.buttons.length;
					
			// alle Tabs verstecken
			base.tabs.css({
				opacity: 0,
				left: base.tab_size.x
			});
			
			// wenn kein Start-Tab angegeben, einen per Zufall ermitteln
			if(false === base.options.start_tab) {
				var rand = Math.round(Math.random()*(base.tabs_cnt-1))+0;
				var start_tab = $(base.tabs[rand]);
			}
			else {
				var start_tab = $(base.options.start_tab);
			}
			
			// Start-Tab anzeigen
			start_tab.css({
				opacity: 1,
				left: base.options.tab_offset.width
			}).addClass('visible');
			if ($.browser.msie) 
			{
				base.ieRemoveFilter(start_tab);
				base.tabs.css('background-color', '#fff');
			}
			
			// Pfeil auf Start-Tab positionieren
			var but_coords = base.getButtonCoords($('#' + start_tab.attr('id').replace(base.options.tab_id_prefix, base.options.button_id_prefix)));
			base.arrow_width = $(base.options.arrow).outerWidth(true);
			$(base.options.arrow).css({
				left: but_coords.left + but_coords.width / 2 - base.arrow_width / 2,
				display: 'block'
			});
			
			
			// buttons onClick-Event zuweisen
			base.buttons.click(function(e) {
				e.preventDefault();

				var button_id = $(e.target).attr('id');
				
				
				// Pfeil positionieren
				if($(base.options.arrow)) { 
					var but_coords = base.getButtonCoords($('#' + button_id));
					
					$(base.options.arrow).animate({
						left: but_coords.left + but_coords.width / 2 - base.arrow_width / 2
					});
				}
				
				
				
				
				
				
				
				
				
				var id = button_id.replace(base.options.button_id_prefix, '');
				
				var new_tab = $('#' + base.options.tab_id_prefix + id);
				
				if(new_tab.attr('id') != $('.visible').attr('id')) {
					// aktuellen Tab ausblenden
					$('.visible').removeClass('visible').animate({
						opacity:0
					}, base.options.anim_duration)
					// danach auf Ursprungsposition setzen und class 'visible' entfernen
					.queue(
						function() {
							$(this).css({
								left: base.tab_size.x + 'px'
							});
							if ($.browser.msie) base.ieRemoveFilter($(this));
							$(this).dequeue();
						}
					);
		
					
					// dem Button zugewiesenen Tab einblenden
					new_tab.animate({
						left: base.options.tab_offset.width,
						opacity: 1
					}, base.options.anim_duration).addClass('visible').queue(function(){
						if ($.browser.msie) base.ieRemoveFilter(new_tab);
						$(this).dequeue();
					});
				}
			});
		}
		
		base.init();
    }
    
    $.sliderBox.defaults = {
		tabs: '#ps_tabs',
		tab_class: '.ps_tabs',
		tab_id_prefix: 'pst',
		buttons: '#ps_buttons',
		button_class: '.ps_buttons',
		button_id_prefix: 'psb',
		
		arrow: '#ps_arrow',
		
		
		tab_offset: {
    		width: -10,
    		height: 0	/* nicht implementiert, nur horizontaler slider */
    	},
    	
//    	start_tab: '#pst3',
		start_tab: false,
		anim_duration: 1000,
		
		
		
		auto_slide: false,
		auto_slide_duration: 10000,
		
		
		
		
		
		button_forward: '#forward',		/* forward button selector */
		button_backward: '#backward',	/* backward button selector */
		
		items_per_page: 4,
		item_size: false,				/* false or size in pixel */
		page_size: false				/* false or size in pixel */
		
		
	};
    
    $.fn.sliderBox = function(options)
	{
		if (typeof(options) == "object") {
			return this.each(function(i)
			{
				(new $.sliderBox(this, options));
				
				// This plugin supports multiple instances, but only one can support hash-tag support
				// This disables hash-tags on all items but the first one
				options.hashTags = false;
			});
		}
		else {
			return this.each(function(i)
			{
				(new $.sliderBox(this, {}));
			});
		}
	}
})(jQuery);

$(document).ready(function() {
	$('#product_slider').sliderBox({a: true});
});
