var VideoAutoPause = false;
function onYouTubePlayerReady(playerid)
{
    $('player').addEventListener("onStateChange", "PlayerStateChanged");
	new PeriodicalExecuter(VideoUpdateProgress, 0.25);
    $('progress').observe("click", VideoProgressClick);
    PlayVideo("http://www.youtube.com/v/FHr2cxHzBHo");
	VideoAutoPause = true;
}

function PlayerStateChanged(state)
{
    switch (state)
    {
        case -1: // unstarted
            VideoSetButton('play_pause', 'disable', 'play');
            VideoShowVolume();
            break;
        case 0: // ended
			VideoSetButton('play_pause', null, 'play');
            break;
        case 1: // playing
			VideoSetButton('play_pause', null, 'pause');
			if (VideoAutoPause) 
			{
				$('player').pauseVideo();
				VideoAutoPause = false;
			}
			break;
        case 2: // paused
			VideoSetButton('play_pause', null, 'play');
			break;
        case 3: // buffering
            VideoSetButton('play_pause', 'off', 'play');
			if (VideoVolume) VideoSetVolume(VideoVolume);
			break;
        case 5: // video cued
            VideoSetButton('play_pause', 'off', 'play');
			VideoShowVolume();
			break;
    }
}

function PlayVideo(url)
{
    var p = $('player');
	p.stopVideo();
    p.clearVideo();
	VideoShowProgress(0, 0);
    p.loadVideoByUrl(url);
}

function VideoPlayPause()
{
	var p = $('player');
	if (p.getPlayerState() == 1)
        p.pauseVideo();
    else
        p.playVideo();
}

function VideoSetButton(btn, state, art)
{
	var url_parts = $(btn).src.match(/^(.*)\/([^\/]*)$/);
	var file_parts = url_parts[2].split(".");
	var bits = file_parts[0].split("_");
	
	if (art == null && bits[3] == 'disable') return;
	
	if (state != null) bits[3] = state;
	if (art != null) bits[2] = art;
	
	file_parts[0] = bits.join('_');
	$(btn).src = url_parts[1] + "/" + file_parts.join('.');
}

function VideoSetVolume(v)
{
	VideoVolume = v;
	$('player').setVolume(v);
	VideoShowVolume();
}

function VideoShowVolume()
{
	var v = $('player').getVolume();
	
	$('volumeControl').childElements().each(function(d) { d.removeClassName('light'); });
	
    if (v > 0) $('volume20').addClassName('light');
    if (v > 30) $('volume40').addClassName('light');
    if (v > 50) $('volume60').addClassName('light');
    if (v > 70) $('volume80').addClassName('light');
    if (v > 90) $('volume100').addClassName('light');
}

function VideoUpdateProgress(pe)
{
	var p = $('player');
	
    var total_bytes = p.getVideoBytesTotal();
    var loaded_bytes = p.getVideoBytesLoaded();
    var duration = p.getDuration();
    var current = p.getCurrentTime();
	
	var loaded_pct = (total_bytes > 0) ? (loaded_bytes / total_bytes) * 100 : 0;
	var played_pct = (duration > 0) ? (current / duration) * 100 : 0;
	VideoShowProgress(loaded_pct, played_pct);
}

function VideoShowProgress(loaded, played)
{
	$('loaded').style.right = (100 - loaded) + "%";
	$('played').style.right = (100 - played) + "%";
}

function VideoProgressClick(ev)
{
	var p = $('player');
	var pt = $('progress').viewportOffset();
	var x = ev.clientX - pt.left;
	var pct = x / this.getWidth();
	p.seekTo(p.getDuration() * pct, true);
}

function VideoValidateEntryForm(frm)
{
	var errors = [];
	var errorFields = [];
	var fields = [];
	
	fields.push($('email'));
    if (!IsValidEmailAddress($F('email')))
    {
        errors.push("Please enter a valid email address.");
        errorFields.push($('email'));
    }
	
    fields.push($('video'));
	if (!$F('video').indexOf('http://www.youtube.com/watch') == 0)
    {
        errors.push("Please enter a valid YouTube link.");
        errorFields.push($('video'));
    }
	
    fields.push($('agree').parentNode);
	if ($F('agree') != "1")
	{
		errors.push("You must agree to the contest terms.");
        errorFields.push($('agree').parentNode);
	}
	
    fields.each(function(el) { $(el).removeClassName('error'); });
	if (errors.length > 0)
	{
        errorFields.each(function(el) { $(el).addClassName('error'); });
		alert(errors.join('\n\n'));
		return false;
	}
	
	return true;
}

function VideoOneVote()
{
	$$(".voteButton").each(function(el)
	{
		el.replace("<div class='novoteButton'></div>");
	});
}
