/************ 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) {
		parent = element.getOffsetParent();
		var ws = Position.GetWindowSize();
		pw = parent.offsetWidth
//		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.popup.style.top = $('container').offsetHeight - this.popup.style.height;
		this.closebtn  = this.popup.down('.close');
		this.closebtn2  = this.popup.down('.close2');
		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.closebtn2, '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 + 999999;
		this.popup.show();
	}
}

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


var Kickout2 = Class.create();
Kickout2.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) {
		new Effect.Appear(this.container, {duration: 0.3});
		event.stop();
	},
	close: function(event) {
		new Effect.Fade(this.container, {duration: 0.1});
		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));
	}		
}

function toolTips() {
	var togglers = $$('.hotSpotContain a');
	togglers.each(function(el) {
	var overlay = el.select('span.contain');
		Event.observe(el, 'mouseenter', function() {
			overlay.each(function(ol) {
				new Effect.Appear(ol, { duration: 0 });
				ol.style.zIndex = 9999;
				el.style.zIndex = 9999;
				togglers.each(function(el2) {
				if(el != el2)
					/*el2.style.zIndex = -9999;*/
					el2.style.zIndex = 9998;
				});
			});
		});
		Event.observe(el, 'mouseleave', function() {
			overlay.each(function(ol) {
			new Effect.Fade(ol, { duration: 0 });
				ol.style.zIndex = -9999;
				ol.hide;
			});
		});
		Event.observe(window, 'unload', function() {
			overlay.each(function(ol) {
			new Effect.Fade(ol, { duration: 0 });
				ol.style.zIndex = -9999;
				ol.hide;
			});
		});
	});
}

function swatches() {
	var swatch = $$('.swTrig');
	swatch.each(function(el) { 
		var stripId = el.id.replace("_","");
		new Kickout(el.id, stripId, function(){
			var trig = el.select('.trigger')[0];
			$('img'+el.id.split('_')[1]).src = trig.readAttribute('title');	
			//trig.href = "javascript:;";
		});
	})
	
	var swatchBubbles = $$('.swatchBubble');
	swatchBubbles.each(function(sb) {
		var lrgThumb = sb.select('.largeThumb');
		Event.observe(sb, 'click', function() {
			lrgThumb.each(function(lt) {
				new Effect.Appear(lt, {duration: 0.2});
				
				Event.observe(lt, 'mouseleave', function() {
					new Effect.Appear(lt, { duration: 0.3, from: 1, to: 0 });							 
				});
			});
		});
	});
}

function formCallback(result, form) {
	window.status = "valiation callback for form '" + form.id + "': result = " + result;
}



/************ dom ready function calls *************/
Event.observe(window, "load", function(){
//Event.onDOMReady(function(){

	/************ product *************/
	if($('product')) {
		swatches();
	}

	/************ info pops *************/
	if(!($('home') || $('category') || $('about') || $('faqs') || $('management') || $('manufacturing') || $('news') || $('admin') || $('planning') || $('custom'))) {
		if($('sitemap'))
			$('leftNav').style.display = "block";
		else
			new Kickout('reveal', 'leftNav');
	}
	
	if(!($('home') || $('category') || $('about') || $('faqs') || $('management') || $('manufacturing') || $('news') || $('admin') || $('planning') || $('custom')) || $('industry')) {
		if($('info_Pop'))
			new PopUp('info_Pop', 'infoPop');
		if($('info_Pop2'))
			new PopUp('info_Pop2', 'infoPop2');
	}
	
	/************ tabs *************/
	if($('feature')) {
		new Tabs('feature', 0);
		//Event.observe(window, 'load', hideAllTabs);
	}
	
	new Kickout('tab5', 'other');

	/************ industry solutions *************/	
	if($('industry')) {
		toolTips();
	}
	
	/************ faqs **********
	if($('faqs')) {
		$$('a[href^=#]:not([href=#])').each(function(element) {			
			element.observe('click', function(event) {
				if(Prototype.Browser.IE)
					new Effect.ScrollTo(this.hash.substr(1), {offset: -130});
				else
					new Effect.ScrollTo(this.hash.substr(1), {offset: 0});
				Event.stop(event);
			}.bindAsEventListener(element))
		})		
	}
	***/
	
	if($('contact_form')) {
		var valid = new Validation('contact_form', {immediate : true, onFormValidate : formCallback});		
	}
	
});


