var trivePopup = Class.create();

trivePopup.wrappers = new Array();
trivePopup.popups = new Array();

trivePopup.prototype = {
	
	//	Jan Jarfalk at Trive AB
	//	jan.jarfalk@trive.se
	//	http://www.trive.se

	//	Requires: 	Prototype
	//			 	Scriptaculous
		
	// EXAMPLE JS
	/*
		new trivePopup({
			activator: 'openPopup',
			deactivator: 'closePopup', 
			popup: 'popupdiv',
			timer: 3000,
			redirectOnClose: 'http://www.google.com'
		});
	*/
	
	// activator: Element id to the element that shall open the popup
	// deactivator: Element id to the element that shall close the popup (optional)
	// popup: The popup itself
	// timer: Automatic close in milliseconds (optional)
	// redirectOnClose: When the popup is closed by timer or event the page will redirect to this location
	
	initialize: function(options) {
		
		var self = this;
		
		this.activator = options.activator;
		this.deactivator = options.deactivator || false;
		this.overlay = options.overlay || 'overlay';
		this.popupwrapper = options.popupwrapper || 'popupwrapper';		
		this.popup = options.popup;
		this.timer = options.timer ||false;
		this.redirectOnClose = options.redirectOnClose || false;
					
		trivePopup.wrappers[trivePopup.wrappers.length] = this.popupwrapper;
		trivePopup.popups[trivePopup.popups.length] = this.popup;
		

		if(this.activator == "window.alert"){
			if(document.getElementById) {
				window.alert = function(alertmessage) {
					self.activate(this,alertmessage);
				}
			}
		} 
		else if(this.activator == "window.confirm") {
			if(document.getElementById) {
				window.confirm = function(alertmessage) {
					self.activate(this,alertmessage);
				}
			}
		} else {
			Event.observe(this.activator, 'click', this.activate.bindAsEventListener(this,null), false);
			if(this.deactivator){
				Event.observe(this.deactivator, 'click', this.deactivate.bindAsEventListener(this,null), false);
			}
					
			//ctrl.onclick = function(){return false;};
		}
	},
	
	deactivate: function(){
		new Effect.Fade($(this.overlay), { duration: 0.3, from: 0.9, to: 0.0 });
		new Effect.Fade($(this.popupwrapper), { duration: 0.3, from: 0.9, to: 0.0 });
		new Effect.Fade($(this.popup), { duration: 0.3, from: 0.9, to: 0.0 });
			
		if(this.redirectOnClose) { window.location.href = this.redirectOnClose; }
	},
	
	activate: function(event, alertmessage){
		var self = this;

		//Event.observe(this.overlay, 'click', this.deativate.bindAsEventListener(this), false);
		//Event.observe(this.popupwrapper, 'click', this.deativate.bindAsEventListener(this), false);	
		
		if(alertmessage){
			$(self.popup).innerHTML = alertmessage;
			Event.observe(this.popup, 'click', this.deactivate.bindAsEventListener(this,null), false);
		}
				
		if(!$(this.overlay).visible())
		{
			new Effect.Appear($(this.overlay), { duration: 0.3, from: 0.0, to: 0.9, afterFinish:initPopup()}); 
		}
		else
		{
			trivePopup.wrappers.each(function(item){
				$(item).hide();
			});
			trivePopup.popups.each(function(item){
				$(item).hide();
			});
			initPopup();
		}
		

		function initPopup(){
			
			$(self.popupwrapper).setStyle({
				display: 'block',
				position: 'absolute',
				top: '100px',
				width: '100%',
				zIndex: '1000'
			});
			
			$(self.popup).setStyle({
				display: 'block',
				margin: '0 auto',
				zIndex: '1001'
			});
			
			if(self.timer){
				window.setTimeout(function(){
					self.deactivate();
					if(self.redirectOnClose) { location.href = self.redirectOnClose; }
				},self.timer);
			}
		}
		
	},
	
	
	prepareIE: function(height, overflow){
		document.getElementsByTagName('body')[0].style.height = height;
		document.getElementsByTagName('html')[0].style.height = height;
	},
	
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
		return(this.yPos);
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	}
	
}