
effects = new Object();

effects.setElementOpacity = function (elem, nOpacity) {
	var opacityProp = effects.getOpacityProperty();
	if (!elem || !opacityProp) return;
	if (opacityProp=="filter") {
		nOpacity *= 100;
		var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
		if (oAlpha) oAlpha.opacity = nOpacity;
			else elem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";
	} else elem.style[opacityProp] = nOpacity;
}

effects.getOpacityProperty = function() {
	if (typeof document.body.style.opacity == 'string') return 'opacity';
	else if (typeof document.body.style.MozOpacity == 'string') return 'MozOpacity';
	else if (typeof document.body.style.KhtmlOpacity == 'string') return 'KhtmlOpacity';
	else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) return 'filter';
	return false;
}

modal = new Object();

modal.getPageSize = function(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	SY = window.scrollY ? window.scrollY : Body.scrollTop; // Gecko / IE,Opera
	SX = window.scrollX ? window.scrollX : Body.scrollLeft;
	
	if (self.innerHeight) {
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight, SX, SY) 
	return arrayPageSize;
}

modal.hideBody = function() {
    
	this.pageSize = this.getPageSize();		
	this.shadowDiv = document.getElementById('shadow_body');
	if (!this.shadowDiv) {
		this.shadowDiv = document.createElement("DIV");
		this.shadowDiv.id = 'shadow_body';
		document.body.appendChild(this.shadowDiv);
	}
	this.shadowDiv.style.position = 'absolute';
	this.shadowDiv.style.top = '0px';
	this.shadowDiv.style.left = '0px';
	this.shadowDiv.style.width = this.pageSize[0]+'px';
	this.shadowDiv.style.height = this.pageSize[1]+'px';
	this.shadowDiv.style.backgroundColor = 'black';
	this.shadowDiv.style.display = 'block';
	this.shadowDiv.style.zIndex = '1000';
	this.shadowDiv.onclick = closeImage;
	effects.setElementOpacity(this.shadowDiv, 0.6);
}

modal.close = function() {
	this.modalDiv.style.display = 'none';
	this.shadowDiv.style.display = 'none';
}

modal.showImage = function(text, dWidth, dHeight) {

	Body = document.getElementsByTagName((document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY")[0];

	pageSize = this.getPageSize();
	this.hideBody();
	if (!dWidth) dWidth = 640;
	if (!dHeight) dHeight = 480;
	this.modalDiv = document.getElementById('modal_image');
	if (!this.modalDiv) {
		this.modalDiv = document.createElement("DIV");
		this.modalDiv.id = 'modal_image';
		document.body.appendChild(this.modalDiv);
	}
	this.modalDiv.style.position = 'absolute';
	this.modalDiv.style.zIndex = '10000';
	oLeft = Math.round((pageSize[2]/2) - (dWidth/2)) + pageSize[4];	
	oTop = Math.round((pageSize[3]/2) - (dHeight/2)) + pageSize[5];
	this.modalDiv.style.left =  oLeft + 'px';
	this.modalDiv.style.top =  oTop + 'px';
	this.modalDiv.style.width = dWidth + 'px';
	this.modalDiv.style.height = dHeight + 'px';
	this.modalDiv.style.backgroundColor = '#FFFFFF';
	this.modalDiv.innerHTML = text;
	this.modalDiv.style.display = 'block';
}

modal.hideAllSelect = function() {
	oSelects = document.getElementsByTagName('select');
	for (i = 0; i < oSelects.length; i++) {
		oSelects[i].style.visibility = 'hidden';
	}
}

modal.showAllSelect = function() {
	oSelects = document.getElementsByTagName('select');
	for (i = 0; i < oSelects.length; i++) {
		oSelects[i].style.visibility = 'visible';
	}
}

function showImage(img, iWidth, iHeight, iTitle) {
	modal.hideAllSelect();
    modal.showImage('<div style="height:40px; text-align:center; margin:5px 15px">'+iTitle+'</div><img onclick="return closeImage();" style="position:absolute; top:5px; right:5px; cursor:pointer; " src="img/ds/delete.icon.gif" alt="Закрыть" /><a href="#" onclick="return closeImage();"><img style="display:block; margin:0 10px 10px 10px" src="'+img+'" width="'+iWidth+'" height="'+iHeight+'" alt="Click to close" /></a>', iWidth+20, iHeight+60);
	return false;
}

function closeImage() {
    modal.showAllSelect();
    modal.close();
	return false;
}
