window.addEvent ('domready', function(){

	var cardHeight = document.getElement('.card').getStyle('height').toInt();
	var offset = 65;
	down = -(cardHeight-offset);
	up 	= 0;
	
//Setup the 'cards'	
	
	cards = $$('.card');
	cards.each(function(element){
		element.store('status', 0);
		element.fxobj = new Fx.Tween($(element), {
			duration:1000, 
			link: 'ignore',
			transition:Fx.Transitions.Quart.easeInOut,
			onComplete: function(element){ 
				toggleStatus(element);
				setTriggerPos(element);
				}
			
		});
		element.setStyle('bottom',0);					
	});
	
//setup the document areas
	docs = $$('.document');
	docs.each(function(element){
		element.fxobj = new Fx.Morph($(element), {
			duration:1000, 
			link: 'chain',
			transition:Fx.Transitions.Quart.easeInOut
		});
		element.setStyle('opacity',0);
	});
	//Now Set the home page document visible
	$('home').getFirst('.document').setStyle('opacity',1);


//setup the triggers
	triggers = $$('.trigger a');			
	triggers.each(function(el){
		el.addEvent('click', function () {
			var card = $(el.id.split("_")[1]);
			slidem(card);
			initPages(card);
		});
	});
	
	

//setup secondary nav
	if (document.getElement('ul.nav')) {
		var navItems = $$('ul.nav li a');
		navItems.each(function(el){
			el.addEvent('click', function(e) {
				//page derived by splitting the 'nav_' off of the
				//id of the sidenav navigation li
				var page = el.id.split("_")[1];
				var card = el.getParent('.card');
				initPages(card, page);
				stop(e);
			});
		});
	} //if sidenav


//Check for hash tag in URL and slide appropriate card

if (window.location.hash != "") {
	//entered page with a product anchor so grab
	var hash = window.location.hash.substring(1).split("/");
	var cardname = hash[0].split("_")[1];
	var pagename = hash[1];
	slidem($(cardname));
	initPages($(cardname), pagename);
}



//setup 'read more' news event links
	var eventLinks = $$('.eventlink');
	eventLinks.each(function(el){
		el.addEvent('click', function () {
		var $uri = el.getProperty('href').split("/");
		var cardname = $uri[0].split("_")[1];
		var pagename = $uri[1];
		slidem($(cardname));
		initPages($(cardname), pagename);
		return false;
		});
	});



//setup the mailing list bottle
	bottleRest = $('join').getStyle('left'); 

	joinFx = new Fx.Morph($('join'), {
		duration:900, 
		link: 'ignore',
		transition:Fx.Transitions.Quart.easeInOut
	}); 

	marqueeFx = new Fx.Morph($('marquee'), {
		duration:900, 
		link: 'ignore',
		transition:Fx.Transitions.Quart.easeInOut
	}); 

	stageFx = new Fx.Morph(document.getElement('.stage'), {
		duration:900, 
		link: 'ignore',
		transition:Fx.Transitions.Quart.easeInOut
	}); 


	var joinLinks = $$('.joinnow');
	joinLinks.each(function(el){
		el.addEvent('click', function () {
			if ( $('join').getStyle('left') != bottleRest ) {
				joinFx.start({'left':bottleRest});	
				stageFx.start({'margin-left':'202px'});
				marqueeFx.start({'left':'0px'});		
			} else {
				joinFx.start({'left':'20px'});	
				stageFx.start({'margin-left':'208px'});
				marqueeFx.start({'left':'-100px'});		
			}
		});
	});


	//call the ajax request setup function
	setupAjaxRequest();		







}); //end domReady handler



window.addEvent ('load', function(){
	
	//setup custom scrollers
	var scrollBox1 = new MooScroll({});
	//var tastingsScroll = new MooScroll({selector:'#tastings .scroll'});
	
	//setup Accordions
	var servicesAccordion = new Fx.Accordion($$('#services .toggle a'), $$('#services dd'), { display: -1, alwaysHide: true });	


//Setup the 'cards'	
	
/*
	cards = $$('.card');
	cards.each(function(element){
		element.setStyle('bottom',0);					
	});
*/



});  //End onLoad handler





