// JavaScript Document

//after the video overlay has been populated, run postLoad to set click functions
function postLoad(qStr){
	$(".video_preview_swap").click(function(){
		$("#video_overlay_container").empty().load(qStr + $(this).attr("id"), function(){
		    postLoad(qStr);																	  
	    });
	});															 
	
	// click function to close and empty the overlay
	$("#video_close_button").click(function(){
		$("#video_overlay_container").fadeOut('fast', function(){
			$("#page_dimmer").fadeOut('fast');
			$(this).empty();
		});
		return false;
	});
}



$(document).ready(function(){						   
	//var for cursor position for keyboard controls  
	var cursor = -1;
	/*begin code for animated drop down selection */
	$("body").click(function(){
	   $(".options:visible").hide();
	   $("#quick_result:visible").hide();
	   $("#hov_content:visible").hide();
	   $(document).unbind('keydown');
	   cursor = -1;
	   currentPreview = null;
	});
	
	//custom animated drop down
	$(".search-field-button").click(function(){
	   $(".options:visible").hide();
	   $(this).parent().next(".options").slideDown("fast").children("li").css('color', '#969696');
	   
	   //keyboard control for drop downs
	   cursor = -1;
	   $(document).bind('keydown', function(e){
	       var keyCode = e.keyCode || e.which;
		   var key = {up:38, down:40, enter:13, tab:9};
		   var optionCount = $("#fee").children("li").length;
		   		   
		   switch(keyCode){
		        case key.up:
					if(cursor > 0){
						cursor--;
						e.preventDefault();
					}
					break;
				case key.down:
					if(cursor < optionCount){
						cursor++;
						e.preventDefault();
					}
					break;
				case key.enter:
					$(".options").children('li').eq(cursor).click();
					e.preventDefault();
					break;
				case key.tab:
					$(".options").children('li').eq(cursor).click();
					e.preventDefault();
					break;
		   }

		   $(".options").children('li').css('color', '#969696').css('background-color','#ffffff').eq(cursor).css('color', '#ffffff').css('background-color','#001f3e');
		   
	   });  
	   
	   return false;
	});
	
	$(".options").children().click(function(){
	   $(this).parent().prev().children(".search-field-input").empty();
	   $(this).parent().prev().children(".search-field-input").append($(this).html());
	   var input = "[name="+$(this).parent().attr("id")+"]";
	   $(input).val($(this).attr("id"));
	});
	/*end animated drop down selection */
	
	
	/* speech preview */
	$("body").append("<div id=\"hov_content\"></div>");
    var currentPreview = null;
	$(".speech_preview").bind({
	    mouseenter: function(event){
			if($(this).parents().index() != currentPreview){
				//before loading a new preview, hide and empty any preview currently open
				$("#hov_content:visible").hide();
				$("#hov_content").empty();
				//ajax to speech page and load preview into the hov_content div. ensure position is not off screen.
				var qStr = "/speakers/speech.cfm?SpeechID=";
				$("#hov_content").load(qStr + $(this).attr("id"), function(){
					var contentWidth = parseInt($("#hov_content").children().css('width')) + 30;
					$("#hov_content").css("top", event.pageY + 5);
					if(event.pageX + contentWidth >= $(window).width()){
						$("#hov_content").css("left", event.pageX - contentWidth + 100);
					}
					else{
						$("#hov_content").css("left", event.pageX - 100);
					}
					//$("#debug").html(event.pageX + " | " + event.pageY + " | " + $(window).width() + " | " + contentWidth);
					$("#hov_content").fadeIn('fast');
					settings = {
						  tl: { radius: 15 },
						  tr: { radius: 15 },
						  bl: { radius: 15 },
						  br: { radius: 15 },
						  antiAlias: true,
						  autoPad: true,
						  validTags: ["div"]
					};
					$("#hov_content").children().children().corner(settings);
					$("#hov_content").click(function(){
					    return false;
					});
					$("#close_button").click(function(){
						$("#hov_content").fadeOut('fast');
						currentPreview = null;
					});			
				});
				currentPreview = $(this).parents().index();
			}
		}
	});
    /* end speech preview */
	
	
	/* video dimmer & content overlay */
	$(".video_preview").click(function(){
	    $("#page_dimmer").fadeTo('fast',.7);
		var qStr = "/video/video.cfm?autoplay=true&VideoID=";
		// load external content and fade in the overlay
		$("#video_overlay_container").load(qStr + $(this).attr("id"), function(){
			$(this).fadeIn('fast');
			postLoad(qStr);
        });
	});
    /* end dimmer & content overlay */
	
	/* video dimmer & content overlay */
	$(".news_video_preview").click(function(){
	    $("#page_dimmer").fadeTo('fast',.7);
		var qStr = "/video/news-video.cfm?autoplay=true&NewsID=";
		// load external content and fade in the overlay
		$("#video_overlay_container").load(qStr + $(this).attr("id"), function(){
			$(this).fadeIn('fast');
			postLoad(qStr);
        });
	});
    /* end dimmer & content overlay */
	
	
	/* begin navigation */
	$("#nav").children("ul").children("li").bind({						 
       mouseenter: function(){
		   $(this).children(".dropd").show();
		   var dropWidth = $(this).children(".drop_content").width();
		   var dropOffset = $(this).children(".drop_content").offset();
		   if(dropWidth+dropOffset.left >= $(window).width()){
			  var moveDrop = $(window).width() - dropWidth - 50;
			  $(this).css("left", moveDrop);
		   }
	   },
	   mouseleave: function(){
		   $(this).children(".dropd").hide();
	   }
	   
	});
	
	 /* added rouned corners to drop down menus */
	settings = {
          tl: { radius: 0 },
          tr: { radius: 0 },
          bl: { radius: 15 },
          br: { radius: 15 },
          antiAlias: true,
          autoPad: true,
          validTags: ["div"]
    };
    $(".drop_content").corner(settings);
	$(".dropd").hide().css("visibility", "visible");
	/*  end rounded corners script */
	
	
	/* begin quick search */
	var currentItem = null;
	$("#keyword").keyup(function(e){
    	var keyCode = e.keyCode || e.which;
		var searchTerm = $("#keyword").val();
		if (keyCode==38 || keyCode==40 || keyCode==13 || keyCode==9){
			//keyboard control for drop downs
			$(".quick_link").children('a').css('color', '#969696')

				var key = {up:38, down:40, enter:13, tab:9};
				var optionCount = $(".quick_link").length;
				
			    switch(keyCode){
					case key.up:
						if(cursor > 0){
							cursor--;
							e.preventDefault();
						}
						break;
					case key.down:
						if(cursor < optionCount){
							cursor++;
							e.preventDefault();
						}
						break;
					case key.enter:
						$(".quick_link").children('a').eq(cursor).click();
						e.preventDefault();
						break;
					case key.tab:
						$(".quick_link").children('a').eq(cursor).click();
						e.preventDefault();
						break;
			    }
	
			    $(".quick_link").children('a').css('color', '#969696').css('background-color','#ffffff').eq(cursor).css('color', '#ffffff').css('background-color','#001f3e');
				currentItem = $(".quick_link").children('a').eq(cursor);
		}
		else {
			if(searchTerm.length >= 3){				
				var regExTerm = new RegExp(searchTerm,"ig");
				var urlTerm = searchTerm.replace(" ", "%20");
				$("#quick_result").hide().empty().load("/inc/quick-search-list.cfm?q="+urlTerm, function(){
					$(this).children(".quick_link").children("a").append(function(){
						var termMatch = $(this).text().match(regExTerm);
						var boldTerms = $(this).text().replace(termMatch, "<b>"+termMatch+"</b>");
						$(this).empty();
						return boldTerms;
					});
					if($(this).text() != ""){
						$(this).prepend("<div class='quick-result-hdr'><img src='/images/buttons/quick-links-close.gif' /></div>").show();
					}
				});
			}
		}
	}).click(function(){cursor=-1}).keydown(function(e){
		var keyCode = e.keyCode || e.which;
		if((keyCode==13 || keyCode==9) && currentItem){
			e.preventDefault();
			window.location = currentItem.attr("href");
		}		
	});
	/* end quick search */		
	
	
	/* FAQ navigation hide/show */
    $("#contentinner").children(".list1").children("li").children("a").click(function(){	
	    $(this).next(".faqanswer").slideToggle();
		return false;
    });
	$(".faqanswer").children("p").children("a").click(function(){
	    $(this).parent("p").parent(".faqanswer").slideToggle();
		return false;
    });
	/* end FAQ navigation hide/show */
	
	
	/* fade quotes 
	$('.quote').css('position', 'absolute').css('width', '200px');
	(function fadeQuotes() {
		var quoteCount = $('.quote').length;
		var quote = $('.quote:visible')
		if(quoteCount>1){
		quote.fadeOut(function(){
			var i;
			if (quote.index() >= quoteCount) {
				i = 0;
			}
			else {
				i = quote.index();
			}
			
			$('.quote').eq(i).fadeIn();
		});
		
		setTimeout(fadeQuotes, 7000);
		}
	})();*/
	
	/* quote navigation  (this is a great example of how NOT to write DRY code.) */
	var loop = 0;
	$(".quoteblock2").hide().eq(0).show().parent().css({height:$(".quoteblock2").eq(0).height()});
	$(".next").click(function(){
	    if(loop < $(".quoteblock2").length-1){
	        $(".quoteblock2").eq(loop).hide("slide");
			loop++;
			height = $(".quoteblock2").eq(loop).height();
			$(".quoteblock2").parent().animate({height: height});
			$(".quoteblock2").eq(loop).show("slide", { direction: "right" }, 500);
		}
		else{
		    $(".quoteblock2").eq(loop).hide("slide");
			loop = 0;
			height = $(".quoteblock2").eq(loop).height();
			$(".quoteblock2").parent().animate({height: height});
			$(".quoteblock2").eq(loop).show("slide",{direction: "right"} , 500);
		}
    });
	$(".previous").click(function(){
	    if(loop > 0){
		    $(".quoteblock2").eq(loop).hide("slide",{ direction: "right"}, 500);
			loop--;
			height = $(".quoteblock2").eq(loop).height();
			$(".quoteblock2").parent().animate({height: height});
			$(".quoteblock2").eq(loop).show("slide");
		}
		else{
		    $(".quoteblock2").eq(loop).hide("slide",{ direction: "right"}, 500);
			loop = $(".quoteblock2").length-1;
			height = $(".quoteblock2").eq(loop).height();
			$(".quoteblock2").parent().animate({height: height});
			$(".quoteblock2").eq(loop).show("slide");
		}
    });
	/* end quote navigation */
	
	/* news more/less show */
	$("#seemorenews").children("li").children("a").click(function(){
		$("#morenewsvideorow5").show();
		$(this).parent("li").parent("ul").hide();
		$("#lessnews").show().find("a").click(function(){
			$("#morenewsvideorow5").hide();
			$("#seemorenews").show();
			$("#lessnews").hide();									 
						
			return false;
		});
		return false;
	});
	
	$(".hideshow").children("li").children("a").click(function(){
		$("#morerows").show();
		$(this).parent("li").parent("ul").hide();
		
		return false;
	});
	
	$(".hideshow2").children("li").children("a").click(function(){
		$("#morerows2").show();
		$(this).parent("li").parent("ul").hide();
		
		return false;
	});
	
});












