/*From Blocks kit by rapidweaver, rapidweaver.org*/
var Site = {
	
	start: function(){
		Site.appearMenu();
		Site.collapsBlock();
	},
		
	/*Loops through each menu on the page
	Extracts the hidden values
	Assigns the function that handles on roll over - pop out
	Assigns the funtion that handles on roll over - hover
	*/
	appearMenu: function(){
		
		var menuWrapper = $$('.navcontainer');
		
		for (i=0; i < menuWrapper.length; i++)
		{
			//alert("here");
			var menu = menuWrapper[i];
			
			//defaults for extracted values
			//extracted values
			var linkV = Site.extractElement(menu, '.linkColour', "#666666");
			var linkHoverV = Site.extractElement(menu, '.linkColourHover', "red");
			var effectV = Site.extractElement(menu, '.effect', "1");
			var effectSpeedV = Site.extractElement(menu, '.effectSpeed', "500");
			var showSubV = Site.extractElement(menu, '.showSubMenu', "1");
			
			var p = menu.getParent();
			var superClass = p.getProperty('class');
			var pp = p.getParent();
			var superId = pp.getProperty('id');//the id wrapper placed around block by blocks
			cssToAdd = "#" + superId + " .navcontainer a{color: " + linkV + "}";
			addCss(cssToAdd);
			
			if(superClass != "nav_css") {//then its rounded blocks so do this...
				isRound = true;
				var splitBorderV = Site.extractElement(menu, '.splitBorder', "black");
				cssToAdd = "#" + superId + " .navcontainer a{border-right-color: " + splitBorderV + "}";
				addCss(cssToAdd);
			} else {//its html/css blocks
				isRound = false;
				var linkBackgroundV = Site.extractElement(menu, '.linkColourBackground', "white");
				var linkBackgroundHoverV = Site.extractElement(menu, '.linkColourBackgroundHover', "white");
				var linkColourBorderV = Site.extractElement(menu, '.linkColourBorder', "black");
				var linkColourBorderHoverV = Site.extractElement(menu, '.linkColourBorderHover', "red");
				var popOutColourV = Site.extractElement(menu, '.popOutColour', "white");
				//border colour, pop out colour
				cssToAdd = "#" + superId + " .nav_css .navcontainer a{background-color: " + linkBackgroundV + "}";
				addCss(cssToAdd);
				cssToAdd = "#" + superId + " .nav_css .navcontainer ul ul a{background-color: " + popOutColourV + "}";
				addCss(cssToAdd);
				cssToAdd = "#" + superId + " .nav_css .navcontainer a:hover{background-color: " + linkBackgroundHoverV + "}";
				addCss(cssToAdd);
				cssToAdd = "#" + superId + " .nav_css .navcontainer a{border-bottom-color: " + linkColourBorderV + "}";
				addCss(cssToAdd);
				cssToAdd = "#" + superId + " .nav_css .navcontainer a:hover{border-bottom-color: " + linkColourBorderHoverV + "}";
				addCss(cssToAdd);
				cssToAdd = "#" + superId + " .nav_css .navcontainer ul ul{background-color: " + popOutColourV + "}";
				addCss(cssToAdd);
			}
			
			//adds the roll over effects to the menu
			Site.menuPopOut(menu, effectV, linkV, linkHoverV, linkBackgroundV, linkBackgroundHoverV, effectV, effectSpeedV, showSubV, isRound);
		}
	},
	
	extractElement: function(lookIn, lookFor, theDefault){
		var value = theDefault;
		var found=lookIn.getElement(lookFor);
		if(found){
			v = found.getProperty('value');
			if(v!=""){
				value = found.getProperty('value');
			}
		}
		return value;
	},
	/* roll over menu effect and drop down*/
	menuPopOut: function(menu, effect, link, linkHover, linkBackground, linkBackgroundHover, effect, effectSpeed, showSub, isRound){
			var li = menu.getElements('li');
			
			
			for (j=0; j < li.length; j++)
			{
				var header = li[j];
				//header.setStyle('visibility', 'hidden');
				
				var subHeader = header.getElements('ul');
				
				//drop down
				if(showSub != "0") {
					if(subHeader.length > 0) {//there is a menu under it....
						Site.popOutEvent(subHeader[0], header, effect, effectSpeed, isRound);
					}
				}
				
				//add roll over effect to all li's
				Site.liEffect(header, link, linkHover, linkBackground, linkBackgroundHover);
			}
	},
	
	/* Adds the pop out effect to the passed id when the passed header is rolled over
	*/
	popOutEvent: function(menu, header, effect, effectSpeed){
		effectSpeed = parseFloat(effectSpeed);
		var extra = 0;
		if(isRound){extra = 20;}
		
		menu.setStyle('display', 'block');
		var menuSize = menu.scrollHeight + extra;
		menu.setStyle('height', 0);
		
		var menufxs = Site.switchEffect(menu, effect, effectSpeed);
		
		header.addEvent('mouseenter', function(){
			menufxs.start({
				'height': menuSize
			});
		});
		header.addEvent('mouseleave', function(){
			menufxs.start({
				'height': 0
			});
		});
	},
	
	switchEffect: function(object, effect, effectSpeed) {
		switch(effect){
			case '1': var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.linear}); break;
			case '2': var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.quadOut}); break;
			case '3': var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.cubicOut}); break;
			case '4': var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.quartOut}); break;
			case '5': var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.quintOut}); break;
			case '6': var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.sineOut}); break;
			case '7': var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.expoOut}); break;
			case '8': var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.elasticOut}); break;
			case '9': var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.backOut}); break;
			case '10': var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.bounceOut}); break;
			default: var menufxs = new Fx.Styles(object, {'duration': effectSpeed, 'wait': false,  'transition': Fx.Transitions.cubicOut});
		}
		return menufxs;
	},
	
	/*Effect when a list item is rolled over*/
	liEffect: function(object, colour, hoverColour) {
		var a = object.getFirst('a');
		object.addEvent('mouseenter', function(){
			a.setStyle('color', hoverColour);
		});
		object.addEvent('mouseleave', function(){
			a.setStyle('color', colour);
		});
	},
	
	
	/**================
	Colapsable block
	=================*/

	/*loops through all the collapsable blocks and adds the event to them*/
	collapsBlock: function(){
		var sideblocksWrapper = $$('.db_infoboxWrapper');
		
		for (i=0; i < sideblocksWrapper.length; i++)
		{
			var theBlock = sideblocksWrapper[i];
			var heading = theBlock.getFirst('h3');
			var content = theBlock.getElement('div');
			
			var linkV = Site.extractElement(theBlock, '.linkColour', "#000000");
			var linkHoverV = Site.extractElement(theBlock, '.linkColourHover', "#FFFFFF");
			var needClickV = Site.extractElement(theBlock, '.needClick', "0");
			
			Site.collapsBlockAddEvent(theBlock, heading, content, linkV, linkHoverV, needClickV);
		}
	},
	
	/*adds the collaps even to the block passed*/
	collapsBlockAddEvent: function(theBlock, heading, content, linkV, linkHoverV, needClick) {
		var p = theBlock.getParent();
		var marginTop = 20;
		var padding = 0;
		//if it is the css block then different padding and margin
		if(p.getProperty('class') == 'db_css'){
			marginTop = 0;
			padding = 10;
		}
		
		var isOpen = false;
		//first finds  scrole height
		content.setStyle('display', 'block');
		var contentSize = content.scrollHeight;
		content.setStyle('height', 0);
		content.setStyle('display', 'none');
		
		var effect = new Fx.Styles(content, {'duration': 400, 'wait': false,  'transition': Fx.Transitions.cubicOut});
		
		//now adds events
		theBlock.addEvent('mouseenter', function(){
			heading.setStyle('color', linkHoverV);
			if(needClick == "0") {
				Site.collapsOpen(heading, content, effect, contentSize, marginTop, padding);
			}
		});
		theBlock.addEvent('mouseleave', function(){
			heading.setStyle('color', linkV);
			if(needClick == "0") {
				Site.collapsClose(heading, content, effect, contentSize);
			}
		});
		
		if(needClick == "1") {
			theBlock.addEvent('click', function(){
				if(isOpen) {
					Site.collapsClose(heading, content, effect, contentSize);
					isOpen = false;
				} else {
					Site.collapsOpen(heading, content, effect, contentSize, marginTop, padding);
					isOpen = true;
				}
			});
		}
	},
	
	collapsOpen: function(heading, content, effect, contentSize, marginTop, padding) {
		heading.addClass('hover');
		content.setStyle('display', 'block');
		effect.start({
			'height': contentSize,
			'margin-top': marginTop,
			'padding-top':padding,
			'padding-bottom':padding
		});
	},
	
	collapsClose: function(heading, content, effect, contentSize) {
		heading.removeClass('hover');
		content.setStyle('display', 'block');
		effect.start({
			'height': 0,
			'margin-top': 0,
			'padding-top':0,
			'padding-bottom':0
		});
	}
};

window.addEvent('domready', Site.start);

