company_id='{/literal}{$company_id}{literal}';
album_id='';
album_name='';

var sURL = unescape(window.location.pathname);

var gRowId = 2;
function movediv(something)
{
  var group1=document.getElementById("group1")
  var group2=document.getElementById("group2")
  var e = document.getElementById('togglepic'+something)	
  if (e.src.match('stage')!=null)
   e.src = "images/btn_wrkbench.png";
  else
   e.src = "images/btn_stage.png";
  var children = group1.childNodes;
   for (var i = 0; i < children.length; i++) 
   {
	if (children[i].id==something)
	{
	group2.appendChild(children[i])
	updateKeywordlist()
	return
	}
   }
  children = group2.childNodes;
   for (var i = 0; i < children.length; i++)
   {
        if (children[i].id==something)
        {
        group1.appendChild(children[i])
	updateKeywordlist()
        return
        }
   }
}
function delRow(button)
{
   var row = button.parentNode.parentNode;
   var tbody = document.getElementById('table1').getElementsByTagName('tbody')[0];
   var deleted_keyword = row.cells[1].innerHTML
   tbody.removeChild(row);
   colorRows()
   xmlHttp=GetXmlHttpObject()
   if (xmlHttp==null)
   {
     alert ("Browser does not support HTTP Request")
     return
   }
   var imageList = lightboxcontents();
   var keywordlist = keywordlistcontents();
   var url="removekeyword.php?deleted_keyword="+deleted_keyword+"&company_id="+company_id+"&album_id="+album_id+"&image_list="+imageList+"&keywords="+keywordlist
   //alert(url);
   xmlHttp.onreadystatechange=keywordRemoved
   xmlHttp.open("GET",url,true)
   xmlHttp.send(null)
}

function refresh()
{
    window.location.href = sURL;
}
function colorRows()
{
var table   = document.getElementById('colorMe');
	var colors  = ['#666666','#535353'];
	var counter = 0;
	var tr      = table.firstChild;
	while(tr){
		tr.style.backgroundColor = colors[counter++ % 2];
		tr = tr.nextSibling;
}
}

//called on the upload/change photos page when moved is click in an image row
//moves an image to a new album
function moveImage(image_id,old_album_id,image_name,old_album_name)
{
  var new_id="move_"+image_id
  var new_album_id=document.getElementById(new_id).options[document.getElementById(new_id).selectedIndex].value
  var new_album_name =escape(document.getElementById(new_id).options[document.getElementById(new_id).selectedIndex].innerHTML)
  //alert("move image "+image_id+" in album "+old_album_id+" to album "+new_album_id+"?")
  input_box=confirm("move image '"+image_name+"' to album '"+new_album_name+"'?");
   if (input_box==true)
	{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="moveImage.php?image_id="+image_id+"&old_album_id="+old_album_id+"&new_album_id="+new_album_id+"&new_album_name="+new_album_name+"&image_name="+escape(image_name)+"&old_album_name="+escape(old_album_name)
	//alert(url);
	xmlHttp.onreadystatechange=imageMoved
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	}
}
function imageMoved()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
      alert(xmlHttp.responseText)
      refresh()
    }
}

function GetXmlHttpObject()
{
   return Try.these(
         function() {return new XMLHttpRequest()},
         function() {return new ActiveXObject('Msxml2.XMLHTTP')},
         function() {return new ActiveXObject('Microsoft.XMLHTTP')}
    ) || false;
}

function delAlbum(company_id, album_id, album_name)
{
 input_box=confirm("delete album '"+album_name+"'?");
 if (input_box==true)
 	{
 	   xmlHttp=GetXmlHttpObject()
 	   if (xmlHttp==null)
 	   {
 	     alert ("Browser does not support HTTP Request")
 	     return
 	   }
 	   var album_id=escape(album_id)
 	   var album_name=escape(album_name)
 	   var company_id=escape(company_id)
 	   var url="delAlbum.php?album_id="+album_id+"&album_name="+album_name+"&company_id="+company_id
 	   //alert(url);
 	   xmlHttp.onreadystatechange=albumRemoved
 	   xmlHttp.open("GET",url,true)
 	   xmlHttp.send(null)
 	}
 	else
 	{
 		//alert ("not removed");
	}
}

