var SSP = {

	Settings: {
		Version: 0.1,
		/*Turns on logging which is displayed in the error or javascript console*/
		logging: true
	},

	/*Global Variables */
	browser_id: '',
	view:	'',

	/*
	 * Function for displaying messages in the browser's script console. Function 
	 * is used throughout 
	 * Values-
	 * message: any message to display
	 * */
	log: function(message) {
		if (!SSP.Settings.logging) {
			return;
		}
		if (!window.console) {
			return;
		}
		window.console.log(message);
	},

	init: function() {
		SSP.browser_id = navigator.appName;
		SSP.log('SSP has been built');
		SSP.log('SSP Initiated for ' + SSP.browser_id);
		SSP.setView();
		
				

		
		/*Starts the init function of the current view.*/
		if (SSP[SSP.view]) {
			SSP[SSP.view].init();
		}

		/*Observes all clicks and changes to the version list element on all pages*/

		$('ssp-wrapper').observe('click', SSP.eventHandler.bindAsEventListener(SSP.eventHandler));

	},


	/*Gets view from the ssp-content element and sets it in javascript*/
	setView: function() {
		SSP.log('SSP.setView Initiated!');
		var view = $('ssp-content').className;
		view = $A(view.split('-')).last().capitalize();
		SSP.view = view;
		SSP.log('SSP.setView : '+view);
		return view;
		
	},
	
	removeClasses: function(el){
		$w(el.className).each( function(c){
			el.removeClassName(c);
		});
	},

	/*Function that sorts number arrays as non strings*/
	sortNumber: function(a, b) {
		return a - b;
	},
	
	/*Funtion that disables submiting the form via a enter or return key press*/
	
	disableSubmit: function(evt) {
		if (evt.metaKey || evt.ctrlKey) {
			return;
		}

		var key_code = (evt.which) ? evt.which: evt.keyCode;

		SSP.log("Code for key pressed: " + key_code);
		var e = event.findElement("input", "textarea","option");
		if (key_code == 13) {
			evt.preventDefault();
			e.blur();

			var inputs = $$("input", "textarea","option","select");
			var i = inputs.indexOf(e);

			if (i == inputs.length - 1) {
				inputs[0].focus();
			} else {
				inputs[i + 1].focus();
				inputs[i + 1].select();
			}
		}
	},
	
	/*Function that determines the event observed and calls the appropriate event handler */
	eventHandler: function(evt) {
		var name = evt.type;
		var eID = evt.findElement();

		SSP.log('EVENT of type ' + name + ' on element with id ' + eID.identify() + '');
		if (SSP[name + "Handler"]) {
			SSP[name + "Handler"](evt);
		}


	},
	/*Function that handles all click events*/
	clickHandler: function(event) {
		var eTarget = event.target;
		var clss = $w(eTarget.className);

		SSP.log('SSP click Observed! element: ' + eTarget.identify() + '');
		SSP.log('Class on element: : ' + clss + '');

		/*Looks at the classes for a particular event element and calls the appropriate click handler*/
		clss.each(function(cls) {

			if (SSP.clickHandlers[cls]) {
				SSP.clickHandlers[cls](event);
			}
			
			if (SSP[SSP.view].clickHandlers) {
				if (SSP[SSP.view].clickHandlers[cls]) {
					SSP[SSP.view].clickHandlers[cls](event);
				}
			}
			
		});

	},
	clickHandlers: {
		"ssp-more_files": function(event){
			copy = $('ssp-art_1').clone(true);
			SSP.Quote.artCount = SSP.Quote.artCount+1;
			suffix = "art_"+SSP.Quote.artCount;
			newID = "ssp-"+suffix;
			order = "";	
			sibCount = $$("#ssp-get_a_quote .ssp-art").size();
			SSP.log("currently have "+sibCount+' file upload fields');		
			copy.writeAttribute("id", newID);
			if (sibCount%2){
				order = "ssp-0";
			}else{
				order = "ssp-1";
			}
			copy.addClassName(order);
			SSP.Quote.fields.push("upload_"+suffix);
			SSP.log(SSP.Quote.fields);
			(copy.down('#ssp-upload_art_1')).writeAttribute("name","upload_"+suffix);
			(copy.down('#ssp-upload_art_1')).writeAttribute("id","ssp-upload_"+suffix);
			
			(copy.down("select[name]=right_chest_art_1")).writeAttribute("name","right_chest_"+suffix);
			
			(copy.down("select[name]=left_chest_art_1")).writeAttribute("name","left_chest_"+suffix);
			(copy.down("select[name]=center_chest_art_1")).writeAttribute("name","center_chest_"+suffix);
			(copy.down("select[name]=center_back_art_1")).writeAttribute("name","center_back_"+suffix);
			
			(copy.down("select[name]=left_sleeve_art_1")).writeAttribute("name","left_sleeve_"+suffix);
			(copy.down("select[name]=right_sleeve_art_1")).writeAttribute("name","right_sleeve_"+suffix);
			
			
			$("ssp-quanities").insert({
						before: copy
					});
			$(newID).scrollTo();
		}
	}
};


