//SHERV.NET
var theCropper = null;

preload_img = new Image(25,25);
preload_img.src="progress.gif";
preload_img.src="wait.gif";


/////// UTIL /////
function getCheckedValue(radioObj){
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

var showhide_state = 'hidden';

function showhide(div1,div2) {
	if (showhide_state == 'visible') {
		showhide_state = 'hidden';
	} else {
		showhide_state = 'visible';
	}
	if (document.all) {
		eval( "document.all." + div1 + ".style.visibility = showhide_state");
		eval( "document.all." + div2 + ".style.visibility = showhide_state");
	}
	if (document.getElementById && !document.all) {
		div1 = document.getElementById(div1);
		div2 = document.getElementById(div2);
		div1.style.visibility = showhide_state;
		div2.style.visibility = showhide_state;
	}
}

function toggleBlock(div1,showhide_state)
{
	if (showhide_state == false){
		showhide_state = 'hidden';
	} else {
		showhide_state = 'visible';
	}

	if(document.all){
		eval( "document.all." + div1 + ".style.visibility = showhide_state");
	}
	if (document.getElementById && !document.all){
		div1 = document.getElementById(div1);
		div1.style.visibility = showhide_state;
	}
}
function toggleDiv(divid){var div = document.getElementById(divid);
div.style.display = (div.style.display == 'block' ? 'none' : 'block');}

function setDivBlock(divid){document.getElementById(divid).style.display='block';}
function setDivNone(divid){document.getElementById(divid).style.display='none';}

//////////////


function onEndCrop(coords, dimensions){
	$( 'x1' ).value = coords.x1;
	$( 'y1' ).value = coords.y1;
	$( 'x2' ).value = coords.x2;
	$( 'y2' ).value = coords.y2;
	$( 'width' ).value = dimensions.width;
	$( 'height' ).value = dimensions.height;
}

function startCropper()
{
	if(theCropper != null)
	{
		removeCropper();
	}

	var cropW = 19;
	var cropH = 19;

	var selType = getCheckedValue(document.forms['cropperForm'].elements['output_type']);

	if(selType == "dp")
	{
		cropW = 96;
		cropH = 96;
	}
	else if(selType == "emo1")
	{
		cropW = 50;
		cropH = 50;
	}
	else if(selType == "aimb")
	{
		cropW = 48;
		cropH = 48;
	}

	theCropper = new Cropper.ImgWithPreview('testImage',{minWidth: cropW, minHeight: cropH, ratioDim:{ x: cropW, y: cropH }, onEndCrop: onEndCrop, previewWrap: 'previewArea'})
}

function selAgain()
{
	setDivBlock("upload_form");
	setDivNone("upload_another");
	return false;
}

function removeCropper()
{
	theCropper.remove();
	//theCropper.reset();
	theCropper = null;
}

function initCropper()
{
	setDivBlock('cropperDiv');
	setTimeout('startCropper()',500);
}

function uninitCropper()
{
	setDivNone('cropperDiv');
	removeCropper();
}


function closeMake(){
document.getElementById('make_icon').src='wait.gif';
setDivNone("make_done");
toggleBlock('make_main', false);
toggleBlock('iconmaker', true);
}
