// State Scope Code
(function(){d=document;e=d.documentElement;c="images-on";i=new Image();t=i.style;s=d.enableStateScope=function(s,o){if(o)e.className+=" "+s;else e.className=e.className.replace(new RegExp("\\b"+s+"\\b"),"");};if(t.MozBinding!=null){t.backgroundImage="url("+d.location.protocol+"//0)";b=window.getComputedStyle(i,'').backgroundImage;if(b!="none"&&b!="url(invalid-url:)"||d.URL.substr(0,2)=="fi")s(c,true);}else{t.cssText="-webkit-opacity:0";if(t.webkitOpacity==0){i.onload=function(){s(c,i.width>0);};i.src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";}else{i.onerror=function(){s(c,true);};i.src="about:blank";}}})();

// Prep for youtube filler-inner
var youtube_requests = new Array();

window.addEvent('domready', function() {
	MakePlaceholders();
	LoadVideos();
});

// Set up placeholders
function MakePlaceholders() {
	$$('[placeholder]').each(function(input) {
		new Form.Placeholder(input);
	});
}

function LoadVideos() {
	var first = youtube_requests.shift();
	if (!first) return;
	MakePlayer(first);

	var others = function() {
		youtube_requests.each(function(r) {
			MakePlayer(r);
		});
	}
	others.delay(1200);
}

function MakePlayer(r) {
	var obj = new Swiff('http://www.youtube.com/v/' + r.youtube_id + '?version=3&enablejsapi=1&fs=1&rel=0&showinfo=0&iv_load_policy=3&feature=player_embedded&playerapiid=ytplayer-' + r.player, {
		id: 'ytplayer-' + r.player,
		width: 480,
		height: 270,
		container: $('ytcontainer-' + r.player),
		params: {
			allowScriptAccess: 'always'
		}
	});
}

// Youtube player ready
var players = new Array();
function onYouTubePlayerReady(playerId) {
	var ytplayer = document.getElementById(playerId);
	ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
	
	players.push(ytplayer);
}

window.addEvent('carousel_moving', function() {
	players.each(function(player) {
		player.pauseVideo();
	});
});

// Film Jump
function JumpToFilm(_urlname) {
	document.location.href = '/events.html?f=' + _urlname;
}

// Calendar Filler
var current_month = '';
function FillCalendar(_month) {
	if (!_month || _month == '-') {
		var date = new Date();
		_month = (date.getYear()+1900) + '-' + zeroFill((date.getMonth()+1),2);
	}
	else if (_month == 'next') {
		var components = current_month.split('-');
		if (components[1] == 12) {
			components[0] = components[0] - 1 + 2;
			components[1] = 0;
		}
		_month = components[0] + '-' + zeroFill((components[1] - 1 + 2),2);
	}
	else if (_month == 'prev') {
		var components = current_month.split('-');
		if (components[1] == 1) {
			components[0] = components[0] - 1;
			components[1] = 13;
		}
		_month = components[0] + '-' + zeroFill((components[1] - 1),2);	
	}
	var request = new Request({
		url: '/cgi-local/calendar.cgi',
		method: 'get',
		onSuccess: function(responseText,responseXML) {
			$('calendar').innerHTML = responseText;
			current_month = _month;
		}
	});
	request.send('month=' + _month);
}

function zeroFill( number, width )
{
  width -= number.toString().length;
  if ( width > 0 )
  {
    return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
  }
  return number;
}
