jQuery.ajaxSetup({
	'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

function makeAnnotation(vidid){
	var player = $f('rtmpPlayer')
	player.pause();
	time = Math.round(player.getTime());
	var x = "/events/add_annotation/"+vidid+'/?t='+time+'.js';
	$.get(x, function(data) {	
		loadFormDivSide(data);
		flashMessage('');
    });
	$('#form_div').show();
};


function cancelUpload(url,title){
	$.ajax({
		type: "POST",
		url: url,
		dataType: 'script'
	})
	flashError('There was an error uploading "'+title+'". Please try again.')
};
function goToTab(tabid){
	$('.css-tabs li a').removeClass('current')
  $('#'+tabid).addClass('current')
  $('.css-panes').hide();
	showATab(tabid)
};
function showATab(tid){
	$('.'+ tid).show();
}
function s3Upload(videoName,fileSize,contenttype,param,url,prefix){
	$.ajax({
		type: "POST",
		url: url,
		data: 'title='+videoName+'&size='+fileSize+'&vid_id='+param+'&prefix='+prefix+'&contenttype='+contenttype,
		dataType: 'script'
	})
};
function updateVideoTime(){};
$(function(){
	
	updateVideoTime(); // defined in /capture/_present_header.html.erb

	  $('.css-tabs li a').bind('click',function(){
			goToTab($(this).attr('id'));
	  })

	
	$('#vitime').live('click', function(event){
		updateVideoTime();
		return false;
	});
  $('#body a').live('click', function(event){
		if($(this).hasClass('get')){
			var x = $(this).attr('href')+'.js';
			
			if($(this).hasClass('pause')){
				var player = $f('rtmpPlayer')
				player.pause();
				$.get(x, function(data) {
					loadFormDivSide(data);
					flashMessage('');
			    });
			}else{
				$.get(x, function(data) {	
					loadFormDiv(data);
					flashMessage('');
			    });
			}
			

		$(".hdble").hide();
			return false;
		}

		if($(this).hasClass('dg')){
			var options = {
				url: $(this).attr('href')+'.js',
				dataType: 'script'
				}
			jQuery(this).ajaxSubmit(options);
			return false;
		}
		if($(this).hasClass('move')){
			var player = $f('rtmpPlayer')
			player.pause();
			time = Math.round(player.getTime());
			if(confirm('Move Event to Current Play Position?')){
				var options = {
					url: $(this).attr('href')+'?time='+time+'.js',
					dataType: 'script'
					}
				jQuery(this).ajaxSubmit(options);
			}
			return false;
		}

		if($(this).hasClass('go_to')){
			var player = $f('rtmpPlayer')
			var seekto = $(this).attr('id').replace('go-','') - 0
			player.seek(seekto);
			return false;
		}

		if($(this).hasClass('nudge')){
			var player = $f('rtmpPlayer')
			var time = Math.round(player.getTime());
			var amount = $(this).attr('id').replace('go-','') - 0
			player.seek(time + amount);
			return false;
		}
		if($(this).hasClass('dgdele')){
			if(confirm('Are you sure you wish to delete this event?')){
				var x = $(this).attr('href')+'.js';
				var y = $(this).attr('id');
		    $.post(x, "_method=post", function(data) {
					jQuery('#sort_'+y).remove();
		    });
				return false;
			}else{
				return false;
			}
		}
		if($(this).hasClass('dgdeln')){
			if(confirm('Are you sure you wish to delete this note?')){
				var x = $(this).attr('href')+'.js';
				var y = $(this).attr('id');
		    	$.post(x, "_method=post", function(data) {
		      		jQuery('#note-'+y).remove();
		    	});	
				return false;
			}else{
				return false;
			}
		}
		if($(this).hasClass('dgdelp')){
			if(confirm('Are you sure you wish to delete this photo?')){
				var x = $(this).attr('href')+'.js';
				var y = $(this).attr('id');
					    	$.post(x, "_method=post", function(data) {
					      		jQuery('#ph-'+y).remove();
					    	});	
				return false;
			}else{
				return false;
			}
		}
		if($(this).hasClass('dged')){
			var x = $(this).attr('href')+'.js';
			$.get(x, "_method=post", function(data) {
		    loadFormDiv(data);
		    });
		$(".hdble").hide();
				return false;
		}

		if($(this).attr('class') == 'photo-link'){
			var x = $(this).attr('href');
			var y = '<img src = "'+x+'" width ="700"></img>'
			$('#ph').css("top",(event.pageY -200 + 'px'))
			$('#ph').css("left",('0px'))
			$('#ph1').html(y)
			$("#ph").show();
			return false;
		}
	if($(this).attr('class') == 'photo-link-close'){
		$("#ph").hide();
		return false;
	}
	if($(this).attr('id')=='more-desc'){
		var x = $(this).attr('href').replace('/','')
		address = '/capture/more_description/'+x+'.js';
		$.get(address, function(data) {
			$('#sort_'+x).replaceWith(data);
	    });
			return false;
	}
	if($(this).attr('id')=='less-desc'){
		var x = $(this).attr('href').replace('/','')
		address = '/capture/less_description/'+x+'.js';
		$.get(address, function(data) {
			$('#sort_'+x).replaceWith(data);
	    });
			return false;
	}
  });
// end first live block
	$('#menclose').live('click', function(){
		$('#capmenlst').html('');
		$('#menn').hide();
	});
	$('.men').live('click', function(event){ // put little drop down menu
		var id = $(this).attr('id').replace('m-','')
		var x = '#m-'+id
		$('#capmenlst').html('');
		if(event.pageY > 350){
			$('#menn').css('top',event.pageY-190 +'px');
		}else{
			$('#menn').css('top',event.pageY +'px');
		}
		
		$('#menn').css('left',event.pageX + 'px');
		$('#menn').attr('class',id);
		$('#menn').show();
		$('#menid').html('<a class = "ignore" href = "/events/edit/'+id+'?return=capture">'+id+'</a>');
		
			$.get('/capture/fill_extra_menu/'+id+'.js',function(data){
				$('#capmenlst').append(data);
			});

		return false;
	});
	$('#menn a').live('click', function(){
		var id = $('#menn').attr('class')
		var x = $(this).attr('href')+id+'.js';
		if($(this).hasClass('ignore')){return}
		if($(this).hasClass('mendel')){
			if(confirm('Are you sure you wish to delete this event?')){
				$.post(x, "_method=post", function(data) {
			    jQuery('#sort_'+id).remove();
			    });
			$(".hdble").show();
			}
		}else if($(this).hasClass('menhi')){
			var options = {
				url: $(this).attr('href')+id+'.js',
				dataType: 'script'
				}
			jQuery(this).ajaxSubmit(options);
		}else if($(this).hasClass('menred')){
			window.location($(this).attr('href'))
		}else{
			$.get(x, "_method=post", function(data) {
		    loadFormDiv(data);
		    });
				$(".hdble").hide();
		}
		$('#menn').hide();
		return false;
	});

		
		$('#form_div a').live('click', function(){
			if($(this).attr('id') == 'uploadfile'){
				$('#uploadfile').hide();
       }
       
			if($(this).attr('class') == 'cancel'){
           $('#form_div').hide();
           $(".hdble").show();
					$(".hdble").show();
           return false;
       }
			if($(this).attr('class') == 'cancel_up'){
           $('#form_div').hide();
           $(".hdble").show();
					 var options = {
	             url: $(this).attr("href")+'.js',
	             dataType: 'script'
	         }
	         jQuery(this).ajaxSubmit(options);
           return false;
       }
       if($(this).attr('class') == 'cancel_mod'){
         $('.formhide').hide();
         var options = {
             url: $(this).attr("href")+'.js',
             dataType: 'script'
         }
         $("form.timer").stopTime('increment');
         jQuery(this).ajaxSubmit(options);
        
         $(".hdble").show();
           return false;
			 }
			 });
	
	
	
	// $('#form_div a').live('click', function(){
	// 	$('#form_div').hide();
	// 	if($(this).hasClass('cancel_mod')){
	//       var options = {
	//       	url: $(this).attr("href")+'.js',
	//       	dataType: 'script'
	//       }
	// 			$("form.timer").stopTime('increment');
	//       jQuery(this).ajaxSubmit(options);
	// 	}
	// 	$(".hdble").show();
	// 	return false;
	//   });
	

	
	$('#form_div select').live('mouseup', function(){
	  if($(this).attr('id') == 'taggs'){
	  	var taggg = $('#event_tags').attr('value');
		if($('#taggs').attr('value') != 'select some tags from this list'){
	  		if(taggg != ''){taggg = taggg + ','};
	  		taggg = taggg + $('#taggs').attr('value');
			$('#event_tags').attr('value',taggg);
		}
	  	
	  }
		if($(this).attr('id') == 'title-taggs'){
	  	var taggg = $('#event_title').attr('value');
		if($('#title-taggs').attr('value') != 'select a title from this list'){
			
			$('#event_title').attr('value',$('#title-taggs').attr('value'));
		}
	  	
	  }
	});
	
  // this is work in progress
$('#form_div form').live('click', function(){
	
	if($(this).attr('class') == 'ajax timer'){
		var changed = false;
		var x = '/capture/incremental_mod_ev/'+$(this).attr("id")
		$(this).everyTime(30000,'increment', function () {
			changed = true;
	  		var options = {
	  			url: x,
	  			dataType: 'script'
	  			}
			if(changed == true){
				jQuery(this).ajaxSubmit(options);
				changed = false;
			}
	   		
	
	
	 	   	}
	,true);
	};
});

$('#form_div input').live('click', function(){
if($(this).attr('type') == 'submit'){
		$('.formhide').hide(); 
var x = $(this).parent().attr('action');

 var options = {
 	url: x,
 	dataType: 'script'
 }
 if($(this).parent().attr('class') == 'ajax timer'){
jQuery($("form.timer")).ajaxSubmit(options);
 $("form.timer").stopTime('increment');
    return false;
}
if($(this).parent().attr('class') == 'ajax'){
jQuery($("form.ajax")).ajaxSubmit(options);	
    return false;
}
}	
});


});
