function ptPopupSystem() {
	this.popups = [];
	this.contents = [];
    this.cache = {};
	this.lookupid = null;
	this.params = [];
	this.uri = [];
	this.click = null;
	//this.uri = '/test123';
}

ptPopupSystem.prototype.create = function(id,cfg,x,y) {
	var b_IE = navigator.userAgent.indexOf('MSIE') > -1;
	if (this.popups[id]) {
		return;
	}
	this.uri[id] = '';
	if ( cfg.uri ) this.uri[id] = cfg.uri;
	var a = [];
	a[id] = '';
	cfg.x = cfg.y = 0;
	if ( x ) cfg.x = x;
	if ( y ) cfg.y = y;
	this.popups[id] = new THints (cfg, a);

	var div = this.popups[id].divs[id].o_obj;
	//var td = getElementsByTagName("td");
	//var child = td[0];
	var child=div.firstChild;
	while (child.tagName != 'td' && child.tagName != 'TD') {
		child=child.firstChild;
	}
	this.contents[id] = child;
	if (window.opera || b_IE) {
		document.onclick = function () {
			if ( ptPopup.click ) ptPopup.click(event.srcElement.offsetParent);
			ptPopup.click = null;
//			ptPopup.click = event.srcElement;
		};
	}
	else {
		document.onclick = function (e) {
			if ( ptPopup.click ) ptPopup.click(e.target.offsetParent);
			ptPopup.click = null;
//			ptPopup.click = e.target;
		};
	}

};

ptPopupSystem.prototype.setContents = function(id,contents) {
	this.contents[id].innerHTML = contents;
};

ptPopupSystem.prototype.show = function(id,nfade) {
	this.popups[id].show(id,nfade);
};

ptPopupSystem.prototype.hide = function(id) {
	this.popups[id].hide();
	this.params[id] = null;
};
ptPopupSystem.prototype.get = function(id) {
	return this.popups[id];
};

var ptPopup;
function ptPopupLoad() {
	ptPopup = new ptPopupSystem();
}
addLoadEvent(ptPopupLoad);

var TestPTPopupCFG = {
	'top'        : 5, // a vertical offset of a hint from mouse pointer
	'left'       : 5, // a horizontal offset of a hint from mouse pointer
	'css'        : 'hintsClass', // a style class name for all hints, TD object
	'show_delay' : 0, // a delay between object mouseover and hint appearing
//	'show_delay' : 100, // a delay between object mouseover and hint appearing
	'hide_delay' : 0, // a delay between hint appearing and hint hiding
//	'hide_delay' : 3000, // a delay between hint appearing and hint hiding
	'wise'       : true,
	'follow'     : true,
	'z-index'    : 100, // a z-index for all hint layers
	'uri'        : '/test123'
};

var tpidx = 0;
var tptimer;
var tptimeout = 3000;
function TestPopups(e) {
	if ( !e ) e = { clientX:0, clientY:0 };
	var pos = clientPos2DocPos(e.clientX, e.clientY);
	ptPopup.create('testPTPopup',TestPTPopupCFG,pos.x,pos.y);

	ptPopup.setContents('testPTPopup','This is a test of the ptPopups');
	ptPopup.show('testPTPopup');
	tptimer = setTimeout(timeoutCB,tptimeout);
}

function timeoutCB() {
	tpidx++;
	if ( tpidx == 5 ) {
		ptPopup.setContents('testPTPopup','<div>This is a test of the emergency broadcast system<br>The broadcasters of your area have created this system<br>to keep you informed in the event of an emergency<br><b>Preparing to hide popup on timer '+tpidx+'</b></div>');
		tptimer = setTimeout(timeoutCB,tptimeout);
	}
	else if ( tpidx > 5 ) {
		clearTimeout(tptimer);
		ptPopup.hide('testPTPopup');
		ptPopup.getData('testPTPopup',104);
	}
	else {
		if ( tpidx == 3 ) {
			ptPopup.setContents('testPTPopup','<div>This is a test of the emergency broadcast system<br>The broadcasters of your area have created this system<br>to keep you informed in the event of an emergency<br>This is a test of the emergency broadcast system<br>The broadcasters of your area have created this system<br>to keep you informed in the event of an emergency<br>This is a test of the emergency broadcast system<br>The broadcasters of your area have created this system<br>to keep you informed in the event of an emergency<br>Switching contents of popup on timer '+tpidx+'</div>');
		} else {
			ptPopup.setContents('testPTPopup','<div>This is a test of the emergency broadcast system<br>The broadcasters of your area have created this system<br>to keep you informed in the event of an emergency<br>Switching contents of popup on timer '+tpidx+'</div>');
		}
		tptimer = setTimeout(timeoutCB,tptimeout);
	}
}

