/**
 * enigma_core - handles loading external scripts
 *
 * The following scripts are contained within:
 *
 *	Countable class written by Stephane moded by nwwhite -> http://forum.mootools.net/viewtopic.php?id=2542
 *
 */
/****************************************************************************
 
	Generic Functions
	
*****************************************************************************/

/**
*
*	Function 	- signUp
*
*	This function launches a SqueezeBox(lightbox) that loads a signup form
*
**/	

	function signUp(){
		if($('sbox-content')){ $('sbox-content').empty(); }
		SqueezeBox.setOptions({
			size: {x:450, y:380},
			sizeLoading: {x:450, y:380},
			onComplete: function(){
				(function(){
					var resizeObj = ($('sbox-content').getFirst().getCoordinates());
					//(resizeObj.height < 400) ? resizeObj.height = 400 : resizeObj.height;
					parent.SqueezeBox.resize({x:resizeObj.width + 6,y:resizeObj.height + 6},false);
				}).delay(20);
			},
			onUpdate: function(){
				if($('signup_form')){			
					$('signup_form').set('send', {
						onComplete: function(response){
							var signupData = JSON.decode(response);
							validateSignup(signupData);
						}
					});
					$('submit_button').addEvent('click', function(e){
						e.stop();
						$('signup_form').send();
					});
				}
			}		
		}).setContent('ajax', '/admin/ajax/signUp.php');
	}
	
/**
*
*	Function 	- signIn
*
*	This function launches a SqueezeBox(lightbox) that loads a signin form
*
**/	

	function signIn(obj, msg){
		if($('sbox-content')){ $('sbox-content').empty(); }
		SqueezeBox.setOptions({
			size: {x:450, y:300},
			sizeLoading: {x:450, y:300},
			ajaxOptions: {
				onComplete: function(){
					(function(){
						if ((!obj) && (!msg)){
						} else if ((obj == '') && (msg)){
							logError('signin_log_container', msg, 'red').inject($('signin_container'), 'top');
						} else if (msg) {
							logError('signin_log_container', msg, 'red').inject($('signin_container'), 'top');
						} else {
							if (obj.header.status != 'Ok'){
								logError('signin_log_container', obj.header.error_message, 'red').inject($('signin_container'), 'top');
							}
						}
						var resizeObj = ($('sbox-content').getFirst().getCoordinates());
						//(resizeObj.height < 400) ? resizeObj.height = 400 : resizeObj.height;
						parent.SqueezeBox.resize({x:resizeObj.width + 6,y:resizeObj.height + 6},false);
					}).delay(20);
				}
			}		
		}).setContent('ajax', '/admin/ajax/signIn.php');
	}
	
/**
*
*	Function 	- mPO
*
*	This function 
*
**/		

function mPO(url){
	var newBox = new Element('div', {'id': 'mBox'});
	newBox.inject(document.body);
	var reqOptions = {
		update: $('mBox'),
		onComplete: function(){
			$('mBox').removeClass('mb_loading');
		}
	}
	var popRequest = new Request.HTML(reqOptions).get(url);
}

/**
*
*	Function 	- clearBox
*
*	This function 
*
**/	

function clearBox(){
	$('mBox').dispose();
}

/**
*
*	Function 	- buildList
*
*	This function
*
**/	

function buildList(listItems){
	listItems.each(function(element) {
	
		var fx = new Fx.Morph(element, {duration:100, wait:false});
		
		element.addEvent('mouseenter', function(){
			fx.start({
	//			'background-color': '#ffe553',
				'height': 60
			});
		});
		element.addEvent('mouseleave', function(){
			if (element.hasClass('hidden') != true){
				if (element.hasClass('on') == true){
					fx.start({
						'background-color': '#fff',
						'height':25
					});
				} else {
					fx.start({
						'background-color': '#c6d8e5',
						'height':25
					});	
				}
			}else{
				fx.start({
		//			'background-color': '#fff',
					'height':25
				});
			}
		});
	});
}

/**
*
*	Function 	- fancyForms
*
*	This function 
*
**/	