function clearBench()
{
	var tbody = document.getElementById('table1').getElementsByTagName('tbody')[0];
	tbody.innerHTML=''

  //document.getElementById("myList").innerHTML=''
  document.getElementById("sug_box").innerHTML= ''
  document.getElementById("results").innerHTML=''
}

function albumRemoved()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
      //alert('removed')
      window.location="{/literal}{$URL}{literal}/photos.php"
    }
}

//called when the "remove" link beside an autoadd is clicked 
function delAutoAdd(s,pid,w)
{
	var url="delautoadd.php?synonym="+s+"&parent_id="+pid+"&company_id="+company_id
	document.getElementById("current_word").innerHTML=w
        //alert(url);
        xmlHttp=GetXmlHttpObject()
   	if (xmlHttp==null)
   	{
   	  alert ("Browser does not support HTTP Request")
   	  return
	}
         xmlHttp.onreadystatechange=autoAddDeleted
         xmlHttp.open("GET",url,true)
         xmlHttp.send(null)
}

function autoAddDeleted()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
      var w=document.getElementById("current_word").innerHTML
      refreshSynBox(w)
      alert('removed')
    }
} 

//called when the autoadd icon next to a suggestion synonym is clicked
function moveToAutoAdd(i,e,w)
{
 	document.getElementById("current_word").innerHTML=w   
        input_box=confirm("autoadd '"+e+"' as synonym for '"+w+"'?");
	if (input_box==true)
	{
	//alert('debug: autoadding word with id '+i)
	 var word=escape(w)
         var synonym=escape(e)
         function3(e,0,w)
         var url="addautoadd.php?synonym="+synonym+"&word="+word+"&company_id="+company_id
        //alert(url);
        xmlHttp=GetXmlHttpObject()
   	if (xmlHttp==null)
   	{
   	  alert ("Browser does not support HTTP Request")
   	  return
	}
         xmlHttp.onreadystatechange=dictAutoadded
         xmlHttp.open("GET",url,true)
         xmlHttp.send(null)
         }
}

function moveToAutoAdd2(e,w) //the non-prompt version
{
 	document.getElementById("current_word").innerHTML=w   
 	   xmlHttp=GetXmlHttpObject()
 	   if (xmlHttp==null)
 	   {
 	     alert ("Browser does not support HTTP Request")
 	     return
 	   }
	 var word=escape(w)
         var synonym=escape(e)
         var url="addautoadd.php?synonym="+synonym+"&word="+word+"&company_id="+company_id
        //alert(url);
         xmlHttp.onreadystatechange=dictAutoadded2
         xmlHttp.open("GET",url,true)
         xmlHttp.send(null)
}

function dictAutoadded2()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
      //var autoadds = document.getElementById("autoaddbox").innerHTML
      if (xmlHttp.responseText=='error')
      {
       alert('aready autoadded')
       return
      }
      //var outputstring=autoadds+xmlHttp.responseText+' <a href="javascript:delAutoAdd(w,pid)">[remove]</a><br />'
      //document.getElementById("autoaddbox").innerHTML= outputstring
      var w=document.getElementById("current_word").innerHTML
      refreshSynBox(w)

      alert('autoadded')
    }
} 

function dictAutoadded()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
     // alert('autoadded')
          if (xmlHttp.responseText=='error')
           {
            alert('aready autoadded')
            return
           }

      //var autoadds = document.getElementById("autoaddbox").innerHTML
      //var outputstring=autoadds+xmlHttp.responseText+' <a href="javascript:delAutoAdd(w,pid)">[remove]</a><br />'
      //document.getElementById("autoaddbox").innerHTML= outputstring
      var w=document.getElementById("current_word").innerHTML
      refreshSynBox(w)

    }
}    

