jQuery(document).ready(function(){
	
	// Formatting replacement
	/*
	jQuery('#content, #rightboxes div').each(function() {
		var newContent = jQuery(this).html();
		newContent = newContent.replace(/'{4}((?:[^']|'(?!'{3}))+)'{4}/g, "<em>$1</em>");
		newContent = newContent.replace(/'{3}((?:[^']|'(?!'{2}))+)'{3}/g, "<strong>$1</strong>");
		newContent = newContent.replace(/\[\[([^\]]+)\]\]/g, "<img src='/i/photos/$1' alt='' />");
		newContent = newContent.replace(/\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}/g, "<a href='mailto:$&'>$&</a>");
		newContent = newContent.replace(/\[((?:http|https|ftp):\/\/[^ \]]+)\]/g, "<a href='$1'>$1</a>");
		newContent = newContent.replace(/\[((?:http|https|ftp):\/\/[^ \]]+) ([^\]]+)\]/g, "<a href='$1'>$2</a>");
		jQuery(this).html(newContent);
	});
	*/
	
	// dropdown menu
	jQuery('#navigation > li').hover(function(){
	    jQuery(this).addClass('hover');
	    jQuery('ul:first', this).css('visibility', 'visible');
	}, function(){
	    jQuery(this).removeClass('hover');
	    jQuery('ul:first', this).css('visibility', 'hidden');
	});
	
	// menu styling adjestment
	jQuery('#navigation > li:eq(4)').find('ul:first').find('a').css({'width': '145px'});
	jQuery('#navigation > li:eq(5)').find('ul:first').find('a').css({'width': '155px'});
                        
	
	// Filled box showing (COMMON PAGES ONLY)
	numShown = 0;
	jQuery('#rightboxes div').each(function() {
		if(jQuery(this).text().length > 2) {
			jQuery('#rightboxes').show();
			jQuery(this).css('cursor', 'pointer').show();
			numShown++;
		}
	});
	if(numShown == 0) {
		jQuery('#rightboxes').hide();
		jQuery('#content').css('width', '690px');
		jQuery('#content').css('margin-right', '0');
	}
	
	// clickable boxes
	var targetLink;
	var hostName = window.location.hostname;

	jQuery('#rightboxes div').click(function() {
	    if(jQuery(this).find('a').length > 0) {
		targetLink = jQuery(this).find('a')[0].href;
	    
		if(targetLink.indexOf('.pdf') != -1 || targetLink.indexOf(hostName) == -1) {
		    window.open(targetLink);	
		} else {
		    window.location = targetLink;
		}
	    }
	});
	jQuery('#rightboxes a').click(function(e) {
	    $(this).parent('div').click;
	    e.preventDefault();
	});
	
	// Menu highlighting (COMMONG PAGES ONLY)
	jQuery('#leftnav a').each(function() {
		if(jQuery(this).attr('href') == window.location.pathname) {
			jQuery(this).addClass('selected');
		}
	});
	
	// Newsletter placeholder text (HOMEPAGE ONLY)
	jQuery('#email').addClass('placeholder');
	jQuery('#email').bind('focusin', function(event){
	var obj = jQuery(event.target);
		obj.removeClass('placeholder');
		if(obj.val() == obj.attr('title')) obj.val('');
	});
	jQuery('#email').bind('focusout', function(event){
		var obj = jQuery(event.target);
		if((obj.val() == '') || (obj.val() == obj.attr('title'))) {
			obj.val(obj.attr('title'));
			obj.addClass('placeholder');
		}
	});
	
	// Initiate the slideshow on homepage
	rotator();
});

function rotator(e) {
        jQuery('#bannerBox img').css('opacity', 0)
                                .eq(0).css('opacity', 1).addClass('active');
        
	var time = jQuery('#bannerBox span').text();
        setInterval('rotateIt()', time);
}

function rotateIt() {
	
    // Get the first image
    var currentImg = jQuery('#bannerBox img.active') ? jQuery('#bannerBox img.active') : jQuery('#bannerBox img:first');

    // Get next image, when it reaches the end, rotate it back to the first image
    var nextImg = (currentImg.next('img').length) ? (currentImg.next('img').hasClass('active')) ? jQuery('#bannerBox img:first') : currentImg.next() : jQuery('#bannerBox img:first');	
	
	// Set the fade in effect for the next image, the active class has higher z-index
	nextImg.css('opacity', 0)
	       .addClass('active')
	       .animate({opacity: 1}, 1500);

	// Hide the current image
	currentImg.animate({opacity: 0}, 1500)
	          .removeClass('active');
                  
}