function fancyForms(els){
	$$(els).each(function(el, i){
		var instruction = el.getProperty('title');
/* 		var type = el.getProperty('name'); */
		el.setProperty('value', instruction);
		el.addEvents({
			'focus': function(){
				if (this.value == instruction) {
					this.setProperty('value', '');
					this.addClass('user_value');
				}
			},
			'blur': function(){
				if (this.value <= 1) {
					(function(){this.setProperty('value', instruction);}).bind(el).delay(100);
					this.removeClass('user_value');
				}
			}
		});
	});
}

/**
*
*	Function 	- buildTabs
*
*	This function 
*
**/	

function buildTabs(tabClass, displayClass){
	var formBtn = $$(tabClass);
	var forms = $$(displayClass);
	forms.each(function(el, i){
		formBtn.each(function(elem, f){
			elem.addEvent('click', function(){
				formBtn.removeClass('selected');
				if (i != f) {
					el.removeClass('selected');
				} else {
					el.addClass('selected');
				}
				this.addClass('selected');
			});
		});
	});
}

/**
*
*	Function 	- initUploader
*
*	This function
*
**/	

function initUploader(url, el){
	new Request.HTML({
		url: url,
		update:	$(el)
	}).send();
}

/**
*
*	Event		-	domready
*
*	This function
*
**/	

window.addEvents({
	'domready': function(){
		SqueezeBox.initialize();
	}
});

/****************************************************************************
 
	Countable Class for mootools v1.11
	by Stephane
	moded by nwhite
	http://forum.mootools.net/viewtopic.php?id=2542
	
*****************************************************************************/

	var Countable = new Class({
 
	  initialize: function(inputId, max, className, errorClass) {
	 
		this.input = $(inputId);
		this.max = max;
		this.className = className ? className : "count";
		this.errorClass = errorClass ? errorClass : "error";
	 	
	 	this.input.addClass('counting');
		this.handle = new Element("div", {'class': this.className});
		this.handle.set('html', '&nbsp;').injectAfter(this.input);
		this.input.addEvent('keydown', this.onKeyPress.bindWithEvent(this));
		this.input.addEvent('keyup', this.onKeyPress.bindWithEvent(this));
		this.update();
	 
	  },
	 
	  onKeyPress: function(event) {
		event = new Event(event);
		if(!event.shift && !event.control && !event.alt && !event.meta) this.update();
	  },
	 
	  update: function() {
	 
//		if (this.input.value.length > this.max)
//		  this.input.value = this.input.value.substring(0, this.max);
	 
		  var count = this.max - this.input.value.length;
	 
		  if (count == 0) {
			var string = "<span class=\""+this.errorClass+"\">No characters left</span>";
		  } else if (count == 1) {
			var string = "<span>1 character left</span>";
		  } else if (count < 0) {
		  	var string = "<span class=\""+this.errorClass+"\">" + (count * -1) + " characters over</span>";
		  } else {
			var string = "<span>" + count + " characters left</span>";
		  }
		  this.handle.set('html', string);
	  }
	 
	});	
	
/****************************************************************************

	SqueezeBox - Expandable Lightbox
	
	Allows to open various content as modal,
	centered and animated box.
	
	Dependencies: MooTools 1.2 trunk (04/2008)
	
	Inspired by
	... Lokesh Dhakar	- The original Lightbox v2
	
	@version		1.1 rc3
	
	@license		MIT-style license
	@author		Harald Kirschner <mail [at] digitarald.de>
	@copyright	Author
	
*****************************************************************************/

