// Helper functions
function prepPlaceholder(el) {
	if(el.attr('value') == '' || el.attr('value') == el.attr('placeholdertext')) {
		el.addClass('placeholder');
		if(el.attr('value') == '') {
			el.attr('value', el.attr('placeholdertext'));
		}
	} else {
		el.removeClass('placeholder');
	}
}
function togglePlaceholder(el) {
	// Check if the input already has a value...
	if((el.attr('value') != '') && (el.attr('value') != el.attr('placeholdertext'))) { return false; }
	if(el.attr('value') == el.attr('placeholdertext')) {
		el.attr('value', '');
	} else if(el.attr('value' == '')) {
		el.attr('value', el.attr('placeholdertext'))
	}
	el.toggleClass('placeholder');
}
function clearPlaceholdersOnSubmit() {
	jQuery('form').submit(function(){
		jQuery('input[placeholdertext]').each(function(){
			var _this = jQuery(this);
			if(_this.attr('value') == _this.attr('placeholdertext')) {
				_this.attr('value','');
			}
		});
	});
}

function inputPlaceholders() {
	var els = jQuery('input[type=text][title].placeholder');
	els.each(function(){
		var _this = jQuery(this);
		// Set the placeholdertext attribute to the title
		_this.attr('placeholdertext',_this.attr('title'));
		prepPlaceholder(_this);
		_this.focus(function(){
			togglePlaceholder(_this);
		});
		_this.blur(function(){
			togglePlaceholder(_this);
		});
	});
	clearPlaceholdersOnSubmit();
}

// jQ hoverFix Class
(function($) {
	$.fn.hoverFix = function(classname) {
		var c = classname || 'hover';
		this.hover(function(){
			$(this).addClass(c);
		},
		function(){
			$(this).removeClass(c);
		});
	};
})(jQuery);

jQuery(function($) {
	inputPlaceholders();
	
	$('.nav li:has(ul)').hoverFix();
	$('.nav li:first-child').addClass('first-child');
	$('.nav li li a:has(+ ul)').addClass('has-plus-ul');
	
	$('a.pop[href]').each(function(){
		$($(this).attr('href')).hide();
	});
	$('a.pop[href]').click(function(){
		$($(this).attr('href')).fadeIn('medium');
		$(this).remove();
		return false;
	});
	
	// Nav-bars
	$('.nav-bar').wrap('<div class="nav-bar-wrapper" />');
	// Nav-bar and pipe lists
	$('.nav-bar li:not(:last-child), .pipe li:not(:last-child)').append(' | ');
	
	$('input[type]').each(function(){
		var _this = $(this);
		_this.addClass(_this.attr('type'));
	});
	
	
	$('#newsroom-tabs').tabs();
	var page_url = String(window.location);
	var url_parts = page_url.split('/');
	if (url_parts[url_parts.length - 2] == 'press-releases') {
		$('#newsroom-tabs').tabs('select', 1);
	};

	// Wrap splash in cite
	$('#splash:has(#splash-destination[href])').css('cursor','pointer').click(function(){
		window.location = $(this).find('#splash-destination[href]').attr('href');
	});
		
	if ($('.image-carousel-images li').length > 4) {
		$('.image-carousel').prepend('<a href="#" title="Scroll Forward" class="ir fwd">Scroll Forward</a><a href="#" title="Scroll Back" class="ir back">Scroll Back</a>');
		$('.image-carousel .image-carousel-images').jCarouselLite({
			btnNext: '.image-carousel .fwd',
			btnPrev: '.image-carousel .back',
			circular: true,
			visible: 4,
			scroll: 3
		});
	}
});