var Basket = new Class({
	Implements: [Events, Options],					   
	
	options : {
		
		basketviewer : 'basketviewer',
		notification: {
			fx: Fx.Transitions.Sine.easeOut
		}
	},
	
    initialize: function(options) {
		this.setOptions(options);
		this.url= '/floor/cs?category_ident=basket&template=basketviewer&basketdetail=margin';
		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() {
		this.urltmp = '/floor/cs?server=mobilis&lang='+this.options.lang+'&category_ident=basket&template=basketviewer&basketdetail=margin';
		
		var url = this.urltmp+'&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'+pid).toQueryString()+'&randomnumber='+Math.random();
		
		if (this.options.notification) {
			//console.log('show notification'+pid);
			//var notificationlLabel = $('item'+pid).getElement('.product .name').get('html');
			var notificationlLabel = '';
			if ($('item'+pid))
				notificationlLabel = $('item'+pid).getElement('.name').get('html');
			else if ($('attach'+pid))
				notificationlLabel = $('attach'+pid).getElement('.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(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);
	}
});



