// Some globals
var _result;
var _filename;
var _downloadname;
var _submittype;

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

function ClearPreviewProgress()
{
  document.getElementById('preview-progress').innerHTML="";
  document.getElementById('download-progress').innerHTML="";
}
function ClearDownloadProgress()
{
  document.getElementById('download-progress').innerHTML="";
}

function ProcessGenCallback() {
 if(Ajax.CheckReadyState(Ajax.request))
 {
//  alert("callback done");
  var response = Ajax.request.responseXML.documentElement;
  var _image = response.getElementsByTagName('image');
  _result = response.getElementsByTagName('result')[0].firstChild.data;
  _filename = response.getElementsByTagName('filename')[0].firstChild.data;
//  _downloadname = decodeURIComponent(response.getElementsByTagName('downloadname')[0].firstChild.data);
  _downloadname = response.getElementsByTagName('downloadname')[0].firstChild.data;
  _isdownload = response.getElementsByTagName('isdownload')[0].firstChild.data;
  _result=RTrim(_result);
  _downloadname=RTrim(_downloadname);

  // Check whether this is a Preview or Download call that has completed
  if (_isdownload=="1") {
   //alert("callback from download");
/*
   document.getElementById('download_fields').filename.value=_filename;
   document.getElementById('download_fields').downloadname.value=_downloadname;
   document.getElementById('download_fields').submit();*/
   ClearDownloadProgress();
   var html_output="<div style='font-size: 1.1em; font-weight:bold;'>";
//   html_output+="Click <a href='download.php?filename="+_filename+"&downloadname="+_downloadname+"'><b>here</b></a> to download the full size image";
   html_output+="Click <a href='download.php?filename="+_filename+"&downloadname="+_downloadname+"'><b>here</b></a> to download your image";
   html_output+="</div>";
  document.getElementById('download-progress').innerHTML=html_output;
  }
  else {
   //alert("callback from preview");
 
   var html_output="<img src='output/"+_filename+".jpg' onload='ShowOutput()'>";
   document.getElementById('hidden_output').innerHTML=html_output;
   //document.getElementById('output').innerHTML=_result;
   /*
     alert("html_output is "+html_output);
     alert("result is "+_result);
     alert("filename is "+_filename);
   */
  }
 }
}

function ShowOutput() {
  document.getElementById('hidden_output').innerHTML="";
  document.getElementById('output').style.backgroundImage="url(output/"+_filename+".jpg)";
  ClearPreviewProgress();
}


function ProcessUpload() {
// Ajax animation
var html_output="<img src='beinguploaded.png'>";
html_output+="<img src='ajax-one.gif' style='margin:0 0 0 20px;'>";

document.getElementById('upload-progress').innerHTML=html_output;
}

function ProcessRetrieve() {
// Ajax animation
var html_output="<img src='beingretrieved.png' style='margin-bottom:10px;'>";
html_output+="<img src='ajax-six.gif' style='margin:0 0 0 20px;'>";

document.getElementById('upload-progress2').innerHTML=html_output;
}

function SetAsDownload() {
//alert("setting type as download");
 _submittype="download";
 return true;
}
function SetAsPreview() {
//alert("setting type as preview");
 _submittype="preview";
 return true;
}

function ProcessGen() {
// Get the slider position
//(thumb_height-pos)*yfactor+half_icon_height
//var slider_pos = eval(document.getElementById('gen_fields').sliderValue.value)*yfactor2-half_icon_height;
var slider_pos = eval(document.getElementById('gen_fields').sliderValue.value)/yfactor2+half_icon_height;
var focus_size = eval(document.getElementById('gen_fields').sliderValue2.value)/18;
var defocus_strength = eval(document.getElementById('gen_fields').sliderValue3.value);
var defocus_gradient = eval(document.getElementById('gen_fields').sliderValue4.value);
var enhance_bokeh = eval(document.getElementById('gen_fields').sliderValue5.value);
var enhance_col = document.getElementById('gen_fields').enhance_col.checked;
var input_filename = document.getElementById('gen_fields').inputFilename.value;
var orig_filename = document.getElementById('gen_fields').origFilename.value;
var thumb_filename = document.getElementById('gen_fields').thumbFilename.value;
var thumb_height = document.getElementById('gen_fields').thumbHeight.value;

var reqstring = 'genimage.php';
reqstring += '?slider_pos='+slider_pos;
reqstring += '&focus_size='+focus_size;
reqstring += '&defocus_strength='+defocus_strength;
reqstring += '&defocus_gradient='+defocus_gradient;
reqstring += '&enhance_bokeh='+enhance_bokeh;
reqstring += '&enhance_col='+enhance_col;
reqstring += '&input_filename='+encodeURIComponent(input_filename);
reqstring += '&orig_filename='+encodeURIComponent(orig_filename);
reqstring += '&thumb_filename='+encodeURIComponent(thumb_filename);
reqstring += '&thumb_height='+thumb_height;
reqstring += '&submit_type='+_submittype; // Global var set by form button actions

//alert("calling "+reqstring);
if (_submittype=="download") {
 // Ajax animation for download
 var html_output="<img src='ajax-four.gif' style='margin:0px 0 0 0;'>";
 html_output+="<br clear='all'>Please wait..";
 document.getElementById('download-progress').innerHTML=html_output;
} else {
 // Ajax animation for preview
 margin_top=thumb_height/2-16;
 margin_left=thumb_width/2-16;
 var html_output="<img src='ajax-five.gif' style='margin:"+margin_top+"px 0 0 "+margin_left+"px'>";
 document.getElementById('preview-progress').innerHTML=html_output;

}

Ajax.Request(reqstring, ProcessGenCallback);
}
