$(function() {
	$("#entry_comment").dodosTextCounter(500, {counterDisplayClass:"entry_comment_count",addLineBreak:false});
	
	
	$('.thumbImage').click(function(event) {
	   var imgsrc = ($('img', this).attr('src'));
 	   var imgalt = ($('img', this).attr('alt'));
		$('#the-img').fadeTo("fast", 0.1 ,function(){ 
			$('#the-img').html('<img src="' + imgsrc + '" height="345" width="611" alt="' + imgalt + '\"/>').fadeTo("fast", 1);
		});
	});
	
	$('.thumb').click(function(event) {
	   var imgsrc = ($('img', this).attr('src'));
 	   var imgalt = ($('img', this).attr('alt'));
		$('#the-img').fadeTo("fast", 0.1 ,function(){ 
			$('#the-img').html('<img src="' + imgsrc + '" height="450" width="600" alt="' + imgalt + '\"/>').fadeTo("fast", 1);
		});
	});
	
	$(".cOffensive").click(function() {
		var getId = this.id.split("_");
		//alert(getId[1]);
		
		// validate state
		if($(this).attr('checked')){
		   	var isOffensive = 'yes';
		}else{
			var isOffensive = 'no';	
		}
		
		dataTest = "recID=" + getId[1] + "&isOffensive=" + isOffensive;
		
		$.ajax({
			type: "POST",
	    	url : "includes/comment_isOffensive.php",
			data: dataTest,
			success : function (data) {
				if (isOffensive == 'yes' ){
					data = jQuery.trim(data);
					var newcount = "("+data+")";
					// hide the box
					$('#comment_'+getId[1]).fadeOut("slow");
					$('.comment-icon').text(newcount);
					$('.comment-img').html('<a href=\"#comments\" id=\"comment-toggle\">Comments '+newcount+'</a>');
				
					
				}
			}
		});
			
	});
	
	$(".eOffensive").click(function() {
		var getId = this.id.split("_");
		//alert(getId[1]);
		
		// validate state
		if($(this).attr('checked')){
		   	var isOffensive = 'yes';
		}else{
			var isOffensive = 'no';	
		}
		
		dataTest = "contestID=" + getId[1] + "&isOffensive=" + isOffensive;
		
		$.ajax({
			type: "POST",
	    	url : "includes/entry_isOffensive.php",
			data: dataTest,
			success : function (data) {
				if (isOffensive == 'yes' ){
					$('#offence').fadeTo("fast", 0 ,function(){ 
						$('#offence').html("This entry has been marked as offensive").fadeIn("def").fadeTo("fast", 1).animate({ opacity: 1.0 },3000).fadeOut(500, function() {
				          	window.location="view_entries.php";
						});
					});	
						
				}
			}
		});
			
	});
	$(".trash").click(function() {
		var getId = this.id.split("_");
		//alert(getId[1]);
		
		dataTest = "recID=" + getId[1];
		
		$.ajax({
			type: "POST",
	    	url : "includes/comment_isDelete.php",
			data: dataTest,
			success : function (data) {
					data = jQuery.trim(data);
					var newcount = "("+data+")";
					// hide the box
					$('#comment_'+getId[1]).fadeOut("slow");
					$('.comment-icon').text(newcount);
					$('.comment-img').html('<a href=\"#comments\" id=\"comment-toggle\">Comments '+newcount+'</a>');
				
			}
		});
			
	});
	
	$(".dtrash").click(function() {
		var getId = this.id.split("_");
		//alert(getId[1]);
		
		dataTest = "contestID=" + getId[1];
		
		$.ajax({
			type: "POST",
	    	url : "includes/entry_isDelete.php",
			data: dataTest,
			success : function (data) {
				$('#offence').fadeTo("fast", 0 ,function(){ 
						$('#offence').html("This entry has been deleted").fadeIn("def").fadeTo("fast", 1).animate({ opacity: 1.0 },3000).fadeOut(500, function() {
				          	window.location="view_entries.php";
						});
					});	
						
			
			}
		});
			
	});
	

	$(".vote-yes").click(function() {
		//alert("clicke yes");
		var getId = this.id.split("_");
		
		dataSet = "contestID=" + getId[1] +"&vote=yes" ;
		//alert(dataSet);
		$.ajax({
			type: "POST",
      			url : "includes/votes.php",
			data: dataSet,
			success : function (data) {
				$('#voted').fadeTo("fast", 0 ,function(){ 
					$('#voted').html("&nbsp;Thanks for voting! Voters are automatically entered into a random draw for a $500 cash prize.").fadeIn("def").fadeTo("fast", 1).animate({ opacity: 1.0 },10000).fadeOut(500);
				});
			
				
			}
			
		});
		
		
		
		return false;
	});
	
	function getCookie(c_name)
	{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    { 
	    c_start=c_start + c_name.length+1; 
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    } 
	  }
	return "";
	}
	
	
	$(".cant-vote").click(function() {
		
		$('#voted').fadeTo("fast", 0 ,function(){
			
			if (getCookie('hasRegistered') == "yes"){
				tb_show('','login.php?height=320&width=388&KeepThis=true&TB_iframe=true&modal=true', 'page');  return false;
			}else{
				tb_show('','enter.php?height=550&width=520&KeepThis=true&TB_iframe=true&modal=true', 'page');  return false;
				
			}
			
			//$('#voted').html("You must be logged in to make your vote count.").fadeIn("def").fadeTo("fast", 1).animate({ opacity: 1.0 },10000).fadeOut(500);
		});
		
		return false;
	});
	
	$(".vote-no").click(function() {
			//alert("clicke yes");
			var getId = this.id.split("_");

			dataSet = "contestID=" + getId[1] +"&vote=no" ;

			$.ajax({
				type: "POST",
	      		url : "includes/votes.php",
				data: dataSet,
				success : function (data) {
					$('#voted').fadeTo("fast", 0 ,function(){ 
						$('#voted').html("Thanks for voting! You’re entered into the random draw for a $2,500 Benjamin Moore style budget.").fadeIn("def").fadeTo("fast", 1).animate({ opacity: 1.0 },10000).fadeOut(500);
					});


				}

			});



			return false;
	});
	
	$(".image-submit").click(function() {
	
			// validate and process form
			// empty the error box error messages
	    	$('.error-boxsmall').html("");
			$('.error-box-fade').css({display:"none"});

			// validate name
			var name = $("input#name").val();
			var nameLength = name.length;
			var nameSpace = name.indexOf(" ");

			if (name == "" || nameLength < 3 || nameSpace < 0) {
				$('.error-box-fade').html("You must enter your first and last names before we post a comment.").fadeIn("def");
				$("input#name").focus();
	      		return false;
	    	}

			// validate email
			var email = $("input#email").val();

			if (email == "" || !isEmail(email)) {
		    	$('.error-box-fade').html("Sorry, that is not a valid email address.").fadeIn("def");
				$("input#email").focus();
		      	return false;
		    }

			function isEmail(string) {
				if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1){
					return true;
				}else{
					return false;
				}
			}
			
			// validate comment
			var comment = $("textarea#entry_comment").val();

			var contestIDComment = $("input#contestIDComment").val();
			
			if (comment == "" ) {
				$('.error-box-fade').html("Please enter a comment.").fadeIn("def");
				$("input#entry_comment").focus();
	      		return false;
	    	}else{

				dataTxt = "txt=" + comment;
				//alert(dataTxt);
				    $.ajax({
						type: "POST",
						url : "includes/check_curse.php",
						data: dataTxt,
						
						success : function (data) {
							
							//alert(data);
							if(data == "bad"){
								//alert("im bad");
								$('.error-box-fade').html("Your Comments have inappropriate words.").fadeIn("def");
								$("input#entry_comment").focus();
								return false;
							}else{
								dataTest = "name=" + name + "&email=" + email + "&comment=" + comment + "&contestIDComment=" + contestIDComment;
								$('.image-submit').css({visibility: "hidden"});


								$.ajax({
									type: "POST",
						      		url : "includes/comment_process.php",
									data: dataTest,
									success : function (data) {
										$('.error-box-fade').html("We have added your comment")
										.fadeIn().animate({ opacity: 1.0 },3000).fadeOut(500, function() {
								          	window.location.reload();
										});

									},
									error : function() {
										$('.error-box-fade').html("there has been an error.").fadeIn("def");

									}
								});
								
							}
						}
					});
				
			}
	
			
	
		return false;
	});
	
	

});

