/* variaveis iniciais do sistema */
	var m_wid = 530;		// largura máxima para a imagem no cropper
	var m_hei = 270;		// altura máxima para a imagem no cropper
	var prp_w = 1;			// largura proporcional do cropper: 3x4 = 3
	var prp_h = 1;			// altura proprocional do cropper: 3x4 = 4
	var cropping = false;
/* variaveis iniciais do sistema - fim */
if (typeof(funcionalidade_codigo) == 'undefined')
	funcionalidade_codigo = null;
function cropper (arquivo , acao) {
	/*
		arquivo = arquivo que processará o cropper
	*/
	if (typeof arquivo != undefined && arquivo !== null) {
		this.arquivo = trim(arquivo);
	}
	if (typeof acao != undefined && acao !== null) {
		this.acao = trim(acao);
	}
}

/* variaveis */
cropper.prototype.acao = "";		// funcao que será executada dentro do arquivo
cropper.prototype.arquivo = "";	// arquivo que processará o cropper
cropper.prototype.img = "";		// nome do arquivo da imagem
cropper.prototype.wid = 0;			// largura proporcional da imagem
cropper.prototype.hei = 0;			// altura proporcional da imagem
cropper.prototype.prp = "1x1";	// proporção do cropper ex.: 3x4
cropper.prototype.prpw = 0;		// largura proporcional do cropper: 3x4 = 3
cropper.prototype.prph = 0;		// altura proprocional do cropper: 3x4 = 4
cropper.prototype.cwid = 0;		// largura inicial do cropper
cropper.prototype.chei = 0;		// altura inicial do cropper
cropper.prototype.owid = 0;		// largura original da imagem
cropper.prototype.ohei = 0;		// altura original da imagem
cropper.prototype.mwid = m_wid;	// largura máxima da imagem no cropper
cropper.prototype.mhei = m_hei;	// altura máxima da imagem no cropper
cropper.prototype.crpt = 0;		// top inicial do cropper
cropper.prototype.crpl = 0;		// left inicial to cropper
/* variaveis - fim */

/* métodos */
cropper.prototype.dimensao_imagem = function () {
	op = this.owid / this.ohei;
	mp = this.mwid / this.mhei;
	if (this.ohei <= this.mhei && this.owid <= this.mwid) {
		this.wid = this.owid;
		this.hei = this.ohei;
	}
	else {
		if (op >= mp || this.wid > this.mwid) {
			// largura
			this.wid = this.mwid;
			this.hei = (this.mwid / this.owid) * this.ohei;
		}
		else {
			// altura
			this.hei = this.mhei;
			this.wid = (this.mhei / this.ohei) * this.owid;
		}
	}
}

cropper.prototype.dimensao_cropper = function () {
	if (this.wid == 0 && this.hei == 0) {
		this.dimensao_imagem();
	}
	tmp = this.prp.split("x");
	prp_w = this.prpw = parseInt(tmp[0]);
	prp_h = this.prph = parseInt(tmp[1]);
	if (this.prpw == this.prph) {
		this.prpw = this.prpw = prp_w = prp_h = 1;
	}
	if (this.prpw == 1 && this.prph == 1) {
		this.cwid = this.chei = (this.wid > this.hei ? ((this.hei / 3) * 2) : ((this.wid / 3) * 2));
	}
	else {
		this.cwid = (this.wid > this.hei ? ((this.hei / 3) * 2) : ((this.wid / 3) * 2));
		this.chei = ((this.cwid / this.prpw) * this.prph);
	}
}

cropper.prototype.posicao_cropper = function () {
	if (this.cwid == 0 || this.hwid == 0) {
		this.dimensao_cropper();
	}
	this.prpl = (this.wid / 2) - (this.cwid / 2);
	this.prpt = (this.hei / 2) - (this.chei / 2);
}

