var videoWidth = 600;

$(document).ready(function(){

	// this determines what thumbnails to load
	var baseURL = 'http://www.engadget.com/video';
	var snapURL = location.href.replace('#nav','').split(baseURL);
	var leftOver = snapURL[1];
	
	if(leftOver && leftOver!=='/' && leftOver!=='#player')
		{
		var lastChar = leftOver.substring(leftOver.length-1);
		if(lastChar=='/') leftOver = leftOver.substr(0,leftOver.length-1);
		leftOver = leftOver.substr(1);
		var parts = leftOver.split('/');
		var countParts = parts.length;
		}
	else
		{
		var countParts = 0;
		}
		
	if(snapURL.length>1)
		{
		switch(countParts)
			{
			case 0:
				var username = (location.href.indexOf('engadget.com/show')>-1) ? 'engadgetshow' : 'engadget' ;
				$.ajax({
					type : 'POST',
					url : '/video-nav-grab-no-tags/engadget/1',
					type: 'html',
					success : function (data) {
						$('#load-video-nav-here-dude').html(data);
						if($('.video_content_item').length==24) $('#video-page-next').show().find('a').attr('href',baseURL+'/page/2');
						changeVideo($('.video_content_item').eq(0),false);
					}   
				});
				if(username == 'engadget') $('#all-tab').addClass('sprite_selected');
				else if(username == 'engadgetshow') $('#show-tab').addClass('sprite_selected');
				break;
			case 1:
				$.ajax({
					type : 'POST',
					url : '/video-nav-grab-tags/'+parts[0]+'/1',
					type: 'html',
					success : function (data) {
						$('#load-video-nav-here-dude').html(data);
						if($('.video_content_item').length==24) $('#video-page-next').show().find('a').attr('href',baseURL+'/'+parts[0]+'/page/2');
						changeVideo($('.video_content_item').eq(0),false);
					}   
				});
				$('#'+parts[0]+'-tab').addClass('sprite_selected');
				break;
			case 2:
				var username = (location.href.indexOf('engadget.com/show')>-1) ? 'engadgetshow' : 'engadget' ;
				$.ajax({
					type : 'POST',
					url : '/video-nav-grab-no-tags/engadget/'+parts[1],
					type: 'html',
					success : function (data) {
						$('#load-video-nav-here-dude').html(data);
						changeVideo($('.video_content_item').eq(0),false);
						var nextPage = parseInt(parts[1])+1;
						var lastPage = parseInt(parts[1])-1;
						if($('.video_content_item').length==24) $('#video-page-next').show().find('a').attr('href',baseURL+'/page/'+nextPage);
						if(lastPage==1) $('#video-page-back').show().find('a').attr('href',baseURL);
						else $('#video-page-back').show().find('a').attr('href',baseURL+'/page/'+lastPage);
					}   
				});
				if(username == 'engadget') $('#all-tab').addClass('sprite_selected');
				else if(username == 'engadgetshow') $('#show-tab').addClass('sprite_selected');
				break;
			case 3:
				$.ajax({
					type : 'POST',
					url : '/video-nav-grab-tags/'+parts[0]+'/'+parts[2],
					type: 'html',
					success : function (data) {
						$('#load-video-nav-here-dude').html(data);
						changeVideo($('.video_content_item').eq(0),false);
						var nextPage = parseInt(parts[2])+1;
						var lastPage = parseInt(parts[2])-1;
						if($('.video_content_item').length==24) $('#video-page-next').show().find('a').attr('href',baseURL+'/'+parts[0]+'/page/'+nextPage);
						if(lastPage==1) $('#video-page-back').show().find('a').attr('href',baseURL+'/'+parts[0]);
						else $('#video-page-back').show().find('a').attr('href',baseURL+'/'+parts[0]+'/page/'+lastPage);
					}   
				});
				$('#'+parts[0]+'-tab').addClass('sprite_selected');
				break;
			
			}
		}
	else
		{
		// ENG SHOW STUFF
		setEngShow(0);
		}
	
	$('.video_content_item a').live('click',function(){
		if(snapURL.length>1) changeVideo($(this).parents('.video_content_item'),true);
		else setEngShow($('.video_content_item').index($(this).parents('.video_content_item')));
		return false;
	});

});

