$(document).ready(function() {
	$("span[id^='img']:first").click();
	var p = $('#makeAvatarBlock');
	var rightcoord = p.offset();
});

function choosePhotoClick(id,thumb_path) {
	photo_id = id;
	$('#choosePhotoCropCont').append('<img id="cropPhoto" src="http://www.avazun.ru/'+thumb_path+'" border="0">');
	modalClose();
	modal('choosePhotoCrop');
}

function choosePhotoCrop_start() {
	$('#cropPhoto').imgAreaSelect({
		minWidth: 100,
		minHeight: 100,
		x1: 0, y1: 0, x2: 200, y2: 200,
		persistent: true,
		handles: true,
		onSelectEnd: function (img, selection) {
			cropW = selection.width;
			cropH = selection.height;
			cropX = selection.x1;
			cropY = selection.y1;
		}
	});
}

function choosePhotoMakeCrop() {
    modalClose();
	modal('choosePhoto');
	$('#choosePhotoUpload').hide();
	$('#choosePhotoProgress').show();
	$.get("http://www.avazun.ru/index.php", {c:"main",a:"cropphoto",id:photo_id,w:cropW,h:cropH,x:cropX,y:cropY},
  		function(data){
	 		document.getElementById("original_photo_path").value = data;
	        $('img#photo_here').attr("src",'http://www.avazun.ru/'+data);
	       	$('img#photo_here').attr("onClick","");
	       	$('img#photo_here').css("cursor","");
			modalClose();
		}
  	);
}

function avatarResult_start() {}

function avatarResult_end() {
	$('img#avatar_here').attr("src","");
}

function effects() {
	effect_type = $('#photo_effect').val();
	if(effect_type != 'no') {
		$.get("http://www.avazun.ru/index.php", {c: "main", a: "effects", type: effect_type},
  			function(data){
 				$('img#photo_here').attr("src",'http://www.avazun.ru/'+data);
  			}
  		);
  	} else {
 		$('img#photo_here').attr("src",'http://www.avazun.ru/'+$('#original_photo_path').val());
  	}
}

function effectsImg() {
	effect_type = $('#img_effect').val();
	path = $('#clipart_source_path').val();
	if(effect_type != 'no') {
		$.get("http://www.avazun.ru/index.php", {c: "main", a: "effects", type: effect_type, path:path},
  			function(data){
 				$('img#image_here').attr("src",'http://www.avazun.ru/'+data);
 				 $('#clipart_path').val(data);
  			}
  		);
  	} else {
  		$('img#image_here').attr("src",'http://www.avazun.ru/'+path);
 		$('#clipart_path').val(path);
  	}
}

function pasteImage(id,path) {
	$('img#image_here').attr("src",'http://88.198.21.149/'+path);
	$('#clipart_id').val(id);
	$('#clipart_path').val(path);
	$('#clipart_source_path').val(path);
	offset = $(document).scrollTop();
	if(offset<180) {
    	offset = 0;
	} else {
		offset = offset - 180;
	}
	$('div#makeAvatarBlock').css("margin-top",offset+'px');
}

function makeAvatar() {
   	photobottom = $('#photobottom').val();
	clipart_path = $('#clipart_path').val();
	clipart_id = $('#clipart_id').val();
	type = 1;
	photo_path = $("img#photo_here").attr("src");
	round = $('#round').val();
	if ($('#shadowcheck').is(':checked')) {
        shadow = 1;
	} else {
        shadow = 2;
	}
	$('#globalLoading').show();
	$.get("http://www.avazun.ru/index.php", {c: "main", a: "create", clipart_id: clipart_id, clipart_path: clipart_path, photo_path: photo_path, round: round, photobottom: photobottom, photobottom: photobottom, shadow: shadow, type:type},
  		function(data){
 			param = data.split("|");
 			$('img#avatar_here').attr("src",'http://www.avazun.ru/'+param[1]);
 			// $('#directlink').val('http://www.avazun.ru/share/a'+param[0]);
			$('#globalLoading').hide();
			modal('avatarResult');
  		}
  	);
}

function photobottomf() {
	if ($('#photobottomcheck').is(':checked')) {
        var pdiv = $("div#photo_cont");
        pdiv.insertAfter(pdiv.next());
        $('#photobottom').val('1');
	} else {
        var pdiv = $("div#image_cont");
        pdiv.insertAfter(pdiv.next());
        $('#photobottom').val('0');
	}
}

function roundcorners() {
	if ($('#roundcheck').is(':checked')) {
		document.getElementById("round").value = 1;
		$('#corn_top').show();
		$('#corn_bott').show();
	} else {
		document.getElementById("round").value = 0;
		$('#corn_top').hide();
		$('#corn_bott').hide();
	}
}
