/**
 * prod/detail
 * @autor:Christoph Gentsch
 * Ironshark Entertainment
 */

var prodDetail_instance;

 var ProdDetail = Class.create();
 ProdDetail.prototype = {
 	initialize:function(){

 		prodDetail_instance = this;
 		
 		$("loaderbig").hide();
 		$("mainImage").show()
 		
 		this.prodId = $("prodId").value;
 		
 		this.attr = new Hash();
 		
 		var regexp = /cart_add_variation_([0-9]*)/;
 		$$("select.detailSelect").each(function(item) {
 			if( item.id!="amountSelect" ) {
 				var attrId = parseInt( regexp.exec(item.name)[1] );
 				var value = parseInt( item.value );
 				if(isNaN(value)) value = -1;
 				if(!isNaN(attrId)) {
 					prodDetail_instance.attr.set(attrId,value);
 				}
 			}
 		});
 		
 		var mags = [$("magnifier1"),$("magnifier2"), $("magnifier3")].concat($A($$("#pictureChooser a") ));

 		$A( mags ).each( function(item) {
 			if(!item) return;
	 		var regexp = /shop\/prod\/gallery\/([0-9]*)\/img\/([0-9]*)/;
			var index = regexp.exec(item.href);	item.writeAttribute("href",null);
			item.addClassName("pointer");
			$(item).setAttribute('prod',index[1]); $(item).setAttribute('img',index[2]);

	 		$(item).observe('click', function(event) {
	 			//Workaround IE6 (Selectboxen müssen versteckt werden damit sie nicht durch overlay scheinen)
	 			$A( $$("select") ).each( function(item)
	 			{
	 				(item).hide();
	 			});
	 	 		
	 	 		//Workaround IE6 ENDE
	 			$("ajaxLoader").hide(); $$("div.overlay")[0].show();
	 			Effect.Appear( $("galleryDummy"), { duration: 0.3 } );
	 			
	 			
	 			new Ajax.Updater( 
	 				$("ajaxLoader"), 
	 				Core.url("shop/prodGallery/show/"+this.getAttribute("prod")+"/"+this.getAttribute("img")), 
	 				{ method:'get', onComplete:function(){ prodGallery.init();} }
	 			);
	 			
	 			new PeriodicalExecuter(function(pe) {
  					if ( prodGallery.ready ) {
  						prodGallery.ready = false;
  						$("galleryDummy").hide();
  						$("ajaxLoader").show();
    					pe.stop();
  					}
				}, 0.3);
	 		});
 		});

 		/**
 		 * get all elements that will trigger the product detail view: <a href="..." do="openProductDetails" ...>
 		 */
 		var elements = $('globalWrapper').select('a[do="openProductDetails"]');

 		for(var i=0;i<elements.length;i++)
 		{
 			var item = elements[i];
 			if(item) {
		 		var regexp = /shop\/prod\/details\/([0-9]*\/[0-9]*)/;
				var index = regexp.exec(item.href);
				item.writeAttribute("href",null); 
				item.addClassName("pointer");
		 		
		 		$(item).observe('click', function(event) {
		 			//Workaround IE6 (Selectboxen müssen versteckt werden damit sie nicht durch overlay scheinen)
		 			$A( $$("select") ).each( function(item)
		 		 	{
		 		 		(item).hide();
		 		 	});
		 	 		//Workaround IE6 ENDE
		 			$("ajaxLoader").hide(); $$("div.overlay")[0].show();
		 			Effect.Appear( $("detailDummy"), { duration: 0.3 } );
		 			
		 			new Ajax.Updater( 
		 				$("ajaxLoader"), 
		 				Core.url("shop/prodDetail/show/"+index[1]), 
		 				{ method:'get', onComplete:function(){ prodDetailView.init(); }}
		 			);
		 			
					new PeriodicalExecuter(function(pe) {
	  					if ( prodDetailView.ready ) {
	  						prodDetailView.ready = false;
	  						$("detailDummy").hide();
	  						$("ajaxLoader").show();
	    					pe.stop();
	  					}
					}, 0.3);

		 		});
			} 			
 		}
 		
// 		$A( $$("#pictureChooser a") ).each( function(item) {
// 
// 			var regexp = /shop\/prod\/([0-9]*)\/1\/([0-9]*)/;
//			var index = regexp.exec(item.href);	item.writeAttribute("href",null); 
//			item.addClassName("pointer");
//			//$(item).setAttribute('color',index[2]);
//			
// 			$(item).observe('click', function(event) {
// 				$("mainImage").hide(); $("loaderbig").show();
//				$("mainImage").src = item.src;
//				console.log(item);//$("mainImage").src = ;
// 				//prodDetail.attr.set("1", this.getAttribute("color"));
// 				
// 				//prodDetail.update();
// 			});
// 		});
 		/*	
 			$A( $$("#pictureChooser a") ).each( function(item) {

 			var regexp = /shop\/prod\/([0-9]*)\/1\/([0-9]*)/;
			var index = regexp.exec(item.href);	item.writeAttribute("href",null); 
			item.addClassName("pointer");
			//$(item).setAttribute('color',index[2]);
			
 			$(item).observe('click', function(event) {
 				//$("mainImage").hide(); $("loaderbig").show();
 				var bi = item.readAttribute('bigimage');
 				if(bi) {
 					$("mainImage").src = item.readAttribute('bigimage');
 				}
				//console.log(item);$("mainImage").src = "/isproductvendor/content/images/products/174/2388500_000_345x345.jpg";
				//$("loaderbig").hide();
 				//prodDetail.attr.set("1", this.getAttribute("color"));
 				
 				//prodDetail.update();
 			});
 		});
 		*/


 		//////////////////////////////////////////////
 		
 		$$("select.detailSelect").each(function(item) {
 			if(item.id!="amountSelect") {
 				$(item).observe('change', function(event) {
 					var regexp = /cart_add_variation_([0-9]*)/;
 					attrId = parseInt( regexp.exec( this.name)[1] );
 					value = parseInt( this.value );
 					if(!isNaN(value)) {
 						prodDetail.attr.set(attrId, value);
 					}
 					prodDetail.update();
 				});
 			}
 		});

 		//verfügbarkeitsbutton nicht zeigen
 		Element.replace("mysubmit2", "");
 		
 		Element.replace("mysubmit", "<a id='mysubmit'>"+$("mysubmit").value+"</a>");
 		
 		Element.replace("mysubmit3", "<a id='mysubmit3'>"+$("mysubmit3").value+"</a>");
 		
 		if($("availability")==null || $("availability").value==1) {
 		
	 		$("mysubmit").observe('click', function(event) {
	 			
	 			var url = Core.url("cart/add/"+prodDetail.prodId+"/");
	 			var amount = parseInt( $("amountSelect").getValue() );
	 			if(isNaN(amount)) amount=1;
	 			url += String(amount);
	 			
	 			prodDetail.attr.each( function(item) {
	 				url += "/"+item[0]+"/"+item[1];
	 			});
	
	 			regexp = /-1/;
	 			var erg = regexp.exec(url);
	 			if( erg!=null )
	 			{
	 				return;
	 			}
	 			
	 			new Ajax.Request(url, {method:'get', onSuccess:function(transport){ 
	 				new Ajax.Updater( $$("div.moduleShopCart")[0], Core.url("cart/applet"), {method:'get'});
	 				
	 				if(!$("noticeArea").visible()) {
	 					$("noticeArea").update( transport.responseText );
	 					Effect.BlindDown( $("noticeArea") );
	 				
		 				new PeriodicalExecuter(function(pe) {
			  					Effect.BlindUp( $("noticeArea") );
			  					pe.stop();
							}, 5
						);
	 				}
	 			} });
	 		});
 		}
 		
 		$("mysubmit3").observe('click', function(event) {
 			var url = Core.url("cart/wishlistAdd/"+prodDetail.prodId+"/");
			var amount = parseInt( $("amountSelect").getValue() );
	 		if(isNaN(amount)) amount=1;
	 		url += String(amount);
	 			
	 		prodDetail.attr.each( function(item) {
	 			url += "/"+item[0]+"/"+item[1];
	 		});
	
 			new Ajax.Request(url, {method:'get', onSuccess:function(transport){
 				new Ajax.Request(Core.url("cart/wishlistApplet"),{
						method:'get',
						onSuccess:function(trans){
							var re = /<span id="wishlist_product_count">([.\s\S]*)<\/span>/g;
							var r = trans.responseText.match(re);
							$("wishlist_product_count").replace(r[0]);
						}
					});

				if(!$("noticeArea").visible()) {
					$("noticeArea").update( transport.responseText );
					Effect.BlindDown( $("noticeArea") );
	
					new PeriodicalExecuter(function(pe) {
							Effect.BlindUp( $("noticeArea") );
							pe.stop();
						}, 5
					);
 				}
 			} });
 		});
 		
 		this.fixUrl();
 	},
 	
 	fixUrl:function(){
 		var a = $('prodDetails').select('div.detailRow')[2].select('a');
 		if(a) {
 			for(var i = 0; i<a.length;i++) {
 				var el = a[i];
 				var elHref = el.readAttribute('href');
 				if(elHref) {
 					var currPath = window.location.pathname;
 					var tmp = currPath.split('__');
 					var ctmp = elHref.split('__');
 					var rep = window.location.protocol + '//' + window.location.host + Core.url('/');
 					if(ctmp.length < 2) {
 						var sub = elHref.split(rep).join('/');
 						sub = sub.split(Core.url('')).join('/');
 						a[i].writeAttribute('href',tmp[0]+'__'+Core.filterUrl(sub));
 					}
 				}
 			}
 		}
 	},
 	
 	update: function(url) {
 		url = "shop/prod/"+prodDetail.prodId;
 		prodDetail.attr.each( function(item) {
 			url += "/"+item[0]+"/"+item[1];
 		});
 		url+=outlet;
 		$("mainImage").hide(); $("loaderbig").show();
 		new Ajax.Request( Core.url(url), { method:'get', onSuccess:function(trans){$("prodDetails").replace(trans.responseText);  prodDetail.initialize();} });
 	}
 }

 var productGalleryLastImgId = 0;
 var productGalleryDB = [];
 var productGalleryVariationSelected = false;
 var ProdGallery = Class.create();
 ProdGallery.prototype = {
 	
 	ready:false,
 	lastImgId:0,
 	lastProductId:0,
 	
 	initialize:function(){
 		
 	},
 	
 	init:function() {
 
 		this.ready = true;
 		
 		$("bigloaderImage").hide();
 		$("bigImage").show()
 		
 		var item = $("galleryClose");
 		if(item) { 		
 			item.writeAttribute("href",null);
 			item.addClassName("pointer");
	 		$(item).observe('click', function(event) {
	 			productGalleryVariationSelected = false;
	 			Effect.Fade($("ajaxLoader"), { duration: 0.3 });
	 			Effect.Fade($$("div.overlay")[0], { duration: 0.4 });
	 			//Workaround IE 6 (selectboxen wieder anzeigen wenn overlay geschlossen wird)
	 			$A( $$("select") ).each( function(item)
	 		 	{
	 		 		(item).show();
	 		 	});
	 	 		//Workaround IE 6 Ende
	 		});
 		}
 		//lastImgId
 		$A( $$("a.thumbs") ).each( function(item) {	
	 		$(item).observe('mouseover', function(event) {	

				if(productGalleryVariationSelected) return;
			
	 			var e = event || window.event;
	 			Event.stop(e);
	 			
	 			var product_id = $(this).readAttribute('product_id');
	 			var image_id = $(this).readAttribute('image_id');
	 			
				
	 			if(productGalleryLastImgId != image_id) {
					
 					productGalleryLastImgId = image_id;
	 				if(!productGalleryDB[image_id]) {
						
						$("bigloaderImage").show();
 						$("bigImage").hide();
						
			 			new Ajax.Updater( $("ajaxLoader"), Core.url("/shop/prodGallery/show/"+product_id+"/"+image_id), { method:'get',
			 				onComplete:function(r) {
			 					prodGallery.init();
			 					productGalleryDB[image_id] = r.responseText;
			 				}
			 			});
	 				} else {
	 					$("ajaxLoader").update(productGalleryDB[image_id]);
	 					prodGallery.init();
	 				}
	 			}
	 		});
	 		$(item).observe('click', function(event) {
	 			var e = event || window.event;
	 			Event.stop(e);
	 			
	 			var product_id = $(this).readAttribute('product_id');
	 			var image_id = $(this).readAttribute('image_id');
	 			
	 			if(isNaN(product_id*1) || isNaN(image_id*1)) {
	 				return;
	 			}
	 			
				productGalleryLastImgId = image_id;
				
				$("bigloaderImage").show();
 				$("bigImage").hide();
				
	 			new Ajax.Updater( $("ajaxLoader"), Core.url("/shop/prodGallery/show/"+product_id+"/"+image_id), { method:'get',
	 				onComplete:function() {
	 					prodGallery.init();
	 					productGalleryVariationSelected = true;
	 				}
	 			});
	 		});
 		});
 	}
 }
 
 
 var ProdDetailView = Class.create();
 ProdDetailView.prototype = {
 	
 	ready:false,
 	
 	initialize:function(){
 		
 	},
 	
 	init:function() {
 				
 		var closer = [$("detailsClose1"),$("detailsClose2")];
 		
 		$A( closer ).each( function(item) {
 			item.writeAttribute("href",null);
 			item.addClassName("pointer");
	 		$(item).observe('click', function(event) {	
	 			Effect.Fade($("ajaxLoader"), { duration: 0.3 });
	 			Effect.Fade($$("div.overlay")[0], { duration: 0.3 });
	 			//Workaround IE 6 (selectboxen wieder anzeigen wenn overlay geschlossen wird)
	 			$A( $$("select") ).each( function(item)
	 		 	{
	 		 		(item).show();
	 		 	});
	 	 		//Workaround IE 6 Ende
	 		});
 		});
 		
 		this.ready = true;
 	}
 } 
 
 
 Event.observe(window,'load',function() {
  	prodDetail = new ProdDetail();
  	prodDetailView = new ProdDetailView();
	prodGallery = new ProdGallery();
	
	// Am ende noch einen DIV Container anhängen für AJAXausgabe
	newDiv = document.createElement('div');
	newDiv.id = "ajaxLoader";
	document.body.appendChild(newDiv);
	
	newDiv = document.createElement('div');
	newDiv.id = "galleryDummy";
	document.body.appendChild(newDiv);
	
	newDiv = document.createElement('div');
	newDiv.id = "detailDummy";
	document.body.appendChild(newDiv);
	
	$("ajaxLoader").hide();
	
	$("galleryDummy").hide();
	new Ajax.Updater( $("galleryDummy"), Core.url("shop/prodGallery/aload"), { method:'get'} );
	
	$("detailDummy").hide();
	new Ajax.Updater( $("detailDummy"), Core.url("shop/prodDetail/aload"), { method:'get'});
 });
