ERCurrentView = '#view-0';
ERLock = false; // prevent clickin' before animation finished 

ERVideoDiv = null;
ERVideoBgDiv = null;
ERVideoIndex = 0;


function ERStart() {
	// set focus to login form
	$("#username").focus();
	
	// set popup menu for video

	
	$("#show-video").click( function(event) {
		ERShowVideoPopUp(event);
	} );
	
	// setup intro menu
	$("#intro-menu a").click(function(event) {
		
		// block browser default action on click
		event.preventDefault();
		
		var newView = $(this).attr('href'); 
		// console.log('click:'+newView);
		
		if ((newView != ERCurrentView) && (!ERLock)) {
			ERLock = true;

			$("#intro-menu li.selected").removeClass('selected');
							
			$(ERCurrentView).fadeOut("fast", function() {
				$(newView).fadeIn("fast");
				
				var newMenu = '#intro-menu-' + newView.substring(6);
				$(newMenu).parent().addClass('selected');

				ERLock = false;
				ERCurrentView = newView;
				
			})
		}
	});
	
}	

function ERShowVideoPopUp(event) 
{
	event.preventDefault();
	
	if (ERVideoDiv == null) {
		$("body").prepend('<div id="video-bg-div" style="position:fixed; top:0; left:0; width:100%; height:100%; background-color:#000; z-index:1000"></div><div id="video-div" style="position:fixed; width:700px; height: 560px; background-color:#fff; z-index:1001; -webkit-border-radius:5px; -moz-border-radius:5px"><p class="exit"><a href="javascript:ERHideVideoPopUp();">x</a></p><div id="video-content"></div></div>');
		
		ERVideoBgDiv = $("#video-bg-div");
		ERVideoDiv = $("#video-div");

		ERVideoBgDiv.css({opacity:0.8}).click(ERHideVideoPopUp);
	}
	
	ERVideoDiv.fadeIn("fast");
	$("#video-content").load("images-eracuni/screencastsHR.html", ERVideoLoadedCallback);
	
	ERVideoBgDiv.fadeIn("fast");
	
	var l = (ERVideoBgDiv.width() - ERVideoDiv.width()) / 2;
	var t = (ERVideoBgDiv.height() - ERVideoDiv.height()) / 2;
	
	$("#video-div").css({top:t+"px", left:l+"px"});	
}

function ERHideVideoPopUp() {
	//console.log("hide");
	ERVideoDiv.hide();
	$("#video-content").empty();
	ERVideoBgDiv.fadeOut("fast");
}

function ERVideoLoadedCallback() {
	//console.log("screencasts html loaded.");
	
	var index = (typeof(ERVideoIndex) == "undefined") ? 0 : ERVideoIndex;
	
	//var firstVideo = $("#slider a:first").attr('href');
	var firstVideo = $("#video-slider a:eq("+index+")").attr('href');

	//console.log(index); // + ": " + firstVideo);

	$('#video').flash({ src:firstVideo, width:"696", height:"480" });
	
	/*
	$("#video-slider a").click(function(event) {
		event.preventDefault();
		$("#video-slider .video-item").removeClass('selected');
		$(this).parent().addClass('selected');

		var video = $(this).attr('href');
		$('#video').html('').flash({ src: video, width:"696", height:"460" });
	});
*/

	$("#video-slider .video-item").click(function(event) {
		event.preventDefault();
		$("#video-slider .video-item").removeClass('selected');
		$(this).addClass('selected');
		var video = $(this).children('a').attr('href');
		$('#video').html('').flash({ src: video, width:"696", height:"460" });
	});
}
