window.addEvent('domready',function(){	
	var ServiceCart = new Class({
		
		Implements: [Options, Events],
		
		options:{
			group: null
		},
		
		initialize: function(options){
			this.setOptions(options);
			this.form = $('service-form');
			this.cart = $('service-cart');
			this.cartSum = $('service-cart-sum');
			this.cartOrder = $('service-cart-order');
			this.data = $H();
			this.inCart = $H();
			if(this.options.group) this.parseGroup();
			this.form.getElements('.service-list-row').each(function(row){
				row.getElements('.service-sub-item tr').each(function(item){
					var id = item.get('sid');
					this.addItem(id, item);
					item.getElement('select').addEvent('change', this.updateItem.bind(this, [id]));
					item.getElement('.btn').addEvent('click', this.addToCart.bindWithEvent(this, [id]));
				}, this);
			}, this);
			this.cartOrder.addEvent('click', this.makeOrder.bind(this));
		},

		addItem: function(id, item){
			this.data[id] = this.data[id] || {};
			var select = item.getElement('select');
			$extend(this.data[id], {
				price: select.options[select.selectedIndex].innerHTML,
				disabled: item.getElement('.btn').hasClass('btn-disabled'),
				name: item.getElement('.service-sub-title .bg-bd').get('text'),
				item: item
			});
			return this;
		},
		
		updateItem: function(id){
			if(!this.data[id]) return false;
			var item = this.data[id]['item'],
				select = item.getElement('select');
			$extend(this.data[id], {
				price: select.options[select.selectedIndex].innerHTML,
				disabled: item.getElement('.btn').hasClass('btn-disabled')
			});
			return this;
		},
		
		parseGroup: function(){
			this.group = this.group || {};
			for(var id in this.options.group){
				this.group[id] = this.group[id] || {'impact': [], 'require': []};
				this.options.group[id].each(function(item){
					this.group[item] = this.group[item] || {'impact': [], 'require': []};					
					this.group[id].require.push(item);
					this.group[item].impact.push(id);
				},this);
			};
		},
		
		updateGroup: function(id, dir){
			if(!this.group[id] || !this.group[id]['impact']) return;
			if(dir > 0){
				this.group[id]['impact'].each(function(impact){
					var bool = this.group[impact]['require'] && this.group[impact]['require'].every(function(require){
						return this.inCart.has(require);
					}, this);
					if(bool) this.updateBtn(impact, dir);
				}, this);
			}else{
				this.group[id]['impact'].each(function(impact){
					this.updateBtn(impact, dir);
					if(this.inCart.has(impact)) this.removeCart(null, impact);
				},this);
			}
		},
		
		updateBtn: function(id, st){
			var btn = this.data[id].item.getElement('.btn');
			(st > 0) ? btn.removeClass('btn-disabled') : btn.addClass('btn-disabled');
			this.updateItem(id);
		},

		addToCart: function(e, id){
			e.stop();
			if(this.data[id]['disabled'] || ($defined(this.inCart[id]) && this.inCart[id].get('text').contains(this.data[id].price))) return;
			if(!this.cartShow) this.showCart();
			this.inCart[id] = $defined(this.inCart[id]) ? this.inCart[id].highlight() : new Element('li', {styles: {background:'#D1EFD8'}}).inject(this.cart.getElement('ul'));
			this.inCart[id].set('html', this.data[id].name + ' ' + this.data[id].price);
			new Element('span', {'class': 'icon del-cart', 'html': '删除'}).addEvent('click', this.removeCart.bindWithEvent(this, id)).inject(this.inCart[id]);
			this.updateGroup(id, 1);
			this.updateCart();
			return this;
		},

		removeCart: function(e, id){
			if(e) e.stop();
			if(!this.inCart[id]) return false;
			this.inCart[id].dispose();
			this.inCart[id] = null;
			delete this.inCart[id];
			this.updateGroup(id, -1);
			this.updateCart();
			return this;
		},

		showCart: function(){
			this.form.retrieve('notice', $$(this.form.getElements('.notice-inline'), this.form.getElements('.notice-info')).setStyle('display', 'none'));
			this.cart.setStyle('display', 'block');
			this.cartShow = true;
			return this;
		},
		
		updateCart: function(){
			var sum = 0;
			this.inCart.getKeys().each(function(id){
				sum += this.data[id].price.toInt();
			},this);
			this.cartSum.set('html', sum + '元');
			return this;
		},
		
		makeOrder: function(){
			//ajax todo
			
			this.form.getElements('.btn').each(function(el){
				el.addClass('btn-disabled');
			});
			this.cart.innerHTML = '<table width="100%" cellpadding="3" cellspacing="0">\
									<tr>\
									<td>订单生成成功，请付款，付款后即可使用所购买的功能。<br />\
									订单号 <span class="c-orange">82775345</span>, 总价 <span class="f-14b c-orange">580元</span></td>\
									<td width="110" style="padding:10px"><a class="btn" href="#" id="service-cart-order"><span>支付宝付款</span></a></td>\
									</tr>\
									</table>';
		}
	});
	
	var serviceCart = new ServiceCart({
		group:{
			102: ['101'],
			103: ['101', '102']
		}
	});

});

