$(document).ready(function() {

	if ($('a#enlarge').length)
	{
		$("a#enlarge").fancybox({
			'zoomSpeedIn':		300, 
			'zoomSpeedOut':	300, 
			'overlayShow':		false
		});
	}
	
	$('input.text').hint();
	
	if ($('#contact_form').length) 
	{
		contact_form();
	}
	
});

function contact_form()
{
	function showRequest(formData, jqForm, options) {
	    if (ValidateForm() == false) {
			$('input.text').hint();
	        return false;
	    }
	    $('#loader').show();
	    return true;
	}

	function showResponse(responseText, statusText) {
	    $('#loader').hide();
	    $('#response').html(responseText).fadeIn();
	}
	
	var options = 
	{
		beforeSubmit: showRequest,
		success: showResponse
	};
	$('#contact_form').ajaxForm(options);
}

function echeck(str) 
{
    var at = '@';
    var dot = '.';
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);
    if (str.indexOf(at) == -1) 
	{
        alert('Invalid E-mail Address');
        return false;
    }
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) 
	{
        alert('Invalid E-mail Address');
        return false;
    }
    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) 
	{
        alert('Invalid E-mail Address');
        return false;
    }
    if (str.indexOf(at, (lat + 1)) != -1) 
	{
        alert('Invalid E-mail Address');
        return false;
    }
    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) 
	{
        alert('Invalid E-mail Address');
        return false;
    }
    if (str.indexOf(dot, (lat + 2)) == -1) 
	{
        alert('Invalid E-mail Address');
        return false;
    }
    if (str.indexOf(' ') != -1) 
	{
        alert('Invalid E-mail Address');
        return false;
    }
    return true;
}

function ValidateForm() 
{
    var nameID = $('#name').val();
    if ((nameID == null) || (nameID == '')) 
	{
        alert('Please Enter your First Name');
        $('#name').focus();
        return false;
    }
    var companyID = $('#company').val();
    if ((companyID == null) || (companyID == '')) 
	{
        alert('Please Enter your Company');
        $('#company').focus();
        return false;
    }
	var departmentID = $('#department').val();
    if ((departmentID == null) || (departmentID == '')) 
	{
        alert('Please Enter your Department');
        $('#department').focus();
        return false;
    }
	var phoneID = $('#phone').val();
    if ((phoneID == null) || (phoneID == '')) 
	{
        alert('Please Enter your Phone Number');
        $('#phone').focus();
        return false;
    }
    var emailID = $('#email').val();
    if ((emailID == null) || (emailID == '')) 
	{
        alert('Please Enter your Email Address');
        $('#email').focus();
        return false;
    }
    if (echeck(emailID) == false) 
	{
        $('#email').val('');
        $('#email').focus();
        return false;
    }
    return true;
}


/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {

$.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }
    
  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this),
    
    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title
      
      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

})(jQuery);