$(document).ready(function() {
	//tab control

	$('.tabs li').click(function(){
		var openThisTab = $(this).attr("rel");
			if (!openThisTab) {
				return false
			}
			else {						
				$(this).siblings('li').removeClass('open');
				$(this).addClass('open');			
				$('.tabContent').children().hide();
				$(openThisTab).show();
			}
	});
	//carousel inits
	$("#photoGalleries").show();
	$(".photoGalleryCarousel").jCarouselLite({
		btnNext: "#photoGalleries .next",
		btnPrev: "#photoGalleries .prev",
		scroll: 3,
		circular: false
 	});
	$("#photoGalleries").hide();
	$("#videos").show();
	$(".videosCarousel").jCarouselLite({
		btnNext: "#videos .next",
		btnPrev: "#videos .prev",
		scroll: 3,
		circular: false
 	});
	$("#videos").hide();
	
	// show the overlay
	$('.btnShowOverlay').click(function(){
		var openThisWindow = $(this).attr("rel");
		$('.overlayBG').fadeIn('slow');
		$(openThisWindow).fadeIn('slow');
	});
	// hide the overlay
	$('.closeOverlayBox').live('click',function(){
		$('.overlayBG').fadeOut('slow');
		$(this).parents('section.overlayBox').fadeOut('slow');
	});
	// Accordion
	/*$('#homeNews').accordion({
		clearStyle:true
		}
	);*/
	$('#homeNews li:first-child').addClass('current');

	$('#homeNews li h3').click(function() {
		$(this).css('outline','none');
		if($(this).parent().hasClass('current')) {
			$(this).next('div').slideUp('slow',function() {
				$(this).parent().removeClass('current');
			});
		} else {
			$('#homeNews li.current div').slideUp('slow',function() {
				$(this).parent().removeClass('current');
			});
			$(this).next('div').slideToggle('slow',function() {
				$(this).parent().toggleClass('current');
			});
		}
		return false;
	});

	//Slider for the Hero

	if ($('#heroSlider img').length) {
		$('#heroSlider').nivoSlider({
			effect: 'fold',
			pauseTime: 5000,
			controlNav: false,
			directionNav:false
		});
	}

	// Functionality that keeps topnav selected when a dropdown occurs
	$('#nav ul.subMenu').hover (
		function() {
			$(this).prev('a').addClass('selected');
		},
		function() {
			$(this).prev('a').removeClass('selected');
		}
	);

	// Functionality that calls a popup form for the "Click Here" on the bottom
	//$(".clickHere[rel]").overlay();


	//Change videos on home Page
	$(".gallery_videos").live('click',function(){
		$.getScript("js/video.js");
		var video = $(this).attr("video");
		var thumbnail = $(this).attr("src");
		var name = $(this).attr("title");
		$("#player").html("");
		$("#player").html(
		'<script type="text/javascript">'+
			'VideoJS.setupAllWhenReady();'+
		'</script>'+
		'<div class="video-js-box">'+
		'<video class="video-js" width="490" height="364" controls="controls" preload="auto" poster="'+thumbnail+'">'+
		'<source  src="'+video+'" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />'+
		'<object id="flash_fallback_1" class="vjs-flash-fallback" width="490" height="346" type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">'+
		'<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />'+
		'<param name="allowfullscreen" value="true" />'+
		'<param name="flashvars" value=\'config='+
		'{"canvas":{"backgroundColor":"#666666",'+
		'"backgroundGradient":"none"},'+		
		'"clip":{"scaling":"orig"},'+
		'"playlist":["'+thumbnail+'", {"url": "http://www.claremont-courier.com'+video+'","autoPlay":false,"autoBuffering":true}]}\'/>'+		
		'<img src="'+thumbnail+'" width="490" height="364" alt="Poster Image" title="No video playback capabilities." />'+
		'</object>'+
		'</video>'+
		'<p class="vjs-no-video"><strong>Download Video:</strong>'+
		'<a href="'+video+'" id="download-link">MP4</a>'+
		'</p>'+
		'</div>');
		return false;
	});

	//Change videos on galleries/videos
	$(".gallery_video").live('click',function(){
			$.getScript("/js/video.js");
			var video = $(this).attr("video");
			var thumbnail = $(this).attr("src");
			var name = $(this).attr("title");
			$("#videoOverlay").html("");
			$("#videoOverlay").show();
			$("#videoOverlay").html(
			'<a href=""  class="closeOverlayBox"></a>'+
			'<script type="text/javascript">'+
				'VideoJS.setupAllWhenReady();'+
			'</script>'+
			'<div class="video-js-box">'+
			'<video class="video-js" width="490" height="364" controls="controls" preload="auto" poster="'+thumbnail+'">'+
			'<source  src="'+video+'" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />'+
			'<object id="flash_fallback_1" class="vjs-flash-fallback" width="490" height="346" type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">'+
			'<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />'+
			'<param name="allowfullscreen" value="true" />'+
			'<param name="flashvars" value=\'config='+
			'{"canvas":{"backgroundColor":"#666666",'+
			'"backgroundGradient":"none"},'+		
			'"clip":{"scaling":"orig"},'+
			'"playlist":["'+thumbnail+'", {"url": "http://www.claremont-courier.com'+video+'","autoPlay":false,"autoBuffering":true}]}\'/>'+		
			'<img src="'+thumbnail+'" width="490" height="364" alt="Poster Image" title="No video playback capabilities." />'+
			'</object>'+
			'</video>'+
			'<p class="vjs-no-video"><strong>Download Video:</strong>'+
			'<a href="'+video+'" id="download-link">MP4</a>'+
			'</p>'+
			'</div>');

		$('.overlayBG').fadeIn();
		return false;
	});
$(".closeOverlayBox").live('click',function(){
	$("#videoOverlay").html("");
	return false;
});

$("#search_keyword").val("Search Website");

$("#search_keyword").focus(function(){
	var value = $(this).val();
	if(value=="Search Website" || value=="" || value==" "){
		$(this).val("");
		$(this).focus();

	}
});

$("#search_keyword").blur(function(){
	var value = $(this).val();
	if(value=="Search Website" || value=="" || value==" "){
		$(this).val("Search Website");
	}
});
});