ptPopupSystem.prototype.getData = function(id,param,loadingMsg) {
	if ( this.cache[id] && this.cache[id][param] ) {
		this.params[id] = param;
		this.setContents(id,this.cache[id][param]);
		this.show(id);
		return;
	}
	
	if ( this.lookupid == null ) {
		this.setContents(id,loadingMsg?loadingMsg:'<div class="'+id+'-loading">Loading, Please Wait...</div>');
		this.show(id,false);
		this.lookupid = id;
		this.params[id] = param;
		this.transport = HTTPGet(this.uri[id] +'/'+id+'/'+param , this.receive, this);
	}
}

ptPopupSystem.prototype.receive = function(string, xmlhttp, ptpopup) {
  // Note: Safari returns 'undefined' status if the request returns no data.
  if (xmlhttp.status != 200 && typeof xmlhttp.status != 'undefined') {
    return alert('An HTTPRequest error '+ xmlhttp.status +' occured.\n');
  }
  // Parse back result
  var result = parseJson(string);
  if (typeof result['status'] == 'undefined' || result['status'] != 0) {
	if ( !ptpopup.cache[ptpopup.lookupid] ) {
		ptpopup.cache[ptpopup.lookupid] = new Object();
	}
    ptpopup.cache[ptpopup.lookupid][ptpopup.params[ptpopup.lookupid]] = result[ptpopup.lookupid];
	ptpopup.setContents(ptpopup.lookupid,result[ptpopup.lookupid]);
	ptPopup.show(ptpopup.lookupid);
	ptpopup.lookupid = null;
  }
}


if (isJsEnabled()) {
	//addLoadEvent(TestPopups);
}


//////////////////////////////////////////////
// business popups
//////////////////////////////////////////////
var ptBusPopupCFG = {
	'top'        : 5, // a vertical offset of a hint from mouse pointer
	'left'       : 5, // a horizontal offset of a hint from mouse pointer
	'css'        : 'bus_popup1', // a style class name for all hints, TD object
	'show_delay' : 0, // a delay between object mouseover and hint appearing
//	'show_delay' : 100, // a delay between object mouseover and hint appearing
	'hide_delay' : 0, // a delay between hint appearing and hint hiding
//	'hide_delay' : 3000, // a delay between hint appearing and hint hiding
	'wise'       : true,
	'follow'     : false,
	'z-index'    : 100, // a z-index for all hint layers
//	'centerx'    : true, // center horizontally
	'fade_delay' : 200, // a delay between hide request and actual hint hiding
	'manualpos'  : true,
	'uri'        : '/test123'
};

function BusinessPopup() {
	if (!ptPopup) return;
	if (!ptPopup.popups['ptBusPopup']) {
		ptPopup.create('ptBusPopup',ptBusPopupCFG);
	}
}
function busPop(id) {
	if (!ptPopup) return;
	if ( id != 0 ) {
		ptPopup.getData('ptBusPopup',id);
	} else {
		ptPopup.hide('ptBusPopup');
	}
}

function busPopTOld(id) {
	if (!ptPopup) return;
	if ( id != 0 ) {
//		if ( !ptPopup.params['ptBusPopup'] || ptPopup.params['ptBusPopup'] != id )
		if ( ptPopup.popups['ptBusPopup'].visible != 'ptBusPopup' )
			ptPopup.getData('ptBusPopup',id);
		else
			id = 0;
	}
	
	if ( id == 0 ) {
		ptPopup.hide('ptBusPopup');
	}
}

function busPopT(id) {
	if (!ptPopup) return;

	if ( id == 0 ) {
		ptPopup.hide('ptBusPopup');
		return;
	}

	var _id = id;
	var cb = function (elem) {
		var popup = ptPopup.get('ptBusPopup');
		var vis = popup.visible;
		var params = ptPopup.params['ptBusPopup'];
		if ( _id != 0 && popup ) {
			var pos = findPos(elem);
			//pos.x += elem.offsetWidth/2;
			pos.y += elem.offsetHeight;
			popup.setPos(pos);
		}
		if ( _id == 0 || vis == 'ptBusPopup' ) {
			ptPopup.hide('ptBusPopup');
		}
		if ( _id != 0 && (vis != 'ptBusPopup' || params != _id) ) {
			ptPopup.getData('ptBusPopup',_id);
		}
	};
	ptPopup.click = cb;
}

