$(document).ready(function() {
	$('#term').hint();
	
	//Slideshow
	if ($('div#slideshow').length)
	{
		$('#slides').cycle({ 
		    speed:  400, 
		    timeout: 6500, 
		 	pause: true
		});
	}
	
	//News Slide
	$('#news div:gt(0)').hide();
     
	var swap = function(item) {
        setTimeout(function() {
            $(item).fadeOut(1000, function() {
                var next = $(item).next("div")[0];
                if (!next) {
                    next = $('#news div')[0];
                }
                
                $(next).fadeIn(1000, function() {
                    swap($(this)[0]);
                })
                    });
        }, 7500);
       
    };
    
    swap($('#news div')[0]);
    
    //Contact Form
	if ($('#contact_form').length)
    {
        contact_form();
    }
});

function contact_form()
 {
    function showRequest(formData, jqForm, options) {
        if (ValidateForm() == false) {
            return false;
        }
        $('#submitbutton').val('Sending...');
        return true;
    }

    function showResponse(responseText, statusText) {
        $('#submitbutton').val('Message Sent - Thank you!');
        $('#contact_form').clearForm();
    }

    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 Name');
        $('#name').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;
}

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-8789986-1']);
  _gaq.push(['_setDomainName', 'none']);
  _gaq.push(['_setAllowLinker', true]);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


/**
* @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);
