//Flash object in the top block is defined here
var flashvars = false;
var params = {wmode: "transparent"};
var attributes = {};
//swfobject.embedSWF("http://artfiles.art.com/images/nav/subjects/feature.swf", "flashcontent", "623", "185", "8.0.0", "", flashvars, params, attributes);

var XMLHttpFactories = [
    function () {return new XMLHttpRequest()},
    function () {return new ActiveXObject("Msxml2.XMLHTTP")},
    function () {return new ActiveXObject("Msxml3.XMLHTTP")},
    function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
    var xmlhttp = false;
    for (var i=0;i<XMLHttpFactories.length;i++) {
        try {
             xmlhttp = XMLHttpFactories[i]();
        }
        catch (e) {
            continue;
        }
        break;
    }
    return xmlhttp;
}


var popupHeight;
var popupWidth;
var selectedID;
var moreTopPos;
var moreLeftPos;
function findPos(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 [curleft,curtop];
}

function requestData(theID, leftPos, topPos)
{
	selectedID = theID;
	theURL = "/asp/subjects/popup.asp?id=" + theID;
	req = createXMLHTTPObject();
	req.open("GET",theURL,true);
   	req.onreadystatechange = getResponse;
   	req.send(null);
	moreLeftPos = leftPos;
	moreTopPos = topPos;
	
}

function getResponse() 
{	
	if (req.readyState == 4) 
	{	
		thePopupHolder.innerHTML = req.responseText;
		requestPopupSize(selectedID); //this call will set the popupHeight & popupWidth
		
	}
}


function requestPopupSize(theID)
{
	var randomnumber=Math.floor(Math.random()*10000)
	theURL = "/asp/subjects/getPopupSize.asp?id=" + theID + "&r=" + randomnumber;
	req2 = createXMLHTTPObject();
	req2.open("GET",theURL,true);
   	req2.onreadystatechange = getResponseSize;
   	req2.send(null);
}

function getResponseSize() 
{	
	if (req2.readyState == 4) 
	{	
		//get the response array in JSON
		var response = eval("(" + req2.responseText + ")");
		popupHeight = response.popupSize.height;
		popupWidth = response.popupSize.width;
		x = findPos(anchorPlacement);
		
		//calc left and top pos here
		qscreenHeight = document.body.clientHeight;
		qscreenWidth = document.body.clientWidth;
		
		popupLeftPos = moreLeftPos;
		popupTopPos = moreTopPos;
		moreLinkWidth = 50;
		moreLinkHeight = 13;
		
		if ( (parseInt(moreLeftPos) + parseInt(popupWidth)) > (qscreenWidth-225) )
		{
			//bump Left
			popupLeftPos = parseInt(moreLeftPos) - parseInt(popupWidth) + moreLinkWidth;
		} 
		
		if ( (parseInt(moreTopPos) - parseInt(document.body.scrollTop) + parseInt(popupHeight)) > (qscreenHeight) ) 
		{
			//bump Up
			popupTopPos = parseInt(moreTopPos) - parseInt(popupHeight) - moreLinkHeight;
			thePopupHolder.className='fixIEHeight';
		}
		
		thePopupHolder.style.left = popupLeftPos;
		thePopupHolder.style.top = popupTopPos;
		thePopupLoading.style.visibility='hidden';
		thePopupHolder.style.visibility='visible';
		thePopupHolder.style.display='block'; /* fix for FF Mac issue */
	}
}

var showMoreSubjects=function(o,e,theID){
       	if(thePopupHolder.closerTimeout) clearTimeout(thePopupHolder.closerTimeout);
		var pnl=get$(o.parentNode.parentNode.getAttribute('mypanel'));
       	bindPopupParentChild(o,pnl);
	   	thePopupHolder.style.visibility = 'hidden';
		thePopupHolder.style.display='none'; /* fix for FF Mac issue */
		thePopupLoading.style.visibility = 'hidden';
		theAnchor = 'moreAnchor_' + theID;
		anchorPlacement = document.getElementById(theAnchor);
		x = findPos(anchorPlacement);
		thePopupLoading.style.left = x[0]+'px';
		thePopupLoading.style.top = x[1]+'px';
		thePopupLoading.style.visibility='visible';
		thePopupHolder.className='fixIE';
		requestData(theID, x[0]+'px', x[1]+'px');
}
