 











$(document).ready(function() {
	//Set-up tabs & container divs
	vList = $("#tabs ul");
	$("li a", vList).click(tabClick);
	$.each(["Notes", "Bookmarks"], function (i,vTab) {
		vId = vTab.match(/\w+/)[0].toLowerCase();
		vLink = $('<a href="#'+ vId +'">' + vTab + '</a>').click(tabClick);
		vList.append($("<li></li>").append(vLink));
		vDiv = $('<div id="'+ vId +'"></div>');
		$("#right-content").append(vDiv); 
	});
	
	//Hide non-default tab content
	$("#notes, #bookmarks").hide();

	//Load users bookmarks into DOM
	$("#bookmarks").load("/bookmarks/", {fragment:"true"}, bookmarksLoad);

	if (!$("#content-wide").length) { //Wide templates do not have tabs
		//On click event for adding bookmark 
		$("#header-links a.bookmark").click(addBookmark);
	
		//On click event for showing bookmarks 
		$("#header-links a.print").click(showBookmarksTab);
		
		$("#search-form-wrap #submit").click(loadSearchResults);
	}
	
	$("#printBookmarks").click(printBookmarks);

	//On-click event for snapshots/snippets, test for all anchors which contain "/snippet/" in their href 
	$("a[href*='/snippet/']").addClass("s-shot").click(loadSnippet);
	
	// On click event for kpi titles
	$(".kpi-title").click(function() {
		vContentDiv = $(this).next();

		if (!$(vContentDiv).is(":hidden")) {
			$(this).removeClass("active-kpi").addClass("inactive-kpi");
		} else {
			$(this).removeClass("inactive-kpi").addClass("active-kpi");
		}
		$(vContentDiv).slideToggle();

		return false;
	});
	
	//View all kpi
	$("#v-kpi").click(function() {
		$(".kpi-title").removeClass("inactive-kpi").addClass("active-kpi");
		$(".kpi-flash").hide().slideToggle();
		
		return false;
	});

	//Hide all except first
	$(".os-flash-wrap:not(':first')").hide();
	$(".inactive-os:first").removeClass("inactive-os").addClass("active-os");
	
	//View click event for our strategy
	$(".os-title").click(function() {
		if (DetectFlashVer(8, 0, 0)) {
			$(".active-os").removeClass("active-os").addClass("inactive-os");
			$(this).parent().parent().removeClass("inactive-os").addClass("active-os");
			
			existingFlashMovie = $(".os-flash-wrap:visible");
			flashMovie = $(this).parent().parent().next();
			changeVideo(existingFlashMovie, flashMovie);

			return false;
		}
	});

	//Auto play first video
	$(".os-title:first").triggerHandler("click");
	
	//Load first
	$(".qa-flash:not(':first')").hide();	
	$(".inactive:first").removeClass("inactive").addClass("active");
	
	//on click event for ceo
	$(".ceo-title").click(function() {
		if (DetectFlashVer(8, 0, 0)) {
		
			$(".active").removeClass("active").addClass("inactive");
			$(this).parent().parent().removeClass("inactive").addClass("active");
			
			existingFlashMovie = $(".qa-flash:visible");
			flashMovie = $(this).parent().parent().next();
			changeVideo(existingFlashMovie, flashMovie);
	
			return false;
		}	
	});

	//Auto play first video
	$(".ceo-title:first").triggerHandler("click");
	
});

$(window).load(function(){ // wait for images to load
	setupNotes(false);
});


function tabClick() {
	//Switch on state
	vId = $(this).attr("href").match(/#.*$/)[0];
	$("#tabs li").removeAttr("id");
	$(this).parent().attr("id","current");

	hideAllRightContent();	
	
	//Show correct tab content
	if (vId == "#relatedlinks") {
		$("#rel-links-text").show();

	} else if (vId == "#notes") {
		$("#notes").show();

	} else if (vId == "#bookmarks") {
		$("#bookmarks").show();
	}
	
	return false;
}

function hideAllRightContent() {
	$("#notes").hide();
	$("#bookmarks").hide();
	$("#rel-links-text").hide();
	$("#search-results-right").hide();
	$("#snapshot").removeAttr("style");
	$("#snapshot").children().remove();
}

function loadSnippet() {
	hideAllRightContent();	
	vSnapshot = $("#snapshot");
	$("#tabs #current").attr("id","inactive");
	vTop = $(this).position().top;
	$(vSnapshot).css({"margin-top": vTop - 200 + "px"});			
	$(vSnapshot).load($(this).attr("href"), {"fragment":"true"});
	
	return false;
}

//Functions to interact with Flash
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function changeVideo(exisitingMovie, movie) {
	exsitingQuestionId = $(exisitingMovie).attr("id");
	questionId = $(movie).attr("id");
	
	if (exsitingQuestionId != questionId) {
		getFlashMovie("flvPlayer" + extractVideoNum(exsitingQuestionId)).sendTextToFlash("stop");
		$(exisitingMovie).hide();
	}
	
	$(movie).show();
	
	setTimeout(function() {
		if ($(movie).hasClass("already-playing")) {
			getFlashMovie("flvPlayer" + extractVideoNum(questionId)).sendTextToFlash("play");
			if (!$.browser.msie) { //Movie does not play again in non-ie browsers, so reload
				getFlashMovie("flvPlayer" + extractVideoNum(questionId)).sendTextToFlash(extractVideoNum(questionId));
			}
		} else {
			getFlashMovie("flvPlayer" + extractVideoNum(questionId)).sendTextToFlash(extractVideoNum(questionId));
			$(movie).addClass("already-playing");
		}
					
	}, 1000);
}

function extractVideoNum(id) {
	return id.substring(id.indexOf('_') + 1);
}

function loadSearchResults() {
	hideAllRightContent();
	$("#tabs #current").attr("id","inactive");

	vSearchDiv = $("#search-results-right");
	vQuery = $("#searchbox").val();
	$(vSearchDiv).children().remove();
	$(vSearchDiv).show();

	$.get("/search/", {query:vQuery,pageSize:"5"}, function(pData) {
		$(vSearchDiv).append("<h2>Search results</h2>");

		vIntro = $("<p></p>").append($(pData).find("#searchintro h2").html());
		$(vSearchDiv).append(vIntro);

		vResultsCount = $(pData).find("p.searchGray span");
		if (vResultsCount.length) {
			vResultsCount = $("<p></p>").append(vResultsCount.html());
			$(vSearchDiv).append(vResultsCount);
			$(vSearchDiv).append("<ul></ul>");

			$(pData).find("#results-list li").each(function(i) {
				$(vSearchDiv).find("ul").append(this);
			});
			
			vMoreLink = $("<a class='sr' href='#'>more results</a>");
			vMoreLink.click(function () {
				$("#search-form").submit();
				return false;
			});
	
			$(vSearchDiv).append(vMoreLink);
		}
		
	});

	return false;
}
