$(document).ready(function() {
	function getTinyURL(longURL, success) {
		var API = 'http://json-tinyurl.appspot.com/?url=';
		var URL = API + encodeURIComponent(longURL) + '&callback=?';
		$.getJSON(URL, function(data){
			success && success(data.tinyurl);
		});
	}
	var page_num = 1;
	var total_pages = $('.thumb-page').size();
	var amount_to_move = $('.thumb-page:first').width();
	if(total_pages>1) {
		$('.pager a.next').css('display','inline');
		$('.pager a.prev').css('display','inline');
		$('.pager a.prev').css('opacity',0);
	} else {
		$('.pager').css('display','none');
	}
	function activateVideo(e) {
		var thumb = e;
		var vid_path = thumb.find('.file-path').text();
		flashObj("videoPlayer").sendToActionScript(vid_path);
	}
	function activateVideoContent(e) {
		var thumb = e;
		var vid_title = thumb.find('.thumb-title').text();
		var vid_body = thumb.find('.body').text();
		var vid_download = thumb.find('.link-download').text();
		var vid_register = thumb.find('.link-register').text();
		var page_id = $('.thumb-page').index(thumb.parent());
		var thumb_id = $('.thumb-page .thumb').index(thumb);
		var base_path = 'http://www.studentlife.com/events/videos.php#'+thumb_id;
		var twitter_link = 'http://twitter.com/home/?status=Video: '+vid_title+' - ';
		$('.video-content h3').html(vid_title);
		$('.video-content p').html(vid_body);
		$('.video-content a.icon-register').attr('href',vid_register);
		$('.video-content a.icon-download').attr('href',vid_download);
		$('.video-content a.icon-twitter').attr('href','');
		getTinyURL(base_path, function(tinyurl){
			twitter_link += tinyurl;
			$('.video-content a.icon-twitter').attr('href',twitter_link);
		});
		$('.thumb.active').animate({ 'backgroundColor': "#000" }, 500).removeClass('active');
		thumb.addClass('active');
		thumb.animate({ 'backgroundColor': "#70E7E9" }, 500);
	}
	function flashObj(movieName) {
		var mov = '#'+movieName;
		if($(mov)[0]) {
			return $(mov)[0];
		} else {
			return $(mov);
		}
	}
	$('.thumb a').each(function() {
		var thumb = $(this).parent();
		var page_id = $('.thumb-page').index(thumb.parent());
		var thumb_id = $('.thumb-page .thumb').index(thumb);
		$(this).attr('href','#'+thumb_id);
	});
	$('.thumb.enabled').click(function() {
		if(!$(this).hasClass('active')) {
			//$('#video-top').scrollTo(1000);
			var thumb_index = $('.thumb').index(this);
			activateVideoContent($(this));
			activateVideo($(this));
		}
	});
$('.pager a.next').click(function() {
		if(page_num<total_pages) {
			var amount = amount_to_move;
			amount = -amount*page_num;
			page_num++;
			moveThumbs(amount);
		}
	});
	$('.pager a.prev').click(function() {
		if(page_num>1) {
			var amount = amount_to_move;
			page_num--;
			amount = -(amount*page_num-amount_to_move);
			moveThumbs(amount);
		}
	});
	function moveThumbs(e) {
		$('.video-thumbs').animate({'marginLeft':e});
		if(page_num>1) {
			$('.pager a.prev').animate({'opacity':1});
		} else {
			$('.pager a.prev').animate({'opacity':0});
		}
		if(page_num==total_pages) {
			$('.pager a.next').animate({'opacity':0});
		} else {
			$('.pager a.next').animate({'opacity':1});
		}
	}
	var str = window.location.toString();
	var pos = str.indexOf("#")+1;
	var thumb_i = 0;
	var page_i = 0;
	var vid_id = 0;
	var play_vid = "false";
	if(pos>1) {
		var loc = str.substr(pos);
		if(loc.indexOf("/")>1) {
			var sp = loc.split("/");
			vid_id = parseInt(sp[1]);
			play_vid = "true";
		} else {
			vid_id = str.substr(pos);
			vid_id = parseInt(vid_id);
			play_vid = "true";
		}
	}
	var first_vid = $('.thumb').eq(vid_id);
	var first_vid_path = '';
	if(!first_vid.hasClass('enabled')) {
		first_vid = $('.thumb.enabled:first');
		first_vid_path = first_vid.find('.file-path').text();
	} else {
		var page = first_vid.parent();
		page_i = $('.thumb-page').index(page);
		first_vid_path = first_vid.find('.file-path').text();
		if(page_i>0) {
			page_num = page_i+1;
			moveThumbs(-amount_to_move*page_i);
		}
	}
	activateVideoContent(first_vid);
	swfobject.embedSWF("flash/player.swf", "videoPlayer", "480", "270", "9.0.0", "../flash/expressInstall.swf",
	{v:first_vid_path, auto_play:play_vid},
	{wmode:"opaque", allowFullscreen:"true", allowScriptAccess:"sameDomain"});
});