var Basket = new Class({
	Implements: [Events, Options],					   
	
	options : {
		scriptname : '/floor/cs',
		basketdetail : 'margin',
		basketviewer : 'basketcontainer',
		basketident: 'basket',
		server : 'pamisa',
		notification: {
			fx: Fx.Transitions.Sine.easeOut
		}
	},
	
    initialize: function(options) {
		this.setOptions(options);
		
		this.url=this.options.scriptname;
		this.url=this.url+'?category_ident='+this.options.basketident;
		this.url=this.url+'&template=basketmargin';
		this.url=this.url+'&basketdetail=margin';
		this.url=this.url+'&server=pamisa';
		
		this.flag = true;
		this.get();
		
		if (this.options.notification) { this.buildNotification(); };
		
		this.plusminus();
		this.manageCCBox();
    },
	
	buildNotification : function() {
		var top = window.getScrollTop()+(window.getHeight()/2)-105;
		var left = window.getScrollLeft()+(window.getWidth()/2)-103;
		
		this.notification = new Element('div', {
			'class': 'growl',
			styles : {
				top : top,
				left:left,
				opacity: 0
			}
		}).inject(document.body);
		
		this.title = new Element('h3').inject(this.notification);
		this.text = new Element('p').inject(this.notification);
		
		
	},
	
	get : function() {
		var url = this.url;
		url = url+'&server='+this.options.server;
		url = url+'&lang='+this.options.lang;
		url = url+'&randomnumber='+Math.random();
		
		var req = new Request.HTML({
			url: url,
			method: 'get',
			update: $(this.options.container)
		}).send();
	},
	
	add : function(pid, price) {
		
		if (this.flag == false) {
			return false;
		}
		
		this.flag = false;
		
		var qty = $('input'+pid).value;

		var url = this.url+'&qty='+qty+'&'+$('prd_form').toQueryString()+'&randomnumber='+Math.random();
		
		if (this.options.notification) {
			//console.log('show notification'+pid);
			//var notificationlLabel = $('item'+pid).getElement('.product .name').get('html');
	
			var notificationlLabel = $(document.body).getElement('.product .name').get('html');
	
			notificationlLabel = notificationlLabel + "<br />CHF " + price;
			
			this.title.set('html','ajout au panier');
			this.text.set('html',notificationlLabel);
			
			var top = window.getScrollTop()+(window.getHeight()/2)-105;
			var left = window.getScrollLeft()+(window.getWidth()/2)-103;
			
			this.notification.setStyles({
				opacity:'0.75',
				top: top,
				left: left,
				width: '160px',
				height:'100px',
				fontSize: '1em'
			});
							
			var pos = $(this.options.basketviewer).getCoordinates();
	
			var fx = new Fx.Morph(this.notification, {
				duration: 'normal', 
				transition: this.options.notification.fx,
				onComplete: function() {
					this.flag = true;		
				}.bind(this)
			});
			
			(function(){ 
				fx.start({
					top: pos.top, 
					left: pos.left,
					width: pos.width, 
					height: pos.height,
					fontSize: '1em',
					opacity: 0
				});
			}).delay(500);
		}
		
		var req = new Request.HTML({
			url : url,
			update: $(this.options.container), 
			method: 'get',
			onComplete : function() {
				var highlight = new Fx.Tween($('basketviewer'),{duration:'long'});
				highlight.set('color','#ffa800');	
				highlight.start('color','#000');	
			}.bind(this),
			
			onFailure : function() {
				alert('Erreur');
			}
		}).send();

		return false;
	},
	
	del : function(bid) {
		var url = this.url+'&subact=delfrombasket&basketdetailID='+bid+'&randomnumber='+Math.random();
	
		var req = new Request.HTML({
			url: url, 
			update: $(this.options.container), 
			method: 'get'
		}).send();	
	},
	
	plusminus : function() {
		var plus = $$('.plus');
		plus.forEach(function(el){
			el.addEvent('click', function() {
				var el_ID = el.id.replace(/^p_/, '');
				$('input'+el_ID).value = $('input'+el_ID).value.toInt() + 1;
			});
		});
		
		var minus = $$('.minus');
		minus.forEach(function(el){
			el.addEvent('click', function(){
				var el_ID = el.id.replace(/^m_/, '');						  
				if ($('input'+el_ID).value != 1){
					$('input'+el_ID).value = $('input'+el_ID).value.toInt() - 1;
				}
			});
		});	
	},
	
	manageCCBox: function(){
		if ($('cc_box')) { this.ccbox_slide = new Fx.Slide('cc_box'); }
		
		$$('.open_ccbox').forEach(function(el){
			el.addEvent('click', function(){
				this.ccbox_slide.slideIn();
				
				$$('.cc_req').forEach(function(el_box){
					var lbl_class = el_box.getProperty('class');
					el_box.setProperty('class',lbl_class.replace(/novalidate/g,"validate"));		
				},this);
				
				order_formcheck.validations = [];
				order_formcheck.checkElements();
			
			}.bind(this));
		},this);
		
		$$('.close_ccbox').forEach(function(el){
			el.addEvent('click', function(){
				
				this.ccbox_slide.slideOut();
				
				$$('.cc_req').forEach(function(el_box){
					//el_box.removeClass("required");
					var lbl_class = el_box.getProperty('class');
					el_box.setProperty('class',lbl_class.replace(/validate/g,"novalidate"));
					el.validation = [];	
				},this);
				
				order_formcheck.validations = [];
				order_formcheck.checkElements();							
			}.bind(this));
		},this);
	}
});



function addToBasket(pid,cid,lng)	{

	//$('basketloader').style.height = Element.getDimensions("basketmargin").height;

	var url = '/floor/cs?server=pamisa&lang='+lng+'&category_ident=basket&template=basketmargin&basketdetail=margin&subact=addtobasketpamisa&item_categoryID='+cid+'&item_ID='+pid+'&randomnumber='+Math.random();
	var myAjax = new Request.HTML( {
		update : 'basketcontainer',
		url :url, 
		method: 'get'
	}).send();
}
