	var availableGems = 12;

function startGemGame() {
  $.get("/gems.php", { a: "init" } );
}

function setGems() {
  // Create Trunk
  $("<div />").attr("id", "gem-trunk").appendTo($("body"));
  $("<div class=\"count\"><div id=\"gemCount\">" + availableGems + "</div> Items Left</div>").appendTo($("#gem-trunk"));

  // Window Resize Procedures
  $(window).bind("resize scroll", moveTrunk);
  moveTrunk();

  // Place Gems
  if ( pageGems.gems && pageGems.gems.length > 0 ) {
    $.each(pageGems.gems, function() {
      if ( this.found == false ) {
        var gem = $("<div />").attr("id", "gem-" + this.id).addClass("gem").addClass("gem" + this.img).css({
          left: this.x + "%",
          top: this.y,
          opacity: 0.75
        }).appendTo($("body"));
      }
    });
  
    $(".gem").draggable();
    $("#gem-trunk").droppable({
      accept: ".gem",
      hoverClass: "hover",
      drop: function(e, ui) {
        $.get("/gems.php", { a: "delete", id: $(ui.draggable).attr("id") } );
        $(ui.draggable).remove();
        updateFoundGems(1);
        foundGems++;
      }
    });
  }
}

function moveTrunk() {
  $("#gem-trunk").css({
    top: $(window).height() + $(window).scrollTop()
  });
}

function updateFoundGems(dec) {
  $("#gemCount").html(availableGems - foundGems - dec);
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 6;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

$(document).ready(function(){
	$("#tellafriendSubmit").click(function(){
		$("div.error").remove();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var email = $("#email").val();
		if(email == '')
		{
			$("#email").before('<span class="error">Enter a valid email address .</span>');
			hasError = true;
		}
		
		var friendsEmail = $("#friendsEmail").val();
		if(friendsEmail == '')
		{
			$("#friendsEmail").before('<span class="error">Enter a valid email address to send to.</span>');
			hasError = true;
		}
		
		var yourName = $("#yourName").val();
		if(yourName == '')
		{
			$("#yourName").before('<span class="error">Enter a real name.</span>');
			hasError = true;
		}
		
		var emailBody = $("#emailBodyTextArea").val();
		if(emailBody == '')
		{
			$("#emailBodyTextArea").before('<span class="error">Enter a message for your friend.</span>');
			hasError = true;
		}
		
		var captchaCode = $("#captcha_code").val();
		if(captchaCode == '')
		{
			$("#captcha_code").before('<span class="error">Enter a characters in the blue box.</span>');
			hasError = true;
		}
		
		var friendsEmail2 = $("#friendsEmail2").val();
		if(friendsEmail2 != '')
		{
			sendEmailPost(email, friendsEmail2, yourName, emailBody, captchaCode, true);			
		}
		
		var friendsEmail3 = $("#friendsEmail3").val();
		if(friendsEmail3 != '')
		{	
			sendEmailPost(email, friendsEmail3, yourName, emailBody, captchaCode, true);
		}
		
		var friendsEmail4 = $("#friendsEmail4").val();
		if(friendsEmail4 != '')
		{		
			sendEmailPost(email, friendsEmail4, yourName, emailBody, captchaCode, true);
		}
		
	  if( hasError == false)
	  {
		  sendEmailPost(email, friendsEmail, yourName, emailBody, captchaCode, true);
	  }
		
		return false;
	});
	
  $("ul#mainmenu li.mm_tellafriend a").click( function() {
    $("div#tellafriend").slideDown('slow');
  });
  
  $("div#tellafriend #close").click( function() {
		$("div#tellAFriendSuccess").remove();
		$("div.error").remove();
		$("#validate").show();
		$("#randomKey").show();		
		$("div#emailBody").show('normal');
 		$("div#tellafriend").slideUp('normal');
		$("#yourName").val("");
		$("#email").val("");
		$("#friendsEmail").val("");
		$("#captcha_code").val("");
	});
	
});

function sendEmailPost(email,friendsEmail,yourName,emailBody,captchaCode,displayMessage)
{
	$.post("http://benandkranky.com/email.php",
		{ email: email, friendsEmail: friendsEmail, yourName: yourName, emailBody: emailBody, captcha_code: captchaCode},
		function(data) {emailCallBack(data,displayMessage);}
	);
}

function emailCallBack(data,displayMessage)
{
	if( data != "incalid_captcha" )
	{
		if( displayMessage )
		{
			$("#emailBody").slideUp("normal", function() {
				$("#emailBody").before('<div id="tellAFriendSuccess"><h1>Success</h1><p>Your email was sent.</p></div>');
				$("#validate").hide();
				$("#randomKey").hide();				
			});
		}
	} 
	else
	{
		$("#captcha_code").before('<span class="error">Enter a characters in the blue box.</span>');
		return false;
	}
}

function showTellAFriend()
{
	 $("#tellafriend").slideDown("normal");
}