//called when the red minus sign is click on next to a suggestion synonym to eliminate it from being suggested again
function confirm_delete(t,e,w)
{
	input_box=confirm("Remove '"+e+"' as synonym for '"+w+"'?");
	if (input_box==true)
	{
	   var holdingcell = ((t.parentNode).parentNode)
	   holdingcell.parentNode.removeChild(holdingcell)

	   xmlHttp=GetXmlHttpObject()
	   if (xmlHttp==null)
	   {
	     alert ("Browser does not support HTTP Request")
	     return
	   }
	   var word=escape(w)
	   var synonym=escape(e)
	   var url="deletesyn.php?synonym="+synonym+"&word="+word+"&company_id="+company_id
	   //alert(url);
	   xmlHttp.onreadystatechange=synonymRemoved
	   xmlHttp.open("GET",url,true)
	   xmlHttp.send(null)
	}
	else
	{
		;//alert ("not removed");
	}
}

function synonymRemoved()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
      ;//alert('removed')
    }
}
//returns the names of the images in the lightbox
function lightboxcontents()
{
  //alert('lightboxcontents')
  var e = document.getElementById("group2")
  var crImages = e.getElementsByClassName('imagelink');
  var crImageNames = [];
  for(var i in crImages)
  {
   if (crImages[i].title!=null)
     crImageNames.push(crImages[i].title);
  }
  var j = crImageNames.join(',');
  //alert(crImageNames.length)
  //alert(j)
  return j
}

//returns the number of images in the lightbox
function lightboxcount()
{
   var e = document.getElementById("group2")
   var crImages = e.getElementsByClassName('imagelink');
   var crImageNames = [];
   for(var i in crImages)
   {
     if (crImages[i].title!=null)
     crImageNames.push(crImages[i].title);
   }
   var j = crImageNames.join(',');
   return crImageNames.length
}

//(1) returns a list of the current keywords
function keywordlistcontents()
{
  //alert('keywordlistcontents')
	var table1 = document.getElementById('table1');
    var keywords = [];
	for (var i=0; i<table1.rows.length; i++)
	{
		if (table1.rows[i].cells[1].innerHTML!=undefined)
		{
			keywords.push(table1.rows[i].cells[1].innerHTML)
		}
	}
    var j = keywords.join(',');
    return j
}

  function moveThisBack(id)
  {
   var popback=document.getElementById(id)
    var group1 = $('group1');
    var group2 = $('group2');
      try
      {
        group2.appendChild(popback);
      }
      catch(e)
      {
  
      }
    }

function moveback()
{
  var group1 = $('group1');
  var group2 = $('group2');
  while(group2.childNodes.length)
  {
    try
    {
      group1.appendChild(group2.firstChild);
    }
    catch(e)
    {
    }
  }
  var $j = jQuery.noConflict();
  $j("#colorMe").html('');
  $j("#sug_box").html('');
  $j("#results").html('');
}

function keywordRemoved()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
      ;//alert('removed')
    }
}

function addrow(keyword)
{
var tbody = document.getElementById('table1').getElementsByTagName('tbody')[0];

var row = document.createElement('TR');

var cell1 = document.createElement('TD');
var cell2 = document.createElement('TD');
var cell3 = document.createElement('TD');

var inp1 =  document.createElement('img');
var inp2 = document.createElement('img');

var image = document.createElement('img');
inp1.setAttribute('src','images/icon_keyword.gif');
inp2.setAttribute('type','img');
inp2.setAttribute('src','images/icon_del.gif');
inp2.onclick=function(){delRow(this);}

cell1.appendChild(inp1);
cell2.innerHTML = keyword;
cell3.appendChild(inp2);
cell1.setAttribute('width', '20');
cell3.setAttribute('width', '20');

row.appendChild(cell1);
row.appendChild(cell2);
row.appendChild(cell3);

tbody.appendChild(row);

gRowId++;
colorRows()
}

