/*
 * EndlessYouTube
 * Copyright (c) EndlessYouTube, http://www.endlessyoutube.com/
 */

var appName = (navigator.appName == 'Microsoft Internet Explorer') ? 'MIE' : null;

var $ = function(id)
{
	return document.getElementById(id);
}

function searchYouTubeVideo()
{
	var search_term = J('#search_term').val();
	J('#search_results').fadeOut();
	J('#search_loader').show();
	J('#search_results').load('/search.php?q=' + encodeURIComponent(search_term), function () {
		J('#search_loader').hide();
		J(this).fadeIn();
	});
}

function repeat(start_m, start_s, end_m, end_s)
{
	this.start = parseInt(start_m) + parseInt(start_s);
	this.end = parseInt(end_m) + parseInt(end_s);
	this.duration = parseInt(this.end) - parseInt(this.start);

	if (this.end > ytplayer.getDuration()) {
		alert('The end time must be within the video length.');
		return;
	}

	if (this.duration < 1) {
		alert('Only a more than 1 second loop duration is allowed.');
		return;
	}

	var display_start_m = start_m / 60;
	var display_end_m = end_m / 60;
	display_start_s = start_s < 10 ? '0' + start_s : start_s;
	display_end_s = end_s < 10 ? '0' + end_s : end_s;
	
	// $("display_duration").innerHTML = display_start_m + ':' + display_start_s + ' - ' + display_end_m + ':' + display_end_s;
	// $('url_v').innerHTML = $('loadvideoid').value;
	
	J('#share_time').show();
	J('#share_start_time').html(display_start_m + 'm' + start_s + 's');
	J('#share_end_time').html(display_end_m + 'm' + end_s + 's');
	J('#share_link').attr('href', J('#share_url_2').text());
	
	$('repeat_stop').style.display = '';
	$('repeat').style.display = 'none';

	this.seekTo = function()
	{
		if (ytplayer) {
			ytplayer.seekTo(this.start, true);
			this.timer();
		}
	}

	this.updateTime = function()
	{
		if (this.end < ytplayer.getCurrentTime()) {
			clearInterval(setinterval);
			this.seekTo();
		}
	}

	this.timer = function()
	{
		setinterval = setInterval('this.updateTime()', 100);
	}

	this.seekTo();
}

function bodyOnLoad(id)
{
	setTimeout("loadNewVideo('" + id + "', true)", 1000);
}

function onYouTubePlayerReady(playerId)
{
	ytplayer = $('ytapiplayer');
	ytplayer.addEventListener('onStateChange', 'onytplayerStateChange');
}

function loadNewVideo(id, from_url)
{
	if (id.length != 11) {
		alert('VIDEO_ID must be 11 characters.');
		return;
	} else {
		J.get('/recent.php?video_id=' + encodeURIComponent(id));
	}

	var date = new Date();
	J.cookie('video_id[' + date.getTime() + ']', id, { expires: 30 });

	if (from_url != true) {
		location.href = '/watch?v=' + id;
		return;
	}

	J('#share_not_yet').fadeOut(function() {
		J('#share').fadeIn();
	});
	
	J('#share_video_id').html(id);
	J('#share_link').attr('href', J('#share_url_1').text());

	if (typeof(setinterval) != 'undefined') {
		clearInterval(setinterval);
	}

    var params = { allowScriptAccess: 'always', allowfullscreen: 'true' };
    var atts = { id: 'ytapiplayer' };

    swfobject.embedSWF('http://www.youtube.com/v/' + id + '&enablejsapi=1&playerapiid=ytplayer&autoplay=1&loop=1&rel=0&fs=1&hd=1',
		'ytapiplayer', '640', '385', '8', null, null, params, atts);

	$('duration').style.display = 'none';
	$('start_m').selectedIndex = '';
	$('start_s').selectedIndex = '';
	$('end_m').selectedIndex = '';
	$('end_s').selectedIndex = '';
	$('repeat').style.display = '';
	$('repeat_stop').style.display = 'none';
}

function onytplayerStateChange(newState)
{
	if (newState == 1) {

		$('duration').style.display = '';

		;

		if (typeof(query) != 'undefined' && query == 'time_set_by_url') {
			var selectedValue = { start_m: q_start_m, start_s: q_start_s, end_m: q_end_m, end_s: q_end_s }
		} else {
			var selectedValue = { start_m: $('start_m').value / 60, start_s: $('start_s').value, end_m: $('end_m').value / 60, end_s: $('end_s').value }
		}
		
		var minute = ytplayer.getDuration() / 60;
		var minute = parseInt(minute) + 1;
		var option = '<option value="0">0</option>';
		var select = '';

		for (i = (appName == 'MIE') ? 0 : 1; i < minute; i++) {
			var minuteValue = i * 60;
		    if (appName == 'MIE') {
				select += '<option value="' + minuteValue + '">' + i + '</option>';
			} else {
				option += '<option value="' + minuteValue + '">' + i + '</option>';
			}
		}

	    if (appName == 'MIE') {
			$('start_m').outerHTML = '<select id="start_m">' + select + '</select>';
			$('end_m').outerHTML = '<select id="end_m">' + select + '</select>';
	    } else {
			$('start_m').innerHTML = option;
			$('end_m').innerHTML = option;
	    }

		for (var i in selectedValue) {
			$(i).selectedIndex = selectedValue[i];
		}

		if (query == 'time_set_by_url' && typeof(through) == 'undefined') {
			through = 'yes';
			repeat(q_start_m * 60, q_start_s, q_end_m * 60, q_end_s);
		}

	}
}

function stopRepeat()
{
	clearTimeout(setinterval);
	$('repeat').style.display = '';
	$('repeat_stop').style.display = 'none';
	
	J('#share_time').hide();
	J('#share_link').attr('href', J('#share_url_1').text());
}
