$(document).ready(function(){
  makepopups();
  makeclickables();
  $("#contact").submit(function(){
    $('.error').hide();
    var hasError = false;
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var crack = /(\r|\n)(to:|from:|cc:|bcc:)/;
		
    var from = $('#from').val();
	if(from == '') {
	  $('#from').after('<br /><span class="error">Please enter your email address</span>');
        hasError = true;
    } else if(!emailReg.test(from)) {	
      $('#from').after('<br /><span class="error">Please enter a valid email address</span>');
      hasError = true;
    }
	var message = $('#message').val();
    if(message == '') {
      $('#message').after('<br /><span class="error">Please enter a message</span>');
      hasError = true;
    } else if(crack.test(message)) {
	  $('#message').after('<br /><span class="error">There appear to be email headers in your message</span>');
	  hasError = true;
	}

    if(hasError == false) {
      $(this).children('#submit').hide();
      $(this).append('<img src="http://www.redtailonline.com/images/elements/progress-spinner.gif" width="16" height="16" alt="Loading..." id="loading" />');
      $.post(
	    'inc/sendmail.php',
        { from: from, message: message },
        function(data){
          $("#contact").slideUp("normal", function() {				   
          $("#contact").before('<div class="success-box"><ul><li>Thank you for your email, I will get back to you shortly.</li></ul></div>');											
          $("#toggle-contact").hide();
        });
      });
	}
    return false;
  });
}); 

function makepopups() {
  $('a.popup-link').click(function() {
    var w = $(window).width();
	var h = $(window).height();
	var defs = 'resizable,toolbar=no,location=no,scrollbars=yes';
    var r = $(this).attr('rel');
    if(!r){
      f = 'width=' + w + ',height=' + h + ',' + defs;
      popup($(this).attr('href'),'redtail',f);
      return false;
    } else {
      r = $(this).attr('rel').split(' ');
	  if (jQuery.browser.msie) {
	    w = parseInt(r[1]) + 40;
		h = parseInt(r[2]) + 40;
	  } else {
	    w = r[1];
		h = r[2]
	  }
      popup($(this).attr('href'),r[0],'width=' + w + ', height=' + h + ',' + defs);
      return false;
	}
  });
}

function makeclickables(){
  $('ul.web-samples li ul li, ul.web-development li').each(function() {
    if($(this).children('a').attr('href') != undefined) {
      clickable($(this),$(this).children('a').attr('href'),$(this).children('a').attr('title'),1);
    }
  });
}

function clickable(e,h,t,p) {
  $(e)
  .mouseover(function(){
    window.status = t;
    $(this).addClass('clickable').attr({ title: t });
  })
  .mouseout(function(){
    window.status = '';
    $(this).removeClass('clickable');
  })
  .bind('click',function(){
    if(p ==1) {
	  var defs = 'resizable,toolbar=no,location=no,scrollbars=yes';
	  popup(h,'mwr','width=' + $(window).width() + ',height=' + $(window).height() + ',' + defs);
	} else {
      window.location = h;
    }
  })
}

/* SHOW/HIDE */
function show_hide (v,id,tog,open,shut) {
  if(v==0) {
    $('#'+id).hide();
  } else {
    $('#'+id).fadeIn('slow');
  }
  $('#'+tog).html(v
  ? '<a href="" onclick="show_hide(0,\'' + id + '\',\'' + tog + '\',\'' + open + '\',\'' + shut + '\'); return false" accesskey="c">' + shut + '</a>'
  : '<a href="" onclick="show_hide(1,\'' + id + '\',\'' + tog + '\',\'' + open + '\',\'' + shut + '\'); return false" accesskey="c">' + open + '</a>'
  );
}

function popup(u,n,f) {
  var newwin = window.open(u,n,f);
  newwin.focus();
}
