// global variables

// sliders used in the add event tab
var start_slider;
var end_slider;
var time_slider, start_s, end_s;
var js_slider;

function subvideo_timer(timenow, length_s) { 
	if (timenow > length_s) timenow = length_s;
	if (document.getElementById('t2')) {
		document.getElementById('t2').innerHTML = timenow;
		var pixels = (1000*(timenow))/length_s;
		if (document.getElementById('point')) {
			 document.getElementById('point').style.left= pixels+"px";		 
		}
	}
	if (timenow> length_s) {
		if (document.getElementById('t2')) {
			document.getElementById('t2').innerHTML = length_s;
		}
	}

	found = 0;
    if (document.getElementById('event2')) {
	for (var j=0; j<image_events.length; j++) {
		if (timenow+1 >= image_events[j][0] && timenow+1 < image_events[j][1]) {
			document.getElementById('event2').innerHTML= image_events[j][2];
			found = 1;
			//document.getElementById('event2').style.visibility='visible'
		}
	}
	if (!found) {
			document.getElementById('event2').innerHTML= "&nbsp;";
	}
    }
}

function record_events(state, sv)  {
	if (state == "ON") {
		var t = +document.getElementById('t2').firstChild.nodeValue;
        	document.getElementById('from').innerHTML = t;
        	document.getElementById('click').innerHTML = "<a onclick=\"javascript:record_events('OFF', "+sv+");\" id=\"on\">ON</a>";
	} else {
		var t = +document.getElementById('t2').firstChild.nodeValue;
		var from = +document.getElementById('from').firstChild.nodeValue;
        	document.getElementById('from').innerHTML = 'done:'+from+'>'+t;
        	document.getElementById('click').innerHTML = "<a onclick=\"javascript:record_events('ON', "+sv+");\" id=\"on\">OFF</a>";

		//alert(document.getElementById('sv').firstChild.nodeValue);
		//var sv = document.getElementById('sv').nodeValue;
		var php_file = "./lib/add.php?add=fly&sv="+ sv +"&start_s="+from+"&end_s="+t;
		new Ajax.Updater("player_foot", php_file  , { method: "get" });
		//FIXME cal passa rvars a timeline per actalitzar-la
		//updatetimeline();
	}
}

function playpause(state, sendToPlayer)  {
	var status = document.getElementById('playpause_status').firstChild.nodeValue;
	if (sendToPlayer) sendEvent('playpause');
	if (state == "play") {
//		t2.start();
       	document.getElementById('playpause').innerHTML = '<a name="start" accesskey="p" onclick="javascript: playpause(\'pause\', 1);"><div id="playpause_status">pause</div><img src="./images/stop.png"  width="35px" /></a>';
	} else {
//		t2.stop();
//		var t = +document.getElementById('t2').firstChild.nodeValue;
       	document.getElementById('playpause').innerHTML = '<a name="start" accesskey="s" onclick="javascript: playpause(\'play\', 1);"><div id="playpause_status">play</div><img src="./images/play.png"  width="35px" /></a>';
	}
}


// events and timeline functions
function updatetimeline() {
	var php_file2 = "./lib/timeline.php?video="+ video +"&video_length_s="+ video_length_s +"&video_name="+ video_name +"&"+  serializedForm;
	new Ajax.Updater("timeline", php_file2  , { method: "get" });
}

//function addevent (sv, video_length_s, video_id) {
function addevent () {
	// variables del form amb prototype.js
	var serializedForm = $('addform').serialize();
	var php_file = "./lib/add.php?"+serializedForm;

	// ajax que envia variables del form a una funcio php que afegeix nou event
	new Ajax.Updater('event_edit', php_file  , { method: 'get' });

	var php_file2 = "./lib/timeline.php?"+  serializedForm;
	new Ajax.Updater("timeline", php_file2  , { method: "get" });

}

function modifyevent (sv) {
	var serializedForm = $('modify').serialize();
	var php_file = "./lib/modify.php?"+  serializedForm;

	new Ajax.Updater('event_edit', php_file  , { method: 'get' });

	var php_file2 = "./lib/timeline.php?"+  serializedForm;
	new Ajax.Updater("timeline", php_file2  , { method: "get" });
}

function removeevent (subvideo_file, video_id, sv) {
	var serializedForm = $('modify').serialize();
	var php_file = "./lib/remove.php?"+  serializedForm;

	new Ajax.Updater('event_edit', php_file  , { method: 'get' });

	var php_file2 = "./lib/timeline.php?"+  serializedForm;
	new Ajax.Updater("timeline", php_file2  , { method: "get" });
}

function new_subvideo () {
	// validator:
	// ...
	document.getElementById('msg').style.visibility= 'hidden';
	// variables del form amb prototype.js
	var serializedForm = $('new_subvideo').serialize();
	var php_file = "./lib/new_subvideo.php?adding=yes&"+  serializedForm;
	//alert (serializedForm);
	// ajax que envia variables del form a una funcio php que afegeix nou event
	new Ajax.Updater('event_edit', php_file  , { method: 'get' });
}

function new_video_check () {
	// validator:
	// ...	

	// variables del form amb prototype.js
	var serializedForm = $('new_video_check').serialize();
	var php_file = "./lib/new_video_check.php?" + serializedForm;

	// ajax que envia variables del form a una funcio php que fa check del video
	new Ajax.Updater('new_video_check', php_file  , { method: 'get' });
	document.getElementById('new_video_loading').style.visibility= 'hidden';
}