//called when a suggestion synonym is click on to be added to the keyword list of an image
//add to list & add to keywords. second parameter is autoadd. 0=no, 1=yes, p=baseword
function function3(m,n,p) 
{
   if (lightboxcount()==0)
   {
	alert('please add one or more images first');
	return
   }
   if (IsInKeywordList(m)==true)
   {
   	//alert('already in list')
   	return
   }
   addrow(m)
   xmlHttp=GetXmlHttpObject()
   if (xmlHttp==null)
   {
     alert ("Browser does not support HTTP Request")
     return
   }
   var imageList = lightboxcontents();
   var url="addone.php?s="+m+"&company_id="+company_id+"&album_id="+album_id+"&image_list="+imageList+"&autoadd="+n+"&baseword="+p
   //alert(url);
   xmlHttp.onreadystatechange=addOne
   xmlHttp.open("GET",url,true)
   xmlHttp.send(null)
}
function addOne()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
      ;//alert('added')
    }
}
function refreshSynBox(e)
{
  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
  {
    alert ("Browser does not support HTTP Request")
    return
  }
  me = document.getElementById("spinner") //shows the progress spinner graphic
	me.style.visibility="visible";
	me.style.height=32;
	me.style.width=32;
	me.style.overflow="visible";
  var imageList = lightboxcontents(); //get the list of images
  var keywordlist = keywordlistcontents(); //get the list of keywords
  var url="syn.php?s="+e+"&company_id="+company_id+"&album_id="+album_id+"&image_list="+imageList+"&keywords="+keywordlist
  xmlHttp.onreadystatechange=refreshChanged
  xmlHttp.open("GET",url,true)
  xmlHttp.send(null)
}

//called when "clear deletes" is clicked on. it clears out any deletes for a word
function ClearDeletes(e)
{
  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
  {
    alert ("Browser does not support HTTP Request")
    return
  }
  me = document.getElementById("spinner") //shows the progress spinner graphic
	me.style.visibility="visible";
	me.style.height=32;
	me.style.width=32;
  var imageList = lightboxcontents(); //get the list of images
  var keywordlist = keywordlistcontents(); //get the list of keywords
  var url="syn.php?s="+e+"&company_id="+company_id+"&album_id="+album_id+"&image_list="+imageList+"&keywords="+keywordlist+"&clearDeletes=1"
  xmlHttp.onreadystatechange=refreshChanged
  xmlHttp.open("GET",url,true)
  xmlHttp.send(null)
}
function refreshChanged()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
      document.getElementById("results").innerHTML=xmlHttp.responseText
      me = document.getElementById("spinner")
      me.style.visibility="hidden"
      me.style.height=0;
	me.style.width=0;
      me.style.overflow="visible"
  }
}

//called when you type in a keyword and click "add keyword"
function getSyn()
{
var $j = jQuery.noConflict();
    $j('#suggestions').hide()
   //alert("add keyword")
   if (lightboxcount()==0) //makes sures there are images to tag
   {
	alert('please add one or more images first');
	return
   }
   me = document.getElementById("spinner") //shows the progress spinner graphic
	me.style.visibility="visible";
        me.style.height=0;
	me.style.width=0;
	me.style.overflow="visible";
	var e = document.getElementById("my_ac").value

  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
  {
    alert ("Browser does not support HTTP Request")
    return
  }
  var imageList = lightboxcontents(); //get the list of images
  //alert(imageList);
  //alert(album_id);
  //alert(album_name);
  //alert(e);
  var keywordlist = keywordlistcontents(); //get the list of keywords
  //ask syn.php for a list of synonyms for the typed in word, 'e'
  //results are sent to stateChanged() function
  var url="syn.php?s="+e+"&company_id="+company_id+"&album_id="+album_id+"&image_list="+imageList+"&keywords="+keywordlist
  //alert(url)
  xmlHttp.onreadystatechange=stateChanged
  xmlHttp.open("GET",url,true)
  xmlHttp.send(null)
}
//(2) check to see if k is in the keyword list
function IsInKeywordList(k)
{
	var table1 = document.getElementById('table1');
    for (var i=0; i<table1.rows.length; i++)
	{
		if (table1.rows[i].cells[1].innerHTML==k)
		{
				return true
		}
	}
	return false
}
//called when the "AutoAdd synonym" box is filled and "add keyword" button under that input box is clicked
function addSyn(s)
{

  var baseword=document.getElementById("baseword").value
  if (document.getElementById("my_syn").value!='')
  {
  	var synonym = document.getElementById("my_syn").value
  	 if (IsInKeywordList(synonym))
	 {
		alert('keyword already added')
		return
 	 }
	if (synonym==s)
	{
		alert('Error: can not autoadd the same word')
		return
	}
	function3(synonym,0,baseword)
	moveToAutoAdd2(synonym,baseword)
	document.getElementById("my_syn").value=''
  }
}

