//<![CDATA[
var thumb_width =0;
var thumb_height = 0;
var type = '';
function save(){
	$('#loader').show("slow");
	$.ajax({
		type: 'POST',
		url: '_functions.php',
		data: 'func=save&imageFile='+_final+'&imgid=',
		cache: false,
		success: function(response){
			response = unescape(response);
			_response = response;
			var response = response.split("|");
			if(response[0]=="success"){
				$('#helper').hide().fadeIn("slow").html(response[1]);
			}else{
				$('#helper').hide().fadeIn("slow").html("There was an unexpected error. Your action cannot be completed. <br><pre>" + _response+"</pre>");	
			}
			$('#loader').hide("slow");
		}
	});
	return false;
}
function bindImg(){
	$("img.thumbDisplay").click(function(){
	 $("div.selectables").hide();
	 $('#upload_link').hide();
	img = str_replace("_",'',this.src);
	_htm = '<img src="'+img+'"id="thumbnail" class="thumbnail"/>'
			+'<div id="preview" style="width:'+thumb_width+'px; height:'+thumb_height+'px;">'
			+'<img src="'+img+'" id="thumbnail_preview"/></div>';
	$('#helper').hide().fadeIn("normal").html("Use your mouse to select the region of the image you would like to use. Click <b>APPLY</b> to select");
	$('#selectedImg').hide().fadeIn("normal").html(_htm);
	$('#selectedImg').fadeIn("normal").find('#thumbnail').imgAreaSelect({  onSelectChange: preview });
	$('#options').hide().fadeIn("normal");
	
	$('#myselected').show();
	
	});//end img.thumbDisplay   
}

$(document).ready(function () {
	bindImg();
	$("[id*='banner-']").click(function(){
										if(this.id=="banner-custom"){
											var height = $("#height").val();
											var width = $("#width").val();
											if((width*height)<=10000){
												alert("Please Enter a width and height larger than 100x100");
												return false
											}else{
												type = width+'x'+height;
											}
										}else{
											var t = this.id.split("-");
											type = t[1];
										}
										$.ajax({type:'POST', url:'_functions.php',data:'func=getsize&type='+type,cache:false,success:function(response){
																																		   var response = response.split("|");
																																		   thumb_width = response[0];
																																		   thumb_height = response[1];
																																		   $('.ecovers').hide();
																																		   $('#upload_link').show();
																																		   $('.selectables').show();
																																		   $('#helper').hide().fadeIn("normal").html("Select an image to use as your main background or upload your own");
																																		   }});
										return false;
										});
	$("#convert").click(function(){
		var x1 = $('#x1').val();
		var y1 = $('#y1').val();
		var x2 = $('#x2').val();
		var y2 = $('#y2').val();
		var w = $('#w').val();
		var h = $('#h').val();
		if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h==""){
		alert("You must make a selection first");
			return false;
		}
		$('#selectedImg').fadeOut("normal").find('#thumbnail').imgAreaSelect({hide:true, disable:true});
		$('#options').fadeOut("normal");
		$('.selectables').hide();
		$('#loader').show("slow");
		$.ajax({
			type: 'POST',
			url: '_functions.php',
			data: 'func=ecover&x1='+x1+'&y1='+y1+'&x2='+x2+'&y2='+y2+'&w='+w+'&h='+h+'&img='+img+'&thumb_width='+thumb_width+'&thumb_height='+thumb_height,
			cache: false,
			success: function(response){
				response = unescape(response);

				_response = response;

				var response = response.split("|");
				var responseType = response[0];
				
				if(responseType=="success"){
					_final = response[1];
					$('#selectedImg').hide().fadeIn("normal").html('<div style="background:url('+response[1]+') no-repeat; width:'+thumb_width+'px; height:'+thumb_height+'px; border:1px solid #404040;"><span id="art"></span></div>');
					$('#helper').hide().fadeIn("normal").html("Use the control panel on the right to add effects and modifications to your image.");
					getContent('edit','#editFunctions');

					var p = $('#myselected').offset();
					_p = p.left + 644;
					$('#editFunctions').css({'left':''+_p+''});
					$('#loader').hide("slow");
				}else{
					$('#selectedImg').hide().fadeIn("normal").html(_response);
				}
			}
		});
	});//end convert


}); //END $(document).ready(function () {
//]]>