/************ window dimensions *************/
//Prototype-based javascript window dimensions
// http://textsnippets.com/posts/show/835 
Position.GetWindowSize = function(w) {
        w = w ? w : window;
        var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        return [width, height]
}

/************ center DOM element *************/
// Center a DOM element, prototype based
// http://textsnippets.com/posts/show/836
Position.Center = function(element, parent) {
	var w, h, pw, ph;
	var d = Element.getDimensions(element);
	w = d.width;
	h = d.height;
	Position.prepare();
	if (!parent) {
			var ws = Position.GetWindowSize();
			pw = ws[0];
			ph = ws[1];
	} else {
			pw = parent.offsetWidth;
			ph = parent.offsetHeight;
	}
	element.style.top = (ph/2) - (h/2) +  Position.deltaY + "px";
	element.style.left = (pw/2) - (w/2) +  Position.deltaX + "px";
}

/************ popups *************/
var PopUp = Class.create();
PopUp.i = 0;
PopUp.prototype = {
	initialize: function(trigger, popup) {
		this.trigger = trigger;
		this.popup   = $(popup);
		this.options = Object.extend({}, arguments[2] || {});
		this.start();
	},
	start: function() {
		if (this.trigger instanceof Array) {
			this.triggers = this.trigger;
		} else {
			this.triggers = [this.trigger];
		}
		
		this.popup.hide();
		
		this.triggers.each(function(t) {
			Event.observe(t, 'click', this.openInternal.bindAsEventListener(this));
		}.bind(this));
	},
	openInternal: function(event) {
		this.toTop();
		Position.Center(this.popup);
		
		this.closebtn  = this.popup.down('.close');
		this.handle    = this.popup.down('.t-bar');
		
		this.draggable = new Draggable(this.popup, {handle: this.handle, starteffect: false, endeffect: false });
		
		this.closeListener = this.closeInternal.bindAsEventListener(this);
		Event.observe(document, 'click', this.closeListener);
		Event.observe(this.closebtn, 'click', this.closeListener);
		Event.observe(this.popup, 'click', function(event) {
			this.falseAlarm = true;
			this.toTop();
		}.bind(this));
		Event.stop(event);
		
		(this.options.onOpen || Prototype.emptyFunction)();
	},
	closeInternal: function(event) {
		if (this.falseAlarm) {
			this.falseAlarm = false;
			return;
		}
		
		this.popup.hide();
		
		this.draggable.destroy();
		
		Event.stopObserving(document, 'click', this.closeListener);
		Event.stopObserving(this.closebtn, 'click', this.closeListener);
		Event.stop(event);
		
		(this.options.onClose || Prototype.emptyFunction)();
	},
	toTop: function() {
		PopUp.i = PopUp.i + 1;
		this.popup.style.zIndex = PopUp.i + 120;
		this.popup.show();
	}
}

/************ kickouts *************/
var Kickout = Class.create();
Kickout.prototype = {
	initialize: function(trigger, container) {
		this.trigger = $(trigger);
		this.container = $(container);
		this.setup();
	},
	setup: function() {
		this.trigger.observe('mouseenter', this.open.bindAsEventListener(this));
		this.trigger.observe('mouseleave', this.close.bindAsEventListener(this));
	},
	open: function(event) {
		this.trigger.addClassName('active');
		this.container.show();
		this.container.style.zIndex = 9999;
		event.stop();
	},
	close: function(event) {
		this.trigger.removeClassName('active');
		this.container.hide();
		event.stop();
	}
}

/************ open new window *************/
function newWindow(url) {
	window.open(url);
}

/************ tabs *************/
var Tabs = Class.create();
Tabs.prototype = {
	initialize: function(container, active) {
		this.container = $(container);

		this.togglers  = this.container.select('ul.togglers li');
		this.tabs      = this.container.select('.tabContent');
	
		this.active    = active || 0;
		this.setup();

	},
	setup: function() {
		this.tabs[this.active].addClassName('active');
		this.togglers[this.active].addClassName('active');
		this.togglers.each(function(el, i) {
			el.onclick = function() {
				if (i != this.active) {
					el.addClassName('active');
					this.togglers[this.active].removeClassName('active');
					
					if(this.tabs.length == this.togglers.length){
						this.tabs[this.active].removeClassName('active');
						this.tabs[i].addClassName('active');
					}
				}
				this.active = i;
				return false;
			}.bind(this);
		}.bind(this));
	}
}

var SimpleReveal = Class.create({
	initialize : function(container, options){
		this.container = $(container);
		this.options = Object.extend({toggleClass:'active', oLinkSel:'.open', cLinkSel:'.close', contentSel:'.content', duration:.5, cb:function(){}}, options || {});
		this.setup();
	},
	setup : function(){
		if (this.options.tLinkSel)
			this.toggles = this.container.select(this.options.tLinkSel).invoke('observe', 'click', this.open.bind(this));
		else{
			this.container.select(this.options.oLinkSel).invoke('observe', 'click', this.open.bind(this));
			this.container.select(this.options.cLinkSel).invoke('observe','click', this.close.bind(this));
		}
		this.content = this.container.select(this.options.contentSel).first().hide();
	},
	open : function(e){
		e.stop();
		if (this.effect != null) return;
		this.effect = new Effect.toggle(this.content, 'blind', {duration:this.options.duration, afterFinish:function(){this.effect=null;}.bind(this)});
		this._cleanUp();
	},
	close : function(e){
		e.stop();
		if (this.effect != null) return;
		this.effect = new Effect.BlindUp(this.content, {afterFinish:this._cleanUp.bind(this, true), duration:this.options.duration});
	},
	_cleanUp : function(reset){
		this.container.toggleClassName(this.options.toggleClass);
		this.options.cb.apply(this);
		if (reset) this.effect = null;
	}
});