function new_video_register () {
	// validator:
	// ...

	// variables del form amb prototype.js
	var serializedForm = $('newvideoregister').serialize();
	var php_file3 = './lib/new_video_register.php?' + serializedForm;
	new Ajax.Updater('home_foot', php_file3  , { method: 'get' });
}

function search_sv (q)  {
	var serializedForm = $('search').serialize();
	php_file = 'lib/browser.php?home=yes&' + serializedForm;
	new Ajax.Updater('home_foot', php_file, { method: 'get' });
}
function search_sv2 (id) {
	var serializedForm = $('search').serialize();
	php_file = 'lib/browser.php?home=yes&editing=yes&' + serializedForm;
	new Ajax.Updater('home_foot3', php_file, { method: 'get' });
	Element.show('home_foot3');
}

function new_video_register_add () {
	// validator:
	// ...
	// variables del form amb prototype.js
	var serializedForm = $('newvideoregister').serialize();
	var php_file3 = './lib/new_subvideo_register.php?' + serializedForm;
	new Ajax.Updater('event_edit_add', php_file3  , { method: 'get' });
}

function reload_js_events(subvideofile)  {
	var php_file4 = './lib/reload_js_events.php?subvideofile=' + subvideofile;
	new Ajax.Updater('js', php_file4  , { method: 'get' });
}


function activate_tab(active_id) {

	/* show which tab is active */
	var kids = $('editor_tabs').childElements();
	for (i=0; i<kids.length; i++) {
		var links = kids[i].childElements();
		var link = links[0];
		if (link.hasClassName('selected')) {
			link.removeClassName('selected');
		}
		var divid = link.id + '-div';
		// hide the other divs
		if ($(divid)) {
			$(divid).hide();
		}
		//$(divid).hide();
	}
	var active_div = active_id + '-tab-div';
	$(active_id + '-tab').toggleClassName('selected');
	/* show the active div */
	if ($(active_div)) { 
		$(active_div).show();
	}
}

/* browser */

function show_sv_details(id, sv, searchstr, length) {
   new Ajax.Updater(
	'matching-subvideos-' + sv + '-details', 
	'lib/matching_subvideos.php?id='+ id +'&sv='+ sv + '&q=' + searchstr + '&length=' + length, 
	{ method: 'get' }
	);
}

function hide_sv_details(id, sv, searchstr, length) {
	$('matching-subvideos-' + sv + '-details').innerHTML = '<a href="#" onclick="show_sv_details('+ id +', '+ sv + ', \'' + searchstr + '\', \''+ length +'\'); return false">show detailed matches</a>';
}

function disable_other(i) {
	if (i == '1') { 
		document.upload_video.videourl.value = "";
		document.upload_video.videourl.disabled = true;
		document.upload_video.Uploadvideo.disabled = false;
	}
	if (i == '0') { 
		document.upload_video.Uploadvideo.value = "";
		document.upload_video.Uploadvideo.disabled = true;
		document.upload_video.videourl.disabled = false;
	}
}

function toggle_description(id) {
	$('description-short-' + id).toggle();
	$('description-' + id).toggle();
}

function update_video_info (video_id) {
	var serializedForm = $('edit_video_info').serialize();
	var php_file = "./lib/edit_video_info.php?"+  serializedForm;

	new Ajax.Updater('video_info', php_file, { method: 'get' });
}

function update_subvideo_info (sv) {
	var serializedForm = $('edit_subvideo_info').serialize();
	var php_file = "./lib/edit_subvideo_info.php?"+  serializedForm;

	new Ajax.Updater('subvideo_info', php_file, { method: 'get' });
}


function init_add_event_tab(videoid, subvideo_id, video_length) {
	var url = 'lib/add.php?id=' + video_id + '&sv=' + subvideo_id + '&video_length_s=' + video_length;
	// alert(url);
 	new Ajax.Updater('add-tab-div', url, { 
		method: 'get',
		onComplete: function(activate_handles) { 
/*
			start_slider = new Control.Slider('handlefrom', 'trackfrom', {
				range: $R(0, video_length),
				sliderValue: 1,
				onSlide: function(v) { $('start_s').value = v.toFixed(0) },
				onChange: function(v) { $('start_s').value = v.toFixed(0) }
			});
			end_slider = new Control.Slider('handleto', 'trackto', {
				sliderValue: 1,
				range: $R(0, video_length),
				onSlide: function(v) { $('end_s').value =  v.toFixed(0) },
				onChange: function(v) { $('end_s').value =  v.toFixed(0) }
			});
*/

			time_slider = $('time_slider');
			start_s = $('start_s');
			end_s = $('end_s');

			js_slider = new Control.Slider(time_slider.select('.handle'), time_slider, {
				range: $R(0, video_length),
				spans: ["time_slider_span"],
				restricted: true,
				sliderValue: [0, 10],
				onSlide: function(values) { 
					start_s.value = Math.round(values[0]); 
					end_s.value = Math.round(values[1]);
				},
				onChange: function(values) { 
					start_s.value = Math.round(values[0]); 
					end_s.value = Math.round(values[1]);

				}
			});
		}
	}); 
}

function setSliderValue(myslider, handleindex, value) {
	// due to onChange code above we need this or 
	// a 0 will be put in the text box when you delete the value
	if (value == '') return;
	
	if (isNaN(value))
		myslider.setValue(0, handleindex);
	else
		myslider.setValue(value, handleindex);
}
