/**
*@author Denis Lemeshko <dlemeshko@intersog.com>
*/

var dialog;

function showPopupDIV(title, DIVname) {

	var infotext = document.getElementById(DIVname).innerHTML;
	dialog = new SimpleDialog(title,infotext);
	dialog.show();

}

function ajaxLoad(title, scriptName) {
	var infotext = '<div id="divLoader">' +
	'<div align=center>' +
	'<img src="img/ajax_loading.gif" style="background-color:transparent">' +
	'</div>'
	'</div>';
	dialog = new SimpleDialog(title,infotext);
	dialog.show();
	popupLoad(scriptName);
}

function showPopup(title, text) {


	dialog = new SimpleDialog(title,text);
	dialog.show();

}

function CloseGo (url) {
	dialog.hide();
	window.location=url;
}

function SimpleDialog(titleText,infoText){

	this.settings = {
		background_color      :'#777777',
		title_background_color:'#990000',
		text_background_color :'#555555',
		font_family           :'Arial',
		font_size             :'11'
	};

	this.titleHeight = 15;

	this.titleText = titleText;

	this.infoText = infoText;

	this.is_ie = typeof(document.all) != 'undefined';
	var opacity = (this.is_ie) ? "filter" : "opacity";

	this.changeOpacity = function (node, percent){
		percent = (this.is_ie) ? "alpha(opacity=" + percent + ")" : percent/100;
		node.style[opacity] = percent;
	}

	this.createBackground = function(){
		var div = document.createElement('div');
		div.style.position = 'absolute';
		div.style.top='0px';
		div.style.left='0px';
		div.style.width= this.getScreenWidth() + 'px';
		div.style.height=this.getScreenHeight() + 'px';
		div.style.backgroundColor = this.settings['background_color'];
		return div;
	}

	this.getScreenWidth = function(){
		return (document.body.scrollWidth > document.body.offsetWidth)?document.body.scrollWidth:document.body.offsetWidth;
	}

	this.getScreenHeight = function(){
		return (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
	}

	this.getWindowPositionTop = function() {
		return parseInt(this.getClientHeight()/2)+this.getBodyScrollTop();
	}

	this.getWindowPositionLeft = function() {
		return parseInt(this.getClientWidth()/2)+this.getBodyScrollLeft();
	}

	this.getClientWidth = function() {
		return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
	}

	this.getClientHeight = function() {

		return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	}

	this.getBodyScrollTop = function()
	{
		return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
	}

	this.getBodyScrollLeft = function() {
		return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
	}

	
	this.getScrollWidth = function()
	{
		var w = window.pageXOffset ||
		document.body.scrollLeft ||
		document.documentElement.scrollLeft;

		return w ? w : 0;
	}

	this.getScrollHeight = function()
	{
		var h = window.pageYOffset ||
		document.body.scrollTop ||
		document.documentElement.scrollTop;

		return h ? h : 0;
	}

	this.getBackground = function(){
		if(typeof this.background == 'undefined'){
			return this.background = this.createBackground();
		}else{
			return this.background;
		}
	}

	this.createElement = function (){
		var background = this.getBackground();
		return background;
	}

	this.getElement = function(){
		if(typeof this.element == 'undefined'){
			return this.element = this.createElement();
		}else{
			return this.element;
		}
	}


	this.makeCancelButton = function(){
		var button = document.createElement('input');
		button.type = 'button';
		button.value = 'Скасувати';

		button.onclick = this.cancelAction;

		button.onmouseout=function(e){
			this.className='button_pas'
		}
		button.onmouseover=function(e){
			this.className='button_act'
		}
		button.className = 'button_pas';
		var center = document.createElement('CENTER');
		center.appendChild(button);
		return center;
	}

	this.createInfoPar = function(){
		var par = document.createElement('P');
		par.innerHTML = this.infoText;
		return par;
	}

	/**
	*/

	this.createMessageTable = function(){

		var result = '<table class=modal width="300" border="0" cellspacing="0" cellpadding="0">';
		result += '<tr>';
		result += '<td width="5" height="4" align="left" valign="top"><img src="img/popup/popup_03.gif" width="5" height="4" /></td>';
		result += '<td style="background-image: url(img/popup/popup_05.gif);';
		result += 'background-repeat: repeat-x;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td width="10" height="4" align="left" valign="top"><img src="img/popup/popup_07.gif" width="10" height="4" /></td>';
		result += '<td width="4" height="4" align="right" valign="top"><img src="img/popup/popup_08.gif" width="4" height="4" /></td>';
		result += '</tr>';
		result += '<tr>';
		result += '<td style="background-image: url(img/popup/popup_15.gif);';
		result += 'background-repeat: repeat-y;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td valign="top" style="color:white" background="img/popup/popup_16.gif"><img src="img/popup/spacer.gif" width="1" height="1" />';

		result += this.titleText;

		result += '</td>';
		result += '<td width="10" height="8" valign="top" background="img/popup/popup_16.gif"><img onclick="dialog.hide();" border=0 src="img/popup/popup_12.gif" width="10" height="8" style="cursor: pointer" /></td>';
		result += '<td style="background-image: url(img/popup/popup_14.gif);';
		result += 'background-repeat: repeat-y;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '</tr>';
		result += '<tr>';
		result += '<td style="background-image: url(img/popup/popup_15.gif);';
		result += 'background-repeat: repeat-y;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td background="img/popup/popup_16.gif"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td background="img/popup/popup_16.gif"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td width="4" height="2" align="right" valign="top" style="background-image: url(img/popup/popup_14.gif);';
		result += 'background-repeat: repeat-y;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '</tr>';
		result += '<tr>';
		result += '<td width="5" height="2" align="left" valign="top"><img src="img/popup/popup_21.gif" width="5" height="2" /></td>';
		result += '<td style="background-image: url(img/popup/popup_22.gif);';
		result += 'background-repeat: repeat-x;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td style="background-image: url(img/popup/popup_22.gif);';
		result += 'background-repeat: repeat-x;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td width="4" height="2" align="right" valign="top"><img src="img/popup/popup_25.gif" width="4" height="2" /></td>';
		result += '</tr>';
		result += '<tr>';
		result += '<td height="21" style="background-image: url(img/popup/popup_32.gif);';
		result += 'background-repeat: repeat-y;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td valign="top" background="img/popup/popup_35.gif">';
		result += '<br>';

		result += this.infoText;

		result += '<br><br>';
		result += '</td>';
		result += '<td background="img/popup/popup_35.gif"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td style="background-image: url(img/popup/popup_30.gif);';
		result += 'background-repeat: repeat-y;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '</tr>';
		result += '<tr>';
		result += '<td width="5" height="5" align="left" valign="bottom"><img src="img/popup/popup_34.gif" width="5" height="5" /></td>';
		result += '<td style="background-image: url(img/popup/popup_37.gif);';
		result += 'background-repeat: repeat-x;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td style="background-image: url(img/popup/popup_37.gif);';
		result += 'background-repeat: repeat-x;"><img src="img/popup/spacer.gif" width="1" height="1" /></td>';
		result += '<td width="4" height="5" align="right" valign="bottom"><img src="img/popup/popup_39.gif" width="4" height="5" /></td>';
		result += '</tr>';
		result += '</table>';

		var div = document.createElement('DIV');
		div.innerHTML = result;
		div.style.position = 'absolute';
		div.style.top=this.getWindowPositionTop() - 150;
		div.style.left=this.getWindowPositionLeft() - 150;
		return div;
	}




	this.getMessageTableLeft = function(){
		var width = this.getMessageTableWidth();
		return Math.round((this.getScreenWidth() - width)/2);
	}

	this.getMessageTableTop = function(){
		var height = this.getMessageTableHeight();
		return Math.round((document.body.offsetHeight - height)/2);
	}

	this.getMessageTableWidth = function(){
		return 300;Math.round(this.getScreenWidth() / 4);
	}

	this.getMessageTableHeight = function(){
		return 100;Math.round(document.body.offsetHeight / 8);
	}

	this.getMessageTable = function(){
		if(typeof this.messageTable == 'undefined'){
			return this.messageTable = this.createMessageTable();
		}else{
			return this.messageTable;
		}
	}

	this.changeTitle = function(newTitle){
		this.tdTitle.innerHTML = newTitle;
	}

	this.changeText = function(newText){
		this.tdInfo.innerHTML = newText;
	}

	this.show = function (){
		document.body.appendChild(this.getElement());
		//-----------------------------------------------
		this.changeOpacity(this.background,30);

		document.body.appendChild(this.getMessageTable());
	}

	this.hide = function (){
		document.body.removeChild(this.getElement());
		document.body.removeChild(this.getMessageTable());
	}
}
