$(document).ready(function(){
	
	// ** Hide Stuff that we need to hide
	$("#expanding-list > li > div, div#quick-menu, ul#search-results, #ringtones-list > li > ul").hide();
	
	
	// ** Back Button on top
	$("#back-btn").click(function(){history.go(-1); return false;})


	/* -----------
	* Top Menu Events
	* ------------ 
	*/
	// ** Make Top Menu button show and hide
	$("a#quick-menu-btn").click(function(){
		$(this).toggleClass("open");
		$("div#overlay").fadeToggle();
		$("div#quick-menu").slideToggle();
		return false;
	});
	
	
	// ** Clear Text On Search Field
	$(function(){
		$("input#quick-search")
		.val('Search')
		.focus(function(){$(this).val('');})
		.blur(function(){($(this).val() === '')? $(this).val('Search') : null; });
	});
	
	
	// ** X button to clear text
	$('#clear-btn').click(function(){
		$('#quick-search').val('');
		clear_results();
	});
	
	
	// ** Hide menu when input search is in
	$("input#quick-search").keyup(function () {
		var length = $(this).val().length;
		if(length > 2) {
			$("ul#quick-nav").fadeOut();
			$.post(BASE_URI + 'index.php/usafsearch/ajax/search', {searchterm : $(this).val(), limit : 3}, function(data){
				$('ul#search-results').html(data);
			});
			$("ul#search-results").animate({opacity:1.0},250).fadeIn();
		}
		if(length === 0) {
			clear_results();
		}
	});
	
	// ** Expanding and Collapsing lists
	$("#expanding-list > li > h4, #ringtones-list > li > h4").click(function(){
		$(this).children('span').toggleClass("open");
		
		if (!$(this).next().is(":visible") && typeof(_gaq) == 'object') {
			var urlToTrack = $('#content  > .title').text().toLowerCase()+"/"+$(this).children('span').text().toLowerCase().replace(" /g","-");
			_gaq.push(['_trackPageview', urlToTrack]);
			
		}
		
		$(this).next().slideToggle();		
		
		return false;
	});
	
	// ** Call Tabs function
	$("#tab-control > li > a").click(function(){
		switch_tabs($(this));
		return false;
	})
	
	// ** Show Default tab
	switch_tabs($('.defaulttab'));
	
	$('.featured .play-btn').css('left', $(document).width()/2-21 ).css('top', $('.featured').height()/2-21);
	
	
});	// end (document).ready





$(window).load(function() {
	
	// ** Load function for sliders
	featureSlider();
	
	// ** When the window is resized, resize the features and the span next to the video thumbnails
	$(window).resize(function(){
		delay(function(){
			$('ul.link-preview span, ul.link-preview .prev-desc').width($(document).width()-194);
			$('#slider-mask, #features li').width($('#main-slider').width());
			$('#slider-mask').height($('#features li img').height() + $('#features li .feat-caption').height());
			$('.featured .play-btn').css('left', $(document).width()/2-21 ).css('top', $('.featured').height()/2-21);
			bmtThumbs();
		}, 500);
	});
	
	bmtThumbs();
	
	// resize span next to image lists
	$('ul.link-preview span, ul.link-preview .prev-desc').width($(document).width()-194);
	//$('.bmt-img-thumbs').width($(window).width());
	
});


/* 
* ----------------
* Cufon
* Switch custom font
* ----------------
*/
Cufon.set('fontFamily', 'Din').replace
	(".feat-caption h3")
	('#sub-feature h3')
	("#main-nav a")
	("ul#expanding-list h4 span")
	("h2.title")
	("h2.job-title")
	("h2.title-alt")
	("span.career-cat")
	('ignoreClass', '.skipcufon');





/* 
* ----------------
* FUNCTIONS!!
* ----------------
*/

	
/* 
* ----------------
* featureSlider()
* This function gets the items in the DOM
* Dependency: newslider();
* ----------------
*/

