(function($){
	
 this.submitLink = {
						mpc: '',
						cpc: 'unlimited-text',
						payType : '',
						handset:'',
						isBusiness: function(){
							return $('buy-now-btn').hasClass('business');
							},
		isUpgrade: function(){
			return $('buy-now-btn').hasClass('upgrade');
		},
						url: function(){
							isBusiness = this.isBusiness();
			if(this.isUpgrade()){
				return	'/service/upgrade/' + 'startHandsetCheckout?phoneId=' + this.handset +'&selectedAccessory=';
				
			} else if(isBusiness){
								var initUrl = '/shop/business/';
							}else {
									var initUrl = '/shop/';
								};
							if(this.payType == "pay-as-you-go"){
				return	initUrl + this.payType + '/' + this.handset + '/voice-reward';			
							}else{
				//return	initUrl + this.payType + '/' + this.handset + '/' + this.mpc + '/'+ this.cpc;		
				return	initUrl + 'pay-monthly/' + this.handset + '/' + this.mpc + '/'+ this.cpc;	
							};	
								

						},
						offerId : '',
						insurance : '',
						rel : function(){
							var selectedCSOs = '{';

							//selectedCSOs += 'offerId:"' + this.offerId +'"';
							if (this.insurance != ""){
									if(this.offerId != ""){
										selectedCSOs +=', insurance:"' + this.insurance + '"'; 
									}
									else{
										selectedCSOs +='insurance:"' + this.insurance  + '"'; 
									}
								}
								return selectedCSOs + '}';
							 }
						
	};
					 
	/* class for initializing select option boosters for business */
	this.BizSelectBooster = new Class({
		Implements:[Events, Options],
		options:{
			submitButton:'#buy-now-btn.iphone',
			cpc:'business-3000-texts',
			mpc:''
		},
		initialize: function(elementID, options){
			if($(elementID)){
				this.setOptions(options);
				submitLink.cpc = $(elementID).get('value') || this.options.cpc;
				this.submitButton = $$(this.options.submitButton);
				$(elementID).addEvent('change',function(event){
					submitLink.cpc = event.target.get('value');
					this.submitButton.set('href',   submitLink.url()+ '/extras/');
				}.bind(this));
			}
 				}
	});
	
	/* class for initializing radios */
	this.Radios = new Class({
		Implements:[Events, Options],
		options:{
			radios:'',
			labels:'',
			csshover:'',
			cssactive:'',
			submitButton:'#buy-now-btn.iphone',
			cpc:'',
			mpc:''
			
		},
		initialize: function(options){
			this.setOptions(options);
			this.radios = $$(this.options.radios);
			this.labels = $$(this.options.labels);
			this.submitButton = $$(this.options.submitButton);
			this.obj1 = this.options.obj1;
			this.radios.each(function(ele, index){
				if(ele.get('checked')){ 
					this.labels[index].addClass(this.options.cssactive);
				}				
			}, this);
			
			this.labels.addEvents({
				'click': this.labelClick.bind(this),
				'mouseover': this.labelOver.bind(this),
				'mouseout': this.labelOut.bind(this)
			});
			
		},
		labelClick: function(event){
			event.stop()

			var target = $(event.target).getParent('label') || $(event.target);
			var index = this.labels.indexOf(target);

			this.labels.removeClass(this.options.cssactive);
			target.addClass(this.options.cssactive);

// start of payg button links
if(target.hasClass('payg-radio'))
{
	submitLink.cpc = this.radios[index].get('id');
	submitLink.handset =  this.radios[index].get('id');
	submitLink.payType = "pay-as-you-go";	
	this.submitButton.set('href',   submitLink.url()+ '/extras/');
				}
	
// end of payg button links
			if(!target.hasClass('label-handset'))  
			{
				submitLink.cpc = this.radios[index].get('id');
				
				
			if(!submitLink.isUpgrade()){
				this.submitButton.set('href',   submitLink.url()+ '/extras/');
			}else{
				this.submitButton.set('href',   submitLink.url());
				};
			
			//	submitLink.offerId = target.getChildren('input').get('value');
				//this.submitButton.set('rel', submitLink.rel() );
			}else{

				submitLink.handset =  this.radios[index].get('id');
				submitLink.payType = this.radios[index].getParent().getParent().getParent().getParent().get('id');				
				}
			// start of business unlimited
				if(target.hasClass('bizunlimited-radio'))  {
					submitLink.payType = "busniess-unlimited";	
					this.submitButton.set('href',   submitLink.url()+ '/extras/');
					};

			this.radios.set('checked', '');
			this.radios[index].set('checked', 'checked');
		},
		labelOver: function(event){	
			var target = $(event.target).getParent('label') || $(event.target);
			target.addClass(this.options.csshover)
		},
		labelOut: function(event){ 
			var target = $(event.target).getParent('label') || $(event.target);
			target.removeClass(this.options.csshover)
		},
		resetRadios: function(){
			this.labels.removeClass(this.options.cssactive);
			this.radios.set('checked','');	
		},
		setIndex: function(index){
			this.labels.each(function(ele, ind){
				if(index ===  ind){
					ele.addClass(this.options.cssactive);
					this.radios[ind].set('checked','checked');
				} else {
					ele.removeClass(this.options.cssactive);
					this.radios[ind].set('checked','');
				}						  
			}, this)
		}
	});
	
	
	/* class for initializing radios on table rows */
	this.RadioTable = new Class({
		Extends: this.Radios,
		options:{
			rows:'',
			rowhover:'',
			rowactive:''
		},
		initialize: function(options){
			this.parent(options);
			this.rows = $$(this.options.rows);
			this.labels.removeEvents();
			
			this.radios.each(function(ele, index){
				if(ele.get('checked')){ 
					this.rows[index].addClass(this.options.rowactive);
				}				
			}, this);
			
			this.rows.addEvents({
				'click': this.rowClick.bind(this),
				'mouseover': this.rowOver.bind(this),
				'mouseout': this.rowOut.bind(this)								
			});
			
		},
		rowClick: function(event){
			event.stop()
			var target = $(event.target).getParent('tr') || $(event.target);
			var index = this.rows.indexOf(target);

			this.rows.removeClass(this.options.rowactive);
			target.addClass(this.options.rowactive);

			this.radios.set('checked', '');
			this.radios[index].set('checked', 'checked');

			submitLink.mpc = this.radios[index].get('id');
			//alert(submitLink.isUpgrade())
			if(!submitLink.isUpgrade()){
			this.submitButton.set('href',   submitLink.url()+ '/extras/');
			}else {
				this.submitButton.set('href',   submitLink.url());
				};
			
			
			
			this.labels.removeClass(this.options.cssactive);
			this.labels[index].addClass(this.options.cssactive);
		},
		rowOver: function(event){				
			var target = $(event.target).getParent('tr') || $(event.target);
			var index = this.rows.indexOf(target);
			target.addClass(this.options.rowhover);
			this.labels[index].addClass(this.options.csshover);
		},
		rowOut: function(event){ 
			var target = $(event.target).getParent('tr') || $(event.target);
			var index = this.rows.indexOf(target);
			target.removeClass(this.options.rowhover);
			this.labels[index].removeClass(this.options.csshover);
		}
	});
	
	/* messaging class - options are for page elements and animation properties */
	this.Messaging = new Class({
		Implements: Options,
		options: {
			'selectphone': 'select-phone',
			'selectplan': 'select-plan',
			'pmselection': 'pm-selection',
			'paygselection': 'payg-selection',
			'props': {duration:500, transition:'quad:in:out'}
		},
		initialize: function(options){
			this.setOptions(options);
			this.elements = new Hash({
				'selectphone': $(this.options.selectphone),
				'selectplan': $(this.options.selectplan),
				'pmselection': $(this.options.pmselection),
				'paygselection': $(this.options.paygselection)						
			});
			this.elements.each(function(val, key){
				val.set('tween', this.options.props);	
			}, this);
		},
		set: function(strkey){
			this.elements.each(function(val, key){
				val.setStyles({
					'opacity': (key==strkey) ? 1 : 0,
					'display': 'block'
				});						
			});				
		},
		display: function(strkey){
			this.elements.each(function(val, key){
				val.tween('opacity', (key==strkey) ? 1 : 0);						
			});	
		}
	});	
	
	

									
									
	/* button class - sets state for button and submits form */
	this.ButtonState = new Class({
		Implements:Options,
		options:{
			csson:'action-btn-magenta',
			cssoff:'action-btn-disabled',
			form:'ProductDetailsForm'
		},
		initialize: function(element, options){
			this.setOptions(options);
			this.form = $(this.options.form);
			this.element = $(element);
			this.element.addEvent('click', function(event){
				event.stop(); 
				var target = $(event.target).getParent('a') || $(event.target);
				if(!target.hasClass(this.options.cssoff)){
					// this.form.submit();

								//	alert(obj.toQueryString());
						//	var miniBasketUrl = '/shop/miniBasketAction/';
							//addToBasket(miniBasketUrl + href);
							//alert(target.get('href'));



							//window.location =  target.get('href');
						//	href = target.get('href');
						//	this.form.set('action', target.get('href'));
							
						//	this.form.submit();
						//	this.form.send();
							
							/*var postSelectedData = new Request.HTML({
								url:href,
								method: 'get',
								onSucess: function(){
										alert("Done");
									}
							});
							postSelectedData.send();*/
							//this.form.send();
							//this.form.submit();
							//window.location(href);
								
				} 
			}.bind(this));
		},
		set: function(state){
			if(state=='disabled'){
				this.element.addClass(this.options.cssoff);
				this.element.removeClass(this.options.csson);
			} else {
				this.element.removeClass(this.options.cssoff);
				this.element.addClass(this.options.csson);
			}
		}
	})
	
	/* form class - sets form based on options selected on interface */
	this.FormState = new Class({
		Implements: Options,
		options:{
			inputBundleTypeGroup: 'bundleTypeGroup',
			inputCpc:'tariffName',
			inputMpc:'handsetName',
			inputOfferId:'offerId',
			inputPaymentType:'paymentType'
		},
		initialize: function(options){
			this.setOptions(options);
			this.paymentType = this.tariff = this.handset = this.offerId = false;
			
			this.inputBundleTypeGroup = $(this.options.inputBundleTypeGroup);
			this.inputCpc = $(this.options.inputCpc);
			this.inputMpc = $(this.options.inputMpc);
			this.inputOfferId = $(this.options.inputOfferId);
			this.inputPaymentType = $(this.options.inputPaymentType);
		},
		setPaymentType: function(strPaymentType){
			this.paymentType = strPaymentType;
			this.inputPaymentType.set('value', this.paymentType);

			if(this.paymentType == 'pay-as-you-go'){
				this.setTariff('voice-reward');	
			} else {
				this.setTariff('');	
			}
		},
		setHandset: function(strHandset){
			this.handset = strHandset;
			this.inputMpc.set('value', this.handset);
		},
		setTariff: function(strTariff){
			this.tariff = strTariff;
			this.inputCpc.set('value', this.tariff);
		},
		setOfferId: function(strOfferId){
			this.offerId = strOfferId;
			this.inputOfferId.set('value', this.offerId);
		},
		clearForm: function(){
			this.setPaymentType('');
			this.setHandset('');
			this.setTariff('');
			this.setOfferId('');
		}
								 
	})
		  
})(document.id)
