function DarkDimmer()
{
	this.dark = null;
	this.prev = null;
	this.prev_width = null;
	this.prev_height = null;
	this.fade = true;
	this.headButtons = false;
	this.fadeInTimeDimmer = null;
	this.fadeOutTimeDimmer = null;
	this.fadeInTimePrev = null;
	this.fadeOutTimePrev = null;
	this.buttonTextApply = null;
	this.buttonTextClose = null;
	this.secondDivWidth = null;
	this.secondDivHeight = null;
	this.workEl = null;
}

DarkDimmer.prototype.setValue = function(key, value)
{
	this[key] = value;
}


DarkDimmer.prototype.windowHeight =  function() {
	var de = document.documentElement;
	return self.innerHeight ||
		( de && de.clientHeight ) ||
		document.body.clientHeight;
}

DarkDimmer.prototype.windowWidth =  function() {
	var de = document.documentElement;
	return self.innerWidth ||
		( de && de.clientWidth ) ||
		document.body.clientWidth;
}

DarkDimmer.prototype.hideOverlay =  function()
{
	if (this.fade)
	{
		this.fadeOut(this.dark, 0, this.fadeOutTimeDimmer);
		this.fadeOut(this.prev, 0,this.fadeOutTimePrev);
	}
	else
	{
		hide(this.dark);
		hide(this.prev);
	}
}
DarkDimmer.prototype.pageHeight= function() {
	return document.body.scrollHeight;
}

DarkDimmer.prototype.pageWidth =  function() {
	return document.body.scrollWidth;
}


	function setOpacitys( elem, level ) {
		if ( elem.filters )
			elem.filters.alpha.opacity = level;
		else
			elem.style.opacity = level / 100;
	}

function hide( elem ) {
	var curDisplay = elem.style.display;
	if ( curDisplay != "none" )
		elem.$oldDisplay = curDisplay;
	elem.style.display = "none";
}

DarkDimmer.prototype.show =  function ( elem ) {
	elem.style.display = elem.$oldDisplay || "block";
}


DarkDimmer.prototype.fadeIn =  function ( elem, to, speed ) {
	setOpacitys( elem, 0 );
	this.show( elem );
	for ( var i = 0; i <= 100; i += 5 ) {
		(function(){
		 var opacity = i;
		 setTimeout(function(){
			 setOpacitys( elem, ( opacity / 100 ) * to );
			 }, ( i + 1 ) * speed );
		 })();
	}
}

DarkDimmer.prototype.fadeOut =  function ( elem, to, speed ) {
	for ( var i = 0; i < 100; i += 5 ) {
		(function(){
		 var opacity = i;
		 setTimeout(function(){
			 setOpacitys(elem, 100 - opacity );
			 if ( opacity == 95 )
			 hide( elem );
			 }, ( i + 1 ) * speed );
		 })();
	}
}

DarkDimmer.prototype.adjust =  function (id)
{
	var obj = getEl(id);
	if ( !obj ) return;
	var w = parseInt(obj.style.width);
	var h = parseInt(obj.style.height);
	var t =  this.windowHeight() / 2  - ( h / 2 );
	if ( t < 0 ) t = 0;
	var l =   this.windowWidth() / 2  - ( w / 2 );
	if ( l < 0 ) l = 0;
	obj.style.top = t+"px";
	obj.style.left = l+"px";
}

DarkDimmer.prototype.buildDimmer = function()
{
	if (!this.dark)
	{
		var d = document.createElement("div");
		d.setAttribute("id", "overlay");
		d.setAttribute("class", "darkDimmer");
		d.setAttribute("className", "darkDimmer");
		document.body.appendChild(d);
		this.dark = d;
	}
	this.setCordinate();
	if(this.fade)
	{
		this.fadeIn(this.dark, 70,this.fadeInTimeDimmer );
	}
	else
	{
		this.show(this.dark);
	}

}

