//create an associative array of document extensions
//this is to decrease extension lookup time to O(1)
var docTypes = new Object();
docTypes["pdf"] = true;
docTypes["doc"] = true;
docTypes["ppt"] = true;
docTypes["pps"] = true;
docTypes["xls"] = true;

var linkHrefs;
var documentLinks = new Array();
var filenames = new Array();
var request = null;

//Scans the document for any document links (.pdf,.doc, etc.)
//any links to document that are found, a control will be added
//to allow the user to add them to the document library
function setupDocumentControls()
{
	//make sure the collection exists on this browser
	if(document.anchors)
	{
		var linkCount = document.links.length;

		var requestString = '';

		documentLinks.length=0;

		//loop through all of the links on the page and check for documents
		for(var linkIndex=0;linkIndex<linkCount;linkIndex++)
		{
			//get the extension of the page linked to
			var ext = document.links[linkIndex].href.substr(document.links[linkIndex].href.lastIndexOf(".")+1);
			var filename = document.links[linkIndex].href.substr(document.links[linkIndex].href.lastIndexOf("/")+1);

			//check if the extension is on our list of types
			if(docTypes[ext] == true)
			{
			    //store the link in our list
			    documentLinks.push(document.links[linkIndex]);
			    filenames.push(filename);
			    //add the file url to the ajax request string
			    requestString+=document.links[linkIndex].href+'|';
			}
		}

		if(documentLinks.length > 0)
		{
    		requestString= requestString.substring(0,requestString.length-1);

    		xmlhttp.open('post', '/community/knowledgeBank/documentCheck',true);
    		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.onreadystatechange = docCheckResponse;
            xmlhttp.send('files='+escape(requestString));

            //set the notification bar
//            document.getElementById('docCount').innerHTML = documentLinks.length;
//            document.getElementById('docCountHeading').style.display = 'block';
		}
		/*
				var addImage = document.createElement("img");
				addImage.alt = "+";
				addImage.title = "Add "+filename+" to the library";
				addImage.src = "/images/knowledgebank/plus_small.jpg";
				addImage.style.cursor = "pointer";

				addImage.onclick = function (){
					Modalbox.show('Add Document To Library','/doclib/modal.php?showframe=true&file='+escape(this.previousSibling.href),{height: 650, width: 600});
				}

				document.links[linkIndex].parentNode.insertBefore(addImage,document.links[linkIndex].nextSibling);

				//if the link is an image then position our graphic as absolute over it
				var linkedElement = document.links[linkIndex].lastChild;
				if(linkedElement.nodeName == "IMG")
				{
					addImage.style.position = "absolute";
					addImage.style.left = findPosX(linkedElement)+"px";
					addImage.style.top = findPosY(linkedElement)+"px";
				}
		*/
	}//end if anchors collection
}

function docCheckResponse()
{
    if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){

            var foundFiles = xmlhttp.responseText;
            if(foundFiles != 'fail')
            {
//            alert(xmlhttp.responseText);

            var foundArray = foundFiles.split('|');

            //alert(foundArray.length);
            //loop through the array of previously found document links
            for(var linkIndex = 0; linkIndex < documentLinks.length; linkIndex++)
            {
            	//create an image element for the button
            	var addImage = document.createElement("img");
            	
            	//set the image's attributes accoring to the found value returned fromthe request
            	if(document.getElementById(filenames[linkIndex]) == null)
            	{
	            	if(foundArray[linkIndex] == 0 || (foundArray[0] == "" && foundArray.length == 1))
	            	{
		            	addImage.alt = "+";
		            	addImage.id = filenames[linkIndex];
						addImage.title = "Add "+filenames[linkIndex]+" to the library";
						addImage.src = "/community/images/knowledgebank/plus_small.gif";
	            	}
	            	else
	            	{
	            		addImage.alt = "edit";
		            	addImage.id = filenames[linkIndex];
						addImage.title = "Edit "+filenames[linkIndex]+" in the library";
						addImage.src = "/community/images/knowledgebank/pencil.gif";
	            	}
            	}


				addImage.style.cursor = "pointer";

				//add the click handler function to the image

				addImage.onclick = function (){
				    hideAllFlash();
					Modalbox.show('/community/knowledgeBank/doclib/?showframe=true&file='+escape(this.previousSibling.href),{height: 650, width: 700,afterHide: reInit});
				}

				var nextSib = documentLinks[linkIndex].nextSibling;

				if(documentLinks[linkIndex].nextSibling &&
				documentLinks[linkIndex].nextSibling.nodeName=='IMG' &&
				(documentLinks[linkIndex].nextSibling.src=='http://'+window.location.hostname+'/images/knowledgebank/pencil.gif' ||
				documentLinks[linkIndex].nextSibling.src=='http://'+window.location.hostname+'/images/knowledgebank/plus_small.gif' ||
				documentLinks[linkIndex].nextSibling.src=='https://'+window.location.hostname+'/images/knowledgebank/plus_small.gif'||
				documentLinks[linkIndex].nextSibling.src=='https://'+window.location.hostname+'/images/knowledgebank/pencil.gif'))
				{
				    documentLinks[linkIndex].parentNode.removeChild(documentLinks[linkIndex].nextSibling);
				}
				documentLinks[linkIndex].parentNode.insertBefore(addImage,documentLinks[linkIndex].nextSibling);

				//if the link is an image then position our graphic as absolute over it
				var linkedElement = documentLinks[linkIndex].lastChild;
				if(linkedElement && linkedElement.nodeName == "IMG")
				{
					addImage.style.position = "absolute";
					addImage.style.left = findPosX(linkedElement)+"px";
					addImage.style.top = findPosY(linkedElement)+"px";
				}
            }//end for each document link
		}
		}
    }
}

function reInit()
{
    redisplayAllFlash();
    setupDocumentControls();

}

function hideAllFlash()
{
    var objectTags = document.getElementsByTagName("object");

    for(var objIndex=0;objIndex<objectTags.length;objIndex++)
    {
        objectTags[objIndex].style.visibility = 'hidden';
    }
}

function redisplayAllFlash()
{
    var objectTags = document.getElementsByTagName("object");

    for(var objIndex=0;objIndex<objectTags.length;objIndex++)
    {
        objectTags[objIndex].style.visibility = 'visible';
    }
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

