var poza,
	text='Text',
	fontColorRGB='0.0.0',
	fontColor='000000',
	fontSize=25,
	posx='nimic',
	posy='nimic',
	x=0,
	y=0,
	finalx=0,
	finaly=0,
	click=false,
	img,
	clicked=false;
	
$(document).ready(function(){
	$("#slider-code img").click(function(){
		$(".selected").removeClass('selected');
		$(this).addClass('selected');
		preview();
	});
	
	$(".font").click(function(){
		$(".selectedd").removeClass('selectedd');
		$(this).addClass('selectedd');
		preview();
	});
	
	$(".genereaza").click(function(){
		poza=$(".selected").attr("alt");
		text=$("#text").val();
		font=$(".font.selectedd").attr("rel");
		fontSize=$("#marime").val();
		
		img = new Image();
		img.src = link+'avatare/avatare/'+poza;
		
		t=text.split('\n');
		text=t.join("!new_line!");
		
		x='act=genereaza&&link=poza='+poza+'&text='+text+'&fontColor='+fontColorRGB+'&fontSize='+fontSize+'&font='+font+'&x='+finalx+'&y='+finaly+'';
		y=x.split('&');
		x=y.join('!~!');
		y=x.split('!~!!~!');
		dataS=y.join('&');
		x=dataS.split('\n');
		dataS=x.join('!new_line!');
		
		$("#previewPHP").html('<img style="float:left;" src="'+link+'avatare/avatar.php?poza='+poza+'&text='+text+'&fontColor='+fontColorRGB+'&fontSize='+fontSize+'&font='+font+'&x='+finalx+'&y='+finaly+'" alt="test" />');
		$.ajax({
			url:link+"avatare/act.php",
			type:"post",
			data:dataS,
			success:function(msg){
				$("#previewPHP").html($("#previewPHP").html()+'<a style="float:left;margin-left:10px;margin-top:25px;" href="'+link+'avatare-personalizate'+msg+'" title="Salveaza avatarul creat">Click aici pentru a salva avatarul creat fara watermark</a><br /><center><img src="'+link+'avatare/watermark.gif" width="100" style="background-color:#cccccc;:" />');
			}
		});
	});
	
	$("#textul").mousedown(function(e){
		click=true;
		var offset=$("#textul").offset();
		left=offset.left;
		tp=offset.top;
		mouseLeft=e.pageX;
		mouseTop=e.pageY;
		posx=mouseLeft-left;
		posy=mouseTop-tp;
		x=Math.round(posx);
		y=Math.round(posy);
		return false;
	});
	
	$("#textul").mouseup(function(){
		click=false;
	});
	
	$("#preview").mousemove(function(e){
		if(click){
			var offset=$("#preview").offset();
				previewLeft=offset.left;
				previewTop=offset.top;
				mouseLeft=e.pageX;
				mouseTop=e.pageY;
				posx=mouseLeft-previewLeft;
				posy=mouseTop-previewTop;
				posx=Math.round(posx);
				posy=Math.round(posy);
				
			if(posx>img.width)	
				posx=img.width;
			if(posx<0)
				posx=0;
			if(posy>img.height)
				posy=img.height;
			if(posy<0)
				posy=0;
			posx=posx-x;
			posy=posy-y;
			finalx=posx;
			finaly=posy;
			$("#textul").css({'left':posx+'px','top':posy+'px'});
		}
	});
	
	$('#colorpickerHolder2').ColorPicker({
			flat: true,
			color: '#00ff00',
			onSubmit: function(hsb, hex, rgb) {
				$('#colorSelector2 div').css('backgroundColor', '#' + hex);
			},
			onChange: function(hsb, hex, rgb) {
				fontColor=hex;
				fontColorRGB=rgb.r+"."+rgb.g+"."+rgb.b;
				preview();
			}
	});
		
	$('#slider-code').tinycarousel({ axis: 'y', display:'2'});
});

$(window).load(function() {
	posx='nimic';
	posy='nimic';
	preview();
});

function preview(){
	poza=$(".selected").attr("alt");
	text=$("#text").val();
	fontu=$(".font.selectedd").attr("rel");
	fontSize=$("#marime").val();
	
	fontu=fontu.replace(".ttf","");
	fontu=fontu.replace(".TTF","");
	
	img = new Image();
	img.src = link+'avatare/avatare/'+poza;
	
	
	tw=parseInt($("#textul").width());
	th=parseInt($("#textul").height());
	
	if(posx=='nimic'){
		posx=img.width/2-tw/2;
		finalx=posx;
	}
	if(posy=='nimic'){
		posy=img.height/2-th/2;
		finaly=posy;
	}
	
	xx=text.split(" ");
	yy=xx.join("&nbsp;");
	xx=yy.split("\n");
	yy=xx.join("<br />");
	
	$("#preview").css({'background-image':'url("'+link+'avatare/avatare/'+poza+'")', 'width':img.width+'px','height':img.height+'px'});
	$("#textul").html(yy);
	$("#textul").css({'font-family':fontu,'color':'#'+fontColor,'font-size':fontSize+'px','left':posx+'px','top':posy+'px'});
	
	return false;
}

function voteaza(id){
	$("#voteaza input").val('Asteapta....');
	if(!clicked){
		clicked=true;
		$.ajax({
			url:link+"avatare/act.php",
			type:"post",
			data:'act=voteaza&id='+id,
			success:function(msg){
				x=msg.split('-');
				if(x[0]=='ok'){
					val=parseInt($(".ramase").html());
					val--;
					$(".ramase").html(val);
					val=parseInt($(".v").html());
					val++;
					$(".v").html(val);
					clicked=false;
					$("#voteaza").html(x[1]);
				}else
					$("#voteaza").html(msg);
			}
		});
	}
}