/** FUNCTIONS **/

function setupAjaxRequest (){

	$('emailRequest').set('send', {
		url: 'email.php', 
		method: 'post', 
		onSuccess: function(responseText, responseXML) { showSuccess(responseText); }
		});	
		
	$('submitRequest').addEvent('click', function() {
		$('emailRequest').send();
		return false;
	
	});	


}


function showSuccess(response) {

	var statusFx = new Fx.Morph($('statusArea'), {
		duration:1500, 
		link: 'ignore',
		onComplete: function() { 
			$('statusArea').set('html', emailForm); 
			$('statusArea').setStyle('opacity',1);
			setupAjaxRequest();
		}
	}); 
	var emailForm = $('statusArea').get('html');	
	var succesMarkup = $('statusArea').set('html', response);
	joinFx.start.delay(900, joinFx, {'left':bottleRest});	
	stageFx.start.delay(900, stageFx, {'margin-left':'202px'});
	marqueeFx.start.delay(900, marqueeFx, {'left':'0px'});		
	statusFx.start.delay(900, statusFx, {'opacity':0});

}




function slidem(card){
	var cardPos = cards.indexOf(card);
	if(card.retrieve('status') == false) { 
		card.getFirst('.document').fxobj.start({'opacity':1});
				start = firstDeployed(card);
				cards.each(function(which){
					if(cards.indexOf(which) < cardPos  && cards.indexOf(which) >= start) {
						which.fxobj.start('bottom', down);
						if (which.id != 'home') {
							which.getFirst('.document').fxobj.start({'opacity':0});
						}

					} 
		
				});
				
		} else {
				card.getFirst('.document').fxobj.start({'opacity':1});
				var end = firstDeployed(card);
				cards[end].getFirst('.document').fxobj.start({'opacity':0});		
				for (i=cardPos; i < end; i++) {
					//console.log('sending card '+cards[i].id+' back up');
					cards[i].fxobj.start('bottom', up);
				}
		
		}
						
}





function toggleStatus (element) {
	element.store('status', !(element.retrieve('status')));
	//console.log('Card: '+element.id+' status set to '+element.retrieve('status'));
}




function firstDeployed(card) {
	var i = 0;
	while (cards[i].retrieve('status') == true)	{ i++; }
	return i;
}



function setTriggerPos(element) {
	var trigger = $('nav_'+element.id);
	var cardVpos = element.getStyle('bottom').toInt();
	var triggerVpos = trigger.getStyle('bottom').toInt();
	if (cardVpos == up){
		trigger.setStyle('bottom', triggerVpos - down);		
	} else {
		trigger.setStyle('bottom', triggerVpos + down);		
	}
}


function hidePages(card) {
	//if there is multipage content, this function
	//hides all pages to allow for fadein
	//We call this on domReady and initPages on dom load.
	
	if (card.getElement('.page')) {
		var pages = card.getElements('.page');
		pages.each (function(element){	
			//hide all pages 
			element.setStyles({ 'display':'none', 'opacity':'0', 'margin-top':'0' });
		});
	}

}


function initPages(card,page){
//function to initialize multi-page content
	
	//Check if this card has paged content		
	if (card.getElement('.page')) {

		hidePages(card);
		
		//If specific pages has been passed in, show it,
		//if not, show the first page of the section		
		if (page===undefined){
			var activePage = card.getElement(' .page');
		} else {
			var activePage = page;
		}
		//console.log('ActivePage = '+activePage);
		$(activePage).setStyle('display','block');
		fadeFx = new Fx.Tween($(activePage), {duration:500});
		fadeFx.start('opacity','1');		
		}	
	
} //end initPages
		


