//
// Verschachteltes Mootools-Accordion
// Nested Mootools Accordion
// 
// von / by Bogdan Gunther
// http://www.medianotions.de
//

window.addEvent('domready', function() {
	
	//	chris - get site zone
	//	jim - add an if to allow for nav to be omitted from markup
	if(document.id('nav')!=null){
		var sideMenu=document.id('nav');
		var navType=sideMenu.get('class');
		
		//	the border bottom for the last item in the  second level is added inline via this variable:
		borderBottom='#FFFFFF';
		if(navType=="consumer") 	borderBottom='#722e90';
		else if(navType=="members") borderBottom='#1b5eb2';
		else if(navType=="trade") 	borderBottom='#8e4600';
		
		
		
		// ie6 height fix - working?
		if(window.ie6) var heightValue='100%';
		else var heightValue='';
		
		// define toggles and containers
		var togglerName='dt.accordion_toggler_';
		var contentName='dd.accordion_content_';
		
		
		var counter=1;	
		var toggler=$$(togglerName+counter);
		var content=$$(contentName+counter);
		
		while(toggler.length>0)	{
			// Accordion
			new Accordion(toggler, content, {
				opacity:true,
				display: -1,
				initialDisplayFx:false,
				alwaysHide:true,
				onComplete: function() { 
					var element=$(this.elements[this.previous]);
					if(element && element.offsetHeight>0) element.setStyle('height', heightValue);			
				},
				onActive: function(toggler, content) {
					toggler.addClass('open');
					//	fix to ADD base line on last item
					if(toggler.hasClass('last')){
						//toggler.getNext('dd').addClass('last_level_2');
						toggler.getNext('dd').setStyle('border-bottom', '1px solid '+borderBottom+'');
					}
					
					
				},
				onBackground: function(toggler, content) {
					toggler.removeClass('open');
					//	fix to REMOVE base line on last item
					if(toggler.hasClass('last')){
						toggler.getNext('dd').setStyle('border-bottom', 'none');
					}
				}
			});
			
			// increase counter for next level
			counter++;
			toggler=$$(togglerName+counter);
			content=$$(contentName+counter);
			
			
		}
		
		//	show accodion menu which was hidden by css
		sideMenu.show();
		
		
		//	CHRIS - www.cbolson.com
		//	detect page url to open accordion at correct place
		
		//	get current file name  from url
		var page_url=document.location.pathname;
		pathArray = page_url.split( '/' );
		page_file=pathArray[pathArray.length-1];
		
		//	test url
		//page_file='level1.html';
		
		//	try to find page in menu
		$$('.accordion a').each(function(link) { 
			this_href=link.get('href');				//	define href link
				link.addEvent('click',function(e){
				window.location = e.target.get('href');		//	load next page
				e.stop();									//	stop parent sliding....
			});
			
			//	open accordion to current url  - DON'T LIKE THIS PART :(
			if(this_href==page_file){
			// underline current page link
				link.addClass('selected');
				
				//	open accordion
				if(link.getParent()){
					link.getParent().fireEvent('click');
				}
				
				//	second level link
				if(link.getParent().getParent().getParent().getPrevious('dt')){
					link.getParent().getParent().getParent().getPrevious('dt').fireEvent('click');
				}
				
					
				//	third level link (specific to this web design)
				if(link.getParent('li')){
					link.getParent().getParent().getParent().getParent().getParent().getPrevious('dt').fireEvent('click');
				}
				
				
				/*
				//	this might work if normal divs are used???
				if(link.getParents('dt')){
					link.getParents('dt').each(function(toggle){
						toggle.fireEvent('click');
						alert('father');
					});
				}
				*/
				
			}
		});
	// end if 
	}

	/* Image Rollovers
	http://davidwalsh.name/mootools-image-mouseovers
	*/
	$$('img.mo').each(function(img) {
		var src = img.getProperty('src');
		var extension = src.substring(src.lastIndexOf('.'),src.length)
		img.addEvent('mouseenter', function() { img.setProperty('src',src.replace(extension,'-mo' + extension)); });
		img.addEvent('mouseleave', function() { img.setProperty('src',src); });
	});
	
});


/*
Random Header Image
Authors: Jim Richards and Chris Bolson
Date: 18-12-2009
Receives an argument of 1 or 2 which indicates the 'level' of the current page
This causes the script to insert either one or two '../' in the pathname to the bg images so they can be found
 */

