var http;
var divid = "";
var dividExt = "";
var idExt = 0;
var strServerRoot = "";
var nAutoLoadAvailability = 0;
var nHighlightActiveTitle = 0;
var strFusionCopyrightNotice = "";
var nLibraryThingEnabled = 0;
var strISBN = "";

function GetCookie(name)
{
    var cookies = document.cookie.split("; ");
    
    for (var index = 0; index < cookies.length; index++)
    {
        var crumb = cookies[index].split("=");
        if (name == crumb[0]) 
            return unescape(crumb[1]);
    }
    
    return null;
}

function SetServerRoot(value)
{
    strServerRoot = value;
}

function SetAutoLoadAvailability(value)
{
    nAutoLoadAvailability = value;
}

function SetFusionCopyrightNotice(value)
{
    strFusionCopyrightNotice = value;
}

function SetLibraryThingEnabled(value) 
{
    nLibraryThingEnabled = value;
}

function SessionTimedOut()
{
    document.body.style.cursor = "default";
    
    if (null == GetCookie("SPOOF"))
    {
        alert("Your session has timed out.");
        window.location = strServerRoot + "default.aspx";
    }
    else
    {
        external.TimeoutSession();
    }
}

function handleHttpResponseContent() 
{
	if (http.readyState == 4)
	{ 
	    if (http.responseText.indexOf("POWERPAC-ERROR:TIMEOUT") != -1)
	    {
	        SessionTimedOut();
	        return;
	    }

		document.getElementById(divid).innerHTML = http.responseText;
        document.body.style.cursor = "default";
	}
}

function handleHttpResponseContentInitial() 
{
	if (http.readyState == 4)
	{ 
	    if (http.responseText.indexOf("POWERPAC-ERROR:TIMEOUT") != -1)
	    {
	        SessionTimedOut();
	        return;
	    }

		document.getElementById(divid).innerHTML = http.responseText;
		ShowExtendedOptions(idExt);
	}
}

function ShowExtendedOptions(pos)
{
    http = getHTTPObject();
    divid = "options_" + pos;

    http.open("GET", strServerRoot + "search/components/extendedoptions.aspx?pos=" + pos, true);
	http.onreadystatechange = handleHttpResponseContent;
	http.send(null);
    document.body.style.cursor = "wait";
}

function handleHttpResponseTitleUpdate() 
{
	if (http.readyState == 4)
	{ 
	    if (http.responseText.indexOf("POWERPAC-ERROR:TIMEOUT") != -1)
	    {
	        SessionTimedOut();
	        return;
	    }

		document.getElementById(divid).innerHTML = http.responseText;
	
		http = getHTTPObject();
		divid = 'dashTitleList'; 

		http.open("GET", strServerRoot + "search/components/titlelistdash.aspx", true);
		http.onreadystatechange = handleHttpResponseContent;
		http.send(null);
        document.body.style.cursor = "wait";
	}
}

function AddToList(pos, controlnumber, listid)
{
	http = getHTTPObject();
	
    divid = 'addtolist_' + pos; 
	document.getElementById(divid).innerHTML = "";
	
    http.open("GET", strServerRoot + "search/components/titlelistlink.aspx?action=add&pos=" + pos + "&cn=" + controlnumber + "&list=" + listid, true);
	http.onreadystatechange = handleHttpResponseTitleUpdate;
	http.send(null);

    var elems = document.getElementsByName('TLMenu_' + pos);
    if (elems.length != 0)
        if(elems[0].style.display == "")
            elems[0].style.display = "none";

    document.body.style.cursor = "wait";
}

function RemoveFromList(pos, controlnumber)
{
	http = getHTTPObject();
	
    divid = 'addtolist_' + pos; 

    http.open("GET", strServerRoot + "search/components/titlelistlink.aspx?action=remove&pos=" + pos + "&cn=" + controlnumber, true);
	http.onreadystatechange = handleHttpResponseTitleUpdate;
	http.send(null);
    document.body.style.cursor = "wait";
}

function IsThisTitleExpanded(id)
{
    var style;

	if (document.getElementById)
        style = document.getElementById('content_' + id).style;
	else if (document.all)
		style = document.all['content_' + id].style;
	else if (document.layers)
		style = document.layers['content_' + id].style;

    if (style.display == "block")
        return true;
    else
        return false;
}

