﻿var bx;
var cover;
var popbox;
var imgbox;
var baseZIndex = 900;

function ImgPop(elmA)
{
	bx = new bw();

	applyCover();
	addPopBox();
	addImgBox();
	addImage(elmA.href);
	addCloseLink();
	return false;
}

function bw()
{
	this.ver=navigator.appVersion
	this.ie=(this.ver.indexOf("MSIE ")>-1)?true:false; 

	this.d = window.document;
	this.dE = this.d.documentElement;
	this.sH = this.dE.scrollHeight;
	this.sw = this.dE.scrollWidth;
	this.iH = this.dE.innerHeight;
	this.iW = this.dE.innerWidth;
	this.cH = this.dE.clientHeight;
	this.cW = this.dE.clientWidth;
	this.oH = this.dE.offsetHeight;
	this.oW = this.dE.offsetWidth;

	if (typeof this.dE.scrollTop != 'undefined' && this.dE.scrollTop > 0)
	{
		this.sL = this.dE.scrollLeft;
		this.sT = this.dE.scrollTop;
	}
	else if (typeof this.d.body.scrollTop != 'undefined')
	{
		this.sL = this.d.body.scrollLeft;
		this.sT = this.d.body.scrollTop;
	} 

	this.dbg = 'sH: '+this.sH+'\nsW: '+this.sW+'\niH: '+this.iH+'\niW: '+this.iW+'\ncH: '+this.cH+'\ncW: '+this.cW+'\noH: '+this.oH+'\noW: '+this.oW+'\nsT: '+this.sT+'\nsL:'+this.sL;

	return this;
}

function applyCover()
{
	cover = bx.d.createElement('div');
	SetElementStyles(
		cover,
		{
			'position':'absolute'
			,'zIndex':baseZIndex
			,'top':'0px'
			,'left':'0px'
			,'textAlign':'center'
			,'background':'transparent url('+rt+'ux/img/cover-bg.png)'
		});

	bx.d.body.appendChild(cover);
	AddEventListener(window,'resize',resizeCover);
	resizeCover();
}

function resizeCover()
{
	if (!cover){return;}

	bx = new bw();

	SetElementStyles(
		cover,
		{
			'width' : Math.max( bx.cW,bx.sW || 0 ) - 1 + 'px'
			,'height' : Math.max( bx.cH,bx.sH || 0 ) - 1 + 'px'
		});
}

function RemoveCover()
{
	return cover.parentNode.removeChild(cover);
}

function addPopBox()
{
	popbox = bx.d.createElement('div');

	SetElementStyles(
		popbox,
		{
			'position':'absolute'
			,'zIndex':baseZIndex+1
			,'top': (bx.sT+20) + 'px'
			,'left':'0px'
			,'padding':'0px'
			,'textAlign':'center'
			,'width' : '100%'
			,'height' : '100px'
			,'background':'transparent'
		});

	cover.appendChild(popbox);

	AddEventListener(window,'scroll',positionPopBox);
	positionPopBox();
}

function positionPopBox()
{
	bx = new bw();
	SetElementStyles(popbox,{'top': (bx.sT+20) + 'px'});
}

function addImgBox()
{
	imgbox = bx.d.createElement('span');

	SetElementStyles(
		imgbox,
		{
			'padding':'15px'
			,'textAlign':'center'
			,'whiteSpace' : 'nowrap'
			,'display' : 'inline-block'
			,'background':'transparent url('+rt+'ux/img/imgbox-bg.png)'
		});

	popbox.appendChild(imgbox);
}

function addImage(u)
{
	var imgelm = bx.d.createElement('img');
	imgelm.src = u;

	SetElementStyles(imgelm,{'margin' : '0px 0px 20px 0px'});

	imgbox.appendChild(imgelm);
}

function addCloseLink()
{
	var br = bx.d.createElement('br');
	imgbox.appendChild(br);

	var closelink = bx.d.createElement('a');
	
	var sp1 = bx.d.createElement('span');
	var sp2 = bx.d.createElement('span');

	sp2.innerHTML = 'close image';

	sp1.appendChild(sp2);
	closelink.appendChild(sp1);
	
	closelink.className = 'CloseImgPop';
	closelink.href='javascript:void(0);';
	closelink.onclick=RemoveCover;
	
	imgbox.appendChild(closelink);
}

function SetElementStyles(elm, styleDict)
{
	var style = elm.style ;
	for (var styleName in styleDict)
	{
		style[styleName] = styleDict[styleName];
	}
}