// Copyright &copy; 2007 Trinex Internet Solutions Inc.;
// Author Steve Barraclough;

//Declare Variables
var x1, x2, setHeight, bodyoffSetHeight, AvailHeight, scrollOffset, BodyMarginTop, BodyMarginBottom, BodyMarginTotal, bodyCurrentMarginTop, bodyCurrentMarginBottom, bodyGetComputedMarginTop, bodyGetComputedMarginBottom, iframeurl, etarget, objframe, newiframe, RemoveObj, iframeobj;

var iStyleSheet = document.styleSheets[0]; //stylesheet to modify
var bgDiv = document.getElementById('listingsbg'); //background Opacity div
var holderDiv = document.getElementById('listingsholder'); //holder window
var iframePreloader = document.getElementById('iframePreloader'); //graphic or div to display
var bodyMarginTopBottom = 20;
var holderDivBorderTopBottom = 5; 
var holderDivBorderLeftRight = 5; 

var AgentString = navigator.userAgent;
var HideFlashBrowser = "Safari";

function getHeight() {
	//Browser Compatibility Hell
	//Get Scroll OffSet Height
	x1 = document.body.scrollHeight;
	x2 = document.body.offsetHeight;
	if (x1 > x2) { bodyoffSetHeight = document.body.scrollHeight; }
	else { bodyoffSetHeight = document.body.offsetHeight; }
	
	//Get Available Inner height
	if (self.innerHeight) { AvailHeight = self.innerHeight; }
	else if (document.documentElement && document.documentElement.clientHeight)
	{ AvailHeight = document.documentElement.clientHeight; }
	else if (document.body)
	{ AvailHeight = document.body.clientHeight; }

	if (bodyoffSetHeight < AvailHeight) {
		setHeight = AvailHeight; 
	} else {
		//Get body marginTop marginBottom Values
		setHeight = bodyoffSetHeight + bodyMarginTopBottom;
	}
	//Get Scroll Offset Y
	if (self.pageYOffset) { scrollOffset = self.pageYOffset; }
	else if (document.documentElement && document.documentElement.scrollTop)
	{ scrollOffset = document.documentElement.scrollTop; }
	else if (document.body)
	{ scrollOffset = document.body.scrollTop; }
};

function ViewerProperties(iwidth, iheight) {
	holderDiv.style.width = iwidth+'px'; //viewer width
	holderDiv.style.height = iheight+'px'; //viewer height
	holderDiv.style.marginLeft = -(iwidth/2)-holderDivBorderLeftRight+'px';
	holderDiv.style.marginTop = -(iheight/2)+(scrollOffset-holderDivBorderTopBottom)+'px'; 
};

function CreateIframeElement(u,h,w) {
	newiframe=document.createElement('iframe');
	newiframe.setAttribute('id','WindowIFrame');
	newiframe.setAttribute('src', u);
	newiframe.setAttribute('height', h);
	newiframe.setAttribute('width', w);
	newiframe.setAttribute('align', 'top');
	newiframe.setAttribute('scrolling', 'no');
	newiframe.setAttribute('frameBorder', '0');
	newiframe.setAttribute('style:visibility', 'hidden');
	//newiframe.setAttribute('onload', LoadStatus());
	holderDiv.appendChild(newiframe);
	
	//Preload iframe
	if (newiframe.readyState) {
		newiframe.onreadystatechange = function () {
		if (newiframe.readyState!="complete") {
				iframePreloader.style.display='';
				iframePreloader.innerHTML = "Loading Listing...";
			} else {	
			iframePreloader.style.display='none';
			newiframe.style.visibility = 'visible';
			holderDiv.style.display = '';
			}
		}	
	} else {
		newiframe.style.visibility = 'visible';
		holderDiv.style.display = '';
	}
};

function RemoveElement(parentel,elrmv) {
	RemoveObj = document.getElementById(elrmv);
	parentel.removeChild(RemoveObj);
};

function BuildListing(pageurl,iwidth,iheight) {
	getHeight();
	if(AgentString.indexOf(HideFlashBrowser)!=-1) { 
		document.getElementById('banner').style.visibility = 'hidden'; 
		}
	bgDiv.style.height = setHeight+"px";
	bgDiv.style.display = '';
	ViewerProperties(iwidth, iheight);
	iframeurl = ""+pageurl+"";
	CreateIframeElement(iframeurl, iheight, iwidth);
};


function LoadListing(url, iwidth, iheight) {
	if (!url || !iwidth || !iheight) {return false;};
	document.body.style.overflow = 'hidden';
	if( window.hasAddedNewRule ) { return; }
	if( iStyleSheet.insertRule ) { 
	 	iStyleSheet.insertRule('html { height:100%; overflow:hidden; }',iStyleSheet.cssRules.length);
	} else if( iStyleSheet.addRule ) { 
		iStyleSheet.addRule('html','height:100%; overflow:hidden;');
	} else { 
		return;
		}
	window.hasAddedNewRule = true;
	if (url) {
		BuildListing(url,iwidth,iheight);
	} else {
		UnloadListing(); 
	}
};

function UnloadListing() {
	
	var iframeObj = document.getElementById('WindowIFrame');

	if (document.getElementById('WindowIFrame')) {
		if (window.frames[0].document.getElementById('playingstate')) {
			window.frames[0].document.getElementById('playingstate').innerHTML = "";
		}
	}
	if (iframeObj) { RemoveElement(holderDiv,'WindowIFrame'); }
	
	bgDiv.style.display = 'none';
	holderDiv.style.display = 'none';
	document.body.style.overflow = '';
	if(AgentString.indexOf(HideFlashBrowser)!=-1) { 
		document.getElementById('banner').style.visibility = 'visible'; 
		}
	if( !window.hasAddedNewRule ) {  return; }
	if( iStyleSheet.deleteRule ) { 
		iStyleSheet.deleteRule(iStyleSheet.cssRules.length-1);
	} else if( iStyleSheet.rules && iStyleSheet.removeRule ) { 
		iStyleSheet.removeRule(iStyleSheet.rules.length-1);
	} else { 
		return; 
		}
	window.hasAddedNewRule = false;
};

window.onresize=function() { 
	getHeight();	
	bgDiv.style.height = setHeight+"px";
};

document.onclick = function(e) {
	etarget = document.all ? event.srcElement : e.target;
	if (!etarget.onclick && !etarget.href && (etarget != holderDiv)) 
		UnloadListing();
    return true; 
};

function stopEvent(e) {
	if(!e) var e = window.event;
	//e.cancelBubble is supported by IE
	e.cancelBubble = true;
	e.returnValue = false;
	//e.stopPropagation works only in Firefox.
	if (e.stopPropagation) {
		e.stopPropagation();
		e.preventDefault();
	}
	return false;
};	
