/////// Copyright Kompan (eo Networks ASP sp z o.o.)
/////// 09/02/2007
/////// Lukasz Jarochowski
///////

intervalid = 0;
targetFlash = null;

function addEvent(obj, event, func, params){
	func = new Function(func+"("+params+");");
	if(obj.addEventListener) { obj.addEventListener(event, func, true); }
	else if(obj.attachEvent) { obj.attachEvent('on'+event, func); }
}

function showTopLayer(src, type) {
	var prefix = 'kompan_';
	var content = new String();

	content = '<div style="border: none; width:600px; height: 600px; margin: 0px;"></div>';
        if (navigator.appName.indexOf("Microsoft") != -1) {
                content = '<iframe frameborder="0" style="border: none; width:600px; height: 600px; margin: 0px;"></iframe>';
        }

	content += '<div class="'+prefix+'containerTop">';
	content += '<div class="'+prefix+'containerFooter">';
	content += '<div class="'+prefix+'containerBody">';

	switch(type) {
		case 'img':
			// XXX
			content += showTopLayerImage(src);
			break;
		case 'flash':
			content += showTopLayerObject(src);
			break;
	}
	//content += '</iframe>';
	content += '</div></div></div>';


	target = null;
	try {
		document.getElementById(prefix + 'toplayer').style;
	} catch (e) {
		div = document.createElement("div");
		div.className = prefix + "hidden";
		div.id = prefix + "toplayer";

		hideDiv = document.createElement("div");
		hideDiv.className = prefix + "hideButton";
		hideDiv.id = prefix+ "hideButton";
		hideDiv.fsrc = src;
		//addEvent(hideDiv, 'click', "this.parentNode.className='"+prefix+"hidden';//", '');
		hideDiv.onclick = function() {
			this.parentNode.className='kompan_hidden';
			hoverOut('loc'+this.fsrc.replace(/.*\//, '').replace(/.swf/, ''));
		}

		cDiv = document.createElement("div");
		cDiv.className = prefix + "content";
		div.appendChild(hideDiv);
		div.appendChild(cDiv);
		document.body.appendChild(div);
	}

	target = document.getElementById(prefix + 'toplayer');
	target.firstChild.fsrc = src;
	// element created so insert content:
	if (target.lastChild.innerHTML != content) {
		target.lastChild.innerHTML = content;
	}

	if (type == 'flash') {
		id = src.replace(/.*\//, '');
		id = id.replace(/\.swf/, '');
		targetFlash.write('flash_'+id);
	}

	// finally show target
	target.className = prefix + "loading";

	cp = new CheckProgress(target);
	intervalid = setInterval(cp.runCheck, 100);
}

function showTopLayerImage(src) {
	// auto gallery
	maxH = 600; maxW = 800;
	src = src.replace(/width=\d+/, 'width='+ maxW);
	src = src.replace(/height=\d+/, 'height='+ maxH);
	// old
	// src = src.replace(/-mini/, '');
	content = new String();
	content = '<img src="'+ src + '"/>';

	return content;
}

function showTopLayerObject(src) {
	targetFlash = new SWFObject("zoomflat.swf?src=" + src, "zoomflat", "600", "600", "8", "#faf3ed");
	targetFlash.addParam('wmode', 'transparent');
	id = src.replace(/.*\//, '');
	id = id.replace(/\.swf/, '');
	html = '<div id="flash_'+id+'"></div>';

	return html;
}

function topLayerInit(gallery, type) {
	elements = document.getElementsByTagName(type);

	for (var ii in elements) {
		if ((gallery.length > 0 && elements[ii].className == gallery) || (gallery.length == 0)) {
			addEvent(elements[ii], 'click', 'showTopLayer',
					'"'+ elements[ii].src + '","' + type.toLowerCase() + '"');
		}
	}

}


/// get window size:
function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return new Array(myWidth, myHeight);
}


//// checking progress of image loading

function CheckProgress(target) {
	//construct
	this.target = target;
}

CheckProgress.prototype.runCheck = function() {
	target = this.target;
	offTop = document.documentElement.scrollTop;
	if (target.offsetHeight >= 50) {
		clearInterval(intervalid);
	} else {
		return false;
	}

	tmp = getWindowSize();
	width = tmp[0]; height = tmp[1];


	target.style.top = ((ypos = (height - target.offsetHeight)/2 + offTop)>0?ypos:0) + "px";
	target.style.left = ((xpos = (width - target.offsetWidth)/2 + 40)>0?xpos:0) + "px";
	target.className = "";
}

