var slide, slide2, pageup;

window.addEvent('domready', function() {

	/* Main Content Scroller */
	slide = new noobSlide({
		box: $('dScrollCont'),
		items: $$('#dScroller h3.hidden'),
		size: getWidth(),
		handles: $$('#dChooser span'),
		handle_event: 'mouseenter',
		fxOptions: {
						duration: 800,
						transition: Fx.Transitions.Cubic.easeOut,
						wait: false
					}
	});

	slide.walk(0); 
	
	/* Ad Content Scroller */
	slide2 = new noobSlide({
		box: $('dAdScrollCont'),
		items: $$('#dAdScroller h3.hidden2'),
		size: 538,
		handles: $$('#dChooser2 span'),
		handle_event: 'mouseenter',
		autoPlay: true,
		fxOptions: {
						duration: 800,
						transition: Fx.Transitions.Cubic.easeOut,
						wait: false
					}
	});

	slide2.walk(0);
	
	// Sail Menu Hovers
		var list = $$('#dSailMenu a');
		list.each(function(element) {

			var fx = new Fx.Morph(element, {duration:200, wait:false});

			element.addEvent('mouseenter', function(){
				fx.start({
					'color' : '#49b9ff'
				});
			});

			element.addEvent('mouseleave', function(){
				fx.start({
					'color' : '#0175bd'
				});
			});

		});

		// Menu Button Hovers
			var list = $$('.menubutton');
			list.each(function(element) {

				var fx = new Fx.Morph(element, {duration:200, wait:false});

				element.addEvent('mouseenter', function(){
					fx.start({
						'background-color' : element.name,
						'color' : '#ffffff'
					});
				});

				element.addEvent('mouseleave', function(){
					fx.start({
						'background-color' : '#FFFFFF',
						'color' : element.name
					});
				});

			});

	fixsize(); // Set Size of boxes.
			
	// Set Morphs
	
	var morphcalls = $$('.clubcont');
	
	morphcalls.each(function(element) {

		element.set('morph', {
			unit: '%', 
			duration: 800, 
			transition: Fx.Transitions.Cubic.easeOut, 

			onComplete: function(){

				pageup = 1;

			}

		});

	});
	
	pageup = 0;
	
	// Close Buttons
	var closebtn = $$('.closebtn');
	
	closebtn.each(function(element) {

		element.addEvent('click', function(e) {
			
			pageup = 0;
			
			e.stop();
			
			tmp = element.id.split("_");
			
			var clubGrow = new Fx.Morph(tmp[0]+'content', {duration: 800, transition: Fx.Transitions.Cubic.easeOut, unit: "%"});
			var clubGrow2 = new Fx.Morph(tmp[0]+'_close', {duration: 800, transition: Fx.Transitions.Cubic.easeOut, unit: "%"});
			
			// Hide Content
			clubGrow.start({
			    'height': [70, 10],
			    'width': [60, 10],
				'left': [20, 100],
			    'opacity': [.98, 0]
			});

			// Hide Close Button
			clubGrow2.start({
			    'opacity': [1, 0]
			});

			$(element.id).style.display = "none";
			
		});

	});
	
	// Add Menu calls	
	var menucalls = $$('.menubutton');
	
	menucalls.each(function(element) {
		
		tmp = element.id.split("_");
		
		var clubGrow = new Fx.Morph(tmp[0]+'content', {duration: 800, transition: Fx.Transitions.Cubic.easeOut, unit: "%"});
		var clubGrow2 = new Fx.Morph(tmp[0]+'_close', {duration: 800, transition: Fx.Transitions.Cubic.easeOut, unit: "%"});

		element.addEvent('click', function(e) {

			tmp2 = element.id.split("_");
			
			pageup = 1;
			
			e.stop();

			// Minimise all pages
			reset();

			// Maximise one page
			$(tmp[0]+'content').style.opacity = 0;
			
			$(tmp[0]+'content').style.height = 10;
			$(tmp[0]+'content').style.width = 10;
			$(tmp[0]+'content').style.left = 100;
			$(tmp[0]+'content').style.opacity = 0;

			clubGrow.start({
			    'height': [10, 70],
			    'width': [10, 60],
				'left': [100, 20],
			    'opacity': [0, .98]
			});

			pageup = 1;

			// Show Close Button
			$(tmp2[0]+'_close').style.display = "block";
			
			clubGrow2.start({
			    'opacity': [0, 1]
			});
			
			$(tmp2[0]+'content').innerHTML = "";

			// Get Content
			$(tmp2[0]+'content').set('load', {evalScripts: true, onComplete: function(e){Slimbox.scanPage();}});
			$(tmp2[0]+'content').load(element.href);

		});

	});
	
}); 

// Morph all windows out
function reset(){

	// Minimise all pages
	$('homecontent').innerHTML = "";
	$('sailingclubcontent').innerHTML = "";
	$('bowlsclubcontent').innerHTML = "";
	$('krackerscontent').innerHTML = "";
	$('spinnakercontent').innerHTML = "";
	$('clubhousecontent').innerHTML = "";

	$('clubhousecontent').style.opacity = 0;
	$('spinnakercontent').style.opacity = 0;
	$('krackerscontent').style.opacity = 0;
	$('homecontent').style.opacity = 0;
	$('bowlsclubcontent').style.opacity = 0;
	$('sailingclubcontent').style.opacity = 0;
	
	$('clubhousecontent').style.height = 0;
	$('spinnakercontent').style.height = 0;
	$('krackerscontent').style.height = 0;
	$('homecontent').style.height = 0;
	$('bowlsclubcontent').style.height = 0;
	$('sailingclubcontent').style.height = 0;
	
	$('clubhousecontent').style.width = 0;
	$('spinnakercontent').style.width = 0;
	$('krackerscontent').style.width = 0;
	$('homecontent').style.width = 0;
	$('bowlsclubcontent').style.width = 0;
	$('sailingclubcontent').style.width = 0;
	
	$('clubhousecontent').style.left = 100;
	$('spinnakercontent').style.left = 100;
	$('krackerscontent').style.left = 100;
	$('homecontent').style.left = 100;
	$('bowlsclubcontent').style.left = 100;
	$('sailingclubcontent').style.left = 100;
	
	// Fade all close buttons
	// Close Buttons
	var closebtn = $$('.closebtn');
	
	closebtn.each(function(element) {

			tmp = element.id.split("_");

			$(tmp[0]+'_close').opacity = 0;
			
			$(element.id).style.display = "none";
		

	});
	
	pageup = 0;
	
};

function fixsize(){
	
	slide.size = getWidth();
	
	$('dScrollCont').style.width = (getWidth() * 6)+"px";
	
	var containers = $$(".dContainerItem");
	
	containers.each(function(element) {

		element.style.width = getWidth()+"px";

	});
	
	// Cycle slide to the correct location
	slide.next();
	slide.next();
	slide.next();
	slide.next();
	slide.next();
	slide.next();
	
}

function getWidth(){
	
	var width = 0;
	
	if(window.innerWidth)
	{
		
		width = window.innerWidth;
		
	}
	else{
		
		width = document.body.clientWidth;
		
	}
	
	return width;
	
}