$(document).ready(function(){

//top nav
//set all ul to same height
$('#top-nav ul').css('height',$('#top-nav-ul-wrapper').innerHeight());
$('#top-nav').hide();
$('#site-index a').click(function() {
	$('#top-nav').slideToggle();
});

//search focus
$('input.searchbox').focus(function() {
	$(this).val('');
});
$('input.searchbox').blur(function() {
	if($(this).val().length<=1) {											 
		$(this).val('SEARCH');
	}
});

//slideshow
var home_slideshow = $('#home-slideshow');
var home_slideshow_wrapper = $('#home-slideshow-wrapper');
//slideshow settings
home_slideshow.jqFancyTransitions({
	navigation: true, 
	links: true,
	titleOpacity: 0,
	width: 1440, 
	height: 442, 
	position: 'top', 
	direction: 'fountain', 
	delay: 6000
});
//add lightbox effect to slideshow
home_slideshow.find('a.ft-home-slideshow').colorbox({'width':'844'});
//adjust controls position
$('#ft-prev-home-slideshow').css({'top':191,'left':200, 'opacity':1});
$('#ft-next-home-slideshow').css({'top':191,'left':'auto', 'right':215, 'opacity':1});
$('#ft-buttons-home-slideshow').css('width','auto');
$('#ft-buttons-home-slideshow a.ft-button-home-slideshow').css('padding',0);
//adjust width of slideshow to match window width
var ww = $('#content-header').width();
resizeHomeSlideshow(ww);
$(window).resize(function() {
	var ww = $(window).width();
	resizeHomeSlideshow(ww);
});
function resizeHomeSlideshow(ww) {
	var slide_width = 1440;
	var min_view_width = 986; //same as in the css
	if(ww <= slide_width && ww > min_view_width) {
		var left = (ww-slide_width)/2;
		home_slideshow_wrapper.css('margin-left',left);
	}
	if(ww >= slide_width) {
		var left = (ww-slide_width)/2;
		home_slideshow_wrapper.css('margin-left',left);
	}
	if(ww <= min_view_width) {
		var left = (min_view_width-slide_width)/2;
		home_slideshow_wrapper.css('margin-left',left);
	}
}

//tabs on the home page
$('#home-tabs').each(function(){
	var t = $(this);
	var tabs_id = t.attr('id');
	var tabs = t.find('ul.tab-titles li a');
	var content = t.find('div.tab-content');
	var controls = t.find('div.controls');
	var active_tab = '';
	var num_shown = 3;
	var $is_animating = false;
	var li = content.find('li').first();
	var item_width = li.width()+parseFloat(li.css('margin-right'))+parseFloat(li.css('padding-right'))+parseFloat(li.css('border-right-width')); //width of each item
	var btn_next = t.find('div.tab_arrow_next');
	var btn_prev = t.find('div.tab_arrow_prev');
	tabs.each(function() {
		var tab = $(this);
		var id = findTabId(tab.parent().attr('id'));
		tab.attr('href','#'+tabs_id+'/'+id);
	});
	content.each(function() {
		var c = $(this);
		var ul = c.find('ul');
		var num = c.find('li').length;
		ul.width(num*item_width+10);
		//var id = findTabId(tab.parent().attr('id'));
		//tab.attr('href','#'+tabs_id+'/'+id);
	});
	tabs.click(function(){
		activateTab($(this));
		//return false;
	});
	
	//activate first item
	var first_item = getIdFromUrl();
	if(first_item) {
		first_item = $('#title-'+first_item+' a');
	} else {
		first_item = tabs.first();
	}
	activateTab(first_item);
	
	function deactivateTabs() {
		tabs.removeClass('active');
		tabs.parent().removeClass('active');
	}
	function activateTab(tab) {
		deactivateTabs(); //deactivate all tabs before activating a new one
		var id = findTabId(tab.parent().attr('id'));
		var new_content = $('#tab-'+id);
		var items = new_content.find('ul li');
		var num = items.length - num_shown;
		$show_controls = false;
		if(num > 0 ) {$show_controls = true;}
		//activate tab
		tab.addClass('active');
		tab.parent().addClass('active');
		//move all content down to the bottom
		content.css({'z-index':50});
		//prepare to show new content
		new_content.css({'z-index':100, 'display':'none'});
		//fade in new content while fading out old
		content.not(new_content).fadeOut('normal');
		new_content.fadeIn('normal');
		active_tab = new_content;
		tabControls($show_controls);
	}
	function tabControls($activate) {
		
		if($activate == true) {
			//alert('hi');
			btn_next.click(tabNext);
			btn_prev.click(tabPrev);
			controls.fadeIn('normal');
		} else {
			btn_next.unbind();
			btn_prev.unbind();
			controls.fadeOut('normal');
		}
	}
	function tabNext() {
		if(!$is_animating) {
			var current_pos = parseFloat(active_tab.css('left'));
			var items = active_tab.find('ul li');
			var num = items.length - num_shown;
			var max_pos = -Math.ceil(num*item_width);
			var pos = current_pos-item_width;
			if(pos >= max_pos) {
				active_tab.animate({'left':pos},300, function(){$is_animating = false});
				$is_animating = true;
			}
		}
		return false;
	}
	function tabPrev() {
		if(!$is_animating) {
			var current_pos = parseFloat(active_tab.css('left'));
			var items = active_tab.find('ul li');
			var num = items.length - num_shown;
			var max_pos = 0;
			var pos = current_pos+item_width;
			if(pos <= max_pos) {
				active_tab.animate({'left':pos},300, function(){$is_animating = false});
				$is_animating = true;
			}
		}
		return false;
	}
	function findTabId(tab_name) {
		tab_name = tab_name.split('-');
		tab_name.splice(0,1);
		tab_name = tab_name.join('-');
		return tab_name;
	}
	function getIdFromUrl() {
		var url = document.location.href.split('#');
		if(typeof(url[1]) != 'undefined') {
			url = url[1].split('/');
			if(typeof(url[1]) != 'undefined') {
				id = url[1];
			}
		}
		if(typeof(id) == 'undefined') {
			id = false;
		}
		return id;
	}
});

//content button rollover
$('.content-btns li a').hover(
	function() {$(this).addClass('active');},
	function() {$(this).removeClass('active');}
);

//drop down menus (accordian style)
$('ul.accordion').each(function() {
	//hide all the sub menus first
	$('ul.accordion .submenu').each(function(){
		if(!$(this).hasClass('active')) {
			$(this).hide();
		}
	});
	var dropdowns = $(this).find('li.dropdown a');
	dropdowns.click(function() {											 
		if(!$(this).hasClass('active')) {
			dropdowns.removeClass('active');										 
			dropdowns.next().slideUp();
			$(this).next().slideDown();
			$(this).addClass('active');
		}
	});
});

//make sidebar full length
var article_height = $('#article-content').outerHeight(true);
var sidebar_top = Math.abs(parseFloat($('#right-sidebar').css('top')));
$('#right-sidebar-content').height(article_height+sidebar_top+5);


$('#article .img').each(function(){
	var t = $(this);
	var pos = t.position();
	var title = t.attr('title');
	t.attr('title','');
	var el = $('<div class="caption">'+title+'</div>');
	t.load(function() {
		var h = t.outerHeight();
		var w = t.outerWidth();
		el.width(w);
		el.css({'top':pos.top+h+10, 'left':pos.left});
		$('#article-content').append(el);
	});
});

//rollover images
$('.rollover').each(function() {
	var is_animating = true;
	$(this).css({'display':'block','position':'relative'});
	//bottom img
	var img = $(this).find('img');
	//top img
	var top_img = $('<img>');
	var src = img.attr('src');
	var new_src = src.split(".");
	new_src = new_src[0]+'_over.'+new_src[1];
	//preload rollover images
	(new Image()).src=new_src;
	top_img.attr('src',new_src);
	
	//setup default css
	img.css({'display':'block','position':'relative','z-index':5});
	top_img.css({'position':'absolute','z-index':8,'opacity':0,'top':0,'left':0});
	
	//add rollover img to a tag
	$(this).append(top_img);
	
	//on mouse over add _over to the src before the extension (ex. "btn.jpg" becoms "btn_over.jpg" on mouseover)
	$(this).mouseover(function() {
		is_animating = true;
		top_img.stop().animate({opacity:1});
	});
	
	//revert back to original image on mouse out
	$(this).mouseout(function() {
		top_img.stop().animate({opacity:0});
	});
});
//roll overs with out the fade
$('.rollover_simple').each(function() {
	//bottom img
	var img = $(this).find('img');
	//top img
	//var top_img = $('<img>');
	var src = img.attr('src');
	var new_src = src.split(".");
	new_src = new_src[0]+'_over.'+new_src[1];
	//preload rollover images
	(new Image()).src=new_src;
	
	//on mouse over add _over to the src before the extension (ex. "btn.jpg" becoms "btn_over.jpg" on mouseover)
	$(this).mouseover(function() {
		img.attr('src',new_src);
	});
	
	//revert back to original image on mouse out
	$(this).mouseout(function() {
		img.attr('src',src);
	});
});

function randomFromTo(from, to){
	 return Math.floor(Math.random() * (to - from + 1) + from);
}

});
