var DFWebsite = {
	reveal: function(event)
	{
		var element = event.element();
		var className = $w(element.className).without('reveal','revealed');
		
		className = className.length ? className[0] : '';
		
		if(className) {
			$$('div.'+className).invoke('toggle');
		} else {
			element.next().toggle();
		}
		
		element.toggleClassName('revealed');
		
		event.stop();
	},
	
	revealAll: function(status)
	{
		var elements = $$('h1.reveal','div.faq h1','h2.reveal','div.faq h2');
		
		var meth = status ? 'show' : 'hide';
		
		elements.each(function(element)
		{
			var className = $w(element.className).without('reveal','revealed');
			className = className.length ? className[0] : '';
			
			if(className) {
				$$('div.'+className).invoke(meth);
			} else {
				element.next()[meth]();
			}
			
			if(status)
				element.addClassName('revealed');
			else
				element.removeClassName('revealed');
		});
	},
	
	portfolioPagePrep: function()
	{
		var portfolioDiv = $('portfolio');
		
		if(portfolioDiv)
		{
			var portfolioList = portfolioDiv.down('ol');
			var portfolioItems = portfolioList.childElements();
			
			var activePortfolioItem = 0;
			
			var portfolioAnimationDelay = 1.0;
			var portfolioAnimationOptions = {
				duration: portfolioAnimationDelay
			};
			
			var activeEffects = $A();
			
			/* Privileged method */
			DFWebsite.portfolioAdvance = function(n)
			{
				if(typeof(n) != 'number') { n = 1; }
				
				activeEffects.invoke('cancel');
				
				activeEffects = $A();
				
				activeEffects.push(new Effect.Fade(portfolioItems[activePortfolioItem],portfolioAnimationOptions));
				Element.hide.delay(portfolioAnimationDelay+0.1,portfolioItems[activePortfolioItem]);
				
				
				activePortfolioItem += n;
				
				while (activePortfolioItem < 0) { activePortfolioItem += portfolioItems.length }
				
				activePortfolioItem = activePortfolioItem % portfolioItems.length;
				
				
				activeEffects.push(new Effect.Appear(portfolioItems[activePortfolioItem],portfolioAnimationOptions));
				Element.show.delay(portfolioAnimationDelay+0.2,portfolioItems[activePortfolioItem]);
			}
			
			
			/* Protected method -- event handler */
			var portfolioPageLinkHandler = function(e)
			{
				var element = e.element();
				
				if(element.hasClassName('prev')) {
					this.portfolioAdvance(-1);
				} else if(element.hasClassName('next')) {
					this.portfolioAdvance(1);
				}
				
				e.stop();
			}.bindAsEventListener(DFWebsite);
			
			
			
			var prevNextDiv = new Element('div',{'class':'nav'});
			
			portfolioItems.invoke('hide');
			
			portfolioItems[activePortfolioItem].show();
			
			prevNextDiv.appendChild(new Element('a',{'class':'prev',href:'#Previous'}).update('&lt; Previous'));
			prevNextDiv.appendChild(new Element('a',{'class':'next',href:'#Next'}).update('Next &gt;'));
			
			prevNextDiv.childElements().invoke('observe','click',portfolioPageLinkHandler)
			
			portfolioList.insert({before: prevNextDiv});
		}
	},
	
	revealPrep: function()
	{
		var sectionHeadings = $$('h1.reveal','div.faq h1');
		var subsectionHeadings = $$('h2.reveal','div.faq h2');
		
		var sections = sectionHeadings.invoke('next');
		var subsections = subsectionHeadings.invoke('next');
		
		sectionHeadings.invoke('observe','click',DFWebsite.reveal);
		//sectionHeadings.invoke('addClassName','revealed');
		sections.invoke('hide');
		
		subsectionHeadings.invoke('observe','click',DFWebsite.reveal);
		subsections.invoke('hide');
		
		//$$('a.revealAll').observe('click',function(e){ this.revealAll(true); event.stop(); }.bindAsEventListener(DFWebsite));
		//$$('a.hideAll').observe('click',function(e){ this.revealAll(false); event.stop(); }.bindAsEventListener(DFWebsite));
	},
	
	faqPageAutoReveal: function()
	{
		if(/^\/products\/faq/.test(window.location.pathname))
		{
			$$('h1.products').invoke('addClassName','revealed');
			$$('div.products').invoke('show');
			$$('h1.services').invoke('addClassName','revealed');
			$$('div.services').invoke('show');
			$$('h1.packages').invoke('addClassName','revealed');
			$$('div.packages').invoke('show');
		}
		else if(/^\/products\/ecommerce\/faq/.test(window.location.pathname))
		{
			$$('h1.products').invoke('addClassName','revealed');
			$$('div.products').invoke('show');
		}
		else if(/^\/products\/cms\/faq/.test(window.location.pathname))
		{
			$$('h1.services').invoke('addClassName','revealed');
			$$('div.services').invoke('show');
		}
		else if(/^\/services\/faq/.test(window.location.pathname))
		{
			$$('h1.services').invoke('addClassName','revealed');
			$$('div.services').invoke('show');
		}
		else if(/^\/products\/cardcustomizer\/faq/.test(window.location.pathname))
		{
			$$('h1.packages').invoke('addClassName','revealed');
			$$('div.packages').invoke('show');
		}
	},
	
	setupRightNav: function()
	{
		DFTools.console.log("Setting up right nav");
		
		if($('content_div').down('div.content.leftnav'))
		{
			list = $('cms_secondary_links_div').down('a[href='+window.location.pathname+']');
			
			if(list.up(3).id == 'leftnav_div')
				list = list.next('ul');
			else
				list = list.up('ul');
			
			if(list)
				$('rightnav_div').update('<ul>'+list.innerHTML+'</ul>');
		}
		
		//$('rightnav_div').update($('cms_secondary_links_div').innerHTML);
	}
};

FastInit.addOnLoad(function()
{
	DFWebsite.revealPrep();
	DFWebsite.faqPageAutoReveal();
	DFWebsite.portfolioPagePrep();
	
	DFWebsite.setupRightNav();
	
	if(Prototype.Browser.IE)
	{
		Event.observe(window,'load',DFWebsite.setupRightNav);
		DFWebsite.setupRightNav.delay(2);
	}
});
