//<![CDATA[
var thumb_width =0;
var thumb_height = 0;
var ratio = 0;
var type = '';
function bindImg(){
	$("img.thumbDisplay").click(function(){
	$("div.selectables").hide("slow");
	$('#upload_status').hide();
	$('#upload_link').hide();
	img = str_replace("_",'',this.src);
	_htm = '<img src="'+img+'"id="thumbnail" class="thumbnail"/>'
			+'<div id="preview" style="width:'+thumb_width/2+'px; height:'+thumb_height/2+'px;">'
			+'<img src="'+img+'" id="thumbnail_preview"/><img class="ecoverType"  src="images/type_'+type+'.gif" style="width:'+thumb_width/2+'px; height:'+thumb_height/2+'px;"/></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   
}
function switchimg(){
	$("#switchimages").click(function(){			  	
		$.ajax({
				type: 'POST',
				url: '_getImages.php',
				data: 'data='+$("#switchimages").val(),
				cache:false,
				success: function(response){
						response = unescape(response);
						$("div.xboxcontent#selectables").html(response);
						bindImg();
						switchimg();
				}
			   });
		return false;
		});	
}

function emailme(){
	var email = $('#email').val();
										$.ajax({type:'POST', url:'_functions.php',data:'func=emailme&email='+email,cache:false,success:function(response){
																																		   var response = response.split("|");
																																			alert(response[1]);
																																		   
																																		   }});
}


function uploadfile(){

		    var myUpload = $('#upload2').upload({
		   name: 'image',
		   action: 'image_upload.php',
		   enctype: 'multipart/form-data',
		   params: {upload:'Upload'},
		   autoSubmit: true,
		   onSubmit: function() {
				$('#loader').show();
		   },

		   onComplete: function(response) {
				response = unescape(response);
				//alert(response);
				var response = response.split("|");
				var responseType = response[0];
				var responseID= response[1];

				if(responseType=="success"){
					var responseThumb = response[2];
					var responseFilename = response[3];
					var responseBigFile = response[4];
					var responseSize = response[5];
	
					insertArt(response[4],response[6],response[7]);	
				}else if(responseType=="error"){
					$('#upload_status').show().html('<span style="color:#ff0000">Error: </span><span style="color:#000; font-weight:normal;">'+response[1]+'</span>');
				}else{
					$('#upload_status').show().html('<span style="color:#ff0000">Unexpected Error - Please try again</span><span style="color:#000; font-weight:normal;">'+response+'</span>');
				}
				$('#loader').fadeOut("slow");

		   }
		});	
}


$(document).ready(function () {
	bindImg();
	switchimg();
	//$(".downloadlink,#comingsoon").click(function(){ alert("Coming soon..."); });
	$("[id*='ecover-']").click(function(){
										var t = this.id.split("-");
										type = t[1];
										if(type=="login"){
										alert("For Members Only");
										return false;
										}
										$.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];
																																		   ratio = response[2];
																																		   $('.ecovers').hide();
																																		   $('#upload_link').show();
																																		   $('.selectables').show();
																																		   
																																		   }});
										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");
		$.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;"><div id="art"> </div>'
																	+'<img class="ecoverType"  src="images/type_'+type+'.gif" /></div>');
					$('#helper').hide().fadeIn("normal").html("Use the control panel on the right to add effects and modifications to your image.");
					getContent('ecover','#editFunctions');

					var p = $('#myselected').offset();
					_p = p.left + 644;
				
					$('#editFunctions').css({'position':'absolute','top':'180px','left':''+_p+'px'});
						uploadfile();
				}else{
					$('#selectedImg').hide().fadeIn("normal").html(_response);
				}
				
			}
		});
	});//end convert
					


}); //END $(document).ready(function () {
//]]>