document.write('<style type="text/css">#sections li ul { display: none; }</style>');

$.ajaxSetup ({
    // Disable caching of AJAX responses
    cache: false
});

$(document).ready(function(){
	
restore();

// check to see if the user has come to direct video link
// if so, result will be > '-1'.
var str = document.location.href;
var result = str.search(/v=/i);

// if result equal's '-1', they aren't a direct link visitor, hide tabs.
if(result == '-1')
{
	$('#tabs').hide();
}
else{
	// user must be coming direct video link.
	// work out URL parameters using 'gup' (get url parameters) function.
	var videoId = gup('v');
	theurl = '/clips/' + videoId;
	
	// send to directUrlLoad to load content in for us.
	directUrlLoad(theurl);
	
	$("#teachernotes").show();
}

// Treeview - setup open/close toggles: 

//$('#sections li ul').css('display', 'none');

$('#sections a').click(function(e){ 
	$(this.parentNode).toggleClass('open'); 
	e.stopPropagation();
	save();
});

$('#sections li').click(function(e){ 
	$(this).toggleClass('open'); 
	e.stopPropagation();
	save();
});

// replace sitetree links with plain text where section cannot be visited directly

$("#sectionmap a").each(function(){
	if ( !$(this).attr('href') )
	{
		$(this).after($(this).html()).remove();
	}
})

$('a[@href$="/resources"]').after('Resources').remove();

// Tabs onclick



$('#tabs a').click(function(e){
	
	tabId = this.href.split('=').pop();
	
	showTabContent(tabId);
	
	
	// Highlight active tab:
	$("#tabs li").removeClass('active');
	$(this.parentNode).addClass('active');
	
	e.preventDefault();
	return false;
});

$('#teachernotes').hide();
$('#transcript').hide();
$('#downloads').hide();
$('#glossary').hide();

initThumbs();

});

function showTabContent(tabId)
{
	$('#teachernotes').hide();
	$('#transcript').hide();
	$('#downloads').hide();
	$('#glossary').hide();
	
	switch(tabId)
	{
		case '1':
			$('#teachernotes').show();
			break;
		case '2':
			$('#transcript').show();
			break;
		case '3':
			$('#downloads').show();
			break;
		case '4':
			$('#glossary').show();
			break;
	}
}

function initThumbs()
{
	//console.log('init thumbs');
	$('#thumbs').find('a.thumb, a.play, a.title').click(function(e){
		
		e.preventDefault();
		
		$('#tabs').show();
		
		$("#tabs li").removeClass('active');
		$("ul#tabs li:first-child").addClass('active');

		$('#teachernotes').show();
		$('#transcript').hide();
		$('#downloads').hide();
		$('#glossary').hide();

		$("#endorser").html('<img src="/images/loading.gif" />');
		$("#panel div.text").html('<img src="/images/loadingwhite.gif" />');
		
		//pageTracker._trackPageview();
	
		/*$.ajax({
		    type: "POST",
		    url: this.href,
		    cache: false,
		    data: "ajax=1",
                    success: function(data) {
			
			$("#panelcontent").html(data);
			
                    	// move teaching notes info to the end of #content:
                        $("#endorsercontent").each(function(){
                                $(this).remove();
                                $("#teachernotes").html(data);
                        });
                        // move transcript info to the end of #content:
                        $("#transcriptcontent").each(function(){
                                $(this).remove();
                                $("#transcript").html(data);
                        });
                        // move transcript info to the end of #content:
                        $("#downloadscontent").each(function(){
                                $(this).remove();
                                $("#downloads").html(data);
                        });
                        // move transcript info to the end of #content:
                        $("#glossarycontent").each(function(){
                                $(this).remove();
                                $("#glossary").html(data);
                        });
		    }
                });	*/

		// load video into panel content:
		$("#panelcontent").load(this.href, {ajax: 1}, function(){
			
			//console.log($(this).html());
		    
			// move teaching notes info to the end of #content:
			$("#endorsercontent").each(function(){
				$(this).remove();
				$("#teachernotes").html($(this).html());
			});
			// move transcript info to the end of #content:
			$("#transcriptcontent").each(function(){
				$(this).remove();
				$("#transcript").html($(this).html());
			});
			// move transcript info to the end of #content:
			$("#downloadscontent").each(function(){
				$(this).remove();
				$("#downloads").html($(this).html());
			});
			// move transcript info to the end of #content:
			$("#glossarycontent").each(function(){
				$(this).remove();
				$("#glossary").html($(this).html());
			});
		});
	
		// add "playing" sticker to thumbnail	
		$('#thumbs div.playing').remove();
		$(this).parents('li').prepend('<div class="playing"></div>');

		
		return false;
	});
}

// this function will load video content/tab content if the user
// has come from directly using the ?v=XXX way.
function directUrlLoad(videoId)
{
		$('#tabs').show();
		$('#teachernotes').show();

		$("#endorser").html('<img src="/images/loading.gif" />');
		$("#panel div.text").html('<img src="/images/loadingwhite.gif" />');

		// load video into panel content
		$("#panelcontent").load(videoId, {ajax: 1}, function(){			
			// move teaching notes info to the end of #content:
			$("#endorsercontent").each(function(){
				$(this).remove();
				$("#teachernotes").html($(this).html());
			});
			// move transcript info to the end of #content:
			$("#transcriptcontent").each(function(){
				$(this).remove();
				$("#transcript").html($(this).html());
			});
			// move transcript info to the end of #content:
			$("#downloadscontent").each(function(){
				$(this).remove();
				$("#downloads").html($(this).html());
			});
			// move transcript info to the end of #content:
			$("#glossarycontent").each(function(){
				$(this).remove();
				$("#glossary").html($(this).html());
			});
		});
			
		// add "playing" sticker to thumbnail
		$('#thumbs div.playing').remove();
		$(this).parents('li').prepend('<div class="playing"></div>');
		
		return false;
}

// Store the treeview state by saving 'open' node ids in a cookie.

function save()
{
	var opennodes = new Array()
	$('#sections li.open').each(function(e){
		opennodes.push(this.id)
	});
	setCookie('tree', opennodes.join(','));
}

// Restore the treeview state by reading a cookie and setting the 'open' nodes

function restore()
{
	cookie = getCookie('tree')
	
	if ( cookie )
	{
		nodes = cookie.split(',')
		
		$.each(nodes, function(key,val){
			$('#' + val).addClass('open')
		});
	}
}

// Store a value in a cookie

function setCookie(name, value)
{
	var date = new Date();
	date.setTime( date.getTime() + (365 * 24 * 60 * 60 * 1000) );
	document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
}

// Retrieve a value from a cookie

function getCookie(name) 
{
	param = name + "=";
	parts = document.cookie.split(';');

	for(i = 0; i < parts.length; i++) 
	{
		part = parts[i];
		while (part.charAt(0) == ' ') 
		{
			part = part.substring(1, part.length);
		}
		if (part.indexOf(param) == 0)
		{
			return part.substring(param.length, part.length);
		}
	}
	return null;
}

function gup(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

