$(document).ready(function() {

	var $resrc_nav_btn = $('.resource-nav-btn');
	$('#resrc-ideas').show();
	
	$resrc_nav_btn.click(function() {
		var $id = $(this).attr('id');
		var $link = $('#resrc-'+$id);
		$('.resrc-content').hide();
		$resrc_nav_btn.removeClass('active');
		$(this).addClass('active')
		$link.slideDown(1000);
		return false;
	});






	var $videos = $('ul.vimeo-video-list li');
	var $video_links = $('.vimeo-video a');
	var $con = $videos.parent();
	var $vid_item = $videos.first();
	var $arrows = $('a.vimeo-arrow');
	var $item_width = $vid_item.outerWidth(true);
	var $con_w = $videos.length*$item_width;
	var $page_w = $('div.vimeo-list-viewport').width();
	var $page_num = 0;
	var $has_pages = false;
	var $pages = Math.floor($con_w/$page_w);
	if($pages > 0) $has_pages = true;
	var $max_dist = -(Math.floor($con_w/$page_w)*$page_w);
	var $is_animating = false;
	if(!$con.hasClass('no_controls')) {
		$con.css('width',$con_w);
	}
	$arrows.each(function() {
		var $this = $(this);
		//if there is only one page, turn off arrows
		if(!$has_pages) $arrows.css({'opacity':.2}); 
		if($this.hasClass('arrow-prev')) $this.css({'opacity':.2});
		$this.click(function() {		
			if($is_animating || !$has_pages) {return false;}																					
			var $this = $(this);
			var $dist = 0;
			var $num = $page_num;
			if($this.hasClass('arrow-next')) {
				$page_num++;
				$dist = -($page_w*$page_num);
				$arrows.not($this).css({'opacity':1});
				if($dist <= $max_dist) $this.css({'opacity':.2}); 
			} else if($page_num > 0) { //prev
				$page_num--;
				$dist = -$page_w*$page_num;
				$arrows.not($this).css({'opacity':1});
				if($dist >= 0) $this.css({'opacity':.2}); 
			}
			if($dist < $max_dist || $dist > 0) return false;
			$is_animating = true;
			$con.animate({'margin-left':$dist},800,function(){$is_animating = false;});
			return false;
		});
	});
	$video_links.click(function() {
		var $id = $(this).attr('href');
		$id = $id.split('#');
		$id = $id[1];
		loadVideo($id);
	});
	$video_links.mouseover(function() {
		var $li = $(this).parent().parent();
		var $title = $li.find('div.vimeo-title a');
		if(!$title.hasClass('no_scroll')) {
			var $c_margin = parseInt($title.css('margin-left'));
			var $margin = 110-parseInt($title.width())+$c_margin;
			if($margin < 0) {
				var $time = Math.abs($margin)/10*300;
				$title.stop().animate({'margin-left':$margin}, $time, 'linear');
			}
		}
	});
	$video_links.mouseout(function() {
		var $li = $(this).parent().parent();
		var $title = $li.find('div.vimeo-title a');
		if(!$title.hasClass('no_scroll')) {
			var $c_margin = parseInt($title.css('margin-left'));
			var $margin = $c_margin;
			if($margin < 0) {
				var $time = Math.abs($margin)/10*300;
				$title.stop().animate({'margin-left':0}, $time, 'linear');
			}
		}
	});
	
	function loadVideo($id, $load_from_url) {
		if($id > 0) {
			$videos.removeClass('active');
			var $vid = $('li.video_'+$id);
			$vid.addClass('active');
			var $width = parseInt($vid.attr('rel'));
			var $player = $('#video-player');
			var $desc = $('#video-desc');
			if(!$width || $width == 0) {
				$width = 955;
			}
			var $height = Math.round($width*.5625);
			var $html_video = '<iframe src="http://player.vimeo.com/video/'+$id+'" width="'+$width+'" height="'+$height+'" frameborder="0"></iframe>';
			var $html_desc = $vid.find('.vimeo-desc-hidden').html();
			$player.html($html_video);
			$desc.html($html_desc);

			if(typeof(scrollTo) == 'function') { 
				$player.scrollTo(500);
			}
			
			if($load_from_url) {
				var $offset = $vid.index()*$item_width;
				$page_num = Math.floor($offset/$page_w);
				$dist = -($page_w*$page_num);
				$is_animating = true;
				activateArrows($dist);
				$con.animate({'margin-left':$dist},800,function(){$is_animating = false;});
			}
		}
	}
	function activateArrows($dist) {
		var $arrow_prev = $('#video-list a.arrow-prev');
		var $arrow_next = $('#video-list a.arrow-next');
		//turn on
		if($dist < 0) $arrow_prev.css({'opacity':1});
		if($dist >= 0) $arrow_next.css({'opacity':1});
		
		//turn off
		if($dist >= 0) $arrow_prev.css({'opacity':.2});
		if($dist >= $max_dist) $arrow_next.css({'opacity':.2});
	}
	//load initial video if available
	var $str = window.location.toString();
	var $id = $str.split('#');
	$id = $id[1];
	if($id > 0) {
		loadVideo($id, true);
	}
});
