
Event.observe(window, 'load', function(){

		
	document.delegate('a.more-news', 'click', function(event){
		if (!this.hasAttribute('data-insert-target'))
			return true;
		event.stop();

		new Ajax.Request(this.href + '?offset=' + this.readAttribute('data-article-offset'), {
			onCreate: function() {
				this.addClassName('loading');
			}.bind(this),
		    onSuccess: function(transport) {
		    	if (!transport.responseText)
		    		this.fade();
		    	var element = new Element('div').update(transport.responseText);
				$(this.readAttribute('data-insert-target')).insert({ bottom : element });
		    	this.removeClassName('loading');
		    	this.writeAttribute('data-article-offset', parseInt(this.readAttribute('data-article-offset')) + 3);
		    	new Effect.ScrollTo(element.select('.article').first().identify());
		    }.bind(this)
		});
		
	});
	
	var Showreel = Class.create({
		initialize : function(element, offsetStep, maxOffset) {
			this.slider = element;
			this.offsetStep = offsetStep;
			this.maxOffset = maxOffset;
			this.direction = true;
			this.isSliding = false;
			
			this.timer = new PeriodicalExecuter(function() {
				this[this.direction ? 'next' : 'previous']();
			}.bind(this), 4);
			
		
			document.delegate('#nominees-reel a', 'click', function(event) {
				event.stop();
				this.timer.stop();
				if (!this.isSliding) 
					this[event.element().hasClassName('next') ? 'next' : 'previous']();
			}.bind(this));
		},
		
		move : function(direction) {
			this.isSliding = true;
			new Effect.Move(this.slider, { 
				x : direction * this.offsetStep, 
				y : 0, 
				mode: 'relative',
				afterFinish : function() {
					this.isSliding = false;
				}.bind(this)
			});
		},
	
		next : function() {
			if (this.slider.getStyle('left').sub('px','') > -this.maxOffset)
				this.move(-1);
			else
				this.direction = false;
		},
		
		previous : function() {
			if (this.slider.getStyle('left').sub('px','') < 0)
				this.move(1);
			else
				this.direction = true;
		}
	});
	
	if ($('nominees-reel-slider'))
		new Showreel($('nominees-reel-slider'), 252, 3780);
	
	document.delegate('a.schedule-deregistration', 'click', function(event) {
		if (!window.confirm('Er du sikker på at du vil melde deg av dette kurset?'))
			event.stop();
	});


	

	document.delegate('#nominees-reel a.video-play', 'click', function(event){
		event.stop();
		
		tpl  = '<div id="lightbox">';
		tpl += '	<div class="lightbox-center">';
		tpl += '		<div class="lightbox-frame">';
		tpl += '			<a href="#" id="lightbox-close" onclick="$(\'lightbox\').remove(); return false;">✖</a>';
			
		tpl += '			<object id="flashObj" width="700" height="442">';
		tpl += '			    <param name="movie" value="http://c.brightcove.com/services/viewer/federated_f9/1909243188?isVid=1&publisherID=376816878" />';
		tpl += '			    <param name="flashVars" value="videoId=#{movie}&playerID=1909243188&domain=embed&" />';
		tpl += '			    <param name="base" value="http://admin.brightcove.com" />';
		tpl += '				<param name="seamlesstabbing" value="false" />';
		tpl += '			    <param name="allowFullScreen" value="true" />';
		tpl += '				<param name="swLiveConnect" value="true" />';
		tpl += '			    <param name="allowScriptAccess" value="always" />';
		tpl += '			    <embed src="http://c.brightcove.com/services/viewer/federated_f9/1909243188?isVid=1&publisherID=376816878" flashVars="videoId=#{movie}&playerID=1909243188&domain=embed&" base="http://admin.brightcove.com" name="flashObj" width="700" height="442" seamlesstabbing="false" type="application/x-shockwave-flash" allowFullScreen="true" swLiveConnect="true" allowScriptAccess="always" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
		tpl += '			</object>';
			
		tpl += '		</div>';
		tpl += '	</div>';
		tpl += '</div>';		
		
		var lightbox = new Template(tpl);
		var assign = { movie: this.data('movie') };
		
		$$('body')[0].insert({ top: lightbox.evaluate(assign) });
		
	});

});

document.observe('dom:loaded', function() {
	$$('form.label-is-placeholder input').each(function(i) {
		i.observe('focus', function() {
			i.previous('label').hide();
		});
	});
	
	$$('input.value-is-placeholder').each(function(i) {
		i.observe('focus', function() {
			var placeholder = i.retrieve('placeholder', null);
			if (placeholder === null)
				i.store('placeholder', i.getValue());
			if (i.getValue() == i.retrieve('placeholder'))
				i.clear();
		})
	});
	
	$$('*[data-movie-url]').each(function(element) {
		var image = element.select('img').first();
		element.insert($E('div', { 'class' : 'play-button' }));	
		if (!image) return;

		element.observe('click', function(event) {
			swfobject.embedSWF(
				element.readAttribute('data-movie-url'), 
				element.identify(), 
				image.getWidth(), 
				image.getHeight(), 
				'9.0.0',
				null,
				{ autoplay : 1 },
				{ allowScriptAccess: 'always', allowfullscreen : true, autoplay : 1 }
			);
			event.stop();
		});
	});

})