function stateChanged()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
      //alert('back from getSyn')
      //put the synonym list into the "results" div
      document.getElementById("results").innerHTML=xmlHttp.responseText
      //add the entered word 'e' to the keyword list
      addrow(document.getElementById("my_ac").value)
      var e = document.getElementById("my_ac").value
      //hide the spinner
      me = document.getElementById("spinner")
      me.style.visibility="hidden"
      me.style.height=0;
      me.style.width=0;
      me.style.overflow="hidden"
      //make the title of the result div say "Suggestions for 'e'"
      document.getElementById("sug_box").innerHTML= '\''+e+'\''
      //clear out the keyword input box
      document.getElementById("my_ac").value=''
      //update the keyword list for autoadds
      updateKeywordlist()
  }
}

function getSyn2(e)
{
  new Ajax.Updater('results','syn.php?s='+$('my_ac').value);
  document.getElementById("results").innerHTML=xmlHttp.responseText
  document.getElementById("my_ac").value=''
}

//called when an image is dragged to/from the lightbox into/out of the work area
function updateKeywordlist()
{ 
  if (lightboxcount()==0) //if there are no images, clear out the keyword list
  {
       moveback()
      var $j = jQuery.noConflict();
      $j("#colorMe").html('');
      return
  }

  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
  {
    alert ("Browser does not support HTTP Request")
    return
  }
  else
  {
    var imageList = lightboxcontents();
    var url="getkeywords.php?s="+imageList+"&company_id="+company_id+"&album_id="+album_id
    //alert(url)
    xmlHttp.onreadystatechange=stateKeywordsBack
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
  }
}

//the ajax callback function that receives the keywords for an image from the database
function stateKeywordsBack()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  { 
      if (xmlHttp.responseText == '{"numArgs": 4,"arr": ,"var1": 0}')//if no keywords returned
      {
         //document.getElementById("myList").innerHTML=''
         //var tbody = document.getElementById('table1').getElementsByTagName('tbody')[0];
	 //tbody.innerHTML=''
	 var $j = jQuery.noConflict();
	 $j("#colorMe").html('');
         return
      }
      //alert(xmlHttp.responseText)
      //document.getElementById("myList").innerHTML=xmlHttp.responseText
      //document.getElementById("myList").innerHTML=''
            var $j = jQuery.noConflict();
            $j("#colorMe").html('');
       //var tbody = document.getElementById('table1').getElementsByTagName('tbody')[0];
	//tbody.innerHTML=''
      //loop through results and 
      //addrow(m)
        var t  = new Object();
        eval("t="+xmlHttp.responseText);
	var arr = t['arr']; 
	//alert(arr.val)
	for (var loop = 0; loop <t['var1']; loop++)
	{
		//alert(arr[loop])
		addrow(arr[loop]);
	}
  }
}
//called when the "Close" button is clicked in the Add Person div
function addPerson()
{
var counter=0;
var addwords = new Array()
for(i=0; i<document.peopleform.elements.length; i++)
{
  if(document.peopleform.elements[i].type=='checkbox')
  {
    if (document.peopleform.elements[i].checked==true) 
    {
      //alert(document.peopleform.elements[i].name + "= " + document.peopleform.elements[i].value )
      addwords[counter++]= document.peopleform.elements[i].value
    }
   }
}
//alert(addwords.length)
for (j=0;j<addwords.length;j++)
{
   if (lightboxcount()==0)
   {
	alert('please add one or more photos first');
	return
   }
   if (IsInKeywordList(addwords[j])==1)
   {
   	alert(addwords[j]+' already in list')
   	return
   }
   //alert(addwords[j])
   function3(addwords[j],0,'')
 }

}
