document.observe("dom:loaded",
	function() {
		rt_anim.initialize();
	}
);

Event.observe(window,'resize',
	function() {
		rt_anim.resize();
	}
);

var rt_anim = {
	objs: $A([]),
	bodies: $A([]),
	layout: -1,
	nav_buttons: $A([]),
	nav_highlights: $A([]),
	timer: null,
	current: -1,
	count: 0,
	container: null,
	sizer: null,
	nav_div: null,
	body_div: null,
	new_body_div: null,
	
	initialize: function() {
		var i;
		
		rt_anim.count = rt_anim.objs.size();
		rt_anim.container = $('rt_animator_div');
		rt_anim.container.setStyle({
			width: '100%',
			height: '300px'
		});
		
		rt_anim.sizer = $('rt_animator_sizer');
		
		rt_anim.nav_div = $(document.createElement("div"));
		for(i=0;i<rt_anim.count;++i) {
			var button = $(document.createElement("div"));
			button.setStyle({
				width: '100%',
				height: '74px',
				overflow: 'hidden',
				borderRight: '1px solid #999',
				borderBottom: '1px solid #999'
			});
			var highlight = $(document.createElement("div"));
			highlight.setStyle({
				backgroundColor: 'white',
				width: '100%',
				height: '75px',
				position: 'absolute',
				top: '0px',
				left: '0px',
				zIndex: '5'
			});
			highlight.setOpacity(0.5);
			highlight.active = false;
			
			highlight.observe("mouseover", function() {
				this.setOpacity(0);
				this.ownsMouse = true;
			});
			highlight.observe("mouseout",function() {
				if(this.active==false)
					this.setOpacity(0.5);
				this.ownsMouse = false;
				if(rt_anim.timer==null)
					rt_anim.timer = setTimeout("rt_anim.advance()",15000);
			});
			highlight.observe("click",rt_anim.onButtonClick);
			
			button.insert(rt_anim.objs[i].getButtonContents());
			button.insert(highlight);
			rt_anim.nav_div.insert(button);
			rt_anim.nav_buttons[i] = button;
			rt_anim.nav_highlights[i] = highlight;
		}
			
		rt_anim.body_div = $(document.createElement("div"));
		rt_anim.body_div.innerHTML = rt_anim.container.innerHTML;
		rt_anim.body_div.setStyle({
			border: '1px solid #999',
			width: '480px',
			zIndex: '1',
			cursor: "pointer"
		});
		rt_anim.body_div.observe("click",rt_anim.onBodyClick);
		
		rt_anim.new_body_div = $(document.createElement("div"));
		rt_anim.new_body_div.setStyle({
			position: "absolute",
			top: '0px',
			left: '0px',
			width: '480px',
			border: '1px solid #999',
			zIndex: '2',
			cursor: "pointer"
		});
		rt_anim.new_body_div.setOpacity(0);
		rt_anim.body_div.observe("click",rt_anim.onBodyClick);
		
		rt_anim.container.innerHTML = "";
		rt_anim.container.insert(rt_anim.body_div);
		rt_anim.container.insert(rt_anim.nav_div);
		rt_anim.container.insert(rt_anim.new_body_div);
		rt_anim.resize();

		rt_anim.timer = setTimeout("rt_anim.advance()",10);
	},
	
	resize: function() {
		var i;
		var max;
		
		var dim = rt_anim.sizer.getDimensions();	
		if(dim.width >= 600) {
			var width = dim.width - 484;
			if(width > 220) width = 220;
			rt_anim.nav_div.setStyle({
				position: 'absolute',
				top: '0px',
				left: '482px',
				width: width + 'px'
			});
			
			if(rt_anim.layout!=0) {
				max = rt_anim.nav_buttons.size();
				for(i=0;i<max;++i) {
					rt_anim.nav_buttons[i].setStyle({
						width: '100%',
						'float': 'none'
					});
				}
				rt_anim.nav_buttons[0].setStyle({
					borderTop: '1px solid #999',
					borderLeft: '0px solid #999',
					height: '75px'
				});
				
				rt_anim.container.setStyle({
					height: '300px'
				});
				rt_anim.layout = 0;
			}
		}
		else {
			if(rt_anim.layout!=1) {
				rt_anim.nav_div.setStyle({
					position: 'relative',
					top: '0px',
					left: '0px',
					width: '482px'
				});
	
				max = rt_anim.nav_buttons.size();
				for(i=0;i<max;++i) {
					rt_anim.nav_buttons[i].setStyle({
						width: '119px',
						'float': 'left'
					});
				}
				rt_anim.nav_buttons[0].setStyle({
					borderLeft: '1px solid #999',
					borderTop: '0px solid #999',
					height: '74px',
					width: '120px'
				});

				rt_anim.container.setStyle({
					height: '375px'
				});
				
				rt_anim.layout = 1;
			}
		}
	},
	
	advance: function() {
		var next = (rt_anim.current + 1) % rt_anim.count;
		if(!rt_anim.objs[next].isReady()) {
			rt_anim.timer = setTimeout("rt_anim.advance()",250);
		}
				
		if(rt_anim.bodies[next]==null)
			rt_anim.bodies[next] = rt_anim.objs[next].getBodyContents();
		else
			rt_anim.objs[next].resetBody();

		if(rt_anim.current!=-1) {
			rt_anim.nav_highlights[rt_anim.current].active = false;
			if(!rt_anim.nav_highlights[rt_anim.current].ownsMouse)
				rt_anim.nav_highlights[rt_anim.current].setOpacity(0.5);
		}
		
		rt_anim.nav_highlights[next].active = true;
		rt_anim.nav_highlights[next].setOpacity(0);
			
		rt_anim.new_body_div.insert(rt_anim.bodies[next]);
		rt_anim.new_body_div.appear({duration: 1.0});
		rt_anim.timer = setTimeout("rt_anim.advance2()",1100);
		rt_anim.stage = 2;
	},
	
	advance2: function() {
		var next = (rt_anim.current + 1) % rt_anim.count;

		if(rt_anim.current!=-1) 
			rt_anim.bodies[rt_anim.current].remove();
		else
			rt_anim.body_div.innerHTML = "";

		rt_anim.bodies[next].remove();
		rt_anim.body_div.insert(rt_anim.bodies[next]);
		rt_anim.new_body_div.setOpacity(0);
		rt_anim.current = next;
		
		rt_anim.timer = setTimeout("rt_anim.advance()",5000);
		rt_anim.stage = 1;
	},
	
	onBodyClick: function(e) {
		if(rt_anim.current==-1) return;
		e.stop();
		
		var Dest = rt_anim.objs[rt_anim.current].getDestination();
		if(Dest!=null) {
			window.location.href = Dest;
		}
	},
	
	onButtonClick: function(e) {
		var i;
		var next = (rt_anim.current + 1) % rt_anim.count;		
		
		e.stop();
		clearTimeout(rt_anim.timer);
		rt_anim.timer = null;
		
		for(i=0;i<rt_anim.count;++i) {
			if(rt_anim.nav_highlights[i].ownsMouse==true) {
				if(rt_anim.current!=-1) 
					rt_anim.bodies[rt_anim.current].remove();
				else
					rt_anim.body_div.innerHTML = "";
					
				if(rt_anim.stage==2) {
					rt_anim.bodies[next].remove();
					rt_anim.new_body_div.setOpacity(0);
					rt_anim.stage = 1;
				}
				
				if(rt_anim.bodies[i]==null)
					rt_anim.bodies[i] = rt_anim.objs[i].getBodyContents();
				else
					rt_anim.objs[i].resetBody();

				rt_anim.body_div.insert(rt_anim.bodies[i]);
				rt_anim.current = i;
				
				rt_anim.nav_highlights[i].active = true;
				rt_anim.nav_highlights[i].setOpacity(0);				
			}
			else {
				rt_anim.nav_highlights[i].active = false;
				rt_anim.nav_highlights[i].setOpacity(0.5);
			}
		}
	}
};

