var obj;
if(typeof timeline_content() == 'string') {
	obj = jQuery.parseJSON(timeline_content());
}

function doLayout() {
	var years = Array("2010","2011","2012","2013","2014","2015","2016","2017","2018");
	
	var html = "blah";
	for (var idx = 0; idx < years.length; idx++) {
		var year = years[idx];
		doYearLayout(year);
	}
}


$.disabledTabs = Array();
function deactivateTab(year) {
	$.disabledTabs.push(year);
	switch (year) {
		case "2010":
			$('#jv-timeline').tabs("disable" , 0)
			break;
		case "2011":
			$('#jv-timeline').tabs("disable" , 1)
			break;
		case "2012":
			$('#jv-timeline').tabs("disable" , 2)
			break;
		case "2013":
			$('#jv-timeline').tabs("disable" , 3)
			break;
		case "2014":
			$('#jv-timeline').tabs("disable" , 4)
			break;
		case "2015":
			$('#jv-timeline').tabs("disable" , 5)
			break;
		case "2016":
			$('#jv-timeline').tabs("disable" , 6)
			break;
		case "2017":
			$('#jv-timeline').tabs("disable" , 7)
			break;
		case "2018":
			$('#jv-timeline').tabs("disable" , 8)
			break;
	}
}

function doYearLayout(year) {
	
	var blurbHtml = "";
	var toolsHtml = "";
	var libraryHtml = "";
	
	
	if (isEmpty(obj[year])) {
		deactivateTab(year);
		return;
	}
	
	var dates = Array();
	var dateHTMLBlocks = {};
	
	
	for (var idx in obj[year]) {
		$.highlightsHtml = "";
		if (obj[year].highlights != undefined) {
			$.highlightsHtml = "<div class=\"tip\" id=\"tips-" + year + "\" >";
			$.each(obj[year].highlights, function(key,val) { 
				$.highlightsHtml += "<div class=\"icon-arrow\"></div><div class=\"tip-text\">" + val + "</div><div class=\"clearfix\">";
				
			});
			
			$.highlightsHtml += "<div class='tip-more'>More...</div></div>";
		}
		
		var yearObj = obj[year][idx];
		
		if (yearObj != undefined && yearObj.provision != undefined && yearObj.provision != null && yearObj.provision != "") {
			
			var dateHtml = ""; 
			if (dates.indexOf(yearObj.date) == -1) {
				//dateHtml += "<div class=\"date\">";
				dateHtml += "<h2>" + yearObj.date + "</h2>";
				
				if (yearObj.provision != "") {
					dateHtml += "<div id=\"date-link-" + year + "-" + idx + "\" class=\"date-link\" rel=\"" + year + "-" + idx + "\">";
					dateHtml += "<div class=\"icon-arrow\"></div><div class=\"provision\">" + yearObj.provision + "</div><div class=\"clearfix\"></div>";
					dateHtml += "</div>";
					dates.push(yearObj.date);
					dateHTMLBlocks[yearObj.date] = dateHtml;
					
				}
				

			} else {
				if (yearObj.provision != "") {
					dateHtml += "<div id=\"date-link-" + year + "-" + idx + "\" class=\"date-link\" rel=\"" + year + "-" + idx + "\">";
					dateHtml += "<div class=\"icon-arrow\"></div><div class=\"provision\">" + yearObj.provision + "</div><div class=\"clearfix\"></div>";
					dateHtml += "</div>";
					dateHTMLBlocks[yearObj.date] += dateHtml;
				}
				
			}
			
			//dateHtml += "</div>";
		
		blurbHtml += "<div id=\"blurb-" + year + "-" + idx + "\" class=\"blurb-content\">" + yearObj.blurb;
			if (yearObj.tools != undefined) {
				$.incTools = false;
				$.toolsHtml = "<div class=\"tools\"><div class=\"icon-tools\"></div>";
				$.each(yearObj.tools, function(key,val) { 
					$.toolsHtml += "<p><a href=\"" + val + "\">" + key + "</a></p>";
					$.incTools = true;
				});
				
				$.toolsHtml += "</div>";
				
				if ($.incTools)
					blurbHtml += $.toolsHtml;
			}
			
			if (yearObj.library != undefined) {
				$.incLibrary = false;
				$.libraryHtml = "<div class=\"library\"><div class=\"icon-library\"></div>";
				$.each(yearObj.library, function(key,val) { 
					$.libraryHtml += "<p><a href=\"" + val + "\">" + key + "</a></p>";
					$.incLibrary = true;
				});
				
				$.libraryHtml += "</div>";
				
				if ($.incLibrary)
					blurbHtml += $.libraryHtml;
			}
			
			blurbHtml += "</div>";
			blurbHtml += "</div>";
			
			$('#tabs-' + year).children(".content-right-col").children(".blurb").html(blurbHtml);
			$('#tips').append($.highlightsHtml);
			$('#blurb-' + year + "-1").show();
			$('#blurb-' + year + "-1").show();
			
			
		} 
		
	}		
	
	var dateStr = "";

	for (var idx in dateHTMLBlocks) {
		dateStr += "<div class=\"date\">" + dateHTMLBlocks[idx] + "</div>";
	}
	$('#tabs-' + year).children(".content-left-col").children(".content-blue").children(".content-left-body").html(dateStr);
	addDateLinkListeners(year + "-1");	
	
}
$.selectedDateLink = 1;
$.selectedDateHeader = null;
function addDateLinkListeners(elId) {
	var dateIds = Array("tabs-2010","tabs-2011","tabs-2012","tabs-2013","tabs-2014","tabs-2015","tabs-2016","tabs-2017","tabs-2018");
	var year = elId.split("-")[0];
	var tabId = "#tabs-" + year;	
	$("#date-link-" + elId).unbind('click mouseout mouseover');
	$("#date-link-" + elId).parent().children('h2').animate({"opacity": "1"}, "fast");
	$("#date-link-" + elId).animate({"opacity": "1"}, "fast");
	$.selectedDateHeader = $("#date-link-" + elId).parent().children('h2').html();
	$("#blurb-" + elId).show();
	if ($.browser.msie != true)
		$("#blurb-" + elId).stop().animate({"opacity": "1"}, "slow");
	$(tabId).children(".content-left-col").children(".content-blue").children(".content-left-body").children(".date").children(".date-link").each(
		function(){
			if ($(this).attr('id') != ("date-link-" + elId)) {
				$(this).stop().animate({"opacity": ".75"}, "fast");
				if ($.selectedDateHeader != $(this).parent().children('h2').html()) {
					$(this).parent().children('h2').stop().animate({"opacity": ".75"}, "fast");
				}
				if ($.browser.msie != true) {
					$("#blurb-" + $(this).attr('rel')).stop().animate({"opacity": "0"}, 0, function() {$(this).hide();} );
				} else {
					$("#blurb-" + $(this).attr('rel')).hide();
				}
				$(this)
				.unbind('click')
				.bind('click', function() {
					//alert($("#date-link-" + year + "-" + idx).click());
					addDateLinkListeners($(this).attr('rel'));
					//$.scrollTo($("#jv-timeline"), 0);
					
				});
				
				
				$(this).mouseover(function() {
					$(this).stop().animate({"opacity": "1"}, "fast");
					if ($.selectedDateHeader != $(this).parent().children('h2').html()) {
						$(this).parent().children('h2').stop().animate({"opacity": "1"}, "fast");
					}
				});
				$(this).mouseout(function() {
					$(this).stop().animate({"opacity": ".75"}, "fast");
					if ($.selectedDateHeader != $(this).parent().children('h2').html()) {
						$(this).parent().children('h2').stop().animate({"opacity": ".75"}, "fast");
					}
				});
			}
		}
	);
}
$.maxHeight = 0;
$.currentTab = 0;
if ($.browser.msie == true && $.browser.version.substr(0, 1) <= 7)
	$.spacer = 60;
