/* jsquery scrollable wrapper functions */

/*<![CDATA[*/

function makeScrollable(wrapper, scrollable){

	$.get("xml_files/students.txt",function(xml){
      	
		// Build an HTML string
		parseValues = parseXml(xml,'student_name');
		counter = 0;
		
		// Build an HTML string

		myHTMLOutput = '';
		// Run the function for each student tag in the XML file

		for (counter in parseValues) {

			studentName = parseValues[counter];

			// Build row HTML data and store in string
			mydata = BuildStudentHTML(studentName);

			myHTMLOutput = myHTMLOutput + mydata;
		};
		
		// Update the DIV called Content Area with the HTML string
		$(".students_images").append(myHTMLOutput);
	});

	// Get jQuery elements
	var wrapper = $(wrapper), scrollable = $(scrollable);
	
	// Hide images until they are not loaded
	scrollable.hide();
	var loading = $('<div class="loading">Loading...</div>').appendTo(wrapper);
	
	// Set function that will check if all images are loaded
	var interval = setInterval(function(){
		var images = scrollable.find('img');
		var completed = 0;
		
		// Counts number of images that are succesfully loaded
		images.each(function(){
			if (this.complete) completed++;	
		});
		
		if (completed == images.length){
			clearInterval(interval);
			// Timeout added to fix problem with Chrome
			setTimeout(function(){
				
				loading.hide();
				// Remove scrollbars	
				wrapper.css({overflow: 'hidden'});						
				
				scrollable.slideDown('slow', function(){
					enable();	
				});					
			}, 1000);	
		}
	}, 100);
	
	function enable(){			
		// height of area at the top at bottom, that don't respond to mousemove
		var inactiveMargin = 100;
		// Cache for performance
		var wrapperWidth = wrapper.width();
		var wrapperHeight = wrapper.height();
		// Using outer height to include padding too
		var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
		// Do not cache wrapperOffset, because it can change when user resizes window
		// We could use onresize event, but it's just not worth doing that
		// var wrapperOffset = wrapper.offset();
	
		//When user move mouse over menu
		wrapper.mousemove(function(e){
			var wrapperOffset = wrapper.offset();
			// Scroll menu
			var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight  - inactiveMargin;
	
			if (top < 0){
				top = 0;
			}
	
			wrapper.scrollTop(top);
		});
	}
}
	
$(function(){	
	makeScrollable("div.students_images_container", "div.students_images");
});
/*]]>*/

/* curtain scrollfunctions */

$(document).ready(function() {	

	$.get("xml_files/home_video.txt",function(data){
		var videoData = new Array();
		videoData = parseXml(data,'short_name');
		videoHomeEpisode = videoData[0].toUpperCase();
	});

	$.get("xml_files/episodes.txt",function(data){
		parseVideoXml(data);
		BuildSelectedVideoHTML(videoLastEpisode);
	});
	$(".leftcurtain").stop().animate({width:'60px'}, 2000 );
	$(".rightcurtain").stop().animate({width:'60px'},2000 );
	
	// Build HTML string and return
	myHTMLOutput = '<fb:fan profile_id="70099637567" stream="1" connections="10" width="340"></fb:fan>';

	$("#content_footer_facebook").append(myHTMLOutput);
	FB.init("86575f8f2cd1a3fc0937f68249d2fc0e");

});	

/* Build Student HTML String */
 
function BuildStudentHTML(studentName){	
	// Build HTML string and return
	output = '<a href="students.html?student=';
	output += studentName;
	output += '"><img src="images/Images_actors/';
	output += studentName;
	output += '.jpg" alt="';
	output += studentName;
	output += '" /></a>';
	return output;
}
