function getHTTPObject() {

  var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {

      try {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (E) {

        xmlhttp = false;

      }

    }

  @else

  xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

    try {

      xmlhttp = new XMLHttpRequest();

    } catch (e) {

      xmlhttp = false;

    }

  }

  return xmlhttp;

}

var http = getHTTPObject(); // We create the HTTP Object

function recent_images()
{
	
	
	http.open("GET", "/ajax/previous_images.php", true);
	http.onreadystatechange = recent_images_response;
	http.send(null);	
	
}

function recent_images_response()
{
	if (http.readyState == 4) {
 	var response =http.responseText;
 	//alert('yes');
 	var photiques = new Array();
 	var sizes = new Array();
 	var widths = new Array();
 	var heights = new Array();
 	
 	var html='';
 	lines = response.split("\n");
 	if(lines.length>2)
 	{
 		//Show History 	
 		
 		//alert('showing history');
 		document.getElementById('recent').style.display="block";
 		document.getElementById('recent_none').style.display="none";	
 		
 		photiques = lines[0].split(",");
 		sizes = lines[1].split(",");
 		widths = lines[2].split(",");
 		heights = lines[3].split(","); 	
 		greys = lines[4].split(","); 
 		sepias = lines[5].split(","); 
 		horizontals = lines[6].split(","); 
 		verticals = lines[7].split(","); 
 		
 	} else 	
 	{
 		//Hide History
 		//alert('hiding history');
 		
 		document.getElementById('recent').style.display="none";
 		document.getElementById('recent_none').style.display="block";
 		 		
 	}
 	
 	//alert(photiques.length);
 	//alert(response); 	
 	
 	if(photiques.length>0)
 	{
 		//alert('We have some images!');
 		html='<table id="t1" cellpadding="0" cellspacing="0" border="0" width="' + 110*photiques.length +'"><tr>';
 		
 		var i=0;
 		for(var i=0; i<photiques.length; i++)
 		{ 			
 			//alert("i:" + i);
 			//alert(photiques[x]); 	
 			
 			html+='<td>';
 			html+='<a href="javascript:change_image(' + widths[i] +',' + heights[i] +  ',' + photiques[i] +',' + greys[i] +',' + sepias[i] +',' + horizontals[i] +',' + verticals[i] +',' + sizes[i] +');"><img src="/userimages/working_thumb/saved_' + photiques[i] + '.jpeg" border="0">'; 			
 			html+='</td>';
 		
 		} 		
 		html+='</tr></table>';
 	
 		
 		
 		//document.getElementById('t1').offsetWidth=110*photiques.length;	
 		
 		//alert(html);
 
 		document.getElementById('lyr1').innerHTML=html;
 		document.getElementById('t1').width=110*photiques.length;	
 		initScrollLayer();
 		//alert(document.getElementById('t1').width)
 		
 		if(photiques.length>6)
 		{
 		
 			document.getElementById('rightarrow').style.visibility='visible';
 		}
 	}
 	
 	
 	 	
 	//initScrollLayer();
 	 		
  }
}


function openwindow_upload()
{
	newwindow=open("/uploadimage.php","newwin","scrollbars=no, toolbar=no, directories=no, menu bar=no, resizable=no, status=no, width=400, height=500");
}


var current_image =0;

