// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

	$(function() {
		$('#stories').before('<ul id="nav-feature">').cycle({
			fx:		'fade',
			speed:	'slow',
			timeout: 0,
			pager:	'#nav-feature',
			cleartype: true,
			pagerAnchorBuilder: function(idx, slide) { 
				return '<li id="'+jQuery(slide).attr("class")+'"><a href="#">'+jQuery(slide).find("class").html()+'</a></li>';
			}
		});
		$('#feature').show();
	});

	function create_tab(for_element) {
		var tabContainers = $('div.nav-'+for_element+' div.tabs > div.feature-img');
				
		
		$('div.nav-'+for_element+' ul.tabNavigation li').click(function () {
			var target = $(this).attr('id').split('_');
			tabContainers.hide();
			tabContainers.filter(target[0]).show();
			$('div.nav-'+for_element+' ul.tabNavigation li').removeClass('selected');
			$(this).addClass('selected');
			return false;
		}).filter(':first').click();
	}

	function hide_tab(for_element) {
		$('div.nav-'+for_element+' div.tabs > div.feature-img').hide().filter(':first').show();
	}

	function create_tabs() {
		create_tab('stories');
		create_tab('hospital');
		create_tab('regional');
		create_tab('raleigh');
		create_tab('medicine');
		create_tab('nursing');
	}
	
	function hide_tabs() {
		hide_tab('stories');
		hide_tab('hospital');
		hide_tab('regional');
		hide_tab('raleigh');
		hide_tab('medicine');
		hide_tab('nursing');
	}

	function initialize_tabs() {
		hide_tabs();
		create_tabs();
	}
	$(initialize_tabs);

	//============================================
	// Location drop down
	//============================================
	$(document).ready(function(){
				
		$("#location img.arrow").click(function(){ 
			$("span.head_menu").removeClass('active');
			submenu = $(this).parent().parent().find("div.sub_menu");
			if(submenu.css('display')=="block"){
				$(this).parent().removeClass("active");		
				submenu.hide();			
				$(this).attr('src','/images/structure/btn-location-arrow-hover.png');
			}else{
				$(this).parent().addClass("active");	
				submenu.fadeIn();			
				$(this).attr('src','/images/structure/btn-location-arrow-select.png'); 
			}
			$("div.sub_menu:visible").not(submenu).hide();
			$("#location img.arrow").not(this).attr('src','/images/structure/btn-location-arrow.png');
		})
		.mouseover(function(){ $(this).attr('src','/images/structure/btn-location-arrow-hover.png'); })
		.mouseout(function(){ 
			if($(this).parent().parent().find("div.sub_menu").css('display')!="block"){
				$(this).attr('src','/images/structure/btn-location-arrow.png');
			}else{
				$(this).attr('src','/images/structure/btn-location-arrow-select.png');
			}
		});

		$("#location span.head_menu").mouseover(function(){ $(this).addClass('over')})
			.mouseout(function(){ $(this).removeClass('over') });

		$("#location div.sub_menu").mouseover(function(){ $(this).fadeIn(); })
			.blur(function(){ 
			$(this).hide();
			$("span.head_menu").removeClass('active');
		});		

		$(document).click(function(event){		
			var target = $(event.target);
			if (target.parents("#location").length == 0) {				
				$("#location span.head_menu").removeClass('active');
				$("#location div.sub_menu").hide();
				$("#location img.arrow").attr('src','/images/structure/btn-location-arrow.png');
			}
		});

		$("a.expand").toggle(
			function () {
				$(this).css({"background":"url(/images/structure/ico-minus.png) 195px center no-repeat"});
			},
			function () {
				$(this).css({"background":"url(/images/structure/ico-plus.png) 195px center no-repeat"});
		});

		$("ul.location-headlines").hide();

		$("a.expand").click(function(event){
			$(this).next().slideToggle();
		});
		
		// 
		// $(".tabNavigation li").click(function(){
		//   window.location=$(this).find("a.link").attr("href"); return false; 
		// });
		$('ul.tabNavigation li:not(.selected)').hover(function () {
			$(this).addClass('hovered');
		},function(){ $(this).removeClass('hovered');})
		$('ul.tabNavigation li a.more').click(function(e){e.stopPropagation();});
	});