var rt_animator = Class.create({
	initialize: function() {
		this._preload_img = $A([]);
	},

	preload: function(ImgURL) {
		var obj = this;
		var idx = this._preload_img.size();
		this._preload_img[idx] = $(document.createElement("img"));
		this._preload_img[idx].onload = function() {
			obj._preload_img[idx].preload_complete = true;
		};
		this._preload_img[idx].src = ImgURL;
	},
	
	isReady: function() {
		var max = this._preload_img.size();
		var i;
		
		for(i=0;i<max;++i) {
			if(this._preload_img.preload_complete != true)
				return false;
		}
		
		return true;
	},
	
	getButtonContents: function() {
		return this.ButtonDiv;
	},

	getBodyContents: function() {
		return this.BodyDiv;
	},
	
	getAnimationSequence: function() {
		return [];
	},
	
	resetBody: function() {
	
	},
	
	getDestination: function() {
		return null;
	}
});

/***/
var rtGWNCandyCane = Class.create(rt_animator, {
	initialize: function($super) {
		$super();
		this.ButtonDiv = $(document.createElement("div"));
		this.ButtonDiv.innerHTML = "<img src='/img/home/rt_anim/001_gwn_button1.jpg' style='display: block; position: absolute; right: 0px; top: 0px; z-index: 1;' /><img src='/img/home/rt_anim/001_gwn_button2.png' style='display: block; position: absolute; left: 0px; top: 0px; z-index: 2;' />";
		this.BodyDiv = $(document.createElement("div"));
		this.BodyDiv.innerHTML = "<img src='/img/home/rt_anim/001_gwn_back.jpg' style='display: block;' />";
	},
	
	getDestination: function() {
		return "http://www.epicureanfoods.com/geo/pdetail.php/104101";
	}
});

