(function($){
	jQuery.fn.presentationTimeline = function(options){
		var $pt = jQuery(this);
		
		var options = jQuery.extend({
			interactive:true
		},options);
		
		var $sections = jQuery([]);
		var $label = $pt.children(".timeline").children(".timeline_inner").children(".top").children(".label");
		var $day_of_label = $pt.children(".timeline").children(".timeline_inner").children(".bottom").children("#day_of");
		var $after_label = $pt.children(".timeline").children(".timeline_inner").children(".bottom").children("#after");
		var $loaderImage = $pt.children(".controls_header").children(".loader_large");
		var current_section = 1;
		
		var initialized = false;
		
		function initialize(){
			setupSections();
			
			openSection($sections[current_section * 1.0]);
			
			initialized = true;
			ns11mmPresentationLoader.trigger("loadStatusEvent",{pct:10,msg:"Timeline Initialized"});
		};
		
		$pt.update = function(data){
			
			clear();
			if(updateSections(data) == true){
				$pt.slideToSectionIndex(getLowestTimeframeIndex(data));
				hideLoader();
			};
		};
		
		function getLowestTimeframeIndex(data){
			var lowest = 99;
			jQuery(data.items).each(function(i,j){
				if(j.timeframe_index != null){
					if(j.timeframe_index < lowest){
						lowest = j.timeframe_index;
					};
				};
			});
			return lowest;
		};
		
		$pt.slideToSectionIndex = function(index){
			if(index != null){
				if(typeof($sections[index * 1.0]) != "undefined" && $sections[index * 1.0] != null){
					openSection($sections[index * 1.0]);
				};
			} else {
				clearLabels();
				deselectAllSections();
			};
		};
		
		$pt.cancel = function(){
			hideLoader();
		};
		
		$pt.showLoader = function(){
			showLoader();
		};
		
		$pt.hideLoader = function(){
			hideLoader();
		};
		
		function setupSections(){
			$pt.children(".timeline").children(".timeline_inner").children(".mid").children(".section_wrap").children(".timeline_section").each(function(i,j){
				var section = jQuery(j);
				section.count = 0;
				section.clearState = function(){
					this.removeClass("has_content");
					section.removeClass("is_selected");
					section.count = 0;
				};
				if(options.interactive == true){
					var section_event = {
						section: section
					}
					section.bind("click",_sectionClickHandler);
					section.bind("mouseover",section_event,_sectionMouseoverHandler);
					section.bind("mouseout",_sectionMouseoutHandler);
				};
				$sections.push(section);
			});
			
			initializeSectionLabels();
		};
		
		function _sectionClickHandler(e,ui){
			var target = jQuery(e.target);
			if(target.hasClass("has_content")){
				openSection(target);
			};
			ns11mmPresentation.trigger("timelineSectionClicked",{timeframeIndex:target.attr("id")});	
		};
		
		function _sectionMouseoverHandler(e,ui){
			slideLabelToSection(jQuery(e.target));
		};
		function _sectionMouseoutHandler(){
			if(current_section.hasClass("is_selected")){
				slideLabelToSection(current_section);
			} else {
				$label.css("overflow","hidden").stop().animate({"height":"0px"});
			};
		};
		
		function initializeSectionLabels(){
			var first_day_marker = $pt.children(".timeline").children(".timeline_inner").children(".mid").children(".section_wrap").children(".day:first");
			var last_day_marker = $pt.children(".timeline").children(".timeline_inner").children(".mid").children(".section_wrap").children(".day:last");
			var last_segment = $pt.children(".timeline").children(".timeline_inner").children(".mid").children(".section_wrap").children(".timeline_section:last");
			$day_of_label.css("left",first_day_marker.position().left);
			$day_of_label.css("width",last_day_marker.position().left - first_day_marker.position().left);
			
			$after_label.css("left",last_day_marker.position().left);
			$after_label.css("width",(last_segment.position().left + last_segment.outerWidth()) - last_day_marker.position().left);			
		};
		
		function clear(){
			clearLabels();
			if($sections.length > 0){
				$sections.each(function(i,section){
					section.clearState();
				});
			};
		};
		
		function deselectAllSections(){
			$sections.each(function(i,_section){
				_section.removeClass("is_selected");
			});
		};
		
		function clearLabels(){
			$pt.children(".controls_header").children(".current_time").children("h2").text("");
			$pt.children(".controls_header").children(".current_time").children("h3").text("");
			$label.css("overflow","hidden").stop().animate({"height":"0px"});
			$day_of_label.animate({"height":"0%"})
			$after_label.animate({"height":"0%"})
		};
		
		function updateSections(data){
			var data = data.timeframes;
			if($sections.length > 0){
				$sections.each(function(i,section){
					if(data[i] > 0){
						section.count = data[i];
						section.addClass("has_content");
					};
				});
				return true;
			} else {
				return false;
			};
		};
		
		function openSection(section){
			deselectAllSections();
			current_section = section;
			section.addClass("is_selected");
			showLabels(section);
		};
		
		function showLabels(section){
			slideLabelToSection(section);
			showTimelineGroupLabel(section);
		};
		
		function showTimelineGroupLabel(section){
			if(section.attr("id") >= 1 && section.attr("id") <= 8){
				$day_of_label.animate({"height":"100%"});
				$after_label.animate({"height":"0%"});
			} else if(section.attr("id") >= 9){
				$day_of_label.animate({"height":"0%"});
				$after_label.animate({"height":"100%"});
			} else {
				$day_of_label.animate({"height":"0%"});
				$after_label.animate({"height":"0%"});
			};
		};
		
		function updateTimelineTitle(section){
			if(section.attr('rel').indexOf(",") > 0){
				var primary = section.attr('rel').substring(0,section.attr('rel').indexOf(","));
				var secondary = section.attr('rel').substring(section.attr('rel').indexOf(",")+1,section.attr('rel').length);
				$pt.children(".controls_header").children(".current_time").children("h2").text(primary);
				$pt.children(".controls_header").children(".current_time").children("h3").text(secondary);
			} else {
				$pt.children(".controls_header").children(".current_time").children("h2").text(section.attr('rel'));
			};
		};
		
		function slideLabelToSection(section){
			var next_left = (section.position().left - 75) + "px";
			if(section.attr('rel').indexOf(",") > 0){
				var label_primary = section.attr('rel').substring(0,section.attr('rel').indexOf(","));
				var label_secondary = section.attr('rel').substring(section.attr('rel').indexOf(",")+1,section.attr('rel').length);
				$label.children("h3").text(label_primary);
				$label.children("h4").text(label_secondary);
			} else {
				$label.children("h3").text(section.attr('rel'));
				$label.children("h4").html("&nbsp;");
			};
			$label.stop().animate({"height":"60px","left":next_left});
		};
		
		function showLoader(){
			$pt.children(".controls_header").stop().animate({height:"100%"},250,function(){
				$loaderImage.show();
			});
		};
		
		function hideLoader(){
			$pt.children(".controls_header").animate({height:"0px"},250,function(){
				$loaderImage.hide();
			});
		};
	
		initialize();
		return $pt;
	}	
})(ns11mmPresentation); 