/*********
validates an email address
*********/
function validateEmail(thisEmail)
{
	//initiate returnMessage variable
	var returnMessage="";

	if(thisEmail == '')
	{
		// alert the user
		returnMessage = "Please type in a valid email address.";
	}
	else
	{
		// function to check email address vilidity
		function emailCheck(emailStr)
		{
			/* The following pattern is used to check if the entered e-mail address
			   fits the user@domain format.  It also is used to separate the username
			   from the domain. */
			var emailPat=/^(.+)@(.+)$/
			/* The following string represents the pattern for matching all special
			   characters.  We don't want to allow special characters in the address.
			   These characters include ( ) < > @ , ; : \ " . [ ]    */
			var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
			/* The following string represents the range of characters allowed in a
			   username or domainname.  It really states which chars aren't allowed. */
			var validChars="\[^\\s" + specialChars + "\]"
			/* The following pattern applies if the "user" is a quoted string (in
			   which case, there are no rules about which characters are allowed
			   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
			   is a legal e-mail address. */
			var quotedUser="(\"[^\"]*\")"
			/* The following pattern applies for domains that are IP addresses,
			   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
			   e-mail address. NOTE: The square brackets are required. */
			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
			/* The following string represents an atom (basically a series of
			   non-special characters.) */
			var atom=validChars + '+'
			/* The following string represents one word in the typical username.
			   For example, in john.doe@somewhere.com, john and doe are words.
			   Basically, a word is either an atom or quoted string. */
			var word="(" + atom + "|" + quotedUser + ")"
			// The following pattern describes the structure of the user
			var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
			/* The following pattern describes the structure of a normal symbolic
			   domain, as opposed to ipDomainPat, shown above. */
			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


			/* Finally, let's start trying to figure out if the supplied address is
			   valid. */

			/* Begin with the coarse pattern to simply break up user@domain into
			   different pieces that are easy to analyze. */
			var matchArray=emailStr.match(emailPat);
			//alert(emailStr);
			var checkMatch='-' + matchArray + '-';
			if (checkMatch == "-null-")
			{
			  /* Too many/few @'s or something; basically, this address doesn't
				 even fit the general mould of a valid e-mail address. */
				returnMessage = "The email address seems incorrect (check @ and .'s).";
			}
			else
			{
				var user=matchArray[1];
				var domain=matchArray[2];

				// See if "user" is valid
				if (user.match(userPat)==null)
				{
					// user is not valid
					returnMessage = "The email's username doesn't seem to be valid (before the @).";
				}

				/* if the e-mail address is at an IP address (as opposed to a symbolic
				   host name) make sure the IP address is valid. */
				var IPArray=domain.match(ipDomainPat)
				if (IPArray!=null) {
					// this is an IP address
					  for (var i=1;i<=4;i++) {
						if (IPArray[i]>255) {
							returnMessage = "The email's destination IP address is invalid.";
						}
					}
				}

				// Domain is symbolic name
				var domainArray=domain.match(domainPat)
				if (domainArray==null) {
					returnMessage = "The email's domain name doesn't seem to be valid (after the @).";
				}

				/* domain name seems valid, but now make sure that it ends in a
				   three-letter word (like com, edu, gov) or a two-letter word,
				   representing country (uk, nl), and that there's a hostname preceding
				   the domain or country. */

				/* Now we need to break up the domain to get a count of how many atoms
				   it consists of. */
				var atomPat=new RegExp(atom,"g")
				var domArr=domain.match(atomPat)
				var len=domArr.length
				if (domArr[domArr.length-1].length<2 ||
					domArr[domArr.length-1].length>4) {
				   // the address must end in a two letter or three letter word.
				   returnMessage = "The email must end in a four-letter domain, three-letter domain, or two letter country.";
				}

				// Make sure there's a host name preceding the domain.
				if (len < 2) {
				   returnMessage="This email is missing a hostname!";
				}
			}
		}
		// call the validation function and return its result
		val=emailCheck(thisEmail);
		// if it returns val=no_submit, stop form
	}
	return returnMessage;
}

/************
verify Contact Form
************/
function verifyContactForm() {
	//form variables
	submitBtnMessage='Saving... Please Wait';
	formName='ContactForm';

	//initialize submitForm value
	submitForm=true;
	warningMessage="";
	formFocus="";

	//check name
	if (submitForm)
	{
		formElement="FullName";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your name.';
			formFocus=formElement;
			submitForm=false;
		}
	}

	//check the email
	if (submitForm)
	{
		formElement="Email";
		thisDOM=eval('document.' + formName + '.' + formElement);
		//check the email address
		thisMessage=validateEmail(thisDOM.value);
		//if there is no message sent back, set the alert.
		if (thisMessage.length != 0)
		{
			warningMessage=thisMessage;
			formFocus=formElement;
			submitForm=false;
		}
	}
	//check comment
	if (submitForm)
	{
		formElement="Comments";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your comments.';
			formFocus=formElement;
			submitForm=false;
		}
	}

	//disable the form if the form checks out
	if (!submitForm)
	{
		alert(warningMessage);
	}

	//focus the form if necessary
	if (formFocus.length > 0)
	{
		thisDOM=eval('document.' + formName + '.' + formFocus);
		thisDOM.focus();
	}

	//return the boolean value whether or not we should submit this form
	return submitForm;
}
