/*
	sxFramework 2.1
	by Tobias Jacksteit
*/

// INIT
var popLayer_interval = 0;
var is_ie = navigator.userAgent.toLowerCase().indexOf("msie") != -1 ? true : false;

function popLayer_show() {
	popLayer_update();
	popLayer_interval = setInterval( "popLayer_update()", 50 );
	document.getElementById( 'popLayer_div' ).style.display = '';
	document.getElementById( 'popLayer_div_inner' ).style.display = '';
}

function popLayer_hide() {
	document.getElementById( 'popLayer_div' ).style.display = 'none';
	document.getElementById( 'popLayer_div_inner' ).style.display = 'none';
	clearInterval( popLayer_interval );
}

function popLayer_update() {
	if( !is_ie ) {
		// Is not IE
		document.getElementById( 'popLayer_div' ).style.height = popLayer_getPageHeight();
		document.getElementById( 'popLayer_div' ).style.top = "0px";
		if( document.getElementById( 'popLayer_div_inner' ).style.height ) {
			document.getElementById( 'popLayer_div_inner' ).style.top = ( window.innerHeight / 2 ) - ( parseInt( document.getElementById( 'popLayer_div_inner' ).style.height ) / 2 ) + document.body.scrollTop + "px";
			document.getElementById( 'popLayer_div_inner' ).style.left = ( window.innerWidth / 2 ) - ( parseInt( document.getElementById( 'popLayer_div_inner' ).style.width ) / 2 )
		}
	} else {
		// Is IE
		document.getElementById( 'popLayer_div' ).style.height = popLayer_getPageHeight();
		document.getElementById( 'popLayer_div' ).style.top = "0px";
		if( document.getElementById( 'popLayer_div_inner' ).style.height ) {
			document.getElementById( 'popLayer_div_inner' ).style.top = ( document.body.offsetHeight / 2 ) - ( parseInt( document.getElementById( 'popLayer_div_inner' ).style.height ) / 2 ) + document.body.scrollTop + "px";
			document.getElementById( 'popLayer_div_inner' ).style.left = ( document.body.offsetWidth / 2 ) - ( parseInt( document.getElementById( 'popLayer_div_inner' ).style.width ) / 2 )
		}
	}
}

function popLayer_getPageHeight() {
	if( window.innerHeight && window.scrollMaxY && window.innerHeight > 0 ) {
		return( ( window.innerHeight + window.scrollMaxY ) + 'px' );
	} else if( document.body && document.body.scrollHeight > document.body.offsetHeight ) {
		return( document.body.scrollHeight + 'px' );
	} else if( document.body && document.body.offsetHeight && document.body.offsetHeight > 0 ) {
		return( document.body.offsetHeight + 'px' );
	} else if( window.innerHeight && window.innerHeight > 0 ) {
		return( window.innerHeight + 'px' );
	} else {
		return( '100%' );
	}
}

function popLayer_getPageWidth() {
	if( window.innerWidth && window.scrollMaxX && window.innerWidth > 0 ) {
		return( ( window.innerWidth + window.scrollMaxX ) + 'px' );
	} else if( document.body && document.body.scrollWidth > document.body.offsetWidth ) {
		return( document.body.scrollWidth + 'px' );
	} else if( document.body && document.body.offsetWidth && document.body.offsetWidth > 0 ) {
		return( document.body.offsetWidth + 'px' );
	} else if( window.innerWidth && window.innerWidth > 0 ) {
		return( window.innerWidth + 'px' );
	} else {
		return( '100%' );
	}
}

function popLayer_insertHTML( html ) {
	var random_string = "insert_html_" + Math.round( Math.random() * 100000000 );
	var newImg = document.createElement( 'img' );
	newImg.src = random_string;
	document.body.appendChild( newImg );
	
	var pat = new RegExp( "<img[^<]*?" + random_string + "[^>]*?>" );
	document.body.innerHTML = document.body.innerHTML.replace( pat, html );
}

function popLayer_get( id ) {
	if( document.getElementById( id ) != undefined ) {
		return( document.getElementById( id ).innerHTML );
	} else {
		return( false );
	}
}

function popLayer( content, className, color, opacity ) {
	if( content.substring( 0, 2 ) == '__' ) {
		if( document.getElementById( content.substring( 2 ) ) != undefined ) {
			content = popLayer_get( content.substring( 2 ) );
		}
	}
	if( color == null ) color = '#000000';
	if( opacity == null ) opacity = 0.75;
	if( className == null ) className = 'popLayer';
	if( document.getElementById( 'popLayer_div' ) == undefined ) {
		var output = '<div style="background: ' + color + '; top: 0px; left: 0px; position: absolute; display: none; z-index: 100; width: ' + popLayer_getPageWidth() + '; height: ' + popLayer_getPageHeight() + '; filter:alpha(opacity=' + ( opacity * 100 ) + '); -moz-opacity: ' + opacity + '; -khtml-opacity: ' + opacity + '; opacity: ' + opacity + '; text-align: center; vertical-align: top;" id="popLayer_div">&nbsp;</div><div style="position: absolute; display: none; z-index: 105;" id="popLayer_div_inner" class="' + className + '">' + content + '</div>';
		if( document.body != undefined ) {
			popLayer_insertHTML( output );
		} else {
			document.write( output );
		}
	} else {
		document.getElementById( 'popLayer_div_inner' ).innerHTML = content;
		document.getElementById( 'popLayer_div_inner' ).className = className;
		document.getElementById( 'popLayer_div' ).style.backgroundColor = color;
	}
	popLayer_show();
}
