var selected_video = -1;



//alert(show_video);

$(document).ready( function() {
	initPager('#pager', '#pager_dots', '.page_');
	$('.pager_thumb').click(openVideo);
	if(show = get_video()) {
		showVideoByName(show);	
	}
} );

// called from a thumbnail
function openVideo(data) {
	showVideo('#' + $(data.currentTarget).attr('id'));
}

function get_video() {
	var hash = document.location.hash;
	//alert(hash);
	var query_vars = hash.split("/");//.indexOf("#");
	query_vars.shift();
	var show_video = null;
	
	for(var i=0; i<query_vars.length; i++) {
		if(query_vars[i]=="video") {
			return query_vars[i+1];
		}
	}
	
	return null;
}

function update_url(video) {
	if(video) {
		document.location.hash = "/video/" + video;
	}else{
		document.location.hash = "";	
	}
}

// called to setup the video display
function setupVideoFrame() {
	var box = $('#video_box');
	if(box.length < 1)
	{
		$('#page').append('<div id="video_box"></div>');
		$('#page').append('<div id="video_content"></div>');
		box = $('#video_content');

		box.append('<div id="video_close">&times;</div>');
		box.append('<div id="video_close_clear" class="clearfix"></div>');
		$('#video_close').click(removeVideo);

		box.append('<div id="video_next" class="video_changer button disabled">'
			+ '<div><span>next</span> <img src="Images/arrows_right.png" alt=""> </div>'
			+ '<div class="spike"></div></div>');
		box.append('<div id="video_sep" class="video_changer">'
			+ '<div>/</div><div class="spike"></div></div>');
		box.append('<div id="video_previous" class="video_changer button disabled">'
			+ '<div> <img src="Images/arrows_left.png" alt=""> <span>previous</span></div>'
			+ '<div class="spike"></div></div>');
		box.append('<div id="video_info"></div>');
	}
}

// called to enable/disable the video changer buttons
function setupVideoArrows(num) {
	
	var count = $('.pager_thumb').length;
	var prev = $('#video_previous');
	var next = $('#video_next');
	//console.log(num);
	if(num!=undefined) {
		prev.show();
		next.show();
		$("#video_sep").show();
		prev.unbind('click');
		next.unbind('click');
	
		if(num < 1) {
			prev.addClass('disabled');
		} else {
			prev.removeClass('disabled');
			prev.click(videoPrevious);
		}
	
		if(num >= count - 1) {
			next.addClass('disabled');
		} else {
			next.removeClass('disabled');
			next.click(videoNext);
		}
	}else{
		//console.log("no num");
		$("#video_sep").hide();
		prev.hide();
		next.hide();
	}
}

// called to show a video
function showVideo(elem) {
	
	var div = $(elem);
	if(div.length < 1)
		return;
	
	// get and change needed data
	var name = div.attr('id');
	var num = parseInt(name.substr(name.lastIndexOf('_') + 1, name.length));
	var video = div.children('img').attr('src');
	video = video.substring(video.lastIndexOf('/') + 1, video.length - 4);
	var url = "Content/gallery/video/" + video + ".mov";

	setupVideoFrame();
	setupVideoArrows(num);

	var info = $('#video_info')
	info.addClass('loading');
	info.html('');
	$.ajax( {
		url: "Content/gallery/info/" + video + ".html",
		success: function(data) {
			info.removeClass('error loading');
			info.html(data);
		},
		error: function(data) {
			info.removeClass('loading');
			info.addClass('error');
			info.html('No information found for this video.');
		}
	} );

	var vid = document.createElement('video');
	// create html5 video
	if(vid.canPlayType && vid.canPlayType('video/quicktime')) {
		vid = $(vid);
		vid.attr('controls', 'true');
	}
	// create embedded video
	else {
		vid = $('<embed></embed>');
		vid.attr('href', url);
		vid.attr('type', 'video/quicktime');
		vid.attr('width', '640');
		if(video=="MARSH_superbowl") {
			vid.attr('height', '370');
		}else{
			vid.attr('height', '496');
		}
	}
	// common embed and html5 attributes
	vid.attr('src', url);
	vid.attr('id', 'video_file');
	vid.attr('autoplay', 'true');

	$('#video_close_clear').after(vid);
	selected_video = num;
	
	update_url(video);
}

// called to show a video
function showVideoByName(video) {
	var url = "Content/gallery/video/" + video + ".mov";

	setupVideoFrame();
	setupVideoArrows();

	var info = $('#video_info')
	info.addClass('loading');
	info.html('');
	$.ajax( {
		url: "Content/gallery/info/" + video + ".html",
		success: function(data) {
			info.removeClass('error loading');
			info.html(data);
		},
		error: function(data) {
			info.removeClass('loading');
			info.addClass('error');
			info.html('No information found for this video.');
		}
	} );

	var vid = document.createElement('video');
	// create html5 video
	if(vid.canPlayType && vid.canPlayType('video/quicktime')) {
		vid = $(vid);
		vid.attr('controls', 'true');
	}
	// create embedded video
	else {
		vid = $('<embed></embed>');
		vid.attr('href', url);
		vid.attr('type', 'video/quicktime');
		vid.attr('width', '640');
		if(video=="MARSH_superbowl") {
			vid.attr('height', '370');
		}else{
			vid.attr('height', '496');
		}
	}
	// common embed and html5 attributes
	vid.attr('src', url);
	vid.attr('id', 'video_file');
	vid.attr('autoplay', 'true');

	$('#video_close_clear').after(vid);
	//selected_video = num;
}

function videoPrevious() {
	clearVideo();
	showVideo('#thumb_' + (selected_video - 1));
}

function videoNext() {
	clearVideo();
	showVideo('#thumb_' + (selected_video + 1));
}

function clearVideo() {
	var prev = $('#video_previous');
	var next = $('#video_next')
	prev.unbind('click');
	next.unbind('click');

	var video = $('#video_file');
	if(video.pause)
		video.pause();
	if(video.DoStop)
		video.DoStop();
	$('#video_file').remove();
	$('#video_info').html('');
}

function removeVideo(data) {
	selected_video = -1;
	clearVideo();
	$('#video_box').remove();
	$('#video_content').remove();
	
	update_url();
}