var SqueezeBox = {

	presets: {
		onOpen: $empty,
		onClose: $empty,
		onUpdate: $empty,
		onResize: $empty,
		onMove: $empty,
		onShow: $empty,
		onHide: $empty,
		size: {x: 600, y: 450},
		sizeLoading: {x: 200, y: 150},
		marginInner: {x: 20, y: 20},
		marginImage: {x: 50, y: 75},
		handler: false,
		target: null,
		closable: true,
		closeBtn: true,
		zIndex: 65555,
		overlayOpacity: 0.7,
		classWindow: '',
		classOverlay: '',
		overlayFx: {},
		resizeFx: {},
		contentFx: {},
		parse: false, // 'rel'
		parseSecure: false,
		shadow: true,
		document: null,
		ajaxOptions: {}
	},

	initialize: function(presets) {
		if (this.options) return this;

		this.presets = $merge(this.presets, presets);
		this.doc = this.presets.document || document;
		this.options = {};
		this.setOptions(this.presets).build();
		this.bound = {
			window: this.reposition.bind(this, [null]),
			scroll: this.checkTarget.bind(this),
			close: this.close.bind(this),
			key: this.onKey.bind(this)
		};
		this.isOpen = this.isLoading = false;
		return this;
	},

	build: function() {
		this.overlay = new Element('div', {
			id: 'sbox-overlay',
			styles: {display: 'none', zIndex: this.options.zIndex}
		});
		this.win = new Element('div', {
			id: 'sbox-window',
			styles: {display: 'none', zIndex: this.options.zIndex + 2}
		});
		if (this.options.shadow) {
			if (Browser.Engine.webkit420) {
				this.win.setStyle('-webkit-box-shadow', '0 0 10px rgba(0, 0, 0, 0.7)');
			} else if (!Browser.Engine.trident4) {
				var shadow = new Element('div', {'class': 'sbox-bg-wrap'}).inject(this.win);
				var relay = function(e) {
					this.overlay.fireEvent('click', [e]);
				}.bind(this);
				['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'].each(function(dir) {
					new Element('div', {'class': 'sbox-bg sbox-bg-' + dir}).inject(shadow).addEvent('click', relay);
				});
			}
		}
		this.content = new Element('div', {id: 'sbox-content'}).inject(this.win);
		this.closeBtn = new Element('a', {id: 'sbox-btn-close', href: '#'}).inject(this.win);
		this.fx = {
			overlay: new Fx.Tween(this.overlay, $merge({
				property: 'opacity',
				onStart: Events.prototype.clearChain,
				duration: 250,
				link: 'cancel'
			}, this.options.overlayFx)).set(0),
			win: new Fx.Morph(this.win, $merge({
				onStart: Events.prototype.clearChain,
				unit: 'px',
				duration: 750,
				transition: Fx.Transitions.Quint.easeOut,
				link: 'cancel',
				unit: 'px'
			}, this.options.resizeFx)),
			content: new Fx.Tween(this.content, $merge({
				property: 'opacity',
				duration: 250,
				link: 'cancel'
			}, this.options.contentFx)).set(0)
		};
		$(this.doc.body).adopt(this.overlay, this.win);
	},

	assign: function(to, options) {
		return to.addEvent('click', function() {
			return !SqueezeBox.fromElement(this, options);
		});
	},

	fromElement: function(from, options) {
		this.initialize();
		if (this.element) this.trash();
		this.element = $(from);
		this.setOptions($merge(this.presets, options || {}));
		if (this.element && this.options.parse) {
			var obj = this.element.getProperty(this.options.parse);
			if (obj && (obj = JSON.decode(obj, this.options.parseSecure))) this.setOptions(obj);
		}
		this.assignOptions();
		this.url = ((this.element) ? (this.options.url || this.element.get('href')) : from) || '';
		var handler = this.options.handler;
		if (handler) return this.setContent(handler, this.parsers[handler].call(this, true));
		var ret = false;
		this.parsers.some(function(parser, key) {
			var content = parser.call(this);
			if (content) {
				ret = this.setContent(key, content);
				return true;
			}
			return false;
		}, this);
		return ret;
	},

	assignOptions: function() {
		this.overlay.set('class', this.options.classOverlay);
		this.win.set('class', this.options.classWindow);
		if (Browser.Engine.trident4) this.win.addClass('sbox-window-ie6');
	},

	close: function(e) {
		var stoppable = ($type(e) == 'event');
		if (stoppable) e.stop();
		if (!this.isOpen || (stoppable && !$lambda(this.options.closable).call(this, e))) return this;
		this.fx.overlay.start(0).chain(this.toggleOverlay.bind(this));
		this.win.setStyle('display', 'none');
		this.fireEvent('onClose', [this.content]);
		this.trash();
		this.toggleListeners();
		this.isOpen = false;
		return this;
	},

	trash: function() {
		this.element = this.asset = null;
		this.content.empty();
		this.options = {};
		this.removeEvents().setOptions(this.presets).callChain();
	},

	onError: function() {
		this.asset = null;
		this.setContent('string', this.options.errorMsg || 'An error occurred');
	},

	setContent: function(handler, content) {
		if (!this.handlers[handler]) return false;
		this.content.className = 'sbox-content-' + handler;
		this.applyTimer = this.applyContent.delay(this.fx.overlay.options.duration, this, this.handlers[handler].call(this, content));
		if (this.overlay.retrieve('opacity')) return this;
		this.toggleOverlay(true);
		this.fx.overlay.start(this.options.overlayOpacity);
		return this.reposition();
	},

	applyContent: function(content, size) {
		if (!this.isOpen && !this.applyTimer) return;
		this.applyTimer = $clear(this.applyTimer);
		this.hideContent();
		if (!content) {
			this.toggleLoading(true);
		} else {
			if (this.isLoading) this.toggleLoading(false);
			this.fireEvent('onUpdate', [this.content], 20);
		}
		if (content) {
			if (['string', 'array'].contains($type(content))) this.content.set('html', content);
			else if (!this.content.hasChild(content)) this.content.adopt(content);
		}
		this.callChain();
		if (!this.isOpen) {
			this.toggleListeners(true);
			this.resize(size, true);
			this.isOpen = true;
			this.fireEvent('onOpen', [this.content]);
		} else {
			this.resize(size);
		}
	},

	resize: function(size, instantly) {
		this.showTimer = $clear(this.showTimer || null);
		var box = this.doc.getSize(), scroll = this.doc.getScroll();
		this.size = $merge((this.isLoading) ? this.options.sizeLoading : this.options.size, size);
		var to = {
			width: this.size.x,
			height: this.size.y,
			left: (scroll.x + (box.x - this.size.x - this.options.marginInner.x) / 2).toInt(),
			top: (scroll.y + (box.y - this.size.y - this.options.marginInner.y) / 2).toInt()
		};
		this.hideContent();
		if (!instantly) {
			this.fx.win.start(to).chain(this.showContent.bind(this));
		} else {
			this.win.setStyles(to).setStyle('display', '');
			this.showTimer = this.showContent.delay(50, this);
		}
		return this.reposition();
	},

	toggleListeners: function(state) {
		var fn = (state) ? 'addEvent' : 'removeEvent';
		this.closeBtn[fn]('click', this.bound.close);
		this.overlay[fn]('click', this.bound.close);
		this.doc[fn]('keydown', this.bound.key)[fn]('mousewheel', this.bound.scroll);
		this.doc.getWindow()[fn]('resize', this.bound.window)[fn]('scroll', this.bound.window);
	},

	toggleLoading: function(state) {
		this.isLoading = state;
		this.win[(state) ? 'addClass' : 'removeClass']('sbox-loading');
		if (state) this.fireEvent('onLoading', [this.win]);
	},

	toggleOverlay: function(state) {
		var full = this.doc.getSize().x;
		this.overlay.setStyle('display', (state) ? '' : 'none');
		this.doc.body[(state) ? 'addClass' : 'removeClass']('body-overlayed');
		if (state) {
			this.scrollOffset = this.doc.getWindow().getSize().x - full;
			this.doc.body.setStyle('margin-right', this.scrollOffset);
		} else {
			this.doc.body.setStyle('margin-right', '');
		}
	},

	showContent: function() {
		if (this.content.get('opacity')) this.fireEvent('onShow', [this.win]);
		this.fx.content.start(1);
	},

	hideContent: function() {
		if (!this.content.get('opacity')) this.fireEvent('onHide', [this.win]);
		this.fx.content.cancel().set(0);
	},

	onKey: function(e) {
		switch (e.key) {
			case 'esc': this.close(e);
			case 'up': case 'down': return false;
		}
	},

	checkTarget: function(e) {
		return this.content.hasChild(e.target);
	},

	reposition: function() {
		var size = this.doc.getSize(), scroll = this.doc.getScroll(), ssize = this.doc.getScrollSize();
		this.overlay.setStyles({
			width: ssize.x + 'px',
			height: ssize.y + 'px'
		});
		this.win.setStyles({
			left: (scroll.x + (size.x - this.win.offsetWidth) / 2 - this.scrollOffset).toInt() + 'px',
			top: (scroll.y + (size.y - this.win.offsetHeight) / 2).toInt() + 'px'
		});
		return this.fireEvent('onMove', [this.overlay, this.win]);
	},

	removeEvents: function(type){
		if (!this.$events) return this;
		if (!type) this.$events = null;
		else if (this.$events[type]) this.$events[type] = null;
		return this;
	},

	extend: function(properties) {
		return $extend(this, properties);
	},

	handlers: new Hash(),

	parsers: new Hash()

};