function ShowContent(pos, show, highlight, isbn) 
{
    if (isbn)
        strISBN = isbn;
    
    if (IsThisTitleExpanded(pos) == false)
        ExpandThisTitle(pos);
       
	if (show != "" && highlight != 0)
		HighlightThisTitle(pos);

	http = getHTTPObject(); 

    divid = "content_" + pos;

    if (show == "localavail0" || show == "autoavail0")
        http.open("GET", strServerRoot + "search/components/availability.aspx?level=local&pos=" + pos + "&morelink=0", true);
    else if (show == "localavail1" || show == "autoavail1")
        http.open("GET", strServerRoot + "search/components/availability.aspx?level=local&pos=" + pos + "&morelink=1", true);
    else if (show == "systemavail")
        http.open("GET", strServerRoot + "search/components/availability.aspx?level=system&pos=" + pos, true);
    else if (show == "details" || show == "autodetails")
        http.open("GET", strServerRoot + "search/components/details.aspx?pos=" + pos, true);
    else if (show == "marc")
        http.open("GET", strServerRoot + "search/components/marc.aspx?pos=" + pos, true);
    else if (show == "similar")
        http.open("GET", strServerRoot + "search/components/similartitles.aspx?isbn=" + isbn, true);
    else if (show == "mapit")
    {
        document.getElementById(divid).innerHTML = "<iframe width=650 height=700 frameborder=0 src=" + strServerRoot + "search/components/mapit.aspx?pos=" + pos + "></iframe>";
        idExt = pos;
        ShowExtendedOptions(idExt);
        return;
    }
    else 
    {
        document.getElementById(divid).innerHTML = "";
        return;
    }

    if (show == "autoavail0" || show == "autoavail1" || "autodetails")
    {
        idExt = pos;
        http.onreadystatechange = handleHttpResponseContentInitial;
    }
    else
	    http.onreadystatechange = handleHttpResponseContent;
	
	http.send(null);
    document.body.style.cursor = "wait";
}

function LogFusionTransaction(pos, status)
{
	http = getHTTPObject(); 
    http.open("GET", strServerRoot + "search/components/fusionProcess.aspx?pos=" + pos + "&status=" + status);
	http.send(null);
}

function FusionAccessDenied(pos, message)
{
    LogFusionTransaction(pos, 0);
    alert(message);
}

function FusionLoginRequired(pos, message)
{
    LogFusionTransaction(pos, 0);
    alert(message);
}

function ShowFusionElement(pos, url, mediaType, videoType)
{
    var strBuff;
    
    if (IsThisTitleExpanded(pos) == false)
        ExpandThisTitle(pos);
       
	HighlightThisTitle(pos);
	
	LogFusionTransaction(pos, 1);

    if (mediaType == 9) // Inline HTML/Text fragment
    {
	    http = getHTTPObject(); 
        divid = "content_" + pos;
        http.open("GET", url, true);
        idExt = pos;
	    http.onreadystatechange = handleHttpResponseContentInitial;
    	http.send(null);
        document.body.style.cursor = "wait";
        return;
    }
    
    if (mediaType == 5) // Images
        strBuff = '<img src="' + url + '">' + strFusionCopyrightNotice;
    else if (mediaType == 1) // Audio -- NOT CURRENTLY BEING USED
        strBuff = '<embed src="' + url + '" autostart=true loop=false volume=60 height=360 width=400>';
    else if (mediaType == 11) // Video -- NOT CURRENTLY BEING USED
    {
        if (videoType == 1) // Windows Media 
            strBuff = '<OBJECT ID="MediaPlayer" WIDTH="400" HEIGHT="360" CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" STANDBY="Loading Windows Media Player components..." TYPE="application/x-oleobject"> <PARAM NAME="FileName" VALUE="' + url + '"><PARAM name="ShowControls" VALUE="true"><param name="ShowStatusBar" value="false"><PARAM name="ShowDisplay" VALUE="false"><PARAM name="autostart" VALUE="true"><EMBED TYPE="application/x-mplayer2" SRC="' + url + '" NAME="MediaPlayer" WIDTH="192" HEIGHT="190" ShowControls="1" ShowStatusBar="0" ShowDisplay="0" autostart="0"></EMBED></OBJECT>';
        else if (videoType == 2) // Quicktime
            strBuff = '<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" WIDTH="400" HEIGHT="360" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab"><PARAM name="SRC" VALUE="' + url + '"><PARAM name="AUTOPLAY" VALUE="true"><PARAM name="CONTROLLER" VALUE="true"><EMBED SRC="' + url + '" WIDTH="400" HEIGHT="360" AUTOPLAY="true" CONTROLLER="true" PLUGINSPAGE="http://www.apple.com/quicktime/download/"></EMBED></OBJECT>';  
    }
         
	document.getElementById("content_" + pos).innerHTML = strBuff;

    ShowExtendedOptions(pos);
}


function HighlightThisTitle(id)
{
	tds = document.getElementsByTagName("td");

	for (i=0; i<tds.length; i++) 
	{
		thisTD = tds[i];
		if (thisTD.id == 'srcp_' + id)
			thisTD.className = "ResultsCoverPaneHigh";
		else if (thisTD.id.indexOf('srcp_') == 0)
			thisTD.className = "ResultsCoverPane";

		if (thisTD.id == 'srtp_' + id)
			thisTD.className = "ResultsTitlePaneHigh";
		else if (thisTD.id.indexOf('srtp_') == 0)
			thisTD.className = "ResultsTitlePane";
	}
}

