var Global	= {};

Global.login	= new Class({
	
	initialize: function() {
		this.button		= $('member').getElement('a.sign-in');
		this.box		= $('sign-in');
		this.box_slide	= this.box.getElement('div.wrapper').set('slide', {'duration': 500, 'transition': 'expo:out'}).slide('hide');
		this.username	= $('memberUsername');
		this.password	= $('memberPassword');
		
		if(!this.username || !this.button) return;
		
		this.username.addEvents({
			'focus': this.clearText.bind(this, [this.username]),
			'blur': this.resetText.bind(this, [this.username, 'Username / Password'])
		});
		
		this.password.addEvents({
			'focus': this.clearText.bind(this, [this.password, true]),
			'blur': this.resetText.bind(this, [this.password, 'Password'])
		});
		
		this.button.addEvent('click', this.toggleLogin.bindWithEvent(this));
	},
	
	clearText: function(i, p) {
		if($type(i) == 'array') {
			i = i[0];
		}
		i.set('value', '');
	},
	
	resetText: function(i, t) {
		if($type(i) == 'array') {
			i = i[0];
			t = i[1];
		}
		
		if( i.get('value') == '' ) {
			i.set( 'value', t);
		}
	},
	
	toggleLogin: function(e) {
		e.stop();
		if(this.box.getStyle('visibility') == 'hidden') {
			var button	= this.button.getCoordinates();
			var box		= this.box.getCoordinates();
			
			this.box.setStyles({
				'visibility': 'visible',
				'top': button.top + button.height - 7,
				'left': $('header').getCoordinates().left + 900 - 210
			});
			this.box_slide.slide('in');
			this.button.addClass('sign-in-on');
		} else {
			this.box_slide.slide('hide');
			this.box.setStyle('visibility', 'hidden');
			this.button.removeClass('sign-in-on');
		}
	}
	
});

window.addEvent('domready', function() {
	new Global.login();
});

var Modal	= new Class({
	options: {},
	
	setContainer: function(container) {
		if($(container)) {
			this.options.container	= $(container);
			this.options.container.inject(document.body, 'top');
		}
	},
	
	showContainer: function() {
		if(!this.options.container) {
			return false;
		}
		
		this.createBackground();
		this.options.container.setStyle('display', '');
		this.options.background.setStyle('display', '');
	},
	
	createBackground: function() {
		if(!this.options.background) {
			var size = window.getScrollSize();
			this.options.background	= new Element('div', {
				'class': 'modal-background',
				'styles': {
					'width': '100%',
					'height': size.y,
					'display': 'none'
				},
				'events': {
					'click': this.hideWindow.bind(this)
				}
			}).inject(document.body, 'top');
		}
	},
	
	hideWindow: function(e) {
		if($chk(e)) { e.stop(); }
		
		this.options.container.setStyle('display', 'none');
		this.options.background.setStyle('display', 'none');
	},
	
	attachCloseEvent: function(element, action) {
		action	= $chk(action) ? action : 'click';
		
		$(element).addEvent(action, this.hideWindow.bindWithEvent(this));
	},
	
	centerX: function() {
		var size	= this.options.container.getSize();
		this.options.container.setStyles({
			'left': '50%',
			'margin-left': (size.x/2)*-1
		});
		return this;
	},
	
	centerY: function() {
		var size	= this.options.container.getSize();
		this.options.container.setStyles({
			'top': '50%',
			'margin-top': (size.y/2)*-1
		});
		return this;
	},
	
	centerWindow: function() {
		this.centerX().centerY();
	}
	
});

Modal.Newsletter	= new Class({
	
	Implements: Modal,
	
	initialize: function(el, container) {
		this.el	= $(el).addEvent('click', this.show.bindWithEvent(this));
		this.setContainer(container);
		this.attachCloseEvent($(container).getElement('a.close-modal'), 'click');
		this.form = $(container).getElement('form').addEvent('submit', this.send.bindWithEvent(this));
		this.message = $(container).getElement('p.message');
	},
	
	show: function(e) {
		e.stop();
		this.showContainer();
		this.centerWindow();
	},
	
	send: function(e) {
		e.stop();
		new Request.JSON({
			'url': Site.url+'/request/newsletter',
			'onSuccess': this.catchResult.bind(this)
		}).post(this.form);
	},
	
	catchResult: function(o, t) {
		this.message.empty().removeClass('error').removeClass('success');
		if(o.result == 'error') {
			this.message.addClass('error').set('html', o.message);
		} else {
			this.form.destroy();
			this.message.addClass('success').set('html', o.message);
		}
	}
	
});


var Table	= new Class({
	
	initialize: function(table) {
		this.table	= $(table);
	},
	
	fixCellHeight: function() {
		this.table.getElements('tbody td').setStyle('height', this.findMaxHeight());
	},
	
	findMaxHeight: function(table) {
		var height = 0;
		var h = 0;
		this.table.getElements('tbody td').each(function(td) {
			h	= td.getSize().y;
			if(h > height) {
				height	= h;
			}
		});
		return height;
	}
	
});