// JavaScript Document

function Style(){
	this.items = new Collection();
	this.styleString = null;
	this.getItem = function(rule){
		var index = this.items.firstIndex();
		while(index!=null){
			if(this.items.get(index).rule == rule){
				return this.items.get(index).value;
			}
			index = this.items.nextIndex();
		}
		return '';
	}
	this.setItem = function(rule, value){
		var index = this.items.firstIndex();
		while(index!=null){
			if(this.items.get(index).rule == rule){
				this.items.set(new StyleItem(rule,value),index);
				this.items.currentIndex = 0;
				return;
			}
			index = this.items.nextIndex();
		}
		this.items.add(new StyleItem(rule,value));	
	}
	this.setStyleString = function(_value){
		this.styleString = _value;	
	}
	this.toString = function(){
		var str = '';
		if(this.styleString != null){
			str = styleString;
			if(str.lastIndexOf(';') != str.length -1){
				str += ';';	
			}
		}
		var i = this.items.firstIndex();
		while(i!=null){
			str += this.items.get(i).toString();
			i = this.items.nextIndex();
		}
		if(str != ''){
			str = 'style="'+str+'"';	
		}
		return str;
	}
}

function StyleItem(_rule,_value){
	this.rule = _rule;	
	this.value = _value;
	this.toString = function(){
		return this.rule + ':' + this.value + ';';	
	}
}

//************** Events 
//*************************************************************************

function Events(){
	this.events = new Collection();
	this.set = function(event,action){
		var i = this.events.firstIndex();
		while(i!=null){
			if(this.events.get(i).event == event){
				this.events.get(i).action = action;
				this.events.currentIndex = 0;
				return;
			}
			i = this.events.nextIndex();
		}
		this.events.add(new AQUAEvent(event,action));
	}
	this.remove = function(event){
		var i = this.events.firstIndex();
		while(i!=null){
			if(this.events.get(i).event == event){
				this.events.remove(i);
				this.events.currentIndex = 0;
				return;
			}
			i = this.events.nextIndex();
		}
	}
	this.toString = function(){
		var str = '';
		var i = this.events.firstIndex();
		while(i!=null){
			str += this.events.get(i).toString() + ' ';
			i = this.events.nextIndex();
		}
		return str;
	}
}

function AQUAEvent(_event, _action){
	this.event = _event;
	this.action = _action;
	this.toString = function(){
		return this.event + '="' + this.action + '"';
	}
}

function getPageAttributes(){
	var xScroll, yScroll, windowWidth, windowHeight;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.scrollWidth;
		yScroll = (false ? parent.innerHeight : self.innerHeight) + (false ? parent.scrollMaxY : self.scrollMaxY);
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.getElementsByTagName("html").item(0).offsetWidth;
		yScroll = document.getElementsByTagName("html").item(0).offsetHeight;
		xScroll = (xScroll < document.body.offsetWidth) ? document.body.offsetWidth : xScroll;
		yScroll = (yScroll < document.body.offsetHeight) ? document.body.offsetHeight : yScroll;
	}
	if (self.innerHeight) {
		windowWidth = (false) ? parent.innerWidth : self.innerWidth;
		windowHeight = (false) ? parent.innerHeight : self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.getElementsByTagName("html").item(0).clientWidth;
		windowHeight = document.getElementsByTagName("html").item(0).clientHeight;
		windowWidth = (windowWidth == 0) ? document.body.clientWidth : windowWidth;
		windowHeight = (windowHeight == 0) ? document.body.clientHeight : windowHeight;
	}
	var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;
	return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
}

function getPageScroll() {
	if (self.pageYOffset) {
		return false ? parent.pageYOffset : self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		return document.documentElement.scrollTop;
	} else if (document.body) {
		return document.body.scrollTop;
	}
}

function Collection(){
	this.index = 0;
	this.currentIndex = 0;
	this.items = new Array();
	this.add = function(item){
		var _index = this.index;
		this.items[_index] = item;
		this.index++;	
		return _index;
	}
	this.set = function(item,_index){
		this.items[_index] = item;
	}
	this.get = function(_index){
		return this.items[_index];
	}
	this.size = function(){
		return this.items.length;
	}
	this.remove = function(index){
		this.items.splice(index, 1);
		this.index--;
	}
	this.clear = function(){
		this.items = new Array();
		this.index = 0;
		this.currentIndex = 0;
	}
	this.firstIndex = function(){
		if(this.index == 0){
			return null;	
		}else{
			this.currentIndex = 1;
			return 0;
		}
	}
	this.nextIndex = function(){
		if(this.currentIndex == this.index){
			this.currentIndex = 0;
			return null;
		}else{
			var _item = this.items[this.currentIndex];
			this.currentIndex++;
			if(_item){
				return this.currentIndex - 1; 
			}
			return this.nextIndex();
		}
	}
}


