
var rotationTimer = null;
var rotationDelay = 7500;


$(document).ready(function(){

	
$("a.fancybox").fancybox();
externalLinks();
selectFeaturedContent();
var rotationTimer = setTimeout('slideshow()', rotationDelay);

$("#tool-print").click(function() {
window.print()
return false;  
});  

$('#alert').jqm({overlay: 0, modal: true, trigger: false});
  

	$('#email-address').each(function(){
	
		$(this).focus(function(){
		
			if (!$(this).data('defaultText'))
			{
				$(this).data('defaultText', $(this).val());
			}
		
			if ($(this).val() == $(this).data('defaultText')) 
			{
				$(this).val('');
			}
		
		});
		
		$(this).blur(function(){
		
			if (!$(this).val().length)
			{
				$(this).val($(this).data('defaultText'));
			}
		
		});
	
	});
	
	$('#registration-form').submit(function(){
											
	
		$('#email-address').each(function() {
		
	
			var emailAddressRegex = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		
			if (!emailAddressRegex.test($(this).val()))
			{
				//invalid
				alert('Invalid email address');
				valid = false;
					
			}
			else
			{
				//valid		
				valid =  true;
			
			}

		});
		return valid;
	});
	


  
  
});


/* Overriding Javascript's Alert Dialog */

function alert(msg) {
  $('#alert')
    .jqmShow()
    .find('div.jqmAlertContent')
      .html(msg);
}

function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
} 

function selectFeaturedContent()
{
    count  = $("ul#carousel-nav").children().length;
	
    k = 0;
    
    while(k < count)
    {
    
        $("ul#carousel-nav li a:eq("+k+")").click(function() { 
        
        clearTimeout(rotationTimer);
        
        index = $('ul#carousel-nav li a').index(this);
        
        $("#carousel .featured-content:eq("+index+")").fadeIn('normal');
        $("ul#carousel-nav li a:eq("+index+")").addClass('selected-item');
        
        $("#carousel .featured-content:lt("+index+")").hide();
        $("ul#carousel-nav li a:lt("+index+")").removeClass('selected-item');
        
        $("#carousel .featured-content:gt("+index+")").hide();
        $("ul#carousel-nav li a:gt("+index+")").removeClass('selected-item');
    
      });
      
      k++;
      
    }
}


function slideshow()
{
  //reset any timers currently running
  clearTimeout(rotationTimer);
  
  //find the current visible featured content
  var featuredContentCounter = 0;
  var visibleFeaturedContent = 0;
  
  $('.featured-content').each(function() {
  
    if($(this).is(':visible'))
    {
      visibleFeaturedContent = featuredContentCounter;
    }
    featuredContentCounter++;
  
  });
  
  //determine the next featured item
  if(visibleFeaturedContent < 2)
  {
    var nextFeaturedContent = visibleFeaturedContent + 1;
  }
  else
  {
    var nextFeaturedContent = 0;
  }
  
  //hide and show the relevant featured content panels
  $('.featured-content').hide();
  $('.featured-content:eq(' + nextFeaturedContent + ')').fadeIn('normal');
  
  $('ul#carousel-nav li a').removeClass('selected-item');
  $('ul#carousel-nav li a:eq(' + nextFeaturedContent + ')').addClass('selected-item');
  
  rotationTimer = setTimeout('slideshow()', rotationDelay);
}