else
	$.spacer = 20;


$(function(){
	$('#jv-timeline').tabs({
		collapsible: true,  
		selected: -1,  
		select: function (event, ui) {
			$("#" + ui.panel.id).hide();
			var year = $(ui.panel).attr("id").split("tabs-")[1];	
			addDateLinkListeners(year + "-1");
			$.currentTab = $("#jv-timeline").tabs('option', 'selected');
			$("#jv-timeline").children(".ui-tabs-panel" ).each( function(){
				$(this).animate({"opacity": "0"}, -1);
			});
		},
	
	   show: function(event, ui) { 
			$("#" + ui.panel.id).show();
			$.maxHeight = Math.max($.maxHeight, ($("#jv-timeline").height() + $.spacer));
			$("#" + ui.panel.id).hide();
			$("#jv-timeline-container").height($.maxHeight);
			$("#"+ui.panel.id).animate({ height: "toggle" }, {
			  duration: "fast",
			  step: function(now, fx){}});
			
			$(ui.panel).animate({"opacity": "1"}, "fast") 
			$(ui.panel).animate({"opacity": "1"}, "fast") 
		}
	});
	
	$('#dialog_link, ul#icons li').hover(
		function() {$(this).addClass('ui-state-hover');}, 
		function() {$(this).removeClass('ui-state-hover');}
	);
});