var rtRRSDT = Class.create(rt_animator, {
	initialize: function($super) {
		$super();
		this.ButtonDiv = $(document.createElement("div"));
		this.ButtonDiv.innerHTML = "<img src='/img/home/rt_anim/002_RR_button1.jpg' style='display: block; position: absolute; right: 0px; top: 0px; z-index: 1;' /><img src='/img/home/rt_anim/002_RR_button2.jpg' style='display: block; position: absolute; left: 0px; top: 0px; z-index: 2;' />";
		this.BodyDiv = $(document.createElement("div"));
		this.BodyDiv.innerHTML = "<img src='/img/home/rt_anim/002_RR_base.jpg' style='display: block;' />";
	},
	
	getDestination: function() {
		return "http://www.epicureanfoods.com/geo/pdetail.php/103257";
	}
});

var rtPCPortobello = Class.create(rt_animator, {
	initialize: function($super) {
		$super();
		this.ButtonDiv = $(document.createElement("div"));
		this.ButtonDiv.innerHTML = "<img src='/img/home/rt_anim/003_PC_button.jpg' style='display: block; position: absolute; right: 0px; top: 0px; z-index: 1;' />";
		this.BodyDiv = $(document.createElement("div"));
		this.BodyDiv.innerHTML = "<img src='/img/home/rt_anim/003_PC_back.jpg' style='display: block;' />";
	},
	
	getDestination: function() {
		return "http://www.epicureanfoods.com/geo/pdetail.php/100019";
	}
});

var rtProsBlackBean = Class.create(rt_animator, {
	initialize: function($super) {
		$super();
		this.ButtonDiv = $(document.createElement("div"));
		this.ButtonDiv.innerHTML = "<img src='/img/home/rt_anim/004_prospero_button1.jpg' style='display: block; position: absolute; left: 0px; top: 0px; z-index: 1;' /><img src='/img/home/rt_anim/004_prospero_button2.jpg' style='display: block; position: absolute; right: 0px; top: 0px; z-index: 2;' />";
		this.BodyDiv = $(document.createElement("div"));
		this.BodyDiv.innerHTML = "<img src='/img/home/rt_anim/004_prospero_base.jpg' style='display: block;' />";
	},
	
	getDestination: function() {
		return "http://www.epicureanfoods.com/geo/pdetail.php/103181";
	}
});

rt_anim.objs[0] = new rtGWNCandyCane();
rt_anim.objs[1] = new rtRRSDT();
rt_anim.objs[2] = new rtPCPortobello();
rt_anim.objs[3] = new rtProsBlackBean();
