/** 
	Class: ShowcaseTabs
	Description: Schafenster 1
	
*/
ShowcaseTabs = Class.create();

Object.extend(ShowcaseTabs,{
	instances: []
});

Object.extend(ShowcaseTabs.prototype,{
	initialize: function(tab_list_container,options, products){

		this.setProducts(products);
		this.current = null;
		
		this.activeContainer = false;
		this.activeLink = false;
		this.containers = $H({});

		this.links = [];
		ShowcaseTabs.instances.push(this);
		
		this.options = {
			autoLinkExternal: true,
			beforeChange: Prototype.emptyFunction,
			afterChange: Prototype.emptyFunction,
			hover: false,
			linkSelector: 'li a', /** Selektor **/
			setClassOnContainer: false,
			activeClassName: 'active',
			defaultTab: 'first',
			showFunction: Element.show,
			hideFunction: Element.hide
		};
		
		Object.extend(this.options,options || {});
		(typeof(this.options.linkSelector == 'string') 
			? $(tab_list_container).getElementsBySelector(this.options.linkSelector)
			: this.options.linkSelector($(tab_list_container))
		).each(function(link){
			this.addTab(link);
		}.bind(this));
			
		if(this.options.defaultTab == 'first')
			this.setActiveTab(this.links.first());
		else if(this.options.defaultTab == 'last')
			this.setActiveTab(this.links.last());
		else
			this.setActiveTab(this.options.defaultTab);
		
	},
	addTab: function(link){
		// replace href with id
		//link.href = "#"+link.id;
		link.href = "javascript:void(null);" 
		this.links.push(link);

		link[this.options.hover ? 'onmouseover' : 'onclick'] = function(link ) {
			if(window.event) {
				Event.stop(window.event);
			}
			this.setActiveTab(link);
			return false;
		}.bind(this,link);
	},

	setActiveTab: function(link){
		
		link.key = link.getAttribute('id').replace(window.location.href.split('#')[0],'').split('/').last().replace(/#/,'');

		if ( this.current != link.key ) {
			this.current = "" + link.key;
			var text = "" + link.key;
			this.processList();
			showcasepager.moveFirstPos();
			if(typeof(link) == 'string'){
				this.links.each(function(_link){
					if(_link.key == link){
						this.setActiveTab(_link);
						throw $break;
					}
				}.bind(this));
			} else{
				this.notify('beforeChange',this.activeContainer);
				/*if(this.activeContainer)
					this.options.hideFunction(this.activeContainer);*/
				this.links.each(function(item){
					(this.options.setClassOnContainer ? $(item.parentNode) : item).removeClassName(this.options.activeClassName);
				}.bind(this));
				(this.options.setClassOnContainer ? $(link.parentNode) : link).addClassName(this.options.activeClassName);
				this.activeContainer = this.containers[link.key];
				this.activeLink = link;
				/*
					this.options.showFunction(this.containers[link.key]);
				*/
				this.notify('afterChange',this.containers[link.key]);
			}
		}
	},

	setProducts: function (products) {
		this.products = products.items;
		this.productsData = products;
	},

	processList: function () {
		//products = new Products();
		masterProducts.load();
		pager = new ProductLayerPager(masterProducts);

		this.setProducts(masterProducts);

		// firstClear the data
		while ($('showcase-list').firstChild) {
			var Knoten = $('showcase-list').firstChild;
			$('showcase-list').removeChild(Knoten);
		}

		for (var i=0;i<this.products.length;i++) {
			var li = document.createElement('li');
			li.className = 'showcase-box';
			li.innerHTML = this.getListItemData(this.products[i], i);
			$('showcase-list').appendChild(li) ;
		}
		
		// change the name from layer and rigttfirst
		$('butRightFirst').className = this.productsData.buttonSrc + " ces";
		
		// nicht einblenden, wenn keinen Daten vorhanden
		if (this.productsData.allDataSrc) {
			$('allContainer').innerHTML = this.productsData.allDataSrc + " &raquo;";
			$('allContainer').href = this.productsData.allDataHref;
		}
		else {
			$('allContainer').innerHTML = "";
			$('allContainer').href = "javascript:void(null);";
		}

		$$('.mod-showcase').each(function(elm){
			if (elm) {
				if (showcasepager) {
					//alert('delting')
					showcasepager.resetPager();
					//showcasepager = null;
				}
				showcasepager = new ShowcasePager(elm);
			}
		});
	},

	getListItemData: function (product, count) {
		// product
		var field = /*'<li class="showcase-box">' +*/
			'<div class="name"><a href="'+ product.productlink + '">' + product.name + '</a> »</div>' +
			'<div class="image" onmouseover="showVorschauButton(\''+product.pc + count+'\', this);" onmouseout="hideVorschauButton(\''+product.pc + count+'\');" >' + 
			'<a href="javascript:pager.loadLayerWithData(\'' + product.pc + '\');javascript:Move.element(\'ProductLayer\', \'productLayerTarget\');">' +
			'<img class="prodImg" src="' + configuration.cestoreMediaPrefix + product.thumbnail + '" alt="' + product.name + '" />' +
  			'</a>' +
			'<div class="Preview ces" id="PreviewBut_'+product.pc + count+'"><a href="javascript:pager.loadLayerWithData(\'' + product.pc + '\');javascript:Move.element(\'ProductLayer\', \'productLayerTarget\');">&nbsp;</a></div></div>';
		
			var price = ((product.isFromPrice) && (product.productPriceInformationList > 1)) ? configuration.fromPricePrefix + product.price :  product.price
			if (product.isDiscount) {
				field += '<ins class="discount">' + price + '</ins>';
				field += '<del>' + product.strokeprice + '</del>';
			}
			else {
				field += '<span class="price">' + price + '</span>';
			}
			if (configuration.showArticleNumber){
				field += '<div class="sc1b2c_article_' + this.current + '_' + product.pc + '">' + configuration.articleText + ': ' + product.pcik + '</div>';
			}
			if (configuration.isRatingActive) {
				field += '<div style="margin-top:4px;"><img src="'+configuration.cestoreImagePrefix+'/default/default/icons/' + product.rating;
			
				if( product.rating == 1 ) 
					field += 'star.gif';
				else
					field += 'stars.gif';
			
				field += '"></div>';
			}
			if (configuration.isRatingActiveBv) {
			
				field += '<div style="margin-top:4px;"><img src="'+configuration.cestoreImagePrefix+'/default/default/bvstars/rating-' + product.bvrating+'.gif'; 
			
				field += '"></div>';
			}
			field += '<div class="rechtstext">';
			
			field += product.net + ', <br /> ' + configuration.zzgl + ' <a href="javascript:showDeliverLayer();">' + configuration.versand + '</a></div>' +
		'</li>';
		return field;
	},
	notify: function(event_name){
		try{
			if(this.options[event_name])
				return [this.options[event_name].apply(this.options[event_name],$A(arguments).slice(1))];
		}catch(e){
			if(e != $break)
				throw e;
			else
				return false;
		}
	}
		
});
if ( typeof(Object.Event) != 'undefined' ) {
	Object.Event.extend(ShowcaseTabs);
}
