

$(document).ready(function() { 

$("#search_text").focus();

// Load tablesorter
$.tablesorter.defaults.widgets = ['zebra'];

$.tablesorter.addParser({ 

    id: 'times', 
    is: function(s) { 
        return false; 
    }, 
    format: function(s) { 

		parts = s.split(':').reverse()
		t = 0
		
		if ( s != 'n/a' ) {
			t = t + parseInt(parts[0])
			if ( parts[1] ) { t = t + parseInt(parts[1]) * 60 }
			if ( parts[2] ) { t = t + parseInt(parts[2]) * 3600 }
		}

        return t 

    }, 

    type: 'numeric' 
}); 

//$('input#id_search').quicksearch('table#all_videos tbody tr');

$('#related_videos').hide();

$('.sidebar_toggle').click(function() {

	$(this).parent().next().slideToggle('fast');
	$(this).html( ( $(this).html() == 'hide' ) ? 'show' : 'hide' );
	return false;

});

function get_download_link( url ) {

	return 'http://video.google.com/videoplay?docid=' + url.replace(/^.*docid=/i, '');

}

$('.download_video').click(function() {

	url = get_download_link( $(this).attr('rel') );
	link = $(this);

	$.get('/scrape/?url='+url, function(data) {
		if ( data == '0' ) {
			alert('Unable to retrieve download link');
			return;
		}
		//$("#download_link_title").html('asdfasdf');
		// link.attr('rel', $('#submit_info_id').val())
		$("#download_link").attr('href', data.replace(/\&amp;/g,'&'));
	});

	$(this).colorbox({width:"300px",height:"300px", inline:true, href:"#download_video"});

});


$(".submit_video_info").colorbox({width:"500px",height:"500px", inline:true, href:"#submit_info_form"});


$("#submit_info_form .submit").click(function() {

	$.post('/videos/submit_info/', { video_id: $('#submit_info_id').val(), email: $('#submit_info_email').val(),info: $('#submit_info_info').val() }, function(data) {
	  if ( data['result'] == 1 ) {
	  	$('#submit_info_info').val('')
	  	$.fn.colorbox.close();
	  }
	});

return false;

});


$(".action_broken").click(function(){

	link = $(this);

	$.post('/videos/submit_info/', { video_id: $('#submit_info_id').val(), email: 'broken@skatevideosonline.net',info: 'broken' }, function(data) {
	  if ( data['result'] == 1 ) {
	  	link.html('Thanks!');
	  }
	  else {
	  	link.html('Error :(');
	  }
	});

	return false;

});


});