cropper.prototype.tela = function (arq_origem , arq_destino , arq_thumb) {
	/*
		retorna a tela com a imagem para o crop
		largura e altura precisam estar definidas
	*/
	var texto = "";
	if (this.owid == 0) {
		alert("largura não definida");
		return false;
	}
	else if (this.ohei == 0) {
		alert("altura não definida");
		return false;
	}
	else if (this.img == "") {
		alert("imagem não definida");
		return false;
	}
	else {
		this.dimensao_imagem();
		this.dimensao_cropper();
		this.posicao_cropper();
//		texto += '<div class="cropper" style="width: ' + this.wid + 'px; height: ' + this.hei + 'px;">';
		texto += '	<img id="crop-imagem" class="cropper_imagem" src="' + this.img + '" border="0" width="' + this.wid + '" height="' + this.hei + '" style="top: 0px; left: 0px; width: ' + this.wid + 'px; height: ' + this.hei + 'px;" />';
//		texto += '	<div id="crop-area" class="cropper_area" style="width: ' + this.cwid + 'px; height: ' + this.chei + 'px; top: ' + this.prpt + 'px; left: ' + this.prpl + 'px;" onmousedown="dragger_start(this , ' + this.wid + ' , ' + this.hei + '); resize_flag = false;">';
//		texto += '		<div id="cropper-fundo" class="cropper_fundo"></div>';
//		texto += '		<div id="cropper-ponteiro-fake" class="cropper_ponteiro_fake"></div>';
//		texto += '	</div>';
//		texto += '	<div id="crop-corner" style="width: 10px; height: 10px; left: ' + (this.prpl + this.cwid - 5) + 'px; top: ' + (this.prpt + this.chei - 5) + 'px;" class="cropper_ponteiro" onmousedown="dragger_start(this , ' + (this.wid + 5) + ' , ' + (this.hei + 5) + '); resize_flag = true;" onmouseup="resize_flag = false; resize_arruma();"></div>';
//		texto += '</div>';
		texto += '<form name="form_cropper" target="server" action="' + this.arquivo + '" method="post">';
		input_tipo = (debug ? "text" : "hidden");

		texto += '	<input type="' + input_tipo + '" name="acao" value="' + this.acao + '" />';					// acao do formulario
		texto += '	<input type="' + input_tipo + '" name="arquivo" value="' + this.arquivo + '" />';			// largura atual da imagem
		texto += '	<input type="' + input_tipo + '" name="arq_origem" value="' + arq_origem + '" />';			// arquivo de origem
		texto += '	<input type="' + input_tipo + '" name="arq_destino" value="' + arq_destino + '" />';		// arquivo de destino
		texto += '	<input type="' + input_tipo + '" name="arq_thumb" value="' + arq_thumb + '" />';				// arquivo de destino
		if (typeof(COD_CADASTRO) != "undefined" && COD_CADASTRO !== null) {
			texto += '	<input type="' + input_tipo + '" name="cod_cadastro" value="' + COD_CADASTRO + '" />';	// codigo de cadastro
		}

		if (typeof(funcionalidade_codigo) !=  "undefined" && funcionalidade_codigo !== null) {
			texto += '	<input type="' + input_tipo + '" name="funcionalidade_codigo" value="' + funcionalidade_codigo + '" />';	// código da funcionalidade
		}
		if (typeof(funcionalidade_tipo) !=  "undefined" && funcionalidade_tipo !== null) {
			texto += '	<input type="' + input_tipo + '" name="funcionalidade_tipo" value="' + funcionalidade_tipo + '" />';		// tipo da funcionalidade
		}
		if (document.getElementById("publicador-texto-codigo") != undefined && document.getElementById("publicador-texto-codigo") !== null) {
			texto += '	<input type="' + input_tipo + '" name="cod_interacao" value="' + document.getElementById("publicador-texto-codigo").value + '" />';						// código da interação
		}

		texto += '	' + (debug ? "<br />wid" : "") + '<input type="' + input_tipo + '" name="wid" value="' + this.wid + '" />';					// largura atual da imagem
		texto += '	' + (debug ? "<br />hei" : "") + '<input type="' + input_tipo + '" name="hei" value="' + this.hei + '" />';					// altura atual da imagem
		texto += '	' + (debug ? "<br />owid" : "") + '<input type="' + input_tipo + '" name="wid_original" value="' + this.owid + '" />';	// largura original da imagem
		texto += '	' + (debug ? "<br />ohei" : "") + '<input type="' + input_tipo + '" name="hei_original" value="' + this.ohei + '" />';	// altura original da imagem
		texto += '	' + (debug ? "<br />cwid" : "") + '<input type="' + input_tipo + '" name="area_wid" id="area_wid" value="' + this.cwid + '" />';		// largura da área de cropper
		texto += '	' + (debug ? "<br />chei" : "") + '<input type="' + input_tipo + '" name="area_hei" id="area_hei" value="' + this.chei + '" />';		// altura da área de cropper
		texto += '	' + (debug ? "<br />prpt" : "") + '<input type="' + input_tipo + '" name="area_t" id="area_t" value="' + this.prpt + '" />';			// propriedade top da área de cropper
		texto += '	' + (debug ? "<br />prpl" : "") + '<input type="' + input_tipo + '" name="area_l" id="area_l" value="' + this.prpl + '" />';			// propriedade left da área de cropper

		texto += '	<br />';
		texto += '	<button class="botao" onclick="cropper_bt_recortar(); return false;" type="button">recortar</button>&nbsp;&nbsp;';
		texto += '	<button class="botao" onclick="cropper_bt_cancelar(); return false;">cancelar</button>';
		texto += '	<span id="crop_info_dimensao"></span>';
		texto += '</form>';
	}
	return texto;
}