function changeVideo($pappa,jumpToPlayer)
	{
	var $port = $('#video_port');
	var autoStart = (jumpToPlayer) ? 't' : 'f' ;
	$('.video_content_item').removeClass('active');
	$pappa.addClass('active');
	var thumbID = $('.video_content_item').index($pappa);
	var videoPlayer = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="[[[WIDTH]]]" height="[[[HEIGHT]]]" id="viddler_[[[ID]]]"><param name="flashvars" value="autoplay='+autoStart+'" /><param name="wmode" value="transparent" /><param name="movie" value="http://www.viddler.com/simple/[[[ID]]]/" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><embed src="http://www.viddler.com/simple/[[[ID]]]/" width="[[[WIDTH]]]" height="[[[HEIGHT]]]" type="application/x-shockwave-flash" allowScriptAccess="always" wmode="transparent" flashvars="autoplay='+autoStart+'" allowFullScreen="true" name="viddler_[[[ID]]]"></embed></object>';
			
	var id = $pappa.find('.video_content_item_image').attr('rel');
	var myDate = $('.video_item_date').eq(thumbID).text();
	var title = $('.video-info-title').eq(thumbID).text();
	var myLength = $('.video-info-length').eq(thumbID).text();
	var description = $('.video-info-description').eq(thumbID).text();
	var views = $('.video-info-views').eq(thumbID).text();
	var width = parseInt($('.video-info-width').eq(thumbID).text());
	var height = parseInt($('.video-info-height').eq(thumbID).text());
	var ratio = width/height;
	var newHeight = Math.floor(videoWidth/ratio)+20;
		
	var newPlayer = videoPlayer.replace(/\[\[\[WIDTH\]\]\]/gi, videoWidth).replace(/\[\[\[HEIGHT\]\]\]/gi, newHeight).replace(/\[\[\[ID\]\]\]/gi, id);
	$port.html(newPlayer);
	if(!jumpToPlayer) $('#video-holder-inner').fadeIn('fast');
		
	$('#featured-video-title').text(title);
	$('#featured-video-date').text(myDate);
	$('#featured-video-length').text(convertTheseSecondsFromViddlerPlease(myLength,1)+' mins');
	$('#featured-video-views').text(views+' views');
	$('#featured-video-description').text(description);
	if(jumpToPlayer) window.location.hash = '#player';
	}

function setEngShow(index)
	{
	$('.video-port-holder-eng-show:visible').hide();
	$('.video_content_item').removeClass('active');
	var $thumb = $('.video_content_item').eq(index);
	var $port = $('.video-port-holder-eng-show').eq(index)
	$port.show();
	$thumb.addClass('active');
	$('#featured-video-title').text($thumb.find('.video_item_title a').text());
	$('#featured-video-date').text($thumb.find('.video_item_date').text());
	window.location.hash = '#player';
	}

function convertThisDateFromViddlerPlease(APIdate,loadHere)
	{
	var unix_timestamp = parseInt(APIdate.substring(0,(APIdate.length-3)));
	var date = new Date(unix_timestamp*1000);
	var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	var month = months[date.getMonth()];
	var day = date.getDate();
	var year = date.getFullYear();
	if(day==1 || day==21 || day==31) var suffix = 'st';
	else if(day==2 || day==22) var suffix = 'nd';
	else if(day==3 || day==23) var suffix = 'rd';
	else var suffix = 'th';
	var goodDate = month+' '+day+suffix+' '+year;
	if(loadHere==1) return goodDate;
	else $(loadHere).text(goodDate);
	}
	
function convertTheseSecondsFromViddlerPlease(secs,loadHere)
	{
	var hours = Math.floor(secs / (60 * 60));
	var divisor_for_minutes = secs % (60 * 60);
	var minutes = Math.floor(divisor_for_minutes / 60);
	var divisor_for_seconds = divisor_for_minutes % 60;
	var seconds = Math.ceil(divisor_for_seconds);
	if(minutes<10) minutes = '0'+minutes;
	if(seconds<10) seconds = '0'+seconds;
	if(secs>3600) var goodTime = hours+':'+minutes+':'+seconds;
	else var goodTime = minutes+':'+seconds;
	if(loadHere==1) return goodTime;
	else $(loadHere).text(goodTime+' mins');
	}