$(document).ready(function(){

	// =====================================================================================
	// App wide
	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  // Rollovers
  $("a.rollover").hover(
    function() {
      curr = $(this).find("img").attr("src");
      overlen = curr.length;
      over = curr.substr(0, overlen-4);
      over = over+'_on.png';
      $(this).find("img").attr({ src: over});
    },
    function() {
      $(this).find("img").attr({ src: curr});
    }
  )
	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  // Rollovers - Preload images
  $("a.rollover").find("img").each(function(i){
    temp = this.src;
    prelen = temp.length;
    pre = temp.substr(0, prelen-4);
    pre = pre+'_on.png';
    preload_image_object = new Image();
    preload_image_object.src = pre;
  });


	$('#header').hover(
		function () { 
			$(this).css({ 'background-image':'url(/img/headerLogo-over.png)' }); 
		}, 
		function () { 
			$(this).css({ 'background-image':'url(/img/headerLogo.png)' }); 
		}
	);

  $("#headerLink").click(function(){
		var link = '/';
		window.location=link;
  });

	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	// Navbar
	$('#navbar table tr td').click(
		function () { 
			var link = $(this).children().attr("href");
			if (link) { window.location=link; }
		}
	);

	$('#navbar table tr td.navItem').hover(
		function () { 
			var section_nav_id = this.id;
			$(this).removeClass('navItemDefaultState'); 
			$(this).addClass(section_nav_id+'_Hover here'); 
			$(this).css({ cursor:"pointer" }); 
		}, 
		function () { 
			var section_nav_id = this.id;
			$(this).addClass('navItemDefaultState'); 
			$(this).removeClass(section_nav_id+'_Hover here'); 
		}
	);
	

	$('#navbar table tr td.home').hover(
		function () { 
			var section_nav_id = this.id;
			$(this).removeClass(section_nav_id+'_Active'); 
			$(this).addClass(section_nav_id+'_Hover here'); 
			$(this).css({ cursor:"pointer" }); 
		}, 
		function () { 
			var section_nav_id = this.id;
			$(this).addClass(section_nav_id+'_Active'); 
			$(this).removeClass(section_nav_id+'_Hover here'); 
		}
	);



	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	// Font size changer
  // Reset Font Size
  var originalFontSize = $('html').css('font-size');
  var originalFontSize = parseFloat(originalFontSize, 10);
	$('a#defaultText').addClass('textResizerActive'); 
  $("#defaultText").click(function(){
  	$('html').css('font-size', originalFontSize);
		$('div#textResizer a').removeClass('textResizerActive');
		$(this).addClass('textResizerActive'); 
    return false;
  });
  // Medium Font Size
  $("#mediumText").click(function(){
    var newFontSize = originalFontSize*1.2;
    $('html').css('font-size', newFontSize);
		$('div#textResizer a').removeClass('textResizerActive');
		$(this).addClass('textResizerActive'); 
    return false;
  });
  // Large Font Size
  $("#largeText").click(function(){
    var newFontSize = originalFontSize*1.5;
    $('html').css('font-size', newFontSize);
		$('div#textResizer a').removeClass('textResizerActive');
		$(this).addClass('textResizerActive'); 
    return false;
  });
  // Decrease Font Size
  $(".decreaseFont").click(function(){
    var currentFontSize = $('html').css('font-size');
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum*0.8;
    $('html').css('font-size', newFontSize);
    return false;
  });


	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	// Article Tabs
	// Article tab navigation via links
	var $article_tabs = $('ul#articleNav').tabs({
		fx: { opacity: "toggle" } 
	});
	$('div.article a.tab1').click(function() {
	    $article_tabs.tabs('select', 0);
	    return false;
	});
	$('div.article a.tab2').click(function() {
	    $article_tabs.tabs('select', 1);
	    return false;
	});
	$('div.article a.tab3').click(function() {
	    $article_tabs.tabs('select', 2);
	    return false;
	});
	$('div.article a.tab4').click(function() {
	    $article_tabs.tabs('select', 3);
	    return false;
	});
	$('div.article a.tab5').click(function() {
	    $article_tabs.tabs('select', 4);
	    return false;
	});

	
	// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	// Videos
	// Reset video icons to default state on page load
	resetVideoIcons();
	// Background hover states for video list items
	$('ul#videoNav li div.videoLoadLink').hover(
		function () { 
			$(this).addClass('videoLoadLink_Hover'); 
		}, 
		function () { 
			$(this).removeClass('videoLoadLink_Hover'); 
		}
	);
	// Handle video loading (takes id of PARENT EMELENT formatted like somethingBar_2 and loads that video by exrtacting id from end.. i.e. '2')
	$(".videoLoadLink").click(function(){
		// Get Item ID
		var id_data = $(this).parent().attr("id");
		var id_data_array = id_data.split("_");
		var item_id = id_data_array[1];
		// Run the load video function
		loadVideo(item_id);
		return false;
	});
	$("a#videoPlayer img").click(function(){
		// Run the load video function
		loadVideo(0);
		$('ul#videoNav li#videoNav_0 div.videoLoadLink').addClass('videoLoadLink_Active'); 
		return false;
	});
	
	
	
	// FUNCTION to set all video icons in place
	function resetVideoIcons() {
		$('ul#videoNav li div.videoLoadLink').each(function() {
			var id_data = $(this).parent().attr("id");
			var id_data_array = id_data.split("_");
			var item_id = id_data_array[1];
			$('ul#videoNav li#videoNav_'+item_id+' div.videoLoadLink').css({ 'background-image':'url(/img/videoIcon_'+item_id+'.jpg)' }); 
		});
	}
	// FUNCTION to Handle inserting videos and data into DOM
	function loadVideo(item_id) {
		// Fix Video Icons
		resetVideoIcons();
		// Load this video
		video_id = parseFloat(item_id);
		flowplayer().play(video_id);
		// Load active states of video icons in nav list
		$('ul#videoNav li#videoNav_'+item_id+' div.videoLoadLink').css({ 'background-image':'url(/img/videoIcon_'+item_id+'_Active.jpg)' }); 
		// Add class to active video list item
		$('ul#videoNav li div.videoLoadLink').removeClass('videoLoadLink_Active'); 
		$('ul#videoNav li#videoNav_'+item_id+' div.videoLoadLink').addClass('videoLoadLink_Active'); 
		// load data vars and insert into layout		
		var video_title = $('li#videoNav_'+item_id+' h4').html();
		$("div#currentVideoInfo h1").html(video_title);
		var video_description = $('li#videoNav_'+item_id+' p span').html();
		$("div#currentVideoInfo p").html(video_description);
		var video_quote = $('li#videoNav_'+item_id+' blockquote').html();
		$("div#currentVideoInfo blockquote span").html(video_quote);
		// Scroll to top of page
		$('html, body').animate({scrollTop:0}, 'slow');
	}


});
