var decoration = null;
var decoration_text = null;
var total_uploads = 0;
var uploaded_files_count = 0;
var current_photo_id = 1;
var collage_interval = null;
var upload_meter = null;
var has_flash = null;
var refresh_timer;
var speed_text;
var time_remaining_text;
var transfered_text;
var file_ext;
var pro_preview = false;

function set_decoration(decoration_id)
{
	decoration = decoration_id;
	$('decoration_image').setAttribute('src', '/images/decorations/' + decoration_id + '.jpg');
	$('box_title').update('');
	$('box_header').hide();
	Lightbox.showBoxByID('decoration_lightbox', 640, 480);
	$('decoration_text').activate();
}

function submit_decoration()
{
	window.location = "/upload" + "/" + effect + "/" + border + "?decoration=" + decoration + "&decoration_text=" + encodeURIComponent($F('decoration_text'));
}

function queue_create_edit(batch, photo_id)
{
 	req = getNewHTTPObject();
	req.open("GET", "/queue/create_edit?batch=" + batch + "&photo_id=" + photo_id);
	req.send(null);
}

function queue_process_image(batch, file_ext, effect, border, decoration, decoration_text, notify, preview)
{
	pro_preview = preview;
 	req = getNewHTTPObject();
	req.open("GET", "/queue/process_image?batch=" + batch + "&file_ext=" + file_ext + "&effect=" + effect + "&border=" + border + "&decoration=" + decoration + "&decoration_text=" + encodeURIComponent(decoration_text) + "&preview=" + preview);
	req.onreadystatechange = function(inner_notify) { return function() { check_queue_process_image_response(notify); }(notify); }
	req.send(null);
}

function queue_process_order(batch, notify)
{
 	req = getNewHTTPObject();
	req.open("GET", "/queue/process_order?batch=" + batch);
	req.send(null);
}

function refresh_upload_stats()
{
	$('speed').update(speed_text);
	$('time').update(time_remaining_text);
	$('transfered').update(transfered_text);
}

function pro_process(pro_batch, file_ext, effect) {
	batch = pro_batch + '-done';
	var text = "Processing image effect in high quality.<br/>This may take a minute or longer ... <img src='/images/working.gif' border='0'></img><br/><br/><span style='font-weight:normal'>Do not refresh this page.<br/><br/>If we couldn't process your image, you will not be charged a credit. Upon successfull processing you will always receive a confirmation e-mail with the download link for your image.</span>";
	openStatusOverlay(false);
	set_status(text);
	Lightbox.setLightboxDimensions(500, 250);
	queue_process_image(batch, file_ext, effect, 'pro', '', '', text, false);
}

function getNewHTTPObject()
{
        var xmlhttp;

        /** Special IE only code ... */
        /*@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 @*/

        /** Every other browser on the planet */
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
        {
            try
            {
                xmlhttp = new XMLHttpRequest();
            }
            catch (e)
            {
                xmlhttp = false;
            }
        }

        return xmlhttp;
}

function hasFlash()
{
	if (has_flash == null) {
		if(FlashDetect.installed && FlashDetect.versionAtLeast(9, 0, 28)) {
			has_flash = true;
		} else {
			has_flash = false;
		}
		return has_flash;
	} else {
		return has_flash;
	}
}

function openStatusOverlay(show_bottom)
{
  $('box_title').update('<img src="/images/working.gif" height="13" border="0"></img> Please wait...');
	if (show_bottom) {
		Element.show('upload_lightbox_bottom');
	} else {
		Element.hide('upload_lightbox_bottom');
	}
	Lightbox.showBoxByID('upload_lightbox', 500, 200);
}

function closeWindowOverlay()
{
	Lightbox.hideBox();
}

function displayUploadControl(container, flash)
{
	$(container).update('<div class="flash-upload"><form action="" method="POST" enctype="multipart/form-data"><span id="spanButtonPlaceHolder"></span></form></div>');
}

function initUpload() {

	Lightbox.init();

	if (hasFlash()) {
		displayUploadControl('upload_control', true);
		swfu = new SWFUpload(swfu_settings);
	} else {
	  alert('To use this site you need Flash 9.0.28 at minimum.\nWe will redirect you now to the Adobe Flash website.');
	  window.location = 'http://get.adobe.com/flashplayer';
	}
}

function refresh_page() {
	if (pro) {
		if (pro_preview) {
			window.location = "/pro/preview/" + batch;
		} else {
			window.location = "/pro/show/" + batch;
		}
	}
	else {
		window.location = "/show/" + batch;
	}
}

function retry_upload() {
	window.location = "/upload/" + effect + "/" + border;
}

function set_status(text) {
	$('dialog_status').setStyle("width:435px;padding:7px;margin-left:10px;border:1px #e0e0e0 solid;background:#DFE9F2;");
	$('dialog_status').update(text);
}

function openPreviewProgressBar(notify, job_id) {
 set_status(notify);
 collage_interval = window.setInterval(
   function () {
     fetch_process_status(job_id);
   },
   4500
 );
}

function fetch_process_status(job_id) {
 req = getNewHTTPObject();
 req.open("GET", "/queue/job_status?job_id=" + job_id);
 req.onreadystatechange = function () {
  if (req.readyState == 4) {
   if (req.status == 200) {
    var done = eval(req.responseText);
  	if (done) {
  		window.clearInterval(collage_interval);
  		refresh_page();
  	}
   }
  }
 }
 req.send(null);
}

function check_queue_process_image_response(notify) {
	if (req.readyState == 4) {
		if (req.status == 200) {
	      	openPreviewProgressBar(notify, batch);
		}
	}
}

function highlight(field) {
	field.focus();
	field.select();
}