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/rsp/pause.gif";
		Images.fix('slideshow-play');
		this.running = true;
	},
	
	pause: function()
	{
		clearInterval(this.timer);
		$('slideshow-play').src = "/images/rsp/play.gif";
		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 popup(url) 
{
   var w = document.viewport.getWidth();
   width = (w/2)-200;
   window.open("http://www.winemag.co.za/" + url,"_blank", 'width=475,height=250,scrollbars=no');
}
function popUpForm(id) {
 var w = document.viewport.getWidth();
 width = (w/2)-200;
var win = window.open("http://www.winemag.co.za/forms/view.php?id="+id, null, "status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes,width=520,height=850,left = "+ width + ",top = 34");
 //return false;

}

// Attach events
document.observe('dom:loaded', initialize);