//////////////////////////////////////////////
// Popup menu popups
//////////////////////////////////////////////
var ptPopupMenuCFG = {
	'top'        : 10, // a vertical offset of a hint from mouse pointer
	'left'       : -60, // a horizontal offset of a hint from mouse pointer
	'centerx'    : true, // center horizontally
	'css'        : 'popup-menu-container', // a style class name for all hints, TD object
	'show_delay' : 0, // a delay between object mouseover and hint appearing
//	'show_delay' : 100, // a delay between object mouseover and hint appearing
	'hide_delay' : 0, // a delay between hint appearing and hint hiding
//	'hide_delay' : 3000, // a delay between hint appearing and hint hiding
	'fade_delay' : 200, // fade popup in and out rate
	'wise'       : true,
	'follow'     : false,
	'manualpos'  : true,
	'z-index'    : 100, // a z-index for all hint layers
	'uri'        : '/popupmenu'
};

function PopupMenu() {
	if (!ptPopup) return;
	if (!ptPopup.get('ptPopupMenu')) {
		ptPopup.create('ptPopupMenu',ptPopupMenuCFG);
		ptPopup.get('ptPopupMenu').divs['ptPopupMenu'].o_obj.onmouseout = function (e) {
			if ( e ) {
				if ( !isChild(e.explicitOriginalTarget,ptPopup.get('ptPopupMenu').divs['ptPopupMenu'].o_obj) ) {
					PMenu(0);
				}
			}
			else {
				if ( !isChild(window.event.toElement,ptPopup.get('ptPopupMenu').divs['ptPopupMenu'].o_obj) ) {
					PMenu(0);
				}
			}
		};
	}
}
function isChild(s,d) {
while(s) {
if (s==d)
return true;
s=s.parentNode;
}
return false;
}
function PMenu(id) {
	if (!ptPopup) return;

	if ( id == 0 ) {
		ptPopup.hide('ptPopupMenu');
		return;
	}

	var _id = id;
	var cb = function (elem) {
		var popup = ptPopup.get('ptPopupMenu');
		var vis = popup.visible;
		var params = ptPopup.params['ptPopupMenu'];
		if ( _id != 0 && popup ) {
			var pos = findPos(elem);
			pos.x += elem.offsetWidth/2;
			pos.y += elem.offsetHeight;
			popup.setPos(pos);
		}
		if ( _id == 0 || vis == 'ptPopupMenu' ) {
			ptPopup.hide('ptPopupMenu');
		}
		if ( _id != 0 && (vis != 'ptPopupMenu' || params != _id) ) {
			ptPopup.getData('ptPopupMenu',_id);
		}
	};
	ptPopup.click = cb;
}

function PMenuT(id) {
	if (!ptPopup) return;
	if ( id != 0 ) {
//		if ( !ptPopup.params['ptPopupMenu'] || ptPopup.params['ptPopupMenu'] != id )
		if ( ptPopup.popups['ptPopupMenu'].visible != 'ptPopupMenu' )
			ptPopup.getData('ptPopupMenu',id);
		else
			id = 0;
	}
	
	if ( id == 0 ) {
		ptPopup.hide('ptPopupMenu');
	}
}