$.tabIndex = 0;
$.firstRun = true;
function activateButtons() {
	$("#timeline-toggler").removeClass("timeline-togglerdown");
	$("#timeline-toggler").addClass("timeline-toggler");
	
		
	$('.tllink')
	  .mouseenter(function()
	  {
		var year = $(this).attr("rel").split("tips-")[1];
		if ($.disabledTabs.indexOf(year) > -1) {return;}
		$(this).parent().css('z-index', '999999');
		$(this).children('.tl-over').show();
		if ($.browser.msie != true)
			$(this).children('.tl-over').stop().animate({"opacity": "1"}, 200);
	  });
	  
	  $('.tllink')
	  .mouseleave(function()
	  {
		$(this).parent().css('z-index', '1');
		$(this).children('.tl-over').hide();
		if ($.browser.msie != true)
			$(this).children('.tl-over').stop().animate({"opacity": "0"}, 0);
	  });
	  
	  $('.tllink')
	  .click(function()
	  {
		if ($.tabIndex > -1 || $.firstRun == true) {
			$.firstRun = false;
		}
	  });
}

function deactivateButtons() {
	$("#timeline-toggler").addClass("timeline-togglerdown");
	$("#timeline-toggler").removeClass("timeline-toggler");
	$('.tllink').unbind("mouseover mouseleave mouseout");
	$('.tllink').children('.tl-over').hide();
}


