var PRE_URL='';
$(document).ready(function(){	
	$("#imgcode").click(function() {
		var img = $("#imgcode");
		var url = PRE_URL+"/index/imgcode";
		img.attr("src", chgUrl(url));
		function chgUrl(url) {
			var timestamp = (new Date()).valueOf();
			urlurl = url.substring(0, 17);
			if ((url.indexOf("?") >= 0)) {
				urlurl = url + "&t=" + timestamp;
			} else {
				urlurl = url + "?t=" + timestamp;
			}
		return urlurl;
		}
	});
});

function groundImg(id){
	var imgurl=$("#"+id).attr("src");
	$("#backimg").attr("value",imgurl);
	groundimg=imgurl.replace(PRE_URL,'');
	$.get(PRE_URL+"/index/create", 
		{waterimg:'/images/captionbar.jpg',img:groundimg}, 
		function (data){
			$("#groundimg").attr("src",data);
			$("#backimg").attr("value",data);
	});
}

function imgPreview(){
	var groundimg=$("#backimg").val();
	groundimg=groundimg.replace(PRE_URL,'');
	if(groundimg=='/images/DownloadFile.jpeg'){
		return false;
	}
	var topwords=$("#topwords").val();
	if(topwords.length>100){
		$("#topwordserror").html("TOP field is limited to 100 characters.");
		return false;
	}else{
		$("#topwordserror").html("");
	}
	var midwords=$("#midwords").val();
	if(midwords.length>100){
		$("#midwordserror").html("MIDDLE field is limited to 100 characters.");
		return false;
	}else{
		$("#midwordserror").html("");
	}
	var bottomwords=$("#bottomwords").val();
	if(bottomwords.length>100){
		$("#bottomwordserror").html("BOTTOM field is limited to 100 characters.");
		return false;
	}else{
		$("#bottomwordserror").html("");
	}
	var topfont=$("#edit-size-top").val();
	var midfont=$("#edit-size-mid").val();
	var btmfont=$("#edit-size-btm").val();
	$.get(PRE_URL+"/index/create", 
		{top:topwords,middle:midwords,bottom:bottomwords,img:groundimg,
		sizetop:topfont,sizemid:midfont,sizebtm:btmfont}, 
		function (data){
			$("#groundimg").attr("src",data);
	});
}
function imgUp(){
	if($("#imgup").val()==""){
		$("#imguperror").html("Please browse an image");		
	}else{
		$("#imguperror").html("");
		$("#imgupform").submit();
	}
}

function imgUrl(){
	if($("#imgurl").val()==""){
		$("#imgurlerror").html("Please enter the image url");
	}else{
		$("#imgurlerror").html("");
		$("#imgurlform").submit();
	}
}

function memeImg(){	
	if($("#title").val().length>100){
		$("#title").addClass("noticeRed");
		$("#titleerror").html("Titled field is limited to 100 characters.");
	}else{
		$("#title").removeClass("noticeRed");
		$("#titleerror").html("");		
	}
	if($("#name").val().length>30){
		$("#name").addClass("noticeRed");		
		$("#nameerror").html("Name field is limited to 30 characters.");
	}else{
		$("#name").removeClass("noticeRed");
		$("#nameerror").html("");
	}
	var groundimg=$("#groundimg").attr("src");
	groundimg=groundimg.replace(PRE_URL,'');
	if(groundimg=='/images/DownloadFile.jpeg' || groundimg==''){
		return false;
	}
	if($("#imgcodevalue").val()==""){
		$("#imgcodevalue").addClass("noticeRed");		
	}else{
		$.get(PRE_URL+"/index/create", 
			{imgcode:$("#imgcodevalue").val()}, 
			function (data){
				if(data==1){
					$("#imgcodevalue").removeClass("noticeRed");
					if($(".noticeRed").length<1){
						//提交表单
						$("#memeimg").attr("value",groundimg);
						$("#memeimgform").submit();
					}else{
						$("#imgcode").click();
					}
				}else{
					$("#imgcodevalue").addClass("noticeRed");
					$("#imgcode").click();
				}
		});
	}
}

function voteUp(id){
	$.get(PRE_URL+"/index/vote", 
	{id:id,type:'up'}, 
	function (data){
		$("#up"+id).html(data);
	});
}

function voteDown(id){
	$.get(PRE_URL+"/index/vote", 
	{id:id,type:'down'}, 
	function (data){
		$("#down"+id).html(data);
	});
}

function commentPost(){	
	if($("#comment").val()=="" || $("#comment").val().length>200){
		$("#comment").addClass("noticeRed");
		$("#commenterror").html("Comment field is limited to 200 characters.");
	}else{
		$("#comment").removeClass("noticeRed");
		$("#commenterror").html("");
	}
	if($("#name").val().length>30){
		$("#name").addClass("noticeRed");
		$("#nameerror").html("Name field is limited to 30 characters.");
	}else{
		$("#name").removeClass("noticeRed");
		$("#nameerror").html("");
	}
	if($("#imgcodevalue").val()==""){
		$("#imgcodevalue").addClass("noticeRed");
	}else{
		$.get(PRE_URL+"/index/create", 
			{imgcode:$("#imgcodevalue").val()}, 
			function (data){
				if(data==1){
					$("#imgcodevalue").removeClass("noticeRed");
					if($(".noticeRed").length<1){
						//提交表单
						$("#showform").submit();
					}else{
						$("#imgcode").click();
					}
				}else{
					$("#imgcodevalue").addClass("noticeRed");
					$("#imgcode").click();
				}
		});
	}
	
}