//************** DIALOG
//*************************************************************************

var AQUA_Dialog_focusedElement = null;

function DialogConfig(){
	this.showHeader = true;
	this.showTitle = true;
	this.showCloseButton = true;
	this.closeOnOutClick = false;
	this.alertOnRender = true;
	this.alertOnOutClick = true;
	this.dialogCss = 'aqua-dialog-div';
	this.dialogOffCss = 'aqua-dialog-div-off';
	this.dialogStyle = new Style();
	this.dialogStyle.setItem('width', '500px');
	this.dialogStyle.setItem('height', '100px');
	this.headerStyle = new Style();
	this.backgroundStyle = new Style();
	this.backgroundCss = 'aqua-screen-transblock';
	this.actionOnCss = 'dialog-action-on';
	this.actionCss = 'dialog-action';
}

function Dialog(_config){
	if(!_config)_config = new DialogConfig();
	this.config = _config;
	this.actions = new Collection();
	this.html;
	this.autoFit = false;
	this.image;
	this.innerhtml = '';
	this.focusableItems = new Array();
	this.addAction = function(text, action, img){
		this.actions.add(new DialogAction(text, img, action));
	}
	this.prepare = function(){
		var div = document.createElement('div');
		div.id = 'screen_block';
		div.className = this.config.backgroundCss;
		div.style.height = getPageAttributes()[1] + 'px';
		var container = document.createElement('div');
		container.id = 'dialog_container';
		container.className = 'aqua-dialog-container';
		var dialog = document.createElement('div');
		dialog.id = 'dialog_div';
		var style = this.config.dialogStyle.toString().replace('style="', '').replace('"', '');
		container.innerHTML = '<table class="table" id="table_dialog" cellpadding="0" border="0" cellspacing="0" style="width:100%; height:'+parseInt(getPageAttributes()[3])+'px">'+
							  ' 	<tr><td colspan="2" id="cell_scroll"></td></tr>'+
							  '    <tr><td style="width:1px;"></td>'+
							  '    <td id="cell_dialog"></td></tr>'+
							  ' 	<tr><td colspan="2" id="cell_bottom"></td></tr>'+
							  '</table>';
		container.onclick = function(event){
			var src = null;
			if(window.event){
				src = window.event.srcElement;
			}else{
				src = event.target;	
			}
			if(src.id == 'cell_dialog' || src.id == 'cell_scroll' || src.id == 'cell_bottom' || src.id == 'aqua_dialog_contentiner'){
				alert_dialog(4,true);	
			}
		}
		div.onclick = function(event){
			var src = null;
			if(window.event){
				src = window.event.srcElement;
			}else{
				src = event.target;	
			}
			if(src.id == 'screen_block'){
				alert_dialog(4,true);	
			}
		}
		container.style.height = getPageAttributes()[1] + 'px';
		_('body','tag')[0].appendChild(div);
		_('body','tag')[0].appendChild(container);
		_('cell_scroll').style.height = getPageScroll() + 'px';
		_('cell_dialog').style.height = getPageAttributes()[3] + 'px';
		_('cell_bottom').style.height = (parseInt(getPageAttributes()[1]) - parseInt(getPageScroll()) - parseInt(getPageAttributes()[3])) + 'px';
		document.getElementById('cell_dialog').appendChild(dialog);
	}
	this.update = function(){
		var dialog = _('dialog_div');
		var style = this.config.dialogStyle.toString().replace('style="', '').replace('"', '');
		dialog.style.cssText = style;
		dialog.setAttribute('style', style);
		document.getElementById('cell_dialog').appendChild(dialog);	  
	}
	this.hide = function(){
		if(_('dialog_container')){
			_('body','tag')[0].removeChild(_('dialog_container'));
			_('body','tag')[0].removeChild(_('screen_block'));
			var nav = navigator.userAgent;
			if(nav.indexOf('MSIE') != -1){			
				var b = _('body', 'tag')[0];
				b.onkeydown = function(){window.event.returnValue = true;}
			}else{
				window.onkeydown = function(evt){return true;}
			}
		}
		//AQUA_toggleFlash('visible');
		window.onresize = function(){}
	}
	this.isVisible = function(){
		return _('dialog_container');
	}
	this.render = function(){
		//AQUA_toggleFlash('hide');
		if(!_('screen_block')){
			this.prepare();
		}else{
			//this.update();	
		}
		var html = '';
		var image_html = '';
		if(this.image){
			image_html = '<tr><td><table class="table" cellpadding="0" cellspacing="0" border="0"><tr><td class="aqua_dialog_image"><img src="'+this.image+'" /></td><td class="aqua_dialog_title" style="width:100%">'+this.html+'</td></tr></table></td></tr>';
		}else if(this.innerhtml){
			image_html = '<tr><td><table class="table" cellpadding="0" cellspacing="0" border="0"><tr><td class="aqua_dialog_image"><img src="../img/chat.png" /></td><td class="aqua_dialog_title" style="width:100%">'+this.html+'</td></tr></table></td></tr>';
		}
		var style_box = '';
		if(!this.config.autoFit){
			style_box += "width:" + this.config.dialogStyle.getItem('width') + ";";
			style_box += "height:" + this.config.dialogStyle.getItem('height') + ";";
		}
		var actions_height = '0px';
		var actions_html = '';
		if(this.actions.size() > 0){
			actions_height = '40px';
			var actions = '';
			var index = this.actions.firstIndex();
			var sep = '';
			while(index!=null){
				var style = '';
				if(this.actions.get(index).image){
					style = 'style="background-image:URL('+this.actions.get(index).image+')"';
				}
				actions += sep + '<td><input onclick="'+this.actions.get(index).action+'" onmouseover="this.className = \''+this.config.actionOnCss+'\'" onmouseout="this.className = \''+this.config.actionCss+'\'" type="button" class="'+this.config.actionCss+'" value="'+this.actions.get(index).text+'" '+style+'a /></td>';
				sep = '<td style="width:20px"></td>';
				index = this.actions.nextIndex();
			}
			actions_html = '<div style="width:100%;"><table  class="table" cellspacing="0" cellpadding="0" border="0" style="margin:0 auto;">'+
					   '	<tr>'+actions +
					   '	</tr>'+
					   '</table></div>';
			
		}
		var html =		  '<div id="aqua_dialog_contentiner" style="padding:20px">'+
						  '	<table  class="table" cellpadding="0" cellspacing="0" border="0" style="margin:0 auto">'+
						  '		<tr>'+
						  '			<td style="background-image:url(../img/red-bg.png); width:20px; height:20px;"></td>'+
						  '			<td style="background-image:url(../img/red-bgcolor.png);"></td>'+
						  '			<td style="background-image:url(../img/red-bg.png); background-repeat:no-repeat; width:20px; height:20px; background-position:-20px 0px"></td>'+
						  '		</tr>'+
						  ' 		<tr>'+
						  '			<td style="background-image:url(../img/red-bgcolor.png); width:20px;"></td>'+
						  '			<td class="aqua_active_dialog" id="aqua_dialog_content" style="'+style_box+'">'+
						  '				<table  class="table" cellpadding="0" cellspacing="0" border="0">'+ image_html + 
						  '					<tr><td class="aqua_dialog_html">'+this.innerhtml+'</td></tr>'+
						  '					<tr><td class="aqua_dialog_actions">'+actions_html+'</td></tr>'+
						  '				</table>'+
						  '			</td>'+
						  '			<td style="background-image:url(../img/red-bgcolor.png); width:20px;"></td>'+
						  '		</tr>'+
						  '		<tr>'+
						  '			<td style="background-image:url(../img/red-bg.png); width:20px; height:20px; background-position:0px -20px"></td>'+
						  '			<td style="background-image:url(../img/red-bgcolor.png);"></td>'+
						  '			<td style="background-image:url(../img/red-bg.png); width:20px; height:20px; background-position:20px -20px"></td>'+
						  '		</tr>'+
						  '	</table>'+
						  '</div>';
		_('dialog_div').innerHTML = html;
		var nav = navigator.userAgent;
		if(nav.indexOf('MSIE') != -1){
			_('dialog_div').focus();
			var b = _('body', 'tag')[0];
			b.onkeydown = function(){
				var evt = window.event;	
				var charCode = (evt.which) ? evt.which : evt.keyCode;
				if(charCode == 9){
					AQUA_CD_focusedElement.setAttribute('focused', null);
					AQUA_CD_focusedElement = document.activeElement;
					AQUA_CD_focusedElement.setAttribute('focused', "focused");
					AQUA_CD_focusElement();	
					evt.cancelBubble = true;
					evt.returnValue = false;
				}			
			}
		}else{
			document.getElementsByTagName("body")[0].blur();
			window.onkeydown = function(evt){
				var charCode = (evt.which) ? evt.which : evt.keyCode;
				if(charCode == 9){
					AQUA_CD_focusedElement.setAttribute('focused', null);
					AQUA_CD_focusedElement = document.activeElement;
					AQUA_CD_focusedElement.setAttribute('focused', "focused");
					AQUA_CD_focusElement();
					evt.preventDefault();
				}
			}
		}
		AQUA_CD_focusedElement = null;
		AQUA_CD_focusElement();
		alert_dialog(4, true);
		window.onresize = function(){
			_('screen_block').style.height = getPageAttributes()[1] + 'px';
			_('dialog_container').style.height = getPageAttributes()[1] + 'px';
			if((parseInt(getPageAttributes()[1]) - parseInt(getPageScroll()) - parseInt(getPageAttributes()[3])) > 0){
				_('cell_bottom').style.height = (parseInt(getPageAttributes()[1]) - parseInt(getPageScroll()) - parseInt(getPageAttributes()[3])) + 'px';
				_('cell_scroll').style.height = getPageScroll() + 'px';
				_('cell_dialog').style.height = getPageAttributes()[3] + 'px';
			}
		}
	}
}

