var note_array = new Array();
var note_obj ;

	function setNote(s,e,i,t,a) { 	
		note = new Notification();
		note.setStartpoint(s);
		note.setEndpoint(e);
		note.setInformation(i);
		note.setType("type");
		note.setAutostart(true);
		note_array[note.getUID()] = note;

		delayedSendingToFlash()

	}
	
	var note_timer;
	var note_timer_count = 1;
	function delayedSendingToFlash() {
		window.clearTimeout(note_timer);
		if (getVideoDuration()>0) {
			note.sendUserNoteToFlash();
		} else if (video_timer_count>5) {
			debug("Konnte Markierung nicht erstellen",'red');
		} else {
			note_timer = window.setTimeout("delayedSendingToFlash();", 1000);
			video_timer_count++;
		}
	}
	
	function start_notes() {
			var position = $("#notifications_content div#notes_start").position();
			$("#notifications_content").animate({"left": position.left}, 1000 );
			actual_qinput_content=1;	
	}
	
	function createNote() {
		if (getVideoDuration()>0) {
			note = new Notification();
			note.setStartpoint(getVideoPlayhead());
			note_array[note.getUID()] = note;
			note.sendToFlash();
		} else {
			debug("Fehler aufgetreten, die Videol&auml;nge konnte nicht ernittelt werden","red");
		}	
	}
	
	function setStartPoint(note) {
		if (note == undefined) {
			createNote();
		} else {
			note.setStartpoint(getVideoPlayhead());
			note.sendToFlash();
		}
		qpointForward();
	}
	
	function setEndPoint(note) {
		note.setEndpoint(getVideoPlayhead());
		if (note.getDuration()<0) {
			$('#note_err').html("Fehler aufgetreten, der Endpunkt darf nicht vor dem Start liegen!");
		} else {
			note.sendToFlash();
			$('#note_err').html('');
			/*qpointInfoInput(note.getEndpoint());*/
			qpointInfoInput_without_type_selection(note.getEndpoint());
		}		
	}
	
	function setInformation(note) {
		note.setInformation(stripslashes($("#set_note_information").val()));
		note.sendToFlash();
	}
	
		

	function stripslashes(str) {
		str=str.replace(/\\'/g,'\'');
		str=str.replace(/\\"/g,'"');
		str=str.replace(/\\0/g,'\0');
		str=str.replace(/\\\\/g,'\\');
		return str;
	}

	
	function setNoteJSListener(note_ids) {
		note = note_array[note_ids.jsid];
		note.setId(note_ids.noteid);
		debug("Note erstellt - startpoint: "+note.getStartpoint()+" - ende: "+note.getEndpoint()+"<br/>Info: "+note.getInformation()+"<br/>noteID("+note.getId()+")");
		
		$('#set_note_startpoint').unbind();
		$('#set_note_startpoint').bind('click', function()				{setStartPoint(note);});
		
		$('#set_note_endpoint').unbind();
		$('#set_note_endpoint').bind('click', function()				{$('#note_endpoint_err').html('');setEndPoint(note);});
		
		$('#set_note_information').unbind();
		$('#set_note_information').bind('change', function()		{setInformation(note);});
		
	}
	
	function deleteNotification(note) {
		getVoDPlayer().deleteNotification(note.getId());
	}
	
	function deleteAllNotifications() {
		note_array = new Array();
		note_obj = null;
		note = null;
		debug('deleteall');
		getVoDPlayer().deleteAllNotifications();
	}
	
	
	
	
	var actual_qinput_content = 1;

	function qpointBack() {
		if (actual_qinput_content>1) {
			var position = $("#notifications_content").position();
			var rtg = position.left+$("#notifications_content div").width();
			$("#notifications_content").animate({"left": rtg}, 1000 );
			actual_qinput_content--;
		}
	}
	
	function qpointForward() {
		if (actual_qinput_content<5) {
			var position = $("#notifications_content").position();
			var rtg = position.left-$("#notifications_content div").width();
			$("#notifications_content").animate({"left": rtg}, 700 );
			actual_qinput_content++;
		}
	}


	function qpointInfoInput(ende) {
		note.setEndpoint(ende);
		note.sendToFlash();
		
		$('#note_edit_end').unbind();
		if (ende>0) {
			$('#note_edit_end').css('background-image','url(http://www.nrw.tv/static/gfx/videoarchiv/btn-25-qp-vor-1.png)');
			$('#note_edit_end').bind('mouseover',function() {
				$('#note_edit_end').css('background-image','url(http://www.nrw.tv/static/gfx/videoarchiv/btn-25-qp-vor-2.png)');}
			);
			$('#note_edit_end').bind('mouseout',function() {
				$('#note_edit_end').css('background-image','url(http://www.nrw.tv/static/gfx/videoarchiv/btn-25-qp-vor-1.png)');}
			);
			qpointForward();
		} else {
			$('#note_edit_end').css('background-image','url(http://www.nrw.tv/static/gfx/videoarchiv/btn-25-qp-fertig-1.png)');
			$('#note_edit_end').bind('mouseover',function() {
				$('#note_edit_end').css('background-image','url(http://www.nrw.tv/static/gfx/videoarchiv/btn-25-qp-fertig-2.png)');}
			);
			$('#note_edit_end').bind('mouseout',function() {
				$('#note_edit_end').css('background-image','url(http://www.nrw.tv/static/gfx/videoarchiv/btn-25-qp-fertig-1.png)');}
			);
			$('#note_edit_end').bind('click',function() {
				saveNote(note);		
			});
			qpointForward();

		}	
	}
	
	
	
	
	function qpointInfoInput_without_type_selection(ende) {
		note.setEndpoint(ende);
		note.sendToFlash();
		
		$('#note_edit_end').unbind();
		$('#note_edit_end').css('background-image','url(http://www.nrw.tv/static/gfx/videoarchiv/btn-25-qp-fertig-1.png)');
		$('#note_edit_end').bind('mouseover',function() {	$('#note_edit_end').css('background-image','url(http://www.nrw.tv/static/gfx/videoarchiv/btn-25-qp-fertig-2.png)');});
		$('#note_edit_end').bind('mouseout',function() {	$('#note_edit_end').css('background-image','url(http://www.nrw.tv/static/gfx/videoarchiv/btn-25-qp-fertig-1.png)');});
		$('#note_edit_end').bind('click',function() {			saveNote(note);		});
		
		qpointForward();
	}
	
	
	
	function qpointMarked(note) {
		note.setType('marked');
		saveNote(note);	
	}		
	function qpointSliced(note) {
		note.setType('sliced');
		saveNote(note);
	}
	
	function saveNote(note) {
		
		var surl = 'http://player.tneg.de/dataprovider/nrw.tv/notification/saveNote.php';
		$('#video_note_response').html('Speichert...');
		$.ajax({
			url: surl,
			data: "videoclipid="+$('#videoclip_id').html()+"&"+note.toString()+"&callback=?",
			dataType: "json",
			beforeSend: function(data) {
			alert('speichern');
			},
			success: function(msg){
				if (msg) {$('#video_note_response').html("<a href='"+msg+"' style='color:#ffffff;text-decoration:underline;font-size:1.1em;font-weight:bold;' target='_blank'>"+msg+"</a>");}
				$('#notes_end').fadeIn();
				 var end = "";
				if (note.getEndpoint() > 0) end =" - Ende: "+note.getEndpoint();
				insertNoteIntoMailtext("Eine Markierung wurde hinterlassen\nLink: "+msg+"\nStart:"+note.getStartpoint()+ end +"\nInfo: "+note.getInformation()+"");
			},
			complete: function() {
			}
		});
	}
	
	function insertNoteIntoMailtext(insert_str) {
		var mail = $('#mailto_text').val();
		var insert_pos = mail.lastIndexOf("Titel des Videos");
		
		$('#mailto_text').val($('#mailto_text').val().substr(0,insert_pos) + insert_str +"\n\n" + $('#mailto_text').val().substr(insert_pos));
	}
	
	
	
	
	
	