function SetDisplayAttribute(id, attr)
{
	if (document.getElementById)
	{
	    document.getElementById('expanded_' + id).style.display = attr;
		document.getElementById('button_less_' + id).style.display = attr;
	    document.getElementById('options_' + id).style.display = attr;
		document.getElementById('content_' + id).style.display = attr;
		if (nLibraryThingEnabled == 1 && strISBN != "")
		    document.getElementById('ltfl_widgets_' + strISBN + '_' + id).style.display = attr;
    }
	else if (document.all)
	{
		document.all['expanded_' + id].style.display = attr;
		document.all['button_less_' + id].style.display = attr;
		document.all['options_' + id].style.display = attr;
		document.all['content_' + id].style.display = attr;
		if (nLibraryThingEnabled == 1 && strISBN != "")
		    document.all['ltfl_widgets_' + strISBN + '_' + id].style.display = attr;
    }
	else if (document.layers)
	{
		document.layers['expanded_' + id].style.display = attr;
		document.layers['button_less_' + id].style.display = attr;
		document.layers['options_' + id].style.display = attr;
		document.layers['content_' + id].style.display = attr;
		if (nLibraryThingEnabled == 1 && strISBN != "")
		    document.layers['ltfl_widgets_' + strISBN + '_' + id].style.display = attr;
    }
}


function ExpandThisTitle(id) 
{
    SetDisplayAttribute(id, "block");

    if (nLibraryThingEnabled == 1 && strISBN != "")
        LibraryThingConnector.backends.polaris.widgets.run(strISBN, id);

    location.hash = '#__pos' + id;
}


function CollapseThisTitle(id)
{
    SetDisplayAttribute(id, "none");
}


function toggleDetails(id, bMoreLink, url, mediaType, videoType, isbn) 
{
    if (isbn)
        strISBN = isbn;

    HighlightThisTitle(id);
    if (IsThisTitleExpanded(id) && url == "")
    {
        CollapseThisTitle(id);
        document.getElementById('content_' + id).innerHTML = "";
    }
    else
    {
        ExpandThisTitle(id);
        if (url != "")
            ShowFusionElement(id, url, mediaType, videoType);
        else if (nAutoLoadAvailability == 1)
        {
            if (bMoreLink == 0)
                ShowContent(id, "autoavail0", 1);
            else
                ShowContent(id, "autoavail1", 1);
        }
        else
            ShowContent(id, "autodetails", 1);
    }
}

function forceOpenAvail(id)
{
    if (IsThisTitleExpanded(id))
 	    ShowContent(id, 'localavail', 1);
    else
        toggleDetails(id, 1);
}

function chilifreshComplete(id, isbn) 
{
    if (isbn)
        strISBN = isbn;
    HighlightThisTitle(id);
    ExpandThisTitle(id);
    idExt = id;
    ShowExtendedOptions(idExt);
}

function ShowSyndetics(pos, ids, key, file) 
{
	HighlightThisTitle(pos);

	http = getHTTPObject(); 

    divid = "content_" + pos;

    http.open("GET", strServerRoot + "search/components/syndetics.aspx?ids=" + ids + "&key=" + key + "&file=" + file, true);

	http.onreadystatechange = handleHttpResponseContent;
	http.send(null);
    document.body.style.cursor = "wait";
}

function ShowBakerAndTaylor(pos, ids, key) 
{
	HighlightThisTitle(pos);

	http = getHTTPObject(); 

    divid = "content_" + pos;

    http.open("GET", strServerRoot + "search/components/bakerandtaylor.aspx?ids=" + ids + "&key=" + key, true);

	http.onreadystatechange = handleHttpResponseContent;
	http.send(null);
    document.body.style.cursor = "wait";
}

function ToggleNC(clusterid)
{        
	http = getHTTPObject();
	divid = "dashNarrowSearch";
    http.open("GET", strServerRoot + "search/components/narrowsearchdash.aspx?clusterid=" + clusterid, true);
	http.onreadystatechange = handleHttpResponseContent;
	http.send(null);
    document.body.style.cursor = "wait";
}

function ToggleDashList(divid)
{
    var element;

	if (document.getElementById)
        element = document.getElementById(divid);
	else if (document.all)
		element = document.all[divid];
	else if (document.layers)
		element = document.layers[divid];
		
    if (element.style.display == "block")
        element.style.display = "none";
    else
        element.style.display = "block";
}

function ToggleTLMenu(e, div)
{
    TLstopB(e);
    
    var elems=document.getElementsByName(div);
    
    if (elems.length == 0) 
        return; 
    
    for(var i=0;i<elems.length;i++)
    {
        var obj = elems[i], dp = "";
        if (obj.style.display == "")
        {
            dp="none";
        }
        obj.style.display = dp;
    }
    
    return false;
}

function TLstopB(e)
{
    if (!e)
        e = window.event;    
    e.cancelBubble = true;
}
      