//////////////////////////////////////////////
// layout popups
//////////////////////////////////////////////
var ptLayoutPopupCFG = {
	'top'        : 5, // a vertical offset of a hint from mouse pointer
	'left'       : 5, // a horizontal offset of a hint from mouse pointer
	'css'        : 'bus_popup1', // a style class name for all hints, TD object
	'show_delay' : 100, // a delay between object mouseover and hint appearing
	'hide_delay' : 3000, // a delay between hint appearing and hint hiding
	'wise'       : true,
	'follow'     : true,
	'z-index'    : 100 // a z-index for all hint layers
};
var layouts = {
				'adlayout1_1' : "<div><img src=\"/images/adlayout_1_1.gif\" width=\"194\" height=\"302\"></div>",
				'adlayout3_4' : "<div><img src=\"/images/adlayout_3_4.gif\" width=\"194\" height=\"302\"></div>",
				'adlayout2_3' : "<div><img src=\"/images/adlayout_2_3.gif\" width=\"194\" height=\"302\"></div>",
				'adlayout1_2' : "<div><img src=\"/images/adlayout_1_2.gif\" width=\"194\" height=\"302\"></div>",
				'adlayout1_3' : "<div><img src=\"/images/adlayout_1_3.gif\" width=\"194\" height=\"302\"></div>",
				'adlayout1_4' : "<div><img src=\"/images/adlayout_1_4.gif\" width=\"194\" height=\"302\"></div>",
				'adlayout1_5' : "<div><img src=\"/images/adlayout_1_5.gif\" width=\"194\" height=\"302\"></div>",
				'adlayoutbb' : "<div><img src=\"/images/layout_billboard_example.jpg\" width=\"194\" height=\"302\"></div>",
				'adlayoutsp' : "<div style=\"font-size:2em; text-align:center;\">No Preview Available</div>"
			};

function LayoutPopup() {
	if (!ptPopup) return;
	if (!ptPopup.popups['ptLayoutPopup']) {
		ptPopup.create('ptLayoutPopup',ptLayoutPopupCFG);
	}
}

function layoutPop(id) {
	if (!ptPopup) return;
	LayoutPopup();
	if ( id != 0 && layouts[id] ) {
		ptPopup.setContents('ptLayoutPopup',layouts[id]);
		ptPopup.show('ptLayoutPopup');
		//ptPopup.getData('ptLayoutPopup',id);
	} else {
		ptPopup.hide('ptLayoutPopup');
	}
}

//////////////////////////////////////////////
// crl popups
//////////////////////////////////////////////
var ptCRLPopupCFG = {
	'top'        : 5, // a vertical offset of a hint from mouse pointer
	'left'       : 5, // a horizontal offset of a hint from mouse pointer
	'css'        : 'bus_popup1', // a style class name for all hints, TD object
	'show_delay' : 0, // a delay between object mouseover and hint appearing
	'hide_delay' : 0, // a delay between hint appearing and hint hiding
	'wise'       : true,
	'follow'     : false,
	'z-index'    : 100, // a z-index for all hint layers
//	'centerx'    : true, // center horizontally
	'fade_delay' : 200, // a delay between hide request and actual hint hiding
	'manualpos'  : true,
	'uri'        : '/crlpopup'
};

function CRLPopup() {
	if (!ptPopup) return;
	if (!ptPopup.popups['ptCRLPopup']) {
		ptPopup.create('ptCRLPopup',ptCRLPopupCFG);
	}
}
function CRLPop(id) {
	if (!ptPopup) return;
	CRLPopup();

	if ( id == 0 ) {
		ptPopup.hide('ptCRLPopup');
		return;
	}

	var _id = id;
	var cb = function (elem) {
		var popup = ptPopup.get('ptCRLPopup');
		var vis = popup.visible;
		var params = ptPopup.params['ptCRLPopup'];
		if ( _id != 0 && popup ) {
			var pos = findPos(elem);
			//pos.x += elem.offsetWidth/2;
			pos.y += elem.offsetHeight;
			popup.setPos(pos);
		}
		if ( _id == 0 || vis == 'ptCRLPopup' ) {
			ptPopup.hide('ptCRLPopup');
		}
		if ( _id != 0 && (vis != 'ptCRLPopup' || params != _id) ) {
			ptPopup.getData('ptCRLPopup',_id);
		}
	};
	ptPopup.click = cb;
}


/////////// Helpers...
function clientPos2DocPos(x,y) {
	var sl = 0, st = 0;
	if (document.documentElement && document.documentElement.scrollTop) {
		st = document.documentElement.scrollTop;
		sl = document.documentElement.scrollLeft;
	}
	else if (document.body) {
		st = document.body.scrollTop;
		sl = document.body.scrollLeft;
	}
	return {x:x+sl, y:y+st};
}

var findPos;
if (typeof YAHOO != "undefined" && typeof YAHOO.util.Dom != "undefined") {
findPos = function (obj) {
	var pos = YAHOO.util.Dom.getXY(obj);
	return {x:pos[0],y:pos[1]};
};
}
else {
findPos = function (obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return {x:curleft,y:curtop};
};
}


//addLoadEvent(TestPopups);
//BusinessPopup();
addLoadEvent(BusinessPopup);
addLoadEvent(PopupMenu);