cropper.prototype.tela_preview = function () {
}
/* métodos - fim */

var resize_flag = false;
function resize () {
	/*
		redimensiona o cropper
	*/
	if (resize_flag) {
		ci = document.getElementById("crop-imagem");
		co = document.getElementById("crop-area");
		cc = document.getElementById("crop-corner");
		cif = (ci === null || ci == undefined ? false : true);
		cof = (co === null || co == undefined ? false : true);
		ccf = (cc === null || cc == undefined ? false : true);

		if (cif && cof && ccf) {
			co.style.width = (((parseInt(cc.style.left) - parseInt(co.style.left))) + 5) + "px";
			co.style.height = ((parseInt(co.style.width) / prp_w) * prp_h) + "px";
			if ((parseInt(co.style.top) + parseInt(co.style.height)) > (parseInt(ci.style.top) + parseInt(ci.style.height))) {
				co.style.height = ((parseInt(ci.style.top) + parseInt(ci.style.height)) - parseInt(co.style.top)) + "px";
				co.style.width = ((parseInt(co.style.height) / prp_h) * prp_w) + "px";
			}
		}
	}

	if (document.form_cropper != undefined && document.form_cropper !== null) {
		obj_dragged = document.getElementById("crop-area");
		obj = document.form_cropper;
		if (obj_dragged != undefined && obj_dragged !== null) {
			if (obj.area_wid != undefined && obj.area_wid !== null && obj.area_hei != undefined && obj.area_hei !== null && obj_dragged.className == "cropper_area") {
				obj.area_wid.value = parseInt(obj_dragged.style.width);
				obj.area_hei.value = parseInt(obj_dragged.style.height);
			}
			if (obj.area_t != undefined && obj.area_t !== null && obj.area_l != undefined && obj.area_l !== null && obj_dragged.className == "cropper_area") {
				obj.area_t.value = parseInt(obj_dragged.style.top);
				obj.area_l.value = parseInt(obj_dragged.style.left);
			}
		}
	}

	else {
		resize_arruma ();
	}
}

function resize_arruma () {
	/*
		coloca o quadrado que arrasta para croppear no lugar (canto inferior direito)
	*/
	co = document.getElementById("crop-area");
	cc = document.getElementById("crop-corner");

	cof = (co === null || co == undefined ? false : true);
	ccf = (cc === null || cc == undefined ? false : true);

	if (cof && ccf) {
		cc.style.top = (parseInt(co.style.top) + parseInt(co.style.height) - 5) + "px";
		cc.style.left = (parseInt(co.style.left) + parseInt(co.style.width) - 5) + "px";
	}
}

