/************ 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));
	}
}

/************ 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, $('container'));
		
		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();
	}
}
*/
/************ slideshows *************/
var Slideshow = Class.create();
Slideshow.prototype = {
initialize: function(iShow, layers, togglers, nextDelay){
		this.iShow = iShow;
		this.layers = layers;
		this.togglers = togglers;
		this.nextDelay = nextDelay;
		this.setup();
	},
	setup: function() {
		this.pause = false;
		this.hideAll(this.iShow);
		this.playShow();		
		this.toggles();	
		
		this.layers.each(function(el) {
			el.observe('mouseenter', this.pauseShow.bindAsEventListener(this))
			el.observe('mouseleave', this.playShow.bindAsEventListener(this))
		}.bind(this));
		
		this.togglers.each(function(el) {
			el.observe('click', this.pauseShow.bindAsEventListener(this))
			el.observe('mouseenter', this.pauseShow.bindAsEventListener(this))
			el.observe('mouseleave', this.playShow.bindAsEventListener(this))
		}.bind(this));			
	},
	playShow: function() {
		this.timer = new PeriodicalExecuter(this.next.bindAsEventListener(this), this.nextDelay);
	},
	pauseShow: function () {
		this.timer.stop();
	},
	toggles: function(){
		var promo = this.layers;
		var buttons = this.togglers;
		
		buttons.each(function(el,i){
		
			if(i == 0) {
				buttons[i].addClassName('first');
			}
			if(i == this.togglers.length-1) {
				buttons[i].addClassName('last');
			}
		
			el.onclick = function() {
				this.timer.stop();
				this.pause = true;
				
				Effect.Fade(promo[this.iShow], {duration: 3});
				buttons[this.iShow].removeClassName('active');
				Effect.Appear(promo[i], {duration: 3});
				el.addClassName('active');
				this.iShow = i;
			}.bind(this);
		}.bind(this));		
	},
	next: function(event){
		if(!this.pause){			
			var promo = this.layers;
			var buttons = this.togglers;
			
			Effect.Fade(promo[this.iShow], {duration: 3});
			buttons[this.iShow].removeClassName('active');
			if(this.iShow != (promo.length-1)) 
				this.iShow += 1;
			else 
				this.iShow = 0;
			Effect.Appear(promo[this.iShow], {duration: 3});
			buttons[this.iShow].addClassName('active')
		}
	},
	hideAll: function(exceptionEl){
		this.layers.each(function(el,i){
			if(i != exceptionEl) 
				el.style.opacity = 0;
			else {
				Effect.Appear(el, {duration: 3});
				this.togglers[exceptionEl].addClassName('active');
			}
		}.bindAsEventListener(this));
	}		
}



/************ dom ready function calls *************/
Event.onDOMReady(function(){
	new PopUp($('_siteCredits'), 'siteCredits');
	new PopUp($('_legal'), 'legal');
	/*new Kickout('currency', 'navCurrency');
	new Kickout('boutiques', 'navBoutiques');*/
});