function change_image(width, height, id, grey, sepia, horizontal, vertical, photique_size)
{
	current_image++;
	//alert(width + " " + height + " " + id + " " +photique_size);

	 //Cropper.Img('remove');
	myCropper.remove();
	
	var html='';
	html+='<img src="/userimages/working/saved_' + id + '.jpeg" alt="test image" id="testImage_' + current_image +'" onload="image_cropper(' + current_image +',' + width + ',' + height + ')"; />';
	document.getElementById('testWrap').innerHTML=html;
	
	/*
	//document.getElementById('testImage').src.width=300;
	//document.getElementById('testImage').src.height=300;
	alert('still ok');
		
		*/
	
	///myCropper.forceReRender();
	
	/*
	 myCropper = new Cropper.Img( 
					'testImage_' + current_image , 
					{ 
						ratioDim: { x:width, y: height }, 
						minWidth: width, 
						minHeight: height, 
						displayOnInit: true, 
						onEndCrop: onEndCrop 
					} 
		) 
		*/
				
	document.getElementById('image_id').value=id;
	
	document.getElementById('photique_size').value=photique_size;		
	document.getElementById('grey').value=grey;
	document.getElementById('sepia').value=sepia;
	document.getElementById('horizontal').value=horizontal;
	document.getElementById('vertical').value=vertical;
	
	if(grey==1)
	{
		document.getElementById('grey_image').src='/images/icon-monochrome-on.gif';
	} else	
	{
		document.getElementById('grey_image').src='/images/icon-monochrome.gif';
	}
}

function image_cropper(current_image, width, height)
{
	 myCropper = new Cropper.Img( 
					'testImage_' + current_image , 
					{ 
						ratioDim: { x:width, y: height }, 
						minWidth: width, 
						minHeight: height, 
						displayOnInit: true, 
						onEndCrop: onEndCrop 
					} 
		) 
}

function modify_image(action, degrees, direction)
{
	if(action=='rotate')
	{
		hide_page();
	
		http.open("GET", "/ajax/rotate_image.php?id=" + document.getElementById('image_id').value + "&action=rotate&degrees=" + degrees, true);
		http.onreadystatechange = modify_image_response;
		http.send(null);	
	}	
	
	if(action=='flip')
	{
		hide_page();
		if(direction=='horizontal')
		{
			if(document.getElementById('horizontal').value==1)
			{
				http.open("GET", "/ajax/rotate_image.php?id=" + document.getElementById('image_id').value + "&action=horizontal&value=0", true);
			} else
			{
				http.open("GET", "/ajax/rotate_image.php?id=" + document.getElementById('image_id').value + "&action=horizontal&value=1", true);
			}
		}
		
		if(direction=='vertical')
		{
			if(document.getElementById('vertical').value==1)
			{
				http.open("GET", "/ajax/rotate_image.php?id=" + document.getElementById('image_id').value + "&action=vertical&value=0", true);
			} else
			{
				http.open("GET", "/ajax/rotate_image.php?id=" + document.getElementById('image_id').value + "&action=vertical&value=1", true);
			}
		}
	
		http.onreadystatechange = modify_image_response;
		http.send(null);
	}
	
	if(action=='grey')
	{
		hide_page();
		if(document.getElementById('grey').value==1)
		{
			http.open("GET", "/ajax/rotate_image.php?id=" + document.getElementById('image_id').value + "&action=grey&value=0", true);
		} else		
		{
			http.open("GET", "/ajax/rotate_image.php?id=" + document.getElementById('image_id').value + "&action=grey&value=1", true);
		}
		http.onreadystatechange = modify_image_response;
		http.send(null);
	}
}

function modify_image_response()
{
	if (http.readyState == 4) {
		
		var response =http.responseText;

		if(response=='resolution')
		{
			alert('Image is not high enough resolution to be rotated that way');
		}else if(response=='problem')
		{
			alert('there was a problem');
		} else
		{
			var details= new Array();
			
			details = response.split(",");
			recent_images();

			//alert(details[0] + " " + details[1] + " " + details[2] + " " + details[3]);
			change_image(details[0], details[1], details[2], details[3], details[4], details[5], details[6], details[7]);

		}


		//document.getElementById('debug').innerHTML=response + "test";
		show_page();
	}
}

function hide_page()
{

//alert(document.body.clientHeight);
document.getElementById('loading').style.visibility='visible';
//document.getElementById('floating').style.height=document.body.clientHeight + 'px';
//alert(document.getElementById('floating').style.height);
}

function show_page()
{
document.getElementById('loading').style.visibility='hidden';
}
