/*
	
	Error Box
	
*/
var ErrorBox = Class.create({
	_errors : [],
	_error_ref : {},
	/*
		Initialize
	*/
	initialize : function() {
		this._id 		= 'error-overlay';
		this._wrap_id	= 'error-wrap';
		this._error_box	= 'errors';
		this._text_id 	= 'error-text';
		this._close 	= 'errors-close';
  	},
	show : function () {
		/* outer */
		error_overlay = document.createElement('div');
		error_overlay.id = this._id;
		
		error_wrap = document.createElement('div');
		error_wrap.id = this._wrap_id;
		
		error_box = document.createElement('div');
		error_box.id = this._error_box;
		
		error_box_inner = document.createElement('div');
		error_box_inner.id = this._error_box+'-inner';
		
		error_box_head = document.createElement('div');
		error_box_head.id = this._error_box+'-head';
		
		head_img = document.createElement('img');
		path = '';
		if (typeof(bp_path) != 'undefined') {
			path = bp_path;
		}
		head_img.src = bp_path+'ap/images/error_box/head_txt.gif';
		head_img.alt = 'Oops! Please correct the following...';
		
		error_text = document.createElement('div');
		error_text.id = this._text_id;
		
		/* close button */
		error_close 		= document.createElement('a');
		error_close.href 	= '#close';
		error_close.title 	= 'Close';
		error_close.id 		= this._close;
		error_close_text 	= document.createTextNode('close');
		
		error_close_span = document.createElement('span');
		error_close_span.appendChild(error_close_text);
		
		/* appendage */
		error_close.appendChild(error_close_span);
		error_box.appendChild(error_box_inner);
		error_box_head.appendChild(head_img);
		error_box_inner.appendChild(error_box_head);
		error_box_inner.appendChild(error_text);
		error_box_inner.appendChild(error_close);
		
		error_wrap.appendChild(error_box);
		
		document.body.appendChild(error_overlay);
		document.body.appendChild(error_wrap);
		
		Event.observe($(this._close), 'click', this.close.bindAsEventListener(this), false);
		Event.observe($(this._wrap_id), 'click', this.close.bindAsEventListener(this), false);
		
		error_list = document.createElement('ul');
		
		for (i = 0; i < this._errors.length; i++) {
			text = this._error_ref[this._errors[i]];
			error = document.createElement('li');
			error_text = document.createTextNode(text);
			error.appendChild(error_text);
			error_list.appendChild(error);
		}
		$(this._text_id).appendChild(error_list);
		
		// site vertically and ensure overlay covers screen
		this.resize();
		// add resize listener
		this._resize = this.resize.bindAsEventListener(this);
		Event.observe(window, 'resize', this._resize);
		
		// hide select elements for IE
		this.hideSelects('hidden');
	},
	addError : function (error) {
		if (this._errors.indexOf(error) == -1) {
			this._errors.push(error);
		}
	},
	removeError : function (error) {
		if (this._errors.indexOf(error) != -1) {
			for ( i = 0 ; i < this._errors.length; i++ ){
				if (this._errors[i] == error) this._errors.splice (i, 1);
			}
		}
	},
	evalError : function (result, name) {
		result ? this.addError(name) : this.removeError(name);
	},
	close : function (event) {
		Event.stop(event);
		$(this._text_id).remove();
		$(this._id).remove();
		$(this._wrap_id).remove();
		this._errors = [];
		Event.stopObserving(window, 'resize', this._resize);
		this.hideSelects('visible');
	},
	hideSelects : function (action) {
		if ( action != 'visible' ){ action='hidden'; }
		if ( navigator.appName.indexOf("Microsoft") >= 0 || navigator.appName.indexOf("MSIE") >= 0 ) {
			
			selects = document.body.getElementsByTagName('SELECT');
				
			for (var i = 0; i < selects.length; i++){
				selects[i].style.visibility = action;
			}
		}
	},
	getPageScroll : function () {

		var yScroll;
	
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
	
		arrayPageScroll = new Array('',yScroll) 
		return arrayPageScroll;
	},
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	getPageSize : function () {
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	},
	resize : function () {
		arrayPageSize = this.getPageSize();
		arrayPageScroll = this.getPageScroll();
		
		$(this._id).style.height 		= (arrayPageSize[1] + 'px');
		$(this._wrap_id).style.height 	= (arrayPageSize[1] + 'px');
		
		var errorsTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - Element.getHeight(this._error_box)) / 2);
		$(this._error_box).style.top = (errorsTop < 0) ? "0px" : errorsTop + "px";
	}
});