function initialize()
{
	Util.resize();
	
	var textsize = Cookie.get('mw_textsize');
	
	if ( textsize )
	{
		if ( textsize == "smaller" )
		{
			Textsize.smaller();
		}
		else
		{
			Textsize.bigger();
		}
	} 
}

var Textsize = {
	bigger: function()
	{
		var container = $('content-body');
		
		if ( !container )
		{
			return;
		}
		
		bigger_image = container.down('a.text-size-bigger img');
		smaller_image = container.down('a.text-size-smaller img');
		
		container.setStyle({fontSize: '14px'});
		bigger_image.src = "/images/mw/text_bigger_disabled.png";
		smaller_image.src = "/images/mw/text_smaller.png";
		
		Images.fix(bigger_image);
		Images.fix(smaller_image);
		
		Cookie.set('mw_textsize', 'bigger', 7);
	},
	
	smaller: function()
	{
		var container = $('content-body');
		
		if ( !container )
		{
			return;
		}
		
		bigger_image = container.down('a.text-size-bigger img');
		smaller_image = container.down('a.text-size-smaller img');
		
		container.setStyle({fontSize: ''});
		smaller_image.src = "/images/mw/text_smaller_disabled.png";
		bigger_image.src = "/images/mw/text_bigger.png";
		
		Images.fix(bigger_image);
		Images.fix(smaller_image);
		
		Cookie.set('mw_textsize', 'smaller', 7);
	}
}

var Slideshow = Class.create({
	seconds: null,
	
	images: null,
	timer: null,
	current: null,
	running: false,
	locked: false,
	
	initialize: function(container, seconds)
	{
		container = $(container);
		this.seconds = seconds || 10;
		
		//this.images = container.getElementsByClassName('image-holder');
		this.images = $$('div.image-holder');
		
		var max_height = Util.getMaxHeight(this.images);
		
		container.setStyle({
			position: 'relative',
			height: max_height + 'px'
		});
		
		this.images.each( function(element) {
			element.setStyle({
				position: 'absolute',
				height: max_height + 'px',
				display: 'none'
			});
		});
		
		this.images[0].show();
		this.current = 0;
		
		// Attach listeners
		$('slideshow-previous').observe('click', this.previous.bind(this));
		$('slideshow-play').observe('click', this.toggle.bind(this));
		$('slideshow-next').observe('click', this.next.bind(this));	
		
		this.play();	
	},
	
	play: function()
	{
		this.timer = setInterval(this.next.bind(this), this.seconds * 1000);
		$('slideshow-play').src = "/images/mw/pause_small.png";
		Images.fix('slideshow-play');
		this.running = true;
	},
	
	pause: function()
	{
		clearInterval(this.timer);
		$('slideshow-play').src = "/images/mw/play_small.png";
		Images.fix('slideshow-play');
		this.running = false;
	},
	
	toggle: function()
	{
		if ( !this.running )
		{
			this.play();
		}
		else
		{
			this.pause();
		}
	},
	
	previous: function()
	{
		if ( this.current == 0 )
		{
			var previous = this.images.length - 1;
		}
		else
		{
			var previous = this.current - 1;
		}
		
		this.effects(this.current, previous);
	},
	
	next: function()
	{
		if ( this.current == (this.images.length - 1) )
		{
			var next = 0;
		}
		else
		{
			var next = this.current + 1;
		}
		
		this.effects(this.current, next);
	},
	
	effects: function(before, after)
	{
		if ( this.locked )
		{
			return;
		}
		
		this.locked = true;
		
		new Effect.Fade(this.images[before], {duration: 0.75});
		new Effect.Appear(this.images[after], {duration: 0.75, afterFinish: this.unlock.bind(this)});
		
		this.current = after;
	},
	
	unlock: function()
	{
		this.locked = false;
	}
});

function selected_style(id, box, arrow, text)
{
	//Initialise all boxes
	if ($(box+"1"))
	{
		$(box+"1").removeClassName("vid-selected");
	}
	if ($(box+"2"))
	{
		$(box+"2").removeClassName("vid-selected");
	}
	if ($(box+"3"))
	{
		$(box+"3").removeClassName("vid-selected");
	}
	
	$(box+id).addClassName("vid-selected");
	//change white arrow location
	$(arrow).removeClassName("first-select");
	$(arrow).removeClassName("second-select");
	$(arrow).removeClassName("third-select");
	
	if ($(text+"1"))
	{
		$(text+"1").setStyle("display:none");	
	}
	if ($(text+"2"))
	{
		$(text+"2").setStyle("display:none");	
	}
	if ($(text+"3"))
	{
		$(text+"3").setStyle("display:none");	
	}
	
	$(text+id).setStyle("display:block");
	//Display hover style and text according to selected box
	if (id == "1")
	{
		$(arrow).addClassName("first-select");
	}
	else if (id == "2")
	{
		$(arrow).addClassName("second-select");
	}
	else
	{
		$(arrow).addClassName("third-select");
	}
}


function select_mm(id, text, text_container, mm_container)
{
	element = $$("#"+mm_container+" .mm-item");
	element.each(function(e){e.removeClassName("selected");});
	$(id).addClassName("selected");
	
	element = $$("#"+text_container+" .mm-text");
	element.each(function(e){e.removeClassName('show-text');});
 	$(text).addClassName("show-text");
}


function selected_tab_style(id)
{
	$("white-arrow").removeClassName('select1');
	$("white-arrow").removeClassName('select2');
	$("white-arrow").removeClassName('select3');
	$("white-arrow").addClassName('select'+id);
}


// Attach events
document.observe('dom:loaded', initialize);