/**
 * 
 * 
 * ---------------------------------------------------------------------------
 * 
 * Copyright (C) 2007 Omnium Research Group
 * 
 * ---------------------------------------------------------------------------
 * 
 * LICENSE:
 * 
 * This file is part of Omnium(R) Software.
 * 
 * Omnium(R) Software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Omnium(R) Software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Omnium(R) Software; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * ---------------------------------------------------------------------------
 * 
 * @author    Sam Bauers <sam@omnium.net.au>
 * @copyright 2007 Omnium Research Group
 * @license   http://www.gnu.org/licenses/gpl.txt GNU GPL v2
 * @link      http://open.omnium.net.au Omnium Open
 **/


// Is available on all pages, to use just call Alert.fire();
var omAlert = Class.create();
omAlert.prototype = {
	initialize: function()
	{
	},
	
	fire: function(textMain, iconMain, textRight, iconRight, actionRight, textLeft, iconLeft, actionLeft)
	{
		
		// Set passed parameters
		this.textMain = textMain;
		this.iconMain = iconMain;
		this.actionRight = actionRight;
		this.textLeft = textLeft;
		this.actionLeft = actionLeft;
		
		if (!textRight) {
			this.textRight = omVars.get('alertOK');
		} else {
			this.textRight = textRight;
		}
		if (!iconRight) {
			this.iconRight = 'generic_tick.png';
		} else {
			this.iconRight = iconRight;
		}
		this.iconRight = '<img src="' + omVars.get('alertIconDir') + this.iconRight + '" class="icon" alt="" />';
		
		if (textLeft && !iconLeft) {
			this.iconLeft = 'generic_cross.png';
		} else {
			this.iconLeft = iconLeft;
		}
		
		// Set the keypress observer bind
		this.bKeypress = this.observer.bindAsEventListener(this);
		// Add a keypress event listener
		Event.observe(document, 'keypress', this.bKeypress);
		// Call the build function
		this.build();
		
	},
	
	// Builds the alert
	build: function()
	{
		
		// MAIN TEXT
		if (this.textMain) {
			
			// Build the icon
			if (this.iconMain) {
				this.iconMain = '<img src="' + omVars.get('alertIconDir') + this.iconMain + '" class="icon" alt="" />';
			}
			
			var alertText = $('omAlertText').update();
			
			// Insert the icon
			if (this.iconMain) {
				alertText.insert(this.iconMain);
			}
			
			// Insert the main text
			alertText.insert('<div id="omAlertTextInner">' + this.textMain + '</div>');
			
		}
		
		// RIGHT ACTIONS
		// Set the link
		var hrefRight = 'javascript:void(0);';
		// If we are dealing with a url
		if (this.actionRight && (typeof(this.actionRight) != 'function')) {
			if (this.actionRight.startsWith('http')) {
				hrefRight = this.actionRight;
				this.actionRight = false;
			}
		}
		
		// Build the link
		rightLink = '<a href="' + hrefRight + '">' + this.textRight + this.iconRight + '</a>';
		alertRight = $('omAlertRight').update(rightLink);
		this.rightLink = alertRight.down('a');
		
		// Set an event observer for the link
		this.bRightLink = this.close.bindAsEventListener(this, this.actionRight);
		this.rightLink.observe('click', this.bRightLink);
		
		// LEFT ACTIONS
		if (this.textLeft) {
			
			// Build the icon
			var leftIcon = '<img src="' + omVars.get('alertIconDir') + this.iconLeft + '" class="icon" alt="" />';
			
			// Set the link
			var hrefLeft = 'javascript:void(0);';
			// If we are dealing with a url
			if (this.actionLeft && this.actionLeft.startsWith('http')) {
				hrefLeft = this.actionLeft;
				this.actionLeft = false;
			}
			
			// Build the link
			leftLink = '<a href="' + hrefLeft + '">' + leftIcon + this.textLeft + '</a>';
			alertLeft = $('omAlertLeft').update(leftLink);
			this.leftLink = alertLeft.down('a');
			alertLeft.show();
			
			// Set an event observer for the link
			this.bLeftLink = this.close.bindAsEventListener(this, this.actionLeft);
			this.leftLink.observe('click', this.bLeftLink);
			
		} else {
			
			$('omAlertLeft').update('');
			$('omAlertLeft').hide();
			
		}
		
		$('omBlockAlertScreen').show();
		$('omBlockAlert').show();
		//$('omAlertFocusThief').show();
		$('omAlertFocusThief').focus();
		
	},
	
	observer: function(e)
	{
		
		var action;
		var completed;
		
		// Get the type of key code
		switch (e.keyCode) {
			case Event.KEY_RETURN:
				action = this.actionRight;
				completed = true;
				break;
			case Event.KEY_ESC:
				action = this.actionLeft;
				completed = true;
				break;
		}
		
		// Close the window
		if (completed) {
			this.close(false, action);
		}
		
	},
	
	// Closes the alert and evaluates any actions
	close: function(e, action)
	{
		// Hide the alert
		$('omAlertLeft').hide();
		$('omBlockAlertScreen').hide();
		$('omBlockAlert').hide();
		
		// Stop observing keypresses
		Event.stopObserving(document, 'keypress', this.bKeypress);
		this.rightLink.stopObserving('click', this.bRightLink);
		
		if (this.leftLink) {
			this.leftLink.stopObserving('click', this.bLeftLink);
		}
		
		if (action) {
			if (typeof(action) == 'function') {
				action.call();
			} else {
				eval(action);
			}
		}
		
	}
}

var Alert = new omAlert();