function alert_dialog(i, v){
	if(_('aqua_dialog_content')){
		if(v){
			_('aqua_dialog_content').className = 'aqua_active_dialog';
		}else{
			_('aqua_dialog_content').className = 'aqua_inactive_dialog';
		}
		if(i>0){
			window.setTimeout('alert_dialog('+(i-1)+', '+!v+')', 50);
		}
	}
}

Dialog.prototype.disableActions = function(){
	var i = 0;
	while(_('dialog-action-' + i)){
		_('dialog-action-' + i).disabled = true;
		i++;
	}
}

Dialog.prototype.enableActions = function(){
	var i = 0;
	while(_('dialog-action-' + i)){
		_('dialog-action-' + i).disabled = false;
		i++;
	}
}


function DialogAction(_text, _image, _action){
	this.image = _image;
	this.text = _text;
	this.action = _action;
}


function AQUA_CD_focusElement(){
	AQUA_CD_setNextFocusedElement();
	if(AQUA_CD_focusedElement){
		AQUA_CD_focusedElement.focus();
	}else{
		AQUA_CD_setNextFocusedElement();
		if(AQUA_CD_focusedElement){
			AQUA_CD_focusedElement.focus();	
		}
	}
}

function AQUA_CD_setNextFocusedElement()
{
  var domElement = _('dialog_div');
  AQUA_CD_setNextFocusedElement_rec(domElement, 1);
}