SqueezeBox.extend(new Events($empty)).extend(new Options($empty)).extend(new Chain($empty));

SqueezeBox.parsers.extend({

	image: function(preset) {
		return (preset || (/\.(?:jpg|png|gif)$/i).test(this.url)) ? this.url : false;
	},

	clone: function(preset) {
		if ($(this.options.target)) return $(this.options.target);
		if (this.element && !this.element.parentNode) return this.element;
		var bits = this.url.match(/#([\w-]+)$/);
		return (bits) ? $(bits[1]) : (preset ? this.element : false);
	},

	ajax: function(preset) {
		return (preset || (this.url && !(/^(?:javascript|#)/i).test(this.url))) ? this.url : false;
	},

	iframe: function(preset) {
		return (preset || this.url) ? this.url : false;
	},

	string: function(preset) {
		return true;
	}
});

SqueezeBox.handlers.extend({

	image: function(url) {
		var size, tmp = new Image();
		this.asset = null;
		tmp.onload = tmp.onabort = tmp.onerror = (function() {
			tmp.onload = tmp.onabort = tmp.onerror = null;
			if (!tmp.width) {
				this.onError.delay(10, this);
				return;
			}
			var box = this.doc.getSize();
			box.x -= this.options.marginImage.x;
			box.y -= this.options.marginImage.y;
			size = {x: tmp.width, y: tmp.height};
			for (var i = 2; i--;) {
				if (size.x > box.x) {
					size.y *= box.x / size.x;
					size.x = box.x;
				} else if (size.y > box.y) {
					size.x *= box.y / size.y;
					size.y = box.y;
				}
			}
			size.x = size.x.toInt();
			size.y = size.y.toInt();
			this.asset = $(tmp);
			tmp = null;
			this.asset.width = size.x;
			this.asset.height = size.y;
			this.applyContent(this.asset, size);
		}).bind(this);
		tmp.src = url;
		if (tmp && tmp.onload && tmp.complete) tmp.onload();
		return (this.asset) ? [this.asset, size] : null;
	},

	clone: function(el) {
		if (el) return el.clone();
		return this.onError();
	},

	adopt: function(el) {
		if (el) return el;
		return this.onError();
	},

	ajax: function(url) {
		this.asset = new Request.HTML($merge({
			method: 'get'
		}, this.options.ajaxOptions)).addEvents({
			onSuccess: function(resp) {
				this.applyContent(resp);
				this.fireEvent('onAjax', [resp, this.asset]);
				this.asset = null;
			}.bind(this),
			onFailure: this.onError.bind(this)
		});
		this.asset.send.delay(10, this.asset, [{url: url}]);
	},

	iframe: function(url) {
		this.asset = new Element('iframe', $merge({
			src: url,
			frameBorder: 0,
			width: this.options.size.x,
			height: this.options.size.y
		}, this.options.iframeOptions));
		if (this.options.iframePreload) {
			this.asset.addEvent('load', function() {
				this.applyContent(this.asset.setStyle('display', ''));
			}.bind(this));
			this.asset.setStyle('display', 'none').inject(this.content);
			return false;
		}
		return this.asset;
	},

	string: function(str) {
		return str;
	}

});

SqueezeBox.handlers.url = SqueezeBox.handlers.ajax;
SqueezeBox.parsers.url = SqueezeBox.parsers.ajax;
SqueezeBox.parsers.adopt = SqueezeBox.parsers.clone;