DarkDimmer.prototype.buildPrev = function()
{
	if(!this.prev)
	{
		var d = document.createElement("div");
		d.setAttribute("id", "over_preview");
		d.setAttribute("class", "preview");
		d.setAttribute("className", "preview");
		document.body.appendChild(d);
		d.style.height = this.prev_height;
		d.style.width = this.prev_width;
		this.prev = d;
		if (this.headButtons)
		{
			this.prev.appendChild(this.buildButtons());
			this.workEl = getEl("preview_content");
		}
		else
		{
			this.workEl = this.prev;
		}
	}
	//        setContent(this.workEl);
	this.adjust("over_preview");
	if(this.fade)
	{
		this.fadeIn(this.prev, 100, this.fadeInTimePrev);
	}
	else
	{
		this.show(this.prev);
	}
}

DarkDimmer.prototype.buildButtons = function()
{
	var t = document.createElement("table");
	t.width="100%";
	t.border="0px";
	t.cellpadding="0";
	t.cellspacing="0";
	var r = t.insertRow(-1);
	var c1 = r.insertCell(-1);
	c1.align="left";
	c1.width = "50%";
	c1.setAttribute("valign", "top");
	c1.innerHTML = "<input type='button' id='prev_button' class='wp_button_main_left' value='' onClick='javascript:useButtonClick();' />";
	var c2 = r.insertCell(-1);
	c2.align="left";
	c2.width = "50%";
	c2.setAttribute("valign", "top");
	c2.innerHTML="<input type='button' id='next_button' class='wp_button_main_right' style='font-weight:bold' value='' onclick='javascript: ' />";
	var r1 = t.insertRow(-1);
	var c3 = null;
	if(is_MSIE)
	{
		c3 = document.createElement("td");
		c3.colSpan = 2;
		r1.appendChild(c3);
	}
	else
	{
		c3 = r1.insertCell(-1);
		c3.setAttribute("colspan", "2");
	}
	c3.align = "center";
	c3.setAttribute("valign", "top");
	c3.innerHTML = "<iframe id='preview_content' style='width:"+this.secondDivWidth+"; height:"+this.secondDivHeight+";' scrolling=\"no\"></iframe>";

	var r2 = t.insertRow(-1);
	var c4 = null;
	if(is_MSIE)
	{
		c4 = document.createElement("td");
		c4.colSpan = 2;
		r2.appendChild(c4);
	}
	else
	{
		c4 = r2.insertCell(-1);
		c4.setAttribute("colspan", "2");
	}
	c4.innerHTML = "<input type='button' id='preview_close' class='wp_button_close' style='' value='' onclick='javascript: hideOverlay();' />";

	return t;
}

DarkDimmer.prototype.shortCordinate = function()
{
	if (this.dark && this.dark.style.display=="block")
	{
		this.setCordinate();
		this.adjust("over_preview");
	}

}

DarkDimmer.prototype.setCordinate = function()
{
	this.dark.style.height = ((this.pageHeight()>this.windowHeight())?this.pageHeight() : this.windowHeight()) + "px";
	this.dark.style.width = ((this.pageWidth()>this.windowWidth())?this.pageWidth() : this.windowWidth()) + "px";
	this.dark.style.left = "0px";
	this.dark.style.top = "0px";
}

DarkDimmer.prototype.setResize = function()
{
	if(typeof(window.onresize)=="function")
	{
		var old = window.onresize;
		window.onresize = function(){old(); shortResize();};
	}
	else
	{
		window.onresize = function(){shortResize();};
	}
}

DarkDimmer.prototype.showOverlay = function()
{
	this.buildDimmer();
	this.buildPrev();
}

DarkDimmer.prototype.hideOverlaySimple = function()
{
	if (this.fade)
	{
		this.fadeOut(this.dark, 0, this.fadeOutTimeDimmer);
		this.fadeOut(this.prev, 0,this.fadeOutTimePrev);
	}
	else
	{
		hide(this.dark);
		hide(this.prev);
	}
}


