$(document).ready(function() { 
  $("#loc").sortable({ 
	handle : '.handle'
  }); 
});

function list_remove(item)
{
	//item.parentNode.parentNode.remove();
	
	item.parentNode.parentNode.parentNode.removeChild(item.parentNode.parentNode);
	
	//alert(item.parentNode.parentNode);
}

function list_add(url, name)
{
	var parent = document.getElementById('loc');
	var li_set = $('ul#loc li');
	var size = li_set.size();
	var eli = document.createElement('li');
	eli.setAttribute('id', 'li' + size);
	eli.innerHTML = '<img src="'+ url +'" style="margin-right: 10px;" class="img" /><span style="vertical-align: top; margin-top: 20px;"><input type="text" name="delay[]" value="1" size="3" class="text short" /> second(s) <img src="/images/drag.gif" alt="Click and drag to change the position." class="handle" /><a href="javascript:void(0);" onclick="list_remove(this);"><img src="images/delete.gif" width="16" height="16" alt="delete" style="margin-left: 10px;" /></a><input type="hidden" name="image[]" value="'+ name +'" /></span>';
	parent.appendChild(eli);         
}    

Rows = {
	Toggle: function(pTarget)
	{
		if (pTarget.value == "custom")
		{
			$('#custom').css("display", "");
		}
		else {
			$('#custom').css("display", "none");
		}
		if (pTarget.value == "none")
		{
			$('#index').css("display", "");
			$('#other').css("display", "none");
		}
		else {
			$('#index').css("display", "none");
			$('#other').css("display", "");
		}
		
		//$('average').checked = false;
	}
}

function createAnimation()
{
	if ($('#loc li').size() < 2)
	{
		alert('Please upload 2 or more pictures to create an animation.');
		return false;
	}
	else {
		$('#create').css('background', 'url(/images/btn-process-image.gif)');
		$('#create').css('width', '123');
		$('#creator').css('visibility', 'visible');
		
		$('#create').attr('disabled', 'true');
		setTimeout("$('#create').attr('disabled', 'false');", 5000);
		return true;
	}
}

function StartEvent(id)
{
	$.ajax({
		method: 'get',
		url : '/files/animated_gif/status.php?id='+ id +'&time='+ Math.round(new Date().getTime() / 1000) +'',
		type: "GET",
		dataType : 'text',
		success: function (text) { if (text == 'Redirecting...') { clearTimeout(statusCheck); window.location.href = '/handler.php?gen=animatedimage&p=view&id='+ id +''; } else { $('#stext').html(text); } }
	});
}

function displayImage(text)
{
	try //Internet Explorer
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(text);
	}
	catch(e)
	{
		try //Firefox, Mozilla, Opera, etc.
		{
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(text,"text/xml");
		}
		catch(e)
		{
			alert(e.message);
		}
	}
	
	var newText = xmlDoc.getElementsByTagName("jsInfo")[0].childNodes[0].nodeValue;
	var splitText = newText.split('|');
	list_add(splitText[0], splitText[1]);
}

function sendParameters()
{
	var parameters = '';
	
	for (i=0; i < document.forms[1].elements.length; i++)
	{
		if (document.forms[1].elements[i].name == "image[]")
		{
			parameters += ""+ document.forms[1].elements[i].name +"="+ document.forms[1].elements[i].value +"!--!";
		}
	}
	return parameters;
}

function getObject(movieName)
{
	//getObject('upload').cancelUpload();
	
	if (navigator.appName.indexOf ("Microsoft") !=-1)
	{
		return window[movieName];
	}
	else
	{
		return window.document[movieName];
	}
}

function toggleFlash()
{
	if ($('#showFlash').is(":visible"))
	{
		$('#showFlash').hide();
		$('#hideFlash').show();
	}
	else
	{	
		$('#showFlash').show();
		$('#hideFlash').hide();
	}
}