/**
 * Cart/Wishlist
 * @autor:Bernhard Zürn
 */
var Zeroclick = Class.create();
 Zeroclick.prototype = {
		initialize:function(){
		
		this.prepareCart();
		
		var aIcon = Element.extend(Builder.node('img',{src:skinDir+'/images/frontend/icons/addProduct.png'}));
		var pIcon = Element.extend(Builder.node('img',{src:''}));
		var pI = Element.extend(Builder.node('div',{wid:0},[pIcon,aIcon]));
		document.body.appendChild(pI);
		$A($$("div.wishlist")).each( function(item)
		{
			item = Element.extend(item);
			
			var widv = item.down().getAttribute("wid");	
			
			$(item).onmousedown = function(e)
			{
				var event = e || window.event;
				var imgSrc = Event.element(event).getAttribute('src');
				pIcon.setAttribute('src',imgSrc);					
 				pI.setAttribute('wid',widv);

				pIcon.setStyle({
					width:60+'px',
					height:60+'px'
				});

				aIcon.setStyle({
					position:'relative',
					top:'-17px',
					left:'43px'
				});

				pI.setStyle({
					position:'absolute',
					width:60+'px',
					height:60+'px',
					opacity:0.00,
					top:Event.pointerY(event)+10+'px',
					left:Event.pointerX(event)+10+'px'
				});

				var dragObj = new Draggable(pI,{scroll:window,
					'onDrag':function(tt,event) {
						new Effect.Opacity(pI, { from: 0.8, to: 0.00, duration: 2 });
					},
					'onStart':function(){
						Effect.Pulsate('cartApplet');
					},
					'onEnd':function(){
						dragObj.destroy();
						pI.setStyle({
							top:-100+'px',
							left:-100+'px'
						});							
					}
				});	
				dragObj.initDrag(event);

				return false;
			};

		});
	},prepareCart:function(){
		Droppables.add( $("cartApplet"), {
			  onDrop: function(element) { 
				var wid = element.getAttribute("wid");
			  	if(wid!=null) {
				  	new Ajax.Request( Core.url("/cart/wishlist/buy/one/"+wid) , {method:'get', onSuccess:function(transport){ 
				  		
		 				new Ajax.Request(Core.url("cart/applet"),{
		 							method:'get',
		 							onSuccess:function(trans){
		 								var re = /<span id="cart_product_count">([.\s\S]*)<\/span>/g;
		 								var r = trans.responseText.match(re);
		 								$("cart_product_count").replace(r[0]);
		 							}
		 						});
		 				location.reload();

		 				$("noticeArea").update( transport.responseText );
		 				
		 				if(!$("noticeArea").visible()) {
			 				Effect.BlindDown( $("noticeArea") );
			 				
			 				new PeriodicalExecuter(function(pe) {
				  					Effect.BlindUp( $("noticeArea") );
				  					pe.stop();
								}, 5
							);
		 				}
						
		 			} });
			  	}
			  } 
			});
	 	}
 };
 
 var wishlist = Class.create();
 	wishlist.prototype = {
		initialize:function(){
 			//Button zum Speichern nicht anzeigen wenn JS aktiv ist
 			Element.replace("saveButton","");
		}
	};
	
 
 Event.observe(window,'load',function() {
		zeroclick = new Zeroclick();
		wishlist = new wishlist();
	 });