function randImg(depth){
	// Set default value for myPath in case depth argument is omitted (only works for docs at 'zone' root level)
	var myPath="../";
	if(depth == 1){var myPath="../";}
	else if(depth == 2){var myPath="../../";}
	
	var images = new Array(); 
	// get site zone
	if(!document.id('nav')){
		//	default image for login (no nav to get class)
		images[0] = 'url('+myPath+'members/img/bg/bg-generic.jpg)';
	}else{
	  	//	get page type to define array of images
	  	var sideMenu=document.id('nav');
	  	var navType=sideMenu.get('class');
	  
		  if(navType=="consumer"){
		    images[0] = 'url('+myPath+'consumer/img/bg/bg-lower1.jpg)';
		    images[1] = 'url('+myPath+'consumer/img/bg/bg-lower2.jpg)';
		    images[2] = 'url('+myPath+'consumer/img/bg/bg-lower3.jpg)';
			images[3] = 'url('+myPath+'consumer/img/bg/bg-lower4.jpg)';
			images[4] = 'url('+myPath+'consumer/img/bg/bg-lower5.jpg)';
			images[5] = 'url('+myPath+'consumer/img/bg/bg-lower6.jpg)';
			images[6] = 'url('+myPath+'consumer/img/bg/bg-generic.jpg)';
		  }else if(navType=="trade"){
		    images[0] = 'url('+myPath+'trade/img/bg/bg-lower1.jpg)';
		    images[1] = 'url('+myPath+'trade/img/bg/bg-lower2.jpg)';
		    images[2] = 'url('+myPath+'trade/img/bg/bg-lower3.jpg)';
			images[3] = 'url('+myPath+'trade/img/bg/bg-lower4.jpg)';
			images[4] = 'url('+myPath+'trade/img/bg/bg-lower5.jpg)';
			images[5] = 'url('+myPath+'trade/img/bg/bg-lower6.jpg)';
			images[6] = 'url('+myPath+'trade/img/bg/bg-lower7.jpg)';
			images[7] = 'url('+myPath+'trade/img/bg/bg-lower8.jpg)';
			images[8] = 'url('+myPath+'trade/img/bg/bg-generic.jpg)';
		  }else if(navType=="members"){
		    images[0] = 'url('+myPath+'members/img/bg/bg-lower1.jpg)';
			images[1] = 'url('+myPath+'members/img/bg/bg-lower2.jpg)';
			images[2] = 'url('+myPath+'members/img/bg/bg-lower3.jpg)';
			images[3] = 'url('+myPath+'members/img/bg/bg-lower4.jpg)';
			images[4] = 'url('+myPath+'members/img/bg/bg-lower5.jpg)';
			images[5] = 'url('+myPath+'members/img/bg/bg-lower6.jpg)';
			images[6] = 'url('+myPath+'members/img/bg/bg-lower7.jpg)';
			images[7] = 'url('+myPath+'members/img/bg/bg-lower8.jpg)';
			images[8] = 'url('+myPath+'members/img/bg/bg-lower9.jpg)';
			images[9] = 'url('+myPath+'members/img/bg/bg-lower10.jpg)';
			images[10] = 'url('+myPath+'members/img/bg/bg-generic.jpg)';
		  }
	}
	index = Math.floor(Math.random() * images.length);
	document.getElementById("header").style.backgroundImage = images[index];
}

/*
Script: simpleTabs.js
Dependencies: Mootools 1.2.4
Author: Chris Bolson <chris[@]cbolson.com>
Url: www.cbolson.com
Version: 1.0
Date: 2009-11-12
License:MIT-style license.
Use:
	Create simple tabs from standard html mark-up
	Relates "tabs" with "tab contents" using the array index
	Fades between slides when tabs clicked

Notes:
	Tab contents are positioned absolutely in the "build" method
	These *could* be absolutley positioned in the css but if the user has js off, they will be stacked on top of each other - not nice!
  
*/
var simpleTabs = new Class({
    Implements: [Options],
    options: {
		tabTriggers:'a',							//	elements - the tabs
		tabContentsWrapper:'contents-wrapper',	//	element - the tab contents wrapper
		tabContents:'.tab-contents',				//	elements - the tabbed contents	
		start_tab:0,								//	define starting item
		tabs_height:23,							//	tab triggers height - I don't like this!
		tabScroll:true
    },
    
    initialize: function(container, options){
    	this.setOptions(options);
        tabContainer = document.id(container);									//	the overall tab container
        this.contentsWrapper=document.id(this.options.tabContentsWrapper);		//	the tabbed contents wrapper
        contents = this.contentsWrapper.getElements(this.options.tabContents);	//	array of content boxes
        tabs = tabContainer.getElements(this.options.tabTriggers);				//	array of tabs
       	currentIndex = this.options.start_tab;									//	initial tab		
       	tabScroll=this.options.tabScroll;
       	this.build();
    },
    
	build: function(){
		//	position contents boxes then fade out
		if(this.contentsWrapper.getStyle('position') != 'absolute') tabContainer.setStyle('position','relative');
       	contents.each(function(content,index){
			content.setStyles({
				'position': 'absolute',
                'top':'53px',							//	DON'T LIKE THIS!!!!
                'left':'0px'
            });
            content.fade('hide');										//	fade content box out (all of them)
        }.bind(this));

		//	tabs
    	tabs.each(function(tab,index){
	        tab.addEvent('click',function(){
			    contents[currentIndex].fade('out');						//	fade out previous selected contents
				tabs[currentIndex].removeClass('active');				//	remove active class from tab
				if(tabScroll){
					height_tab		= 49;						//	tab height
					height_content 	= contents[index].getScrollSize().y;	//	get selected contents he
					tabContainer.tween('height',height_content+height_tab);	//	scroll container to new height (contents + height)
				}
				contents[index].fade('in');								//	fade in selected contents
				this.addClass('active');								//	mark selected tab
		        currentIndex=index;										//	reassign current index
		        
			}).setStyle('cursor','pointer');
		}.bind(this));
        
        //	activate frst tabs
        tabs[currentIndex].fireEvent('click');
        
        return false;
    }
    
});
