// JavaScript Document

var scroll;

window.addEvent('domready', function() {
	//setTimeout(killIntro,6000);
	
	// grab all the elements
	var navEl = $('nav').getElements('a');
	
	// create a morph for the bar that follows them
	var redBlockFX = new Fx.Morph('red_block', {duration: 500, transition: Fx.Transitions.Back.easeOut});
	
	redBlockFX.set({
		'width' : $('nav_home').getCoordinates().width,
		'left' : $('nav_home').getCoordinates($('nav')).left
		});
	
	// slide the background on and off on mouseover
	navEl.addEvent('mouseover', function(){
		var elFx = new Fx.Morph(this, {duration: 300, delay : 2500});
		
		elFx.start({'background-position' : '0 -21'});
		});
	// and slide back
	navEl.addEvent('mouseout', function(){
		var elFx = new Fx.Morph(this, {duration: 300});
		
		elFx.start({'background-position' : '0 0'});
		});
		
	// setup the scroller
	scroll = new Fx.Scroll('content', {
		wait: false,
		duration: 1000,
		offset: {},
		transition: Fx.Transitions.Quad.easeInOut
	});
	
	// click event for the elements
	navEl.addEvent('click', function(evt){
		evt.stop();
		// animate the red block
		var myX = this.getCoordinates($('nav')).left;
		var myWidth = this.getCoordinates().width;
		
		redBlockFX.start({
			'width' : myWidth,
			'margin-left' : myX
			});
			
		//kill quote timeout
		clearTimeout(qt);
			
		// scroll cases
		var myId = this.getParent('li').getProperty('id');
		var pos;
		
		switch (myId) {
			case 'nav_home':
				pos = $('pnl_home').getPosition('content').x;
				break;
			case 'nav_testimonials':
				pos = $('pnl_testimonials').getPosition('content').x;
				qt = setTimeout(nextQuote,7000);
				break;
			case 'nav_philosophy':
				pos = $('pnl_philosophy').getPosition('content').x;
				break;
			case 'nav_services':
				pos = $('pnl_services').getPosition('content').x;
				break;
			case 'nav_press':
				pos = $('pnl_press').getPosition('content').x;
				break;
			case 'nav_contact':
				pos = $('pnl_contact').getPosition('content').x;
				break;
			case 'nav_tour':
				pos = $('pnl_tour').getPosition('content').x;
				break;
			default:
				pos = 0;
				break;
			}
				
		scroll.start(pos-150,0);
		
	});
	
	/* SUBNAV FOR PRESS */
	
	// grab all the elements
	var press_subnavEl = $('subnav_press').getElements('a');
	
	// create a morph for the bar that follows them
	var press_sub_redBlockFX = new Fx.Morph('subnav_press_red_block', {duration: 500, transition: Fx.Transitions.Back.easeOut});
	
	press_sub_redBlockFX.set({
		'width' : $('sub_print').getCoordinates().width,
		'left' : $('sub_print').getCoordinates($('subnav_press')).left
		});
	
	// slide the background on and off on mouseover
	press_subnavEl.addEvent('mouseover', function(){
		var elFx = new Fx.Morph(this, {duration: 300, delay : 2500});
		
		elFx.start({'background-position' : '0 -21'});
		});
	// and slide back
	press_subnavEl.addEvent('mouseout', function(){
		var elFx = new Fx.Morph(this, {duration: 300});
		
		elFx.start({'background-position' : '0 0'});
		});
	
	// setup the scroller
	var press_scroll = new Fx.Scroll('sub_press_content', {
		wait: false,
		duration: 1000,
		offset: {},
		transition: Fx.Transitions.Quad.easeInOut
	});
	
	// click event for the elements
	press_subnavEl.addEvent('click', function(evt){
		evt.stop();
		// animate the red block
		var myX = this.getCoordinates($('subnav_press')).left;
		var myWidth = this.getCoordinates().width;
		
		press_sub_redBlockFX.start({
			'width' : myWidth,
			'margin-left' : myX
			});
			
		// scroll cases
		var myId = this.getParent('li').getProperty('id');
		
		switch (myId) {
			case 'sub_print':
				press_scroll.toElement('sub_pnl_print');
				break;
			case 'sub_tv':
				press_scroll.toElement('sub_pnl_tv');
				break;
			case 'sub_web':
				press_scroll.toElement('sub_pnl_web');
				break;
			case 'sub_archive':
				press_scroll.toElement('sub_pnl_archive');
				break;
		}
		
	});
	
	
	
	/* SUBNAV FOR CONTACTS */	
	
	
	// grab all the elements
	var contact_subnavEl = $('subnav_contact').getElements('a');
	
	// create a morph for the bar that follows them
	var contact_sub_redBlockFX = new Fx.Morph('subnav_contact_red_block', {duration: 500, transition: Fx.Transitions.Back.easeOut});
	
	contact_sub_redBlockFX.set({
		'width' : $('sub_contact').getCoordinates().width,
		'left' : $('sub_contact').getCoordinates($('subnav_contact')).left
		});
	
	// slide the background on and off on mouseover
	contact_subnavEl.addEvent('mouseover', function(){
		var elFx = new Fx.Morph(this, {duration: 300, delay : 2500});
		
		elFx.start({'background-position' : '0 -21'});
		});
	// and slide back
	contact_subnavEl.addEvent('mouseout', function(){
		var elFx = new Fx.Morph(this, {duration: 300});
		
		elFx.start({'background-position' : '0 0'});
		});
	
	// setup the scroller
	var contact_scroll = new Fx.Scroll('sub_contact_content', {
		wait: false,
		duration: 1000,
		offset: {},
		transition: Fx.Transitions.Quad.easeInOut
	});
	
	// click event for the elements
	contact_subnavEl.addEvent('click', function(evt){
		evt.stop();
		// animate the red block
		var myX = this.getCoordinates($('subnav_contact')).left;
		var myWidth = this.getCoordinates().width;
		
		contact_sub_redBlockFX.start({
			'width' : myWidth,
			'margin-left' : myX
			});
			
		// scroll cases
		var myId = this.getParent('li').getProperty('id');
		
		switch (myId) {
			case 'sub_contact':
				contact_scroll.toElement('sub_pnl_contact');
				break;
			case 'sub_gettingstarted':
				contact_scroll.toElement('sub_pnl_gettingstarted');
				break;
		}
		
	});
	
	/* XML Loading for testimonials */
	
	/*var loadXML = new Request({
							url: 'assets/quotes.xml',
							method: 'get',
							onSuccess: loadQuotes,
							onFailure: failQuotes
								   }).send();*/
	var quotesArray = new Array();
	var currentQuote = 0;
	var qt;
	
	
    var xml = new JKL.ParseXML("assets/quotes.xml");
    var data = xml.parse();
	for (i = 0; i< data.quotes.quote.length; i++)
	{
		quotesArray[i] = new Object();
		quotesArray[i].txt = data.quotes.quote[i].txt.replace(/^\s+|\s+$/g,"");
		quotesArray[i].from = data.quotes.quote[i].from.replace(/^\s+|\s+$/g,"").replace(/\n/g,"<br />");
	}
	
	$('txt').set("text",quotesArray[currentQuote].txt);
	$('from').set("html",quotesArray[currentQuote].from);
	
	$('prev_quote').addEvent('click',prevQuote);
	$('next_quote').addEvent('click',nextQuote);
	/*
	function loadQuotes(responseText, responseXML)
	{
		
		quotes = responseXML.getElementsByTagName("quote");
		for (i = 0; i < quotes.length; i++)
		{
			quotesArray[i] = new Object();
			quotesArray[i].txt = quotes[i].getElementsByTagName("txt")[0].textContent.replace(/^\s+|\s+$/g,"");
			//<![CDATA[
			quotesArray[i].from = quotes[i].getElementsByTagName("from")[0].textContent.replace(/^\s+|\s+$/g,"").replace(/\n/g,"<br />");
			//]]>
		}
		
		$('txt').set("text",quotesArray[currentQuote].txt);
		$('from').set("html",quotesArray[currentQuote].from);
		
		
		
	}*/
	
	function failQuotes(xhr){
		try{console("fail");}catch(e){}	
	}
	
	function nextQuote(evt)
	{
		if (evt)
			evt.stop();
		
		clearTimeout(qt);
		currentQuote++;
		if (currentQuote > quotesArray.length-1)
			currentQuote = 0;
		$('txt').set("text",quotesArray[currentQuote].txt);
		$('from').set("html",quotesArray[currentQuote].from);
		
		qt = setTimeout(nextQuote,7000);
	}
	
	function prevQuote(evt)
	{
		if (evt)
			evt.stop();
		
		clearTimeout(qt);
		currentQuote--;
		if (currentQuote < 0)
			currentQuote = quotesArray.length-1;
		$('txt').set("text",quotesArray[currentQuote].txt);
		$('from').set("html",quotesArray[currentQuote].from);
		
		qt = setTimeout(nextQuote,7000);
	}	
	
	/* INTRO ANIMATION 
	
	var textMorph = new Fx.Morph("enter", {duration: 1000, transition: Fx.Transitions.Sine.easeOut, onComplete: function(){setTimeout(animateOut,500);}});
		
	textMorph.start({'background-position' : '300 0'});
	
	function animateOut()

	{
		$('vault').setStyle("background-color","transparent");
		var vaultMorph = new Fx.Morph("vault", {duration: 2000, transition: Fx.Transitions.Sine.easeIn, onComplete: function(){$("vault").destroy();}});
		textMorph = new Fx.Morph("enter", {duration: 2000, transition: Fx.Transitions.Sine.easeIn});
		vaultMorph.start({'background-position':'-1000 0'});
		textMorph.start({'background-position':'-700 0'});
	}
	
	*/
	
});


 
	
	