$(document).ready(function() {
	$('#jv-timeline-container').height(30 + $.spacer);
	doLayout();
	activateTips();
	activateButtons();
	$('#jv-timeline').show();
	$('#jv-timeline').bind('tabsselect', function(event, ui) {
		 if ($.tabIndex == ui.index && $.firstRun != true) {
			activateTips();
			activateButtons();
			$.tabIndex = -1;
			$('#jv-timeline-container').height(30 + $.spacer);
			$.maxHeight = 30 + $.spacer;
		  } else { 
			try {$(".qtip").remove();} catch(err) {}
			deactivateTips();
			deactivateButtons();
			var ccc = ui.index;
			
			switch (ccc) {
				case 0:
					$("#link-2010").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2010").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 1:
					$("#link-2011").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2011").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 2:
					$("#link-2012").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2012").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 3:
					$("#link-2013").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2013").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 4:
					$("#link-2014").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2014").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 5:
					$("#link-2015").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2015").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 6:
					$("#link-2016").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2016").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 7:
					$("#link-2017").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2017").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 8:
					$("#link-2018").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2018").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
			}
			
			
			
			
			$.tabIndex = ui.index;
			
			var year = $(ui.panel).attr("id").split("tabs-")[1];	
			
			$(".tl-" + year).parent().css('z-index', '999999');
			var years = Array("2010","2011","2012","2013","2014","2015","2016","2017","2018");
			for (var idx = 0; idx < years.length; idx++) {
				var yearToCheck = years[idx];
				if (year != yearToCheck) {
					$(".tl-" + yearToCheck).parent().css('z-index', '1');
				}
					
			}
				
		}
		});
		
		
	$('.tip').click(function() {
		switch ($(this).attr("id")) {
			case "tips-2010":
				$('#jv-timeline').tabs("select" , 0)
				break;
			case "tips-2011":
				$('#jv-timeline').tabs("select" , 1)
				break;
			case "tips-2012":
				$('#jv-timeline').tabs("select" , 2)
				break;
			case "tips-2013":
				$('#jv-timeline').tabs("select" , 3)
				break;
			case "tips-2014":
				$('#jv-timeline').tabs("select" , 4)
				break;
			case "tips-2015":
				$('#jv-timeline').tabs("select" , 5)
				break;
			case "tips-2016":
				$('#jv-timeline').tabs("select" , 6)
				break;
			case "tips-2017":
				$('#jv-timeline').tabs("select" , 7)
				break;
			case "tips-2018":
				$('#jv-timeline').tabs("select" , 8)
				break;
		}
	});
	
	$("#timeline-toggler").click(function() {
		if ($('#jv-timeline').tabs('option', 'selected') == -1) {

			deactivateButtons();
			var ccc=$.currentTab;
			$('#jv-timeline').tabs( "option", "selected", $.currentTab );
			switch (ccc) {
				case 0:
					$("#link-2010").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2010").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 1:
					$("#link-2011").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2011").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 2:
					$("#link-2012").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2012").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 3:
					$("#link-2013").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2013").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 4:
					$("#link-2014").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2014").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 5:
					$("#link-2015").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2015").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 6:
					$("#link-2016").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2016").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 7:
					$("#link-2017").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2017").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
				case 8:
					$("#link-2018").children('.tl-over').show();
					if ($.browser.msie != true)
						$("#link-2018").children('.tl-over').stop().animate({"opacity": "1"}, 200);
					break;
			}
		} else {	
			$('.tllink').children('.tl-over').hide();
			$('#jv-timeline').tabs( "option", "selected", -1 );
			$('#jv-timeline-container').height(30 + $.spacer);
			$.maxHeight = 30 + $.spacer;
			activateButtons();
			activateTips();
		}
	});
});
	
function deactivateTips() {
	$('.hastip').unbind('mouseenter mouseover');
}




function activateTips() {
	$('.hastip')
      .mouseenter(function()
      {
		if ($("#" + $(this).attr("rel")).html() == null) 
			return; 
	
		if($(this).data("qtip")) $(".qtip").remove();
        $(this).qtip({
		   content: {text: $("#" + $(this).attr("rel"))},
		   position: {
			  corner: {
				 tooltip: "topMiddle", 
				 target: "bottomMiddle" 
			  }
		   },
		   show: {
			  delay: 0,
			  ready: true, 
			  effect: { type: 'fade', length: 0 }
		   },
		   hide: {
				when: {event: "mouseleave"},
				fixed: true,
				delay: 0,
				effect: { length: 0 }
			 },
		   style: {
			  color: 'white',
			  background: '#0078AB',
			  width: { min: 300 },
			  border: {
				 width: 5,
				 radius: 5,
				 color: '#0078AB'
			  },
			  title:{
				 background:"#4d73ba",
				 color:"#f3f3f3"
			  },
			  padding: 5, 
			  textAlign: 'left',
			  tip: true 
			  
		   }
		})
	})
}

function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}

if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
    return -1;
  }
}
