$(function(){	

	Cufon.replace('h1');
	Cufon.replace('h2', {textShadow:'0 1px #000', hover:'true'});
	
	$(".hide").toggle(
		function () {
			$('#comments').fadeIn(500);
		},
		function () {
			$('#comments').fadeOut(500);
		}
    );




/* -- CodeJoust! Form Code -- */
    $('#spread-word-email').click(function(){
        $('.form').fadeIn('fast');
    });
    $('.page_body').click(function(evt){
            $('.form').fadeOut('slow');
    });
    Array.prototype.has=function(v){
      for (var i=0; i<this.length; i++){
      if (this[i]===v){ return i; } }
      return false;
    }
    $.isValid = function(text){
      if (typeof(text) != 'undefined' && text.length > 0){
         var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
        if (text.match(re)){
        return true; }}
        return false;
    }
    $.fn.checkValid = function(){
      
      $(this).live('keyup', function(evt){
        if ($.isValid($(this).val())){
          $(this).addClass('valid').removeClass('invalid');
        } else {
          $(this).addClass('invalid').removeClass('valid');
        }
        
    });
    if ($.isValid($(this).val())){ return true; } else { return false; }
    };

      $('input.temail.invalid').checkValid();
      $('#to-email').keydown(function(evt){
        if ([13,188,186].has(evt.keyCode)){
          $('#email-list').trigger('add-form');
          setTimeout(function(){$te = $('#to-email'); 
            if ($te.val()[0].match(',|;')){ $te.val(''); } }, 120); 
            return false; } return true;
      });
      $('#add-email').click(function(){ $('#email-list').trigger('add-form'); });
      $('#email-list').bind('add-form', function(){
        var form = {
          emailform: $('#to-email'),
          emaillist: $(this),
          template:   $('#tpl-email'),
          fvalue: function(){return form.emailform.val();} };
        form.value = form.fvalue();
        if ($.isValid(form.value)){
          form.emailform.removeClass('invalid');
          form.nemail = form.template.clone();
          form.nemail.find('.email').html(form.value);
          form.nemail.find('.emailin').val(form.value);
          form.newli = form.emaillist.append(form.nemail.find('li').addClass('vh'));
          form.offset = form.emailform.offset();
          $('<span></span>', {text:form.value, class:'temp'}).css({top:form.offset.top, left:form.offset.left
          , position:'absolute'}).appendTo('body')
          .animate({left:form.newli.offset().left, 
                    top:(form.newli.offset().top + (Math.abs(30 * (form.emaillist.find('li').length - 1))))},
            520, function(){
              $(this).remove();
              $('.form li.vh').removeClass('vh');
          });
          form.emailform.val('').focus();
        } else { form.emailform.addClass('invalid'); }
        return false;
      });
      $('#emailform').submit(function(){
        $(this).find('#email-list').trigger('add-form');
        return false;
      });
      $('#email-list .delete').live('click',function(){ $(this).parent().slideUp('fast', function(){$(this).remove();}); });
      $('#email-list li').live('click', function(){ $i = $(this).find('input'); $(this).find('input:disabled').length ? $i.attr('disabled','disabled') : $i.removeAttr('disabled'); });
      $('#send-all').click(function(){
        if ($('li#has-email-list input.emailin').length > 0){
        if ($('#your-email').css({fontWeight:'normal'}).checkValid()){
          $.ajax({
          type: "POST",
          url: './check.php',
          data: $('form#emailform').serialize(),
          success: function(request){
             $('.form').html("<h2>Your invites have been sent!, Thanks.</h2>")
             .delay(2500).hide('slow');
             if (typeof(console) != 'undefined'){
                console.log(request);
             }
          },
          error: function(xhr, ajaxOptions, thrownError){
            $("<h3>The server couldn't complete your request, please try again later and double-check your data.</h3>")
            .addClass('invalid').append(eval(xhr.responseText).error).prependTo('.form')
            .delay(2500).slideUp('slow');
          }});
        } else {
          $('#your-email').focus().css({fontWeight:'bold'});
        }} else {
          alert('Please type an email into the add email box, the press the add email button to add it to the list.');        
        }
        return false;
      });
      /*$('#msg').ajaxError(function(e, xhr, settings, exception){
        $(this).html("The server couldn't complete your request.").show('fast');
      });*/
    });
/*-- End Form Code --*/

