/**
 *	fires when the dom is ready
 *
 */
jQuery(document).ready(function() {
	cufonReplace();
	searchHandler();
	addMenuClasses();
	tabHandler();
	subMenuHandler();
});

jQuery(window).load( function() {
	activateSlides();
});

/**
 * Take care of special menu's
 *
 */
function subMenuHandler() {
	if ( !jQuery('#subnav_holder').length )
		return false;
		
	if ( jQuery('.item_110').length ) {
		
		jQuery('.item_110 li a').click( function(event) {
			event.preventDefault();
			
			var reference = jQuery(this).parent('li[class*="object_"]').first().attr('class');
			
			jQuery('html, body').animate({
				scrollTop: jQuery('#' + reference).offset().top - 10
			}, 1000);
			

		});
	
	}
}

/**
 * Activate slides on window.load
 *
 */
function activateSlides() {
	if ( !jQuery('#spotlight').length )
		return false;

	jQuery('.slides_container div').each( function(i, e) {
		jQuery(e).css('visibility', 'visible');
	});
	
	jQuery('.slides_container').each( function(i, e) {
		jQuery(e).css('background-color', 'transparent');
	});
}

/**
 * Replace targeted fonts using cufon
 *
 */
function cufonReplace() {
	Cufon.replace('h1, h2, h3, #testimonial .content', {
		fontFamily: 'Arial'
	});
	
	Cufon.replace('#nav_holder a', {
		fontFamily: 'Arial'
	});
}

/**
 * Handle tabs
 *
 */
function tabHandler() {
	if ( !jQuery('#tabbed').length )
		return false;
		
	// attach 'set active' 
	jQuery('#tabbed h3.tab_title').each( function(i, e) {
		jQuery(e).click( function() {

			if ( jQuery(this).closest('div').hasClass('active') )
				return true;
				
			jQuery('#tabbed div.active').removeClass('active');
			
			jQuery(this).closest('div').addClass('active');
		});
	});
}

/**
 * Take care of the first menu class since it has a different design
 *
 */
function addMenuClasses() {
	if ( !jQuery('#nav_holder').length )
		return false;
	
	var firstChild = jQuery('#nav_holder li').first();
	
	// set class
	firstChild.addClass('first');
	firstChild.children('a').addClass('first');
	
	if ( firstChild.hasClass('active') ) {
		firstChild.children('a').addClass('first_selected');
	}
}

/**
 *	handle any css or ajax of the search button
 *
 */
function searchHandler() {
	if ( !jQuery('#s').length )
		return false;
	
	// set field
	var field = '#s';
	
	// set some references
	var label 		= jQuery(field).prev();
	var message 	= jQuery(field).next('.message');
	var startValue 	= label.html();
	
	// don't show value if something is filled in
	if ( jQuery(field).val() != '' )
		label.css('opacity', 0);
	
	// what happens on focus
	jQuery(field).focus( function() {
		label.addClass('selected');
	// what happens on blur
	}).blur( function() {
		
		// restore if empty
		if ( jQuery(this).val() == '') {
			label.animate({
				opacity: 1
			}, 450);
			
			jQuery('#emailform').find('.message').animate({
				'height': '0'
			}, 500);
		}
		
		label.removeClass('selected');
	});
	
	// check value on keypress
	jQuery(field).keypress( function() {
		if ( jQuery(this).value != '')
			label.css('opacity', 0);
	});
}