function featureSlider() {
	var speed = 7000;
	var feats = $('#features li');

	$('#slider-mask, #features li').width($('#main-slider').width());
	$('#features').width($('#main-slider').width() * feats.length);
	$('#slider-mask').height($('#features li img').height() + $('#features li .feat-caption').height());
	

	var run = setInterval('newsslider(0)', speed);  

	$('#features li:first').addClass('selected');
	
	// Count how many there are and put an unique number to them
	feats.each(function(index){
		$(this).removeClass('feat1'); //feat1 is hardcoded :(
		$(this).addClass('feat'+ index);
	});
	
	// determine if there is more than 1 feature
	if(feats.length > 1) {
		// Add link bullets container
		var featControl = $('<ul id="feat-links"></ul>');
		$("#main-slider").append(featControl);
		// blackberry does not recognize length?

		
		// Add link bullets below features
		for(var i = 0; i < feats.length; i++ ){
			featControl.append('<li><a href="#" class="feat'+ i +'">&nbsp;</a></li>');
			$('#feat-links li:first-child a').addClass('selected');
		}
	
		// Center #feat-links based upon the width of the total li's width
		$('#feat-links li:last-child').css('margin-right', 0);
		var liw = $('#feat-links li').outerWidth(true);
		var fw = liw * feats.length;
		$('#feat-links').css('width', fw);
	
	
	
		// Switch Feature on click
		$('#feat-links a').live('click', function(){
			if($(this).hasClass('selected')) return false;
			clearInterval(run);
			jumpto('.' + $(this).attr('class'));
			run = '';
			run = setInterval('newsslider(0)', speed);
			$('#feat-links a').removeClass('selected');
			$(this).addClass('selected');
			return false;
		});
	}
}


/* 
* ----------------
* newsslider(prev);
* This function gets the items in the DOM.
* newsslider(0);
* Dependency: scrollTo
* ----------------
*/
function newsslider(prev) {
	
	//Get the current selected item (with selected class), if none was found, get the first item
	var current_image = $('#features li.selected').length ? $('#features li.selected') : $('#features li:first');
	//if prev is set to 1 (previous item)
	if (prev) {
		//Get previous sibling
		var next_image = (current_image.prev().length) ? current_image.prev() : $('#features li:last');
		//if prev is set to 0 (next item)
	} else {
		//Get next sibling
		var next_image = (current_image.next().length) ? current_image.next() : $('#features li:first');
	}
	
	//clear the selected class
	$('#features li').removeClass('selected');
	//reassign the selected class to current items
	next_image.addClass('selected');
		
	//Scroll the items
	if ($('#slider-mask').length) {
		$('#slider-mask').scrollTo(next_image, 800);
		active_feat();
		clearInterval(this); //experimental?
	}
}



/* 
* ----------------
* jumpto(item)
* Feature Slider Jump To Specific Feature
* jump('.' + $(this).attr('rel')) or jump(feat1)
* Dependency: featureSlider();
* ----------------
*/
function jumpto(item) {
	$('#features li').removeClass('selected');
	$('#slider-mask').scrollTo(item, 800);
	$('#features li').addClass('selected');
}




/* 
* ----------------
* switch_tabs(obj)
* This makes the tabs in clients switch
* switch_tabs($('.defaulttab'));
* Dependency: none
* ----------------
*/
function switch_tabs(obj) {
	$(".tab-content").hide();
	$("#tab-control > li > a").removeClass("selected");
	var id = obj.attr("rel");
	var urlToUse = '/career/'+jQuery.trim($('.job-title').text())+"/"+jQuery.trim(obj.text());
	urlToUse = urlToUse.toLowerCase().replace(/ /g,"-");
	$('#'+id).show();
	obj.addClass("selected");
}





/* 
* ----------------
* switch_tabs(obj)
* This makes the tabs in clients switch
* switch_tabs($('.defaulttab'));
* Dependency: none
* ----------------
*/
function clear_results() {
	$("ul#search-results").fadeOut();
	$("ul#quick-nav").animate({opacity:1.0},500).fadeIn();
}




/* 
* ----------------
* active_feat()
* This updates the selected bullet
* Dependency: none
* ----------------
*/
function active_feat() {
	var fi = $('#features li.selected').index(); //get index value of li.selected (0)
	var fr = '#feat-links a.'+$('#feat-links a:eq('+ fi +')').attr('class'); // get value of rel (feat0)
	$('#feat-links a').removeClass('selected');
	$(fr).addClass('selected');
}



/* 
* ----------------
* bmtThumbs()
* Adusts the width of the ul to be centered
* Dependency: none
* ----------------
*/
function bmtThumbs() {

	var wWidth = $(window).width();

	if(wWidth >= 320 && wWidth < 480) {
		$('.bmt-img-thumbs').width(300);
	}

	if(wWidth >= 480) {
		$('.bmt-img-thumbs').width(400);
	}
}





/* 
* ----------------
* delay
* This is used to delay the resize events
* 	delay(function(){
*		$('ul.link-preview span').width($(document).width()-194);
*		$('#slider-mask, #features li').width($('#main-slider').width());
* 	}, 500);
* Dependency: none
* ----------------
*/
var delay = (function(){
	var timer = 0;
	return function(callback, ms){
		clearTimeout (timer);
		timer = setTimeout(callback, ms);
	};
})();
