﻿var ProductSelector = Class.create();
ProductSelector.prototype = {
	isFinalLoad: false,

	initialize: function() {
		this.hookupSelectors();
	},

	hookupSelectors: function() {
		var links = $$('a[rel="/selector/add"]');
		for (var x = 0, y = links.length; x < y; x++) {
			var link = links[x];
			Event.observe(link, 'click', this.onSelectorAdd.bind(this));
		}

		links = $$('#products div a.remove');
		for (var x = 0, y = links.length; x < y; x++) {
			var link = links[x];
			Event.observe(link, 'click', this.onSelectorRemove.bind(this));
		}

		var header = $$('#myselectionheader h3');
		if (header && header.length > 0) {
			header = header[0];
			Event.observe(header, 'click', this.onNameChange.bind(this));
		}
		
		var cancel = $$('a#selectorNameChangeCancel');
		if (cancel && cancel.length > 0) {
		    cancel = cancel[0];
			Event.observe(cancel, 'click', this.onNameChangeCancel.bind(this));
		}

		var submit = $$('#myselectioncontrols input');
		if (submit && submit.length > 0) {
			submit = submit[0];
			Event.observe(submit, 'click', this.onNameChangeSave.bind(this));
		}
	},


	/*******
	EVENT HANDLERS
	********/

	onSelectorAdd: function(e) {
		var element = Event.element(e);
		var listItem = element.up();

		try {
			listItem.addClassName('loading');

			/* Call the partial page */
			var updater = new Ajax.Updater('tools', element.href + '&partial=tools', {
				method: 'get',
				onComplete: this.onSelectorComplete.bind(this),
				onFailure: this.onSelectorFailure.bind(this)
			}
			);

			var updater = new Ajax.Updater('selectornav', element.href + '&partial=selector', {
				method: 'get',
				onComplete: this.onSelectorComplete.bind(this),
				onFailure: this.onSelectorFailure.bind(this)
			}
			);


			/* Google Analytics tracking */
			if (analytics) {
				analytics.trackPageView(element.href);
			}

			/* Stop the event */
			Event.stop(e);
		}
		catch (err) {
			location.href = element.href;
		}
	},


	onSelectorRemove: function(e) {
		var element = Event.element(e);

		try {
		
			/* Call the partial page */
			var products = $$('#products div');
			if (products.length > 1) {
				    var updater = new Ajax.Updater('selectornav', element.href + '&partial=selector', {
				    method: 'get',
				    onComplete: this.onSelectorComplete.bind(this),
				    onFailure: this.onSelectorFailure.bind(this)
			    }
				);
			}

			var updater = new Ajax.Updater('products', element.href + '&partial=products', {
				method: 'get',
				onComplete: this.onSelectorComplete.bind(this),
				onFailure: this.onSelectorFailure.bind(this)
			}
			);

			/* Google Analytics tracking */
			if (analytics) {
				analytics.trackPageView(element.href);
			}

			var products = $$('#products div');
			if (products.length <= 1) {
				$('products').hide();
				location.href = element.href;
				return;
			}

			/* Stop the event */
			Event.stop(e);
		}
		catch (err) {
			location.href = element.href;
		}
	},

	onSelectorComplete: function(e) {
		if (e.transport.responseText.startsWith('<hr/>')) {
			if ($('products')) {
				$('products').addClassName('text');
			}
			
		}
        if ($('myselectioncontrols')) {
			$('myselectioncontrols').hide();
			global.horizontalRules();
		}
		this.hookupSelectors();

		if (this.isFinalLoad) {
			global.removeLoadingClass();
		}

		if (!this.isFinalLoad) {
			isFinalLoad = true;
		}

		/* Redo menu */
		menu.registerEvents();

		/* Replace sifr elements */
		//text.sifr();

		/* Redo shadowboxes */
		global.shadowboxes();
	},

	onSelectorFailure: function(e) {
		this.hookupSelectors();
		global.removeLoadingClass();
		alert('URL call failed. Please try again.');
	},

	onNameChange: function(e) {
		var header = $('myselectionheader');
		if (header) {
			header.select('h3')[0].hide();
			header.select('input')[0].show();
			header.select('input')[0].focus();
		}
		var controls = $('myselectioncontrols');
		if (controls) {
		    controls.show();
		}
	},
	
	onNameChangeCancel: function(e) {
	    var header = $('myselectionheader');
		if (header) {
			var updater = new Ajax.Replacer('myselectionheader', Prototype.ApplicationPath + 'selector.aspx?partial=header', {
				method: 'get',
				onComplete: this.onSelectorComplete.bind(this),
				onFailure: this.onSelectorFailure.bind(this)
			    }
			);
		}
	},

	onNameChangeSave: function(e) {
		var input = $('myselectionheader').select('input')[0];
		var header = $('myselectionheader');
		if (header) {
			var updater = new Ajax.Replacer('myselectionheader', Prototype.ApplicationPath + 'selector.aspx?partial=header&name=' + input.value, {
				method: 'get',
				onComplete: this.onSelectorComplete.bind(this),
				onFailure: this.onSelectorFailure.bind(this)
			    }
			);
		}
	}
};

var productSelector;
FastInit.addOnLoad(function() {
	productSelector = new ProductSelector();
});
