/*
Script: account.js
	Contains <AccountJS>

Author:
	Alan Roemen
	July 15, 2008

Class: AccountJS

Options:
	baseURL: Base directory for script. Default: false
	scriptName: Name of javascript file. Default: 'account'
*/

var AccountJS = new Class({
	options: {
		baseURL: false,
		scriptName: 'account',
		
		//Scripts
		removeAddress: false,	//Set to class name of the link
		setCountry: false	//Set to ID of Country Select
	},	

	initialize: function(options) {
		this.setOptions(options);
		
		// Get script base path
		if(!this.options.baseURL) {
			var elements = document.getElementsByTagName('script');
			for (var i=0; i<elements.length; i++) {
				if (elements[i].src && (elements[i].src.indexOf(this.options.scriptName+'.js') != -1)) {
					var src = elements[i].src;
					this.options.baseURL = src.substring(0, src.lastIndexOf('/'));
					break;
				}
			}
			// Get document base path
			this.documentBasePath = document.location.href;
			if (this.documentBasePath.indexOf('?') != -1)
				this.documentBasePath = this.documentBasePath.substring(0, this.documentBasePath.indexOf('?'));
			this.documentBasePath = this.documentBasePath.substring(0, this.documentBasePath.lastIndexOf('/'));
			if (this.options.baseURL.indexOf('://') == -1 && this.options.baseURL.charAt(0) != '/')
				this.options.baseURL = this.documentBasePath + "/" + this.options.baseURL;
		}
		
		this.init = {};
		if(this.options.removeAddress !== false) this.initRemoveAddress();
		if(this.options.setCountry !== false) this.initSetCountry();
	},

	initConfirm: function(){
		if(this.init.addConfirm === true) return;
		this.init.addConfirm = true;
		new Asset.css(this.options.baseURL + '/confirm.css');
		
		this.confirmBox = {};
		this.confirmBox.parent = new Element('div', {id: 'confirm_window'});
		this.confirmBox.title = new Element('div', {id: 'confirm_window_title'});
		this.confirmBox.footer = new Element('div', {id: 'confirm_window_footer'});
		this.confirmBox.btnContinue = new Element('div', {
			'class': 'confirm_window_btn'
		}).setHTML('Continue').injectInside(this.confirmBox.footer);
		this.confirmBox.btnSeparator = new Element('span',{'styles':{'width':'1px'}}).setHTML('|').injectInside(this.confirmBox.footer);
		this.confirmBox.btnCancel = new Element('div', {
			'class': 'confirm_window_btn',
			'events': {
				'click': function(){
					this.confirmBox.btnContinue.removeEvent('click');
					this.confirmBox.parent.remove();
				}.bindWithEvent(this)
			}
		}).setHTML('Cancel').injectInside(this.confirmBox.footer);
		
		this.confirmBox.title.injectInside(this.confirmBox.parent);
		this.confirmBox.footer.injectInside(this.confirmBox.parent);
	},

	confirm: function(act, pos, msg){
		if(!pos) return false;
		msg = msg || 'Confirm';
		this.initConfirm();
		
		this.confirmBox.title.setHTML(msg);
		this.confirmBox.parent.setStyles({ top: pos.y, left: pos.x });
		this.confirmBox.btnContinue.removeEvents('click');
		this.confirmBox.btnContinue.addEvent('click', function(e){
			e = new Event(e).stop();
			this.confirmBox.parent.remove();
			act.delay(0, this);
		}.bind(this));
		
		this.confirmBox.parent.inject(document.body);
		
		if(window.ie){
			var w = this.confirmBox.parent.getSize().size.x.toInt();
			this.confirmBox.btnContinue.setStyle('width', (w/2-1).toInt()+'px');
			this.confirmBox.btnSeparator.setStyle('width', '2px');
			this.confirmBox.btnCancel.setStyle('width', (w/2-1).toInt()+'px');
		}
	},

	initSetCountry: function(){
		if(this.init.setCountry === true) return;
		this.init.setCountry = true;
		this.setCountry = {
			state: $(this.options.setCountry[0]),
			country: $(this.options.setCountry[1])
		}
		this.runSetCountry(true);
		this.setCountry.country.addEvent('change', function(){
			this.setCountry.state = $(this.options.setCountry[0]);
			this.runSetCountry();
		}.bind(this));
	},

	runSetCountry: function(noFocus){
		var noFocus = noFocus || false;
		new Ajax(this.options.baseURL+'/state.php', {
			method: 'get',
			data: 'country=' + this.setCountry.country.value + '&state=' + this.setCountry.state.value,
			onComplete: function(html){
				this.setCountry.state.getParent().setHTML(html);
				this.setCountry.state = $(this.options.setCountry[0]);
				if(!noFocus) this.setCountry.state.focus();
			}.bind(this)
		}).request();
	},

	initRemoveAddress: function(){
		if(this.init.removeAddress === true) return;
		this.init.removeAddress = true;
		$$(this.options.removeAddress).each(function(el, i){
			if(i==0) {
				this.addressParent = el.getParent().getParent().getParent();
			}
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				this.addressLink = el.getProperty('href');
				this.confirm(this.removeAddress, el.getPosition(), 'Delete This Address?');
				this.addressToRemove = el.getParent().getParent();
			}.bind(this))
		}.bind(this));
	},

	removeAddress: function(){
		this.addressToRemove.setStyle('overflow', 'hidden');
		this.addressToRemove.effect('opacity', {duration: 600}).start(0).chain(function(){
			this.addressToRemove.effect('width', {duration: 600}).start(0).chain(function(){
				this.addressToRemove.remove();
				this.updateAddress();
			}.bind(this));
		}.bind(this));
		
		var address_type = this.addressLink.split('type=')[1];
		new Ajax(location.href, {
			method: 'post',
			data: 'action=account_update_request&no_redirect=true&remove_address_type='+address_type
		}).request();
	},

	updateAddress: function(){
		var title, addresses = this.addressParent.getElements('div.left');
		addresses.pop();
		addresses.each(function(el, i){
			if(i == 0) title = 'Primary Address:';
			else if(i == 1) title = 'Additional Address:';
			else title = 'Additional Address '+i+':';
			el.getElement('strong').setHTML(title);
		}.bind(this));
	}
});

AccountJS.implement(new Options, new Events);