/** 
	ProductLayerPager
	---
	Handles the Paging from ProductDetail-Layer with given products ( Products-Object )
*/
ProductLayerPager = Class.create({

	// constants
	LAYER_NAME: 'ProductLayer',

	initialize: function( products, layerName ) {
		this.product = 1;
		this.current = null;
		this.next = null;
		this.prev = null;
		this.items = new Array();
		this.products = products;
		this.layer = $(this.LAYER_NAME);
		this.nextHandle = $('plNextArticle');
		this.prevHandle = $('plPrevArticle');
		this.detailViewHandle = $('plLinkDetail');
		//this.detailViewHandle1 = $('plLinkDetail1');
		this.detailViewImageHandle  = $('plImageLinkDetail');
		this.prevContainer = this.prevHandle.parentNode;
		this.nextContainer = this.nextHandle.parentNode;
	},

	/* shows the layer */
	showLayer: function () {
		this.layer.style.display = '';
	},
	/* hides the layer */
	hideLayer: function () {
		this.layer.style.display = 'none';
	},
	/* Load the data in the layer with the current sk */
	loadLayerWithData: function (sku) {
		this.determinePosition(sku);
		this.fillLayer();
		this.showLayer();
	},
	
	loadLayerWithDataSC2: function() {
		this.determinePositionSC2();
		this.fillLayer();
		this.showLayer();		
	},
	
	/* Gets the Position within the array */
	determinePosition: function (pc) {
		for (var i=0;i<this.products.items.length;i++) {
			if ( this.products.items[i].pc == pc ) {
				this.current = i;
			}
		}

		this.determineNextPrev();
	},
	
	determinePositionSC2: function () {
		// Sonderbehandlung fuer Showcase2-Artikelliste -> nur ein Bild
		this.current = 0;
	},
	
	/* Determine */
	determineNextPrev: function () {
		if (this.current != null) {

			if ((this.current-1) >= 0)  {
				this.prev = this.current-1;
			}
			else {
				this.prev = null;
			}

			if ((this.current+1) < this.products.items.length) {
					this.next = this.current+1;
			}
			else {
				this.next = null;
			}
		}
	},
	/* Get Data from Object an Fill the Layer with data */
	fillLayer: function () {
		// Fill Layer with Data from Current Item
		if (this.current == null)
			return;
		var item = this.products.items[this.current];
		if(!item) { return };

		$('plName').innerHTML = item.name;
		// Set the SKU
		$('plArtNr').innerHTML = item.pcik;
		$('plFormSku').value = item.pc;
		
		//alert("addtocartlink");
		$('plForm').action = configuration.productDetailAction + '?addToCart'; //item.addtocartlink;

		// Update the Image with alt Text
		$('plImage').src = configuration.cestoreMediaPrefix + item.image;
		$('plImage').alt = item.name;
		
		var price = ((item.isFromPrice) && (item.productPriceInformationList > 1)) ? configuration.fromPricePrefix + item.price :  item.price

				
				
		if (item.isDiscount == true) {
			$('plStrokeprice').innerHTML = item.strokeprice;
			$('plStrokeprice').style.display = 'inline';
			
			
			$('plPrice').style.display = "none";
			$('plPrice').innerHTML = "";

			$('plDiscountPrice').innerHTML = price;
			$('plDiscountPrice').style.display = "";
			$('plDiscountValue').innerHTML = item.discount;

			$('plDiscount').style.display = 'inline';
		}
		else {
			$('plPrice').innerHTML = price;
			$('plPrice').style.display = "";
			$('plDiscountPrice').innerHTML = "";
			$('plDiscountPrice').style.display = 'none';
			$('plDiscount').style.display = 'none';
			$('plStrokeprice').style.display = 'none';
		}

		// Availability		
		var availability = $('availability');
		availability.innerHTML = item.availabilityText;
		if (item.availability == "g") {
			availability.className = "avaibility_green";
		}
		else if (item.availability == "r") {
			availability.className = "avaibility_red";
		}
		else {
			availability.className = "avaibility_yellow";
		}

		// Ratings		
		if (configuration.isRatingActive) {
			$('rating').src = configuration.cestoreImagePrefix + "/default/default/icons/" + item.rating;
			
			if( item.rating == 1 ) 
				$('rating').src += "star.gif";
			else
				$('rating').src += "stars.gif";
			
			if (item.rating == "0") {
				$('rating').alt = configuration.text.norating;
			}
			else if (item.rating == "1") {
				$('rating').alt = configuration.text.onerating;
			}
			else {
				$('rating').alt = item.rating + " " + configuration.text.moreratings;
			}
		}
		if (configuration.isRatingActiveBv) {
		
			$('rating').src = configuration.cestoreImagePrefix + "/default/default/bvstars/rating-" + item.bvrating+".gif";
			
			if (item.bvrating == "0") {
				$('rating').alt = configuration.text.norating;
			}
			else if (item.bvrating == "1") {
				$('rating').alt = configuration.text.onerating;
			}
			else {
				$('rating').alt = item.bvratingAlt + " " + configuration.text.moreratings;
			}
		}

		//$('plDescription').innerHTML = item.description;
		$('plDescription').innerHTML = item.highlights;

		productLink = configuration.cestorePrefix + "/" + configuration.language + "/product/" + item.pc + "/" + item.beautifyName;

		if(!document.cookie && !baseUrl.indexOf(";jsessionid") > 0) {
			productLink = productLink + ";jsessionid=" + sessionId;
		}
		this.detailViewHandle.href = productLink;
		//this.detailViewHandle1.href = item.productlink;
		this.detailViewImageHandle.href = productLink;
		
		if (this.prev != null) {
			this.prevContainer.style.display = 'block';
			this.prevHandle.href = "javaScript:pager.showPrev();";
		}
		else {
			this.prevContainer.style.display = 'none';
		}
		if (this.next != null) {
			this.nextContainer.style.display = 'block';
			this.nextHandle.href = "javaScript:pager.showNext();";
		}
		else {
			this.nextContainer.style.display = 'none';
		}
		
		// Merkzettel		
		$('merkzettel_showcase').href = configuration.actionShoppingListShowcase + "" + item.pc;
		
		if( configuration.categorycode != '' )  
			$('merkzettel_showcase').href = $('merkzettel_showcase').href + "&categorycode=" + configuration.categorycode;
		
		// Compare
		$('productCompare').href = configuration.actionProductsCompare + "?add&productcode=" + item.pc + "&categorycode=";
		
		// Recommend
		$('productRecommend').href = configuration.actionRecommend + "?productcode=" + item.pc + "&categorycode=";
	},
	/* Shows the next Product */
	showNext: function () {
		this.current++;
		this.determineNextPrev();
		this.fillLayer();
	},
	/* Shows the previous Product */
	showPrev: function () {
		this.current--;
		this.determineNextPrev();
		this.fillLayer();
	}
});
