// Create a JavaScript object
var application = {};

application.mainObject = {
	// Initialize the main object
	init: function() {
		// Enable rotation of personnel on the front page
		jQuery('#personnelslideshow').tabs({
			// Fade tabs
			fx: { 
				opacity: 'toggle'
			}
		}).tabs('rotate', 5000, true);
		
		// Enable navigation for the portfolio items on the frontpage
		application.mainObject.tabsNextPrevious(jQuery('#tabs'), 'Volgende', 'Vorige', null);
		
		// Enable main menu dropdowns for all browsers
		jQuery('#main-navigation > ul > li:not(.active)').hover(
			function() {
				jQuery(this).addClass('show-sub-navigation');
				jQuery(this).children('a').css('color', '#333');
				
				// Refresh Cufon when leaving the dropdown
				jQuery(this).bind('mouseleave', function() {
					jQuery(this).parent().children('a').css('color', '#fff');
					Cufon.refresh('#main-navigation>ul>li>a');
				});
			},
			function() {
				jQuery(this).removeClass('show-sub-navigation');
				jQuery(this).children('a').css('color', '#fff');
			}
		);
		
		// Handle a focus on an input with a default value
		jQuery('input.default').bind('focus', function() {
			// Check if the value is the original value, if so clear it
			if (jQuery(this).val() == jQuery(this).attr('title'))
				jQuery(this).val('');
		});

		// Handle a blur on an input with a default value
		jQuery('input.default').bind('blur', function() {
			// Check if the value is empty, if so replace it with the default
			if (application.trim(jQuery(this).val()) == '')
				jQuery(this).val(jQuery(this).attr('title'));
		});
		
		// Open external links in a new window
		jQuery('a.internal-link-new-window').attr('target','_blank');
		
		// Open external links in a new window
		jQuery('a.external-link-new-window').attr('target','_blank');
		
		// Open files in a new window
		jQuery("a[href*='uploads/media']").attr('target','_blank');
		
		// Make the team overview interactive
		application.mainObject.interactiveTeam('#interactive-team');
		
		// Enable lightbox browsing for portfolio items
		if (jQuery('.news-single-img').children().size() == 1) {
			jQuery('.news-single-img').children(':first-child').removeClass('last').addClass('first');
		}
	},
	
	interactiveTeam: function(container) {
		jQuery(container + ' ul li a').bind('mouseover', function() {
			// Get the member's image sprite
			var memberImageSprite = jQuery(this).css('background-image');
			var memberName = jQuery(this).text();
			var memberFunction = jQuery(this).attr('title');
			
			// Set the member's image as main image
			jQuery('#activeMember').css('background-image', memberImageSprite);
			
			// Show the member's name
			jQuery('#activeMember h3').text(memberName);
			jQuery('#activeMember span').text(memberFunction);
			
			// Refresh cufon
			Cufon.refresh();
			
			// Set the member as active
			jQuery('#team-members li').removeClass('active');
			jQuery(this).parent().addClass('active');
		});
	},
		
	tabsNextPrevious: function(tabContainer, nextText, previousText, options) {
		// Enable tabbed navigation
		var allTabs = tabContainer.tabs(options);
		
		// Get the tab container id
		var tabContainerId = tabContainer.attr('id');
		
		// Create navigation buttons for each tab
		jQuery('#' + tabContainerId + ' .ui-tabs-panel').each(function(i){
			var totalSize = jQuery('#' + tabContainerId + ' .ui-tabs-panel').size() - 1;
			var nextTab = i + 2;
			var previousTab = i;
			
			// Determine the tabs to link to
			if (i == totalSize) {
				var nextTab = 1;
			}
			if (i == 0) {
				previousTab = totalSize + 1;
			}
			
			// Create the navigation links		
			jQuery(this).append("<a href='#' class='gotoNextTab tabNavigator' rel='" + nextTab + "'>" + nextText + "</a>");
			jQuery(this).append("<a href='#' class='gotoPreviousTab tabNavigator' rel='" + previousTab + "'>" + previousText + "</a>");
			
			// Add a class to this panel
			jQuery(this).addClass('previousNextPanel');
		});
		
		// Make elements outside tabs visible
		tabContainer.css('overflow', 'visible');
		
		jQuery('#' + tabContainerId + ' .tabNavigator').click(function() {
			allTabs.tabs('select', jQuery(this).attr("rel"));
			return false;
		});
	}
};

// Replace text with Cufon
Cufon.replace('h1')('h2')('h3', { hover: true })('#main-navigation>ul>li>a', { hover: true })('div.personnel blockquote')('.banner a', { hover: true })('#content-right #quote blockquote')('#activeMemberInfo span');

// Initialize the main object, which handles all website initialization
jQuery(document).ready(function() {
	// Initialize the main object
	application.mainObject.init();
});