SSP.Home = {
	init: function() {
	SSP.log('Home Initiated and observing');
	},
	
	startObservers: function() {
	
	}
};

SSP.Quote = {
	artCount: '1',
	fields: ['upload_art_1'],
	quantity: parseInt($F('ssp-total')),

	init: function() {
		SSP.Quote.startObservers();
		SSP.Quote.getArtCount();
		SSP.log('Quote Initiated and observing');
		SSP.log('Current Quanities: '+SSP.Quote.quantity);
	},
	clickHandlers: {
		"ssp-remove": function(event){
			//find parent element
			e = event.findElement('.ssp-art');
			
			//reset all siblings to
			art = $(e).siblings(); 
			art.each( function(el){
				if(el.identify() != 'ssp-art_1' && el.hasClassName('ssp-art')){
					SSP.removeClasses(el);
					i = art.indexOf(el);
					if (i%2 == 0){
						el.addClassName('ssp-art ssp-0');
						
					}else{
						el.addClassName('ssp-art ssp-1');
					}
				}
				e.addClassName('ssp-art');
			});
			//remove clicked parent
			upload = e.down('input[type="file"]');
			i = SSP.Quote.fields.indexOf(upload.readAttribute('name'));
			SSP.Quote.fields.splice(i, 1);
			SSP.log(SSP.Quote.fields);
			e.fade();
			e.remove();
			
		}
	},
	getArtCount: function(){
		SSP.Quote.artCount = $$('#ssp-get_a_quote .ssp-art').size();
				
	},
	startObservers: function() {
		$$('#ssp-quanities .ssp-third input').invoke("observe", 'change', SSP.Quote.setTotalQuantity.bindAsEventListener(SSP.Quote.setTotalQuantity));
		$$('#ssp-quanities .ssp-third input').invoke("observe", 'keyup', SSP.Quote.setTotalQuantity.bindAsEventListener(SSP.Quote.setTotalQuantity));
		$$('#ssp-get_a_quote input').invoke("observe", 'keypress', SSP.disableSubmit.bindAsEventListener(SSP.disableSubmit));					
					
	},
	setTotalQuantity: function(e){
		SSP.log(e.type+': Quantity change observed!');
		var num = 0;
		$$('#ssp-quanities .ssp-third input').each(function(e){
			if(parseInt(e.value) > 0){
				num = parseInt(e.value) + num;
			}
		});
		
		SSP.log(num);
		SSP.Quote.quantity = num;
		SSP.log('Change Quanities to: '+num);
		$('ssp-total').value = SSP.Quote.quantity;
	},
	getFields: function(){
		$$('fieldset.ssp-art input[type="file"]').each(function(el){
			
		});
	}
};




/*Calls SSP.init function when this js document is loaded
  * 
  */
document.observe('dom:loaded', SSP.init);
