// JavaScript Document

$(document).ready(function(){
	$('input').focus(function(){
		$(this).select();
	});
	var ie6 = false;
	if (jQuery.browser.msie) {
		if(parseInt(jQuery.browser.version) == 6) {
			ie6 = true;
		}
	}
	var randomPhoto = Math.ceil(Math.random() * $('#homeHeader .homePhoto').length);
	totalHomePhotos = 0;
	nextHomePhoto = 1;
	$('.homePhoto').each(function(i){
		if (ie6) { return;}
		$('#homeHeader #homePhotosLinks').append('<li id="photoLink-'+(i+1)+'"><a href="#homePhoto-'+(i+1)+'">'+(i+1)+'</a></li>');
		totalHomePhotos = i+1;
	});
	$('#homePhotosLinks a').click(function(event){
		if (ie6) { return;}
		event.preventDefault();
		homepageRotate($(this));
	});
	if (totalHomePhotos) {
		if (ie6) { return;}
		homepageRotate($('#photoLink-'+randomPhoto+' a'));
		setInterval(function () { homepageRotate($('#photoLink-'+nextHomePhoto+' a'));}, 8000);
	}
	$(".popup").colorbox();
	//$(".popup-iframe").colorbox({width:"838px", height:"80%", iframe:true});
	$(".popup-iframe").click(function(event){
		event.preventDefault();
		var url = $(this).attr('href');
		$.scrollTo(0,'',function(){
			$.fn.colorbox({href:url, width:"838px", height:"80%", iframe:true, open:true, opacity:.40}); 
		});
	});
	$(".popup-iframeShort").click(function(event){
		event.preventDefault();
		var url = $(this).attr('href');
		$.scrollTo(0,'',function(){
			$.fn.colorbox({href:url, width:"838px", height:"50%", iframe:true, open:true, opacity:.40}); 
		});
	});
	$(".popup-iframeNarrow").click(function(event){
		event.preventDefault();
		var url = $(this).attr('href');
		$.scrollTo(0,'',function(){
			$.fn.colorbox({href:url, width:"500px", height:"90%", iframe:true, open:true, opacity:.40}); 
		});
	});
	
	$('#enewsForm').submit(function(event){
		event.preventDefault();
		var email = $('#enews_email').val();
		if (isValidEmail(email)) {
			$.fn.colorbox({width:"480px",height:"90%",href:"/emailform.php?email="+email, iframe:true, open:true, opacity:.40});
		} else {
			alert('Please input a valid e-mail address.');	
		}
	});
	$('#enewsForm_side').submit(function(event){
		event.preventDefault();
		var email = $('#enews_email_side').val();
		if (isValidEmail(email)) {
			$.fn.colorbox({width:"480px",height:"90%",href:"/emailform.php?email="+email, iframe:true, open:true, opacity:.40});
		} else {
			alert('Please input a valid e-mail address.');	
		}
	});
	$('#latestBlog').hide();
	$('#latestBlog').load("/content/news.php",function(){
		$('.loading',$(this).parents('.imageIndent')).hide();
		$(this).animate({
			height:'show',
			opacity:'show'
		}, 'slow');
	});
	$('#researchlibrary_latest').hide();
	$('#researchlibrary_latest').load("/content/researchlibrary-latest.php",function(){
		$('.loading',$(this).parents('.imageIndent')).hide();
		$(this).animate({
			height:'show',
			opacity:'show'
		}, 'slow');
	});
	$('#pressrelease_latest').hide();
	$('#pressrelease_latest').load("/content/pressreleases-latest.php",function(){
		$('#pr_loading').hide();
		$(this).animate({
			height:'show',
			opacity:'show'
		}, 'slow');
	});
	$('#pressReleasesDiv').load("/content/pressreleases.php",function(){
		$('a.tooltip').tooltip({
			delay: 0, 
			showURL: false,
			track: true
		});
	});
	$('#researchLibraryDiv').load("/content/researchlibrary.php");
	$('#researchFilter').change(function(event){
		$('#researchLibraryDiv').load("/content/researchlibrary.php?cat="+$(this).val());
	});
	$('#pressFilter').change(function(event){
		if ($(this).val()!='all') { 
			$('#pressReleasesDiv').load("/content/pressreleases.php?cat="+$(this).val(),function(){
				$('a.tooltip').tooltip({
					delay: 0, 
					showURL: false,
					track: true
				});
			});
		} else {
			$('#pressReleasesDiv').load("/content/pressreleases.php",function(){
				$('a.tooltip').tooltip({
					delay: 0, 
					showURL: false,
					track: true
				});
			});
		}
	});
	$('#homeBlogCell').load("/content/news-home.php");
	$('a[title]').tooltip({
		delay: 0, 
    	showURL: false,
		track: true
	});
	$('.colorbox').colorbox();
});

function homepageRotate($theLink) {
	var myID;
	if ($theLink.attr('href').indexOf('.html') > -1 || $theLink.attr('href').indexOf('/#') > -1) {
		var href = $theLink.attr('href');
		myID = href.substring(href.indexOf('#'),href.length);
	} else {
		myID = $theLink.attr('href');
	}
	$('#homeHeader .homePhoto:visible').hide();
	$(myID).show();
	$('#homeHeader #homePhotosLinks li.selected').removeClass('selected');
	$theLink.parents('li').addClass('selected');
	var id = $theLink.parents('li').attr('id');
	var thisImageNumber = parseFloat(id.substring(10,id.length));
	nextHomePhoto = thisImageNumber+1;
	if (nextHomePhoto > totalHomePhotos) {
		nextHomePhoto = 1;	
	}
}


var whitespace = " \t\n\r";
function isValidEmail(s) {   
	if (isEmpty(s)) return false;
   	if (isWhitespace(s)) return false;
	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@")) {
		i++
	}
	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;
	while ((i < sLength) && (s.charAt(i) != ".")) {
		i++
	}
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else return true;
}
function isEmpty(s) {   
	return ((s == null) || (s.length == 0))
}
function isWhitespace(s) {   
	var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++) {   
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}