function croppeia (img , wid , hei , prp , arquivo , acao , arq_origem) {
	/*
		inicia o cropper
		img = imagem que será croppada, url completa
		wid = largura original da imagem
		hei = altura original da imagem
		prp = proporção do crop da imagem. ex.: "1x1"
		arquivo = arquivo que processará o crop
		arq_origem = img = imagem original com caminho completo
		arq_destino = imagem que receberá o crop da imagem original
		arq_thumb = imagem que será salva como thumb
	*/
// 	crop = new cropper(arquivo , acao);
// 	crop.owid = wid;
// 	crop.ohei = hei;
// 	crop.img = img;
// 	crop.prp = prp;
	
	
	
//		texto += '	' + (debug ? "<br />wid" : "") + '<input type="' + input_tipo + '" name="wid" value="' + this.wid + '" />';					// largura atual da imagem
//		texto += '	' + (debug ? "<br />hei" : "") + '<input type="' + input_tipo + '" name="hei" value="' + this.hei + '" />';					// altura atual da imagem
//		texto += '	' + (debug ? "<br />owid" : "") + '<input type="' + input_tipo + '" name="wid_original" value="' + this.owid + '" />';	// largura original da imagem
//		texto += '	' + (debug ? "<br />ohei" : "") + '<input type="' + input_tipo + '" name="hei_original" value="' + this.ohei + '" />';	// altura original da imagem
//		texto += '	' + (debug ? "<br />cwid" : "") + '<input type="' + input_tipo + '" name="area_wid" id="area_wid" value="' + this.cwid + '" />';		// largura da área de cropper
//		texto += '	' + (debug ? "<br />chei" : "") + '<input type="' + input_tipo + '" name="area_hei" id="area_hei" value="' + this.chei + '" />';		// altura da área de cropper
//		texto += '	' + (debug ? "<br />prpt" : "") + '<input type="' + input_tipo + '" name="area_t" id="area_t" value="' + this.prpt + '" />';			// propriedade top da área de cropper
//		texto += '	' + (debug ? "<br />prpl" : "") + '<input type="' + input_tipo + '" name="area_l" id="area_l" value="' + this.prpl + '" />';			// propriedade left da área de cropper

	

	var input_tipo = 'hidden';
	var htm = '' +
	'<center><img src="' + img + '" id="cropbox" width="' + wid + '" height="' + hei + '" />' +
	'<form name="form_crop" action="' + arquivo + '" target="server" method = "post">' +
	'<input type="' + input_tipo + '" id="x" name="area_l" />' +
	'<input type="' + input_tipo + '" id="y" name="area_t" />' +
	'<input type="' + input_tipo + '" id="x2" name="x2" />' +
	'<input type="' + input_tipo + '" id="y2" name="y2" />' +
	'<input type="' + input_tipo + '" id="w" name="area_wid" />' +
	'<input type="' + input_tipo + '" id="h" name="area_hei" />' +
	'<input type="' + input_tipo + '" name="wid_original" value="' + wid + '" />' +
	'<input type="' + input_tipo + '" name="hei_original" value="' + hei + '" />' +
	'<input type="' + input_tipo + '" name="wid" value="' + wid + '" />' +
	'<input type="' + input_tipo + '" name="hei" value="' + hei + '" />' +
	'<input type="' + input_tipo + '" name="acao" value="' + acao + '" />' +
	'<input type="' + input_tipo + '" name="arquivo" value="' + arquivo + '" />' +
	'<input type="' + input_tipo + '" name="arq_origem" value="' + arq_origem + '" />' +
	((typeof(COD_CADASTRO) != "undefined" && COD_CADASTRO !== null)? '	<input type="' + input_tipo + '" name="cod_cadastro" value="' + COD_CADASTRO + '" />': '') +
	((typeof(funcionalidade_codigo) !=  "undefined" && funcionalidade_codigo !== null)? '	<input type="' + input_tipo + '" name="funcionalidade_codigo" value="' + funcionalidade_codigo + '" />': '') + 
	((typeof(funcionalidade_tipo) !=  "undefined" && funcionalidade_tipo !== null)? '	<input type="' + input_tipo + '" name="funcionalidade_tipo" value="' + funcionalidade_tipo + '" />': '') +
	((document.getElementById("publicador-texto-codigo") != undefined && document.getElementById("publicador-texto-codigo") !== null)? '	<input type="' + input_tipo + '" name="cod_interacao" value="' + document.getElementById("publicador-texto-codigo").value + '" />': '') +
	'	<button class="botao" type="button" onClick="this.form.submit()">recortar</button>&nbsp;&nbsp;' +
	'	<button class="botao" onclick="bodyAlpha_close(); return false;">cancelar</button>' +
	'</form></center>';
	//alert(htm);
	if (wid == 300 && hei == 300) {
	var htm = '' +
	'<center><img src="' + img + '" id="cropbox" />' +
	'<form name="form_crop" id="form_crop" action="' + arquivo + '" target="server" method = "post">' +
	'<input type="' + input_tipo + '" id="x" name="area_l" value="0" />' +
	'<input type="' + input_tipo + '" id="y" name="area_t" value="0" />' +
	'<input type="' + input_tipo + '" id="x2" name="x2" value="' + wid + '" />' +
	'<input type="' + input_tipo + '" id="y2" name="y2" value="' + hei + '" />' +
	'<input type="' + input_tipo + '" id="w" name="area_wid" value="' + wid + '" />' +
	'<input type="' + input_tipo + '" id="h" name="area_hei" value="' + hei + '" />' +
	'<input type="' + input_tipo + '" name="wid_original" value="' + wid + '" />' +
	'<input type="' + input_tipo + '" name="hei_original" value="' + hei + '" />' +
	'<input type="' + input_tipo + '" name="wid" value="' + wid + '" />' +
	'<input type="' + input_tipo + '" name="hei" value="' + hei + '" />' +
	'<input type="' + input_tipo + '" name="acao" value="' + acao + '" />' +
	'<input type="' + input_tipo + '" name="arquivo" value="' + arquivo + '" />' +
	'<input type="' + input_tipo + '" name="arq_origem" value="' + arq_origem + '" />' +
	((typeof(COD_CADASTRO) != "undefined" && COD_CADASTRO !== null)? '	<input type="' + input_tipo + '" name="cod_cadastro" value="' + COD_CADASTRO + '" />': '') +
	((typeof(funcionalidade_codigo) !=  "undefined" && funcionalidade_codigo !== null)? '	<input type="' + input_tipo + '" name="funcionalidade_codigo" value="' + funcionalidade_codigo + '" />': '') + 
	((typeof(funcionalidade_tipo) !=  "undefined" && funcionalidade_tipo !== null)? '	<input type="' + input_tipo + '" name="funcionalidade_tipo" value="' + funcionalidade_tipo + '" />': '') +
	((document.getElementById("publicador-texto-codigo") != undefined && document.getElementById("publicador-texto-codigo") !== null)? '	<input type="' + input_tipo + '" name="cod_interacao" value="' + document.getElementById("publicador-texto-codigo").value + '" />': '') +
	'<div id="loading">Aguarde...</div></form></center>';
		bodyAlpha_open("janela-alert-90" , htm);
		$('#form_crop').submit();
	}
	else {
		bodyAlpha_open("janela-alert-90" , htm);
	
	
	/*bodyAlpha_open("janela-alert" , crop.tela(arq_origem , arq_destino , arq_thumb));
	if (crop.arquivo == "") {
		alert("você deve especificar o arquivo que processará o cropper")
		cropper_bt_cancelar();
	}
	document.getElementById("crop-imagem").style.border = "1px SOLID #AAA";
	//document.getElementById("crop-area").style.width = (parseInt(document.getElementById("crop-area").style.width) + 1) + "px";
	document.getElementById("body-alpha-janela").style.width = "360px";
	document.getElementById("body-alpha-janela").style.height = "430px";
	cropping = true;
	window.parent.imagemCrop();*/
		$(function() {
			$('#cropbox').Jcrop({
				onChange: showCoords,
				onSelect: showCoords,
				setSelect: [ 0, 0, 300, 300 ],
				allowSelect: false,
				minSize: [ 300, 300 ],
				aspectRatio: 1
			});
	// 		$.Jcrop('#cropbox',{
	// 		});
			function showCoords(c) {
				jQuery('#x').val(c.x);
				jQuery('#y').val(c.y);
				jQuery('#x2').val(c.x2);
				jQuery('#y2').val(c.y2);
				jQuery('#w').val(c.w);
				jQuery('#h').val(c.h);
			};
		});
	}
}

function cropper_bt_recortar () {
	var area_wid = document.getElementById('area_wid');
	var area_hei = document.getElementById('area_hei');
	if ((area_wid.value < 570) && (area_hei.value < 570)) {
		if (confirm('A área selecionada é menor que o tamanho recomendado.\nTem certeza que deseja enviar esta foto?')) {
			document.form_cropper.submit();
		}
	} else {
		document.form_cropper.submit();
	}
}

function cropper_bt_cancelar () {
	cropping = false;
	bodyAlpha_close();
}

window.onunload = cropper_cancela_forcado;