
//var	xmlHttp;
function xml_job_list(){
	
	this.job_list= new Array();
	this.job_cnt=new Number();
	this.status=0;
	this.working=false;
  this.xmlHttp=new GetXmlHttpObject();
}

var photo_list = new xml_job_list();

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;
  }




function GetXmlHttpObject()
{
var xmlHttp2=null;

try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp2=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp2;
}




//UI HANDLING FUNCTIONS

var cnt=0;

xml_job_list.prototype.add_job = function ( job )
{
 //job_params=job.split(' ');
 //if(job_params[0]=='load') 
 //alert(job);
 this.job_list.push(job);
 //else this.job_list.unshift(job);
 this.job_cnt++;
 
}

xml_job_list.prototype.process_list = function ()
{
 //document.write('process_list called ' + this.working);
 this.xmlHttp=new GetXmlHttpObject();
 cnt++;
 //alert(cnt);
 //if(this.working==true){return;alert('called while true')};
 //alert(this.working);
 this.working=true;
 this.status=1;
 

  var job=this.job_list.shift();
  
 	job_params=job.split(' ');
	var id=job_params[1];
	switch(job_params[0])
	{
	
	case 'load':
			   
	
			 	 
				 var new_pic = document.createElement('span');
				 //new_pic.className = "photo_box";

				 new_pic.innerHTML="<table class=\"photo_box\" height=\"100\"><tr><td><b><div style=\"font-size: 14pt; text-align: center;\">Loading...</font></b></div></td></tr></table>";
				 new_pic.id="pic"+id;
				 document.getElementById('pic_list').appendChild(new_pic);
				 

				 
				 var url="/albums_action.php?a=get_photo";
				 url=url+"&t="+id;
				 url=url+"&sid="+Math.random();
				 
				 //alert("This is the id:"+id);
				 //alert("this is the url:"+url);
				 
				 var new_id="pic"+id;
				 //this.xmlHttp.open("GET",url,true);
				 this.xmlHttp.onreadystatechange=function(){
				 														if (photo_list.xmlHttp.readyState==4)
																		{
																		 	 
																			 //alert("This is the new_id: "+new_id);
																			 //alert(xmlHttp.responseText);
																			
																			 document.getElementById(new_id).innerHTML=photo_list.xmlHttp.responseText;
																			 photo_list.working=false;
																		}
				 															}																			
				 this.xmlHttp.open("GET",url,true);
				 //alert('sending');
				 this.xmlHttp.send(null);
				 //setTimeout("photo_list.xmlHttp.send(null)",1000);
				 		
	
	
	break;
	
	case 'rotate':
 				 
				 
				 
				 var new_pic = document.createElement('div');
				 new_pic.className = "overlay_box";
				 new_pic.style.display = ''; 
				 new_pic.innerHTML="<br /><br /><br />Rotating...";
				 new_pic.id="rotate"+id;
				 //new_pic.style.width="";
				 new_pic.style.position="absolute";
				
				 new_pic.style.left=findPosX(document.getElementById('photo'+id));
				 new_pic.style.top=findPosY(document.getElementById('photo'+id));
				 new_pic.style.height=document.getElementById('photo'+id).offsetHeight;
				 new_pic.style.width=document.getElementById('photo'+id).offsetWidth;
				 
				 document.getElementById('pic_list').appendChild(new_pic);
				 
				 
				 
				 
				 
				 
				 //alert('attempting to rotate');
				 var dir=job_params[2];
				 
				 var url="/albums_action.php";
				 url=url+"?pid="+id;
				 url=url+"&a=rotate";
				 url=url+"&dir="+dir;
				 url=url+"&sid="+Math.random();
				 //alert("This is the id:"+id);
			
		
				 this.xmlHttp.onreadystatechange=function(){
				 														if (photo_list.xmlHttp.readyState==4)
																		{
																			
																			var photo_id=photo_list.xmlHttp.responseText;
																			var new_id="photo"+id;
																			//alert(photo_list.xmlHttp.responseText);
																			document.getElementById(new_id).src="/static/"+id+"_t.jpg?"+Math.random();
																			photo_list.working=false;
																			document.getElementById('pic_list').removeChild(document.getElementById("rotate"+id)); 
																		}
}
				 this.xmlHttp.open("GET",url,true);
				 this.xmlHttp.send(null);
	break;
	
	
	
	
	
	
	
	
	
	case 'save':

	 var comment = document.getElementById("comment"+id).value;
 	 var main_photo =  document.getElementById("main_photo"+id).value;

   //alert(main_photo); 

				 
				 
				 var url="albums_action.php";
				 url=url+"?pid="+id;
				 url=url+"&comment="+comment;
				 url=url+"&main_photo="+main_photo;
				 url=url+"&a=save_photo";
				 //url=url+"&aid="+ <?php echo $_GET['aid']; ?>;
				 url=url+"&sid="+Math.random();
				 //alert("This is the id:"+id);
				 document.getElementById("div"+id).style.display='none'; 
				 document.getElementById("div"+id).style.border='0'; 
		     //document.getElementById('pic_list').removeChild(document.getElementById("pic"+id));
		
		
				 this.xmlHttp.onreadystatechange=function(){
				 														if (photo_list.xmlHttp.readyState==4)
																		{
																			
																			//alert(photo_list.xmlHttp.responseText);					
																			photo_list.working=false;
																			
																		}
}
				 this.xmlHttp.open("GET",url,true);
				 this.xmlHttp.send(null);
	break;
	
	
	
	case 'delete':

				
		     var url="/albums_action.php";
				 url=url+"?pid="+id;
				 url=url+"&a=delete_photo";
				 url=url+"&sid="+Math.random();
				 document.getElementById("div"+id).style.display='none'; 
				 //document.getElementById("div"+id).style.border='0'; 

		 
		
				 this.xmlHttp.onreadystatechange=function(){
				 														if (photo_list.xmlHttp.readyState==4)
																		{

																		  //alert(photo_list.xmlHttp.responseText);					
																			
																			photo_list.working=false;
																			
																		}
															}
				 this.xmlHttp.open("GET",url,true);
				 this.xmlHttp.send(null);
	break;
	
	}

}


xml_job_list.prototype.grab_photos = function( id )
{

 															

}

function check_list()
{
 //error_out(photo_list.working+"    list_length: "+photo_list.job_list.length);
 if(photo_list.working==false && photo_list.job_list.length>0){
 photo_list.working=true;
 photo_list.process_list();
 return;
 }
 
 return false;
}
 
setInterval("check_list()", 100); 
function error_out(str)
{
 //var text = document.createElement('div');
 //text.innerHTML=str+"<br>";
 
 //document.getElementById('debug').innerHTML='';
 //document.getElementById('debug').appendChild(text);
}


