var triveAdminPopup = Class.create();

triveAdminPopup.wrappers = new Array();
triveAdminPopup.popups = new Array();

triveAdminPopup.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.popupData = options.popupData;
		this.beforeShowFunction = options.beforeShowFunction;
		this.afterCloseFunction = options.afterCloseFunction;
		
		this.overlay = options.overlay || 'overlay';
		this.popupwrapper = options.popupwrapper || 'popupwrapper';		
		this.popup = options.popup;
		this.timer = options.timer || false;
		this.topPosition = options.topPosition || 100;
		this.redirectOnClose = options.redirectOnClose || false;
				
		triveAdminPopup.wrappers[triveAdminPopup.wrappers.length] = this.popupwrapper;
		triveAdminPopup.popups[triveAdminPopup.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);
			//3Soft change
			//if(this.deactivator){
			//	Event.observe(this.deactivator, 'click', this.deactivate.bindAsEventListener(this,null), false);
			//}
			//ctrl.onclick = function(){return false;};
		}
	},
	
	deactivate: function(){	    
	    //3Soft change
    	if(this.deactivator)
    	{
			Event.stopObserving(this.deactivator, 'click');
		}
		
		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.afterCloseFunction)
		{
		  this.afterCloseFunction(this);
		}		

		if(this.redirectOnClose) { 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 (this.beforeShowFunction)
		{
		    if (this.beforeShowFunction(this) == false)
		    {
    		    return;
	        }
		}
		
	    //3Soft change
    	if(this.deactivator)
    	{
			Event.observe(this.deactivator, 'click', this.deactivate.bindAsEventListener(this,null), 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
		{
			triveAdminPopup.wrappers.each(function(item){
				$(item).hide();
			});
			triveAdminPopup.popups.each(function(item){
				$(item).hide();
			});
			initPopup();
		}
		
		function initPopup(){
			
			$(self.popupwrapper).setStyle({
				display: 'block',
				position: 'absolute',
				top: self.topPosition + 'px',
				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); 
	}
	
}