function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}



window.addEvent('domready', function() {
	if (Browser.Engine.trident4 == true) {
		var body = $(document.body);
		var children = body.getChildren();
		children.each(function(kid) {
			kid.dispose();
		});
		body.set('html', '<center>Internet Explorer 6 is no longer supported.<br /><br />We recommend that you upgrade to one of the following browsers:' +
			'<ul><li><a href="http://www.firefox.com/">Mozilla Firefox</a></li><li><a href="http://www.microsoft.com/windows/products/winfamily/ie/default.mspx">Internet Explorer (latest)</a></li></ul></center>');
		return;
	}
	
	var scale = new ScaleOfFail();
	var mail = new MailForm();
	//var vidscroll = new VideoScroller();
	
	if (Browser.Plugins.Flash.version < 9 || (Browser.Plugins.Flash.version == 9 && Browser.Plugins.Flash.build < 115)) {
		var body = $(document.body);
		body.grab(new Element('div', {
			html: 'Your Adobe Flash player is out of date and does not support HD content. Click <a href="http://www.adobe.com/products/flashplayer/">here</a> to update.',
			styles: {
				'position': 'absolute',
				'z-index': '10',
				'top': '16px',
				'left': '0',
				'width': '100%',
				'text-align': 'center',
				'color': '#ff0000',
				'font-weight': 'bold'
			}
		}));
	}
	
	$('logo').addEvent('click', function() {
		window.location = 'http://www.todaysbigfail.com/';
	});
	
	// Prev and Next button effects
	var prevButton = $('prevButton');
	var nextButton = $('nextButton');
	if (prevButton != null) {
		var prevButtonFx = new Fx.Tween(prevButton, {
			property: 'right',
			duration: 150,
			fps: 33,
			transition: Fx.Transitions.Sine.easeOut
		});

		prevButton.addEvent('mouseenter', function() {
			prevButtonFx.start(495);
		});
		prevButton.addEvent('mouseleave', function() {
			prevButtonFx.start(490);
		});
	}
	if (nextButton != null) {
		var nextButtonFx = new Fx.Tween(nextButton, {
			property: 'left',
			duration: 150,
			fps: 33,
			transition: Fx.Transitions.Sine.easeOut
		});
		nextButton.addEvent('mouseenter', function() {
			nextButtonFx.start(490);
		});
		nextButton.addEvent('mouseleave', function() {
			nextButtonFx.start(485);
		});
	}

	makeScrollbar( $('gallery'), $('gallery-scroll'), $('scrollhandle'), true, false );
});

var ScaleOfFail = new Class({
	initialize: function() {
		this.voteresult = $('voteresult');
		this.voteresultFx = new Fx.Tween(this.voteresult, {
			property: 'opacity',
			duration: 100,
			fps: 33,
			link: 'cancel'
		});
		
		for(var i = 1; i <= 5; i++)
		{
			this.resizeBar(i, failValues[i-1], false);
			this.setupNums(i);
		}
	},
	reset: function(){
		for(var i = 1; i <= 5; i++)
		{
			this.resizeBar(i, 0, true);
		}
		for(var i = 1; i <= 5; i++)
		{
			this.resizeBar(i, failValues[i-1], false);
		}
	},
	setupNums: function(i) {
		var num = $('ratethefail_' + i);
		var scale = this;
		
		var numFx = new Fx.Tween(num, {
			property: 'margin-top',
			duration: 150,
			fps: 33,
			link: 'cancel',
			transition: Fx.Transitions.Sine.easeOut
		});
		num.addEvent('click', function() {
			scale.recordVote(i);
		});
		num.addEvent('mouseenter', function() {
			scale.checkDescFirst(function() {
				scale.showFailDesc(i);
			});
			if (clearDescription)
				numFx.start(-3);
		});
		num.addEvent('mouseleave', function() {
			scale.clearDesc(i);
			numFx.start(0);
		});
	},
	resizeBar: function(i, iHeight, fast) {
		var bar = $('failbarmid' + i);
		var barContainer = $('failbar' + i);
		iHeight = 50 * iHeight / 100
		var speed = 500;

		if( fast == true )
			speed = 1;

		barFx = new Fx.Morph(bar, {
			duration: speed,
			fps: 33,
			transition: Fx.Transitions.Sine.easeInOut
		});
		 contFx = new Fx.Morph(barContainer, {
			duration: speed,
			fps: 33,
			transition: Fx.Transitions.Sine.easeInOut
		});
		barFx.start({
			'height': iHeight
		});
		contFx.start({
			'top': 47 -  iHeight
		});
	},
	recordVote: function(rating) {
		var basePath = "http://www.todaysbigfail.com/todaysbigfail/vote/" + dateString + "/" + rating;
		var scale = this;
		var req = new Request({url:basePath, 
			onSuccess: function(txt) {
				scale.checkDescFirst(function() {
					scale.voteresult.set('text', txt);
					scale.voteresultFx.start(1);
				});
				clearDescription = false;
			},
			onFailure: function() {
				scale.checkDescFirst(function() {
					scale.voteresult.set('text', 'Vote Failed');
					scale.voteresultFx.start(1);
				});
				clearDescription = false;
			}
		});
		req.send();
	},
	showFailDesc: function(rating) {
		if (!clearDescription)
			return;

		var failText = "";
		switch(rating)
		{
			case 1:
			  failText = "No Fail";
			  break;    
			case 2:
			  failText = "Minor Fail";
			  break;
			case 3:
			  failText = "Fail";
			  break;
			case 4:
			  failText = "LOL Fail";
			  break;
			case 5:
			  failText = "EPIC FAIL!";
			  break;
		}
		this.voteresult.set('text', failText);
		this.voteresultFx.start(1);
	},
	clearDesc: function() {
		if( clearDescription == true ) {
			//this.voteresult.set('text', '');
			this.voteresultFx.start(0);
		}
	},
	checkDescFirst: function(f) {
		if (this.voteresult.getOpacity() > 0.01 && clearDescription) {
			this.voteresultFx.start(0).chain(f);
		} else {
			f();
		}
	}
});

var MailForm = new Class({
	initialize: function() {
		var button = $('mail');
		var form = $('mailform');
		var input = $('mailinput');
		var submit = $('mailsend');
		var mstatus = $('mailstatus');
		
		var formFx = new Fx.Tween(form, {
			property: 'opacity',
			duration: 200,
			fps: 33
		});
		
		var inputFx = new Fx.Tween(input, {
			property: 'border-color',
			duration: 150,
			fps: 33
		});
		
		var mstatusFx = new Fx.Tween(mstatus, {
			property: 'opacity',
			duration: 200,
			fps: 33
		});
		
		form.setOpacity(0);
		form.setStyle('visibility', 'hidden');
		button.addEvent('click', function() {
			if (form.getStyle('visibility') == 'hidden') {
				formFx.start(1);
			} else {
				formFx.start(0);
			}
		});
		
		input.set('value', 'Enter email');
		input.addEvent('mouseenter', function () {
			inputFx.start('#444');
		});
		input.addEvent('mouseleave', function () {
			inputFx.start('#eee');
		});
		input.addEvent('focus', function () {
			if (input.get('value') == 'Enter email') {
				input.set('value', '');
			}
		});
		input.addEvent('blur', function () {
			if (input.get('value') == '') {
				input.set('value', 'Enter email');
			}
		});
		
		mstatus.setOpacity(0);
		var setStatus = function(v) {
			mstatus.set('text', v);
			mstatusFx.start(1).chain(function() {
				mstatusFx.start.delay(3000, mstatusFx, 0);
			});
		};
		
		form.addEvent('submit', function () {
			var value = input.get('value');
			if (value.length > 5) {
				var req = new Request({
					url: '/subscribe', 
					onSuccess: function(txt) {
						if (txt == 'success') {
							setStatus('Email subscribed!');
						} else if (txt == 'exists') {
							setStatus('You have already subscribed!');
						} else {
							setStatus('Failed to subscribe.');
						}
					},
					onFailure: function() {
						setStatus('Failed to subscribe.');
					}
				});
				req.send('__email='+ value);
			}
			return false;
		});
	}
});

/*
var VideoScroller = new Class({
	initialize: function() {
		var vidscroll = $('vidscroll');
		var vidscroll2 = $$('#vidscroll div')[0];
		var scrollFx = new Fx.Scroll(vidscroll, {
			wheelStops: false,
			transition: Fx.Transitions.Quad.easeOut
		});
		
		var left = $('vidscroll_left');
		left.addEvent('click', function() {
			var scroll = vidscroll.getScroll();
			if (scroll.x >= 102) {
				scrollFx.start(scroll.x - 102);
			} else if (scroll.x != 0) {
				scrollFx.start(0);
			}
		});
		
		var right = $('vidscroll_right');
		right.addEvent('click', function() {
			var scroll = vidscroll.getScroll();
			var diff = vidscroll2.getStyle('width').toInt() - scroll.x;
			if (diff >= 102) {
				scrollFx.start(scroll.x + 102);
			} else if (diff != 0) {
				scrollFx.start(vidscroll2.get('width').toInt());
			}
		});
	}
});
*/
