var winPadding = 5;
var winTimeout = 200;
var winMaxWidth = 1700;
var winMaxHeight = 1700;
var winWidth = null;
var winHeight = null;
var winTop = null;
var winLeft = null;
var imgDisplayPct = 1;
var imgWidth = 1;
var imgHeight = 1;
var paoImages = null;
var currentPao = null;
var showStatus = false;
var trackingParams = null;
var defaultsrc = "http://c66.smartpages.com/shared/space.gif";
var searchTop=-1; 

// pao class
function pao(listing, url) {
	this.listing   = listing;
	this.image 	   = new Image();
	this.image.src = url;
}

// builds array of paos on the page
function registerpao(listing, location) {
	if (!paoImages) {
		paoImages = new Array();
	}
	paoImages.push(new pao(listing, location));
}

// this function initiates the setting of the properties for a pao then
// sets the properties to display it
function showpao(listing, tracking) {
	if (showStatus == true) {
		// already visisble, do nothing
	} else {
		// set some properties
		currentPao = listing;
		showStatus = true;
		
		var paoIframe = document.getElementById? document.getElementById('paoifrm'): document.all? document.all['paoifrm']: null;
		if (paoIframe) {
			thisPao = getPao(listing);
			setSize(thisPao.image.width, thisPao.image.height);
			paoIframe.src = "/abpages/pao.html?noskin"
			paoIframe.style.top = winTop + "px";
			paoIframe.style.left = winLeft + "px";
			paoIframe.style.width = imgWidth + "px";
			paoIframe.style.height = imgHeight + "px";
			paoIframe.style.visibility = "visible";
			imgWidth = imgWidth + "px";
		}
	}
}

function logstat2( tracking ) {

	var pathparts = escape(window.location.pathname).split(unescape("%2F"));
	var baseurl = window.location.protocol + unescape("%2F%2F") + window.location.hostname;
	var test = window.location.href.replace('http://','').replace('https://','').substr( 0, 11 );
	// check to see if we are on reverse proxy
	if( test == 'www.bbb.org' || test == 'w3.bbb.org/' ) {
		baseurl = baseurl + unescape("%2F") + pathparts[1];
	}
	
	new Request({
		url: baseurl + "/abpages/logstat.html",
		method: 'get'
	}).send( tracking.replace("?","") + "&noskin" );

}

function logstat(tracking) {
		var pathparts = escape(window.location.pathname).split(unescape("%2F"));
		var baseurl = window.location.protocol + unescape("%2F%2F") + window.location.hostname;
		// check to see if we are on reverse proxy
		if( window.location.href.replace('http://','').replace('https://','').substr( 0, 12 ) == 'www.bbb.org/' ) {
			baseurl = baseurl + unescape("%2F") + pathparts[1];
		}
		var paoIframe = document.getElementById? document.getElementById('paoifrm'): document.all? document.all['paoifrm']: null;
		if (paoIframe) paoIframe.src = baseurl+"/abpages/logstat.html"+tracking+"&noskin";
}
// this function is the first step at hiding the pao
// introduces a small delay before actually closing the pao
function hidepao() {
	showStatus = false;
	setTimeout("try_hidepao()", winTimeout)
}

// this function sets the default values to some properties and
// does all the real hiding
function try_hidepao() {
	if (showStatus == false) {
		// set all default values again
		currentPao = null;
		trackingParams = null;
		var paoIframe = document.getElementById? document.getElementById('paoifrm'): document.all? document.all['paoifrm']: null;
		if (paoIframe) {
			paoIframe.style.visibility = "hidden";
			paoIframe.src = defaultsrc;
			imgDisplayPct = 1;
			imgWidth = 1;
			imgHeight = 1;
		}
	}
}

// sets all the size properties
function setSize(thisImgWidth, thisImgHeight) {
	thisWin.getWinHeight();
	thisWin.getWinWidth();
	thisWin.getScrollX();
	thisWin.getScrollY();
	// set the width/height to the largest dimensions possible within the max settings
	((thisWin.width - (winPadding*2)) > winMaxWidth) ? winWidth = winMaxWidth : winWidth = thisWin.width - (winPadding*2);
	((thisWin.height - (winPadding*2)) > winMaxHeight) ? winHeight = winMaxHeight : winHeight = thisWin.height - (winPadding*2);
	// set top/left properties, include padding
	if (thisImgWidth< thisWin.width && thisImgWidth < winMaxWidth) {
		// center horizontally
		winLeft = thisWin.scrollX + ((thisWin.width/2) - (thisImgWidth/2));
	} else {
		// set at leftmost, including padding
		winLeft = thisWin.scrollX + winPadding;
	}
	if (thisImgHeight < thisWin.height && thisImgHeight < winMaxHeight) {
		// center vertically
		winTop = thisWin.scrollY + ((thisWin.height/2) - (thisImgHeight/2));
	} else {
		// set at topmost, including padding
		winTop = thisWin.scrollY + winPadding;
	}
	// if image is larger then current displayable area then constrain size
	if (thisImgWidth > winWidth || thisImgHeight > winHeight) {
		imgWidth = thisImgWidth;
		imgHeight = thisImgHeight;
//		imgDisplayPct = shrinkpao(thisImgWidth, thisImgHeight)
//		imgWidth = Math.round(thisImgWidth * imgDisplayPct);
//		imgHeight = Math.round(thisImgHeight * imgDisplayPct);
	} else {
		imgWidth = thisImgWidth;
		imgHeight = thisImgHeight;
	}
}

// this function looks at which dimension (height or width) that exceeds the maximum
// space allowable then sets the percentage number for constraining the size at a
// correct aspect ratio 
function shrinkpao(thisImgWidth, thisImgHeight) {
	var widthPct = winWidth / thisImgWidth;
	var heightPct = winHeight / thisImgHeight;
	if (widthPct > heightPct) {
		return heightPct;
	} else {
		return widthPct;
	}
}

// grab pao object by id
function getPao(id) {
	for(index in paoImages) {
		if (id == paoImages[index].listing) {
			return paoImages[index];
		}
	}
}

var thisWin = {
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		this.width = document.body.clientWidth;
  },
  
  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		this.height = document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		this.height = document.body.clientHeight;
  },
  
  getScrollX: function () {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft) 
  		this.scrollX = document.body.scrollLeft; 
  	else if (window.scrollX) this.scrollX = window.scrollX;
  },
  
  getScrollY: function () {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop) 
  		this.scrollY = document.body.scrollTop; 
  	else if (window.scrollY) this.scrollY = window.scrollY;
  },
  
  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  }
  
}

window.onresize = function() { setSize(); }

// all hail the W3C event model!
if (!document.all) {
	//window.addEventListener("click", hide_popup, false);
}

function FollowLink(id) {
	var obj = document.getElementById("link"+id);
	window.open(obj.href);
}
function MoveSearch() {
	var obj=document.getElementById('listingSearchBar'); 
	if(searchTop<0) searchTop=obj.offsetTop;
	status=obj.clientHeight+":"+document.body.scrollTop+":"+document.body.clientTop+":"+obj.offsetTop+":"+obj.style.top;
//alert(obj.clientHeight+":"+document.body.scrollTop+":"+document.body.clientTop+":"+obj.offsetTop+":"+obj.style.top);
//document.body.clientTop
if(document.body.scrollTop>(2+searchTop)) obj.style.top=document.body.scrollTop-searchTop; else obj.style.top=0;
}

function ShowPhone(bid) {
	var obj = document.getElementById("showphone"+bid);
	obj.style.display="none";
	obj = document.getElementById("phone"+bid);	
	obj.style.display="block";
	return false;
}
function ShowEmail(bid) {
	var obj = document.getElementById("showemail"+bid);
	obj.style.display="none";
	obj = document.getElementById("email"+bid);	
	obj.style.display="block";
	return false;
}
