  $(function() {
    $('.error').hide();
    
    $('#threecoloverlay').corner("round 30px tr");
    
    roundInput('name','rounded_container','rounded_border');
	roundInput('email','rounded_container','rounded_border');
    roundInput('message','rounded_container','rounded_border');
    
    //setup tag cloud
    //get tag feed
	$.getJSON("php/tagcloud.php?callback=?", function(data) {
		//create list for tag links
		$("<ul>").attr("id", "tagList").appendTo("#tagCloud");
		
		var counter = 0;
							
		//create tags for both the cloud and the list
		$.each(data.tags, function(i, val) {
		
				

		
				//create table row
				var tblRow =
					"<tr>"
					+"<td><a href='../searchResults.php?search="+ val.url_tag + "'>" +val.tag+"</a></td>"
					+"<td>"+val.freq+"</td>"
					+"</tr>"
				$(tblRow).appendTo("#tagdata tbody");

								
				//create item
				var li = $("<li>");
									
									
				//create link
				$("<a>").text(val.tag).attr({title:"See all pages tagged with " + val.tag, href:"search/" + val.url_tag + "/"}).appendTo(li);
				
				//adjust the size of the text relative to the frequency Number
				li.children().css("fontSize", fontSize(val.freq, 0.5, 2.0));
				
				//alternate the font color
				//if(counter % 2 == 0){
				//	li.children().css("color", "orange");
				//}
												
				//add to list
				li.appendTo("#tagList");
				counter++;
		});
		$('#tagList').randomize('li');
		$('#tagdata').tablesorter();
		$('.tagtableheader').hide();
		
					
	});
    
    $(".btn").click(function() {
      // validate and process form here
      
      $('.error').hide();
  	  var name = $("input#name").val();
  		if (name == "") {
			$("label#nameerror").fadeIn("slow");
			$("input#name").focus();
			return false;
      	}
  		var email = $("input#email").val();
  		if (email == "") {
			$("label#emailerror").show();
			$("input#email").focus();
			return false;
      	}
      	if (!IsEmail(email)) {
			$("label#emailerror").show();
			$("input#email").focus();
			return false;
      	}

  		var message = $("textarea#message").val();
  		if (message == "") {
			$("label#messageerror").show();
			$("input#message").focus();
			return false;
      	}
      
      var dataString = 'name='+ name + '&email=' + email + '&message=' + message;
	  //alert (dataString);
	  //return false;
	  $.ajax({
		type: "POST",
		url: "php/process.php",
		data: dataString,
		success: function() {
		  $('#contact_form').html("<div id='responsemessage'></div>");
		  $('#responsemessage').html("<h2>Thanks for the email!</h2>")
		  .append("<p>I'll be sure to get back to you as soon as possible.</p>")
		  .hide()
		  .fadeIn("slow");
		}
	  });
	  return false;
  
      
    });
    
    
    
  });
  
  
  (function($) {

$.fn.randomize = function(childElem) {
  return this.each(function() {
      var $this = $(this);
      var elems = $this.children(childElem);

      elems.sort(function() { return (Math.round(Math.random())-0.5); });  

      $this.remove(childElem);  

      for(var i=0; i < elems.length; i++)
        $this.append(elems[i]);      

  });    
}
})(jQuery);

  
  
  
function fontSize(freq, min, max)
{
	var size = freq;
	if(size > max) size = max;
	else
	{
		if(size < min) size = min;
	}
	return size + "em";
}
  
  
function IsEmail(email) {
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (regex.test(email)) return true;
	else return false;
}  
  
function roundInput(input_id, container_class, border_class){
	var input = $('#'+input_id+'');
	var input_width = input.css("width"); //get the width of input
	var wrap_width = parseInt(input_width) + 10; //add 10 for padding
	wrapper = input.wrap("<div class='"+container_class+"'></div>").parent();
	wrapper.wrap("<div class='"+border_class+"' style='width: "+wrap_width+"px;'></div>"); //apply border
	wrapper.corner("round 8px").parent().css('padding', '2px').corner("round 10px"); //round box and border
}
 