function AQUA_CD_setNextFocusedElement_rec(currentElement, depth)
{
  if (currentElement)
  {
    var j;
    var tagName=currentElement.tagName;
	if(tagName){
		var disabled = currentElement.disabled;
		if(tagName.toLowerCase() == 'a' || tagName.toLowerCase() == 'input' || tagName.toLowerCase() == 'select' || tagName.toLowerCase() == 'textarea' || tagName.toLowerCase() == 'button' || tagName.toLowerCase() == 'object'){
			if(!disabled && !AQUA_isHidden(currentElement)){
				if(AQUA_CD_focusedElement == null){
					AQUA_CD_focusedElement = currentElement;
					AQUA_CD_focusedElement.setAttribute('focused', "focused");
					return;
				}else if(currentElement.getAttribute('focused') != null){
					if(currentElement.getAttribute('focused') == "focused"){
						currentElement.setAttribute('focused', null);
						AQUA_CD_focusedElement = null;
					}
				}
			}
		}
	}
    var i=0;
    var currentElementChild=currentElement.childNodes[i];
    while (currentElementChild)
    {
      AQUA_CD_setNextFocusedElement_rec(currentElementChild, depth+1);
      i++;
      currentElementChild=currentElement.childNodes[i];
    }
  }
}

function AQUA_isHidden(el){
	while(el){
		if(el.style){
			if(el.style.visibility == 'hidden' || el.style.display == 'none'){
				return true;	
			}
		}
		el = el.parentNode;
	}
	return false;
}
