/*From Blocks kit 1.3 by rapidweaver.org, works with mootools 1.2*/
var Page = new Class({
	initialize: function(){
		document.idUpTo = 0;
		this.findMenu();//done
    	this.findSubmenu();//done
    	this.findInfobox();//done
    },

	findSubmenu: function(){
        var submenuList = $$('.rb_submenu');
        this.submenus = [];
        for (i=0; i < submenuList.length; i++)
		{
			this.submenus[i] = new submenu(submenuList[i]);
		}
    },
    findInfobox: function(){
    	var infoboxList = $$('.db_infoboxWrapper');
    	this.infoboxs = [];
    	for (i=0; i < infoboxList.length; i++)
		{
			this.infoboxs[i] = new infoboxControl(infoboxList[i]);
		}
    	
    },
    findMenu: function(){
        var menuList = $$('.navcontainer');
        this.menus = [];
    	for (i=0; i < menuList.length; i++)
		{
			this.menus[i] = new menu(menuList[i]);
		}
    }
});

var siteBlock = new Class({
	initialize: function(wrapper){
		this.wrapper = wrapper;
	},
	//helper functions.
	/*looks inside wrapper for value like this,  <INPUT TYPE=HIDDEN NAME="effect" class="effect" value="8">
	extracts it, or returns the default if not found*/
	extractValue: function(lookFor, theDefault){
		var value = theDefault;
		var found=this.wrapper.getElement(lookFor);
		if(found){
			var v = found.getProperty('value');
			//make sure property exists, and block text has been replaced.
			if((v!="") && (v.search('<blocks') == -1) && (v.search('%id=') == -1)){
				value = found.getProperty('value');
			}
		}
		return value;
	},
	findLinkInLi: function(object) {
		var a = object.getElement('a'); 
		var b = a.getElement('a');
		if(b!=null) {a = b};
		return a;
	},
	getSpanStyle: function() {
		var span = this.wrapper.getElements('span');
		if(span.length > 0) {
			span = span[0];
			//use get rather than getproperty as the former does not work with IE
			var style = '';
			if(span.getStyle('font-size') != '0px') {
				style += 'font-size: ' + span.getStyle('font-size') + ';';
			}
			if(span.getStyle('font-family') != '') {
				style += 'font-family: ' + span.getStyle('font-family') + ';';
			}
			if(span.getStyle('color') != '') {
				style += 'color: ' + span.getStyle('color') + ';';
			}
			if(span.getStyle('font-weight') != '') {
				style += 'font-weight: ' + span.getStyle('font-weight') + ';';
			}
			return style;
		}
	},
	/*gets id and if there is no id, it creates one*/
	getId: function(object) {
		var id = object.getProperty('id');
		if(id == null) {
			id = 'blockkit_' + document.idUpTo;
			object.setProperty('id', id);
			document.idUpTo = document.idUpTo + 1;
		}
		return id;
	}
});

var submenu = new Class({
	Extends: siteBlock, 
	initialize: function(wrapper){
		this.parent(wrapper);
		this.extract();
		this.addColour();
		this.wrapCurrent();
		this.setupArrow();
	},
	extract: function(){
		this.linkHoverColour = this.extractValue('.linkColourHover', "red");
		this.padding = this.extractValue('.padding', 0);
		this.padding = parseFloat(this.padding);
		
		var current = this.wrapper.getElements('a[id=current]');
		this.currentLi = current[0].getParent();//use in setupArrow
		this.currentUl = this.currentLi.getParent();
		this.wrapperPos = this.wrapper.getCoordinates();
		
		this.style = this.getSpanStyle();
	},
	addColour: function(){
		var p = this.wrapper.getParent().getParent().getParent();
		var superId = this.getId(p);
		var cssToAdd = "#" + superId + " .rb_submenu a{" + this.style + "}";
		addCss(cssToAdd);
	},
	wrapCurrent: function(){
		//need to check on deeper submenu
		//wrap grandparents to current
		var grandParents = this.wrapper.getElements('a.currentAncestor');
		for (j=0; j < grandParents.length; j++)
		{
			var grandParentsLi = grandParents[j].getParent();
			grandParentsLi.addClass("grandParent");
		}
		this.currentUl.addClass("currentParent");
	},
	setupArrow: function(){
		var arrow = this.wrapper.getFirst('.arrowOnTheMove');
		this.startPos = this.findPos(this.currentLi);
		arrow.setStyle('top', this.startPos);
		
		this.arrowfxs = new Fx.Morph(arrow, {duration: 600, wait: false,  transition: Fx.Transitions.Sine.easeOut});
		var subLi = this.currentUl.getElements('li');
		for (j=0; j < subLi.length; j++)
		{
			this.rollOver(subLi[j]);
		}
	},
	findPos: function(li){
		var liPos = li.getCoordinates();
		return liPos.top - this.wrapperPos.top - 10 + liPos.height/2+this.padding;
	},
	rollOver: function(object) {
		var a = this.findLinkInLi(object);
		var varLinkHoverColour = this.linkHoverColour;
		var s = this.style;
		var varArrowfxs = this.arrowfxs;
		var varStartPos = this.startPos;
		var thisPos = this.findPos(object);
		
		a.set('style', s);
		
		object.addEvent('mouseenter', function(){
			a.setStyle('color', varLinkHoverColour);
			varArrowfxs.start({'top': thisPos});
		});
		object.addEvent('mouseleave', function(){
			a.set('style', s);
			varArrowfxs.start({'top': varStartPos});
		});
	}
});

var infoboxControl = new Class ({
	initialize: function(wrapper) {
		var instance = new infobox(wrapper);
		wrapper.addEvent('mouseenter', function(){instance.rollOver();});
		wrapper.addEvent('mouseleave', function(){instance.rollOut()});
		wrapper.addEvent('click', function(){instance.click()});
	}
});
var infobox = new Class({
	Extends: siteBlock, 
	initialize: function(wrapper){
		this.parent(wrapper);
		this.extract();
		this.addColour();
	},
	extract: function(){		
		this.parent = this.wrapper.getParent();
		this.heading = this.wrapper.getFirst('.rbheading');
		this.link = this.heading.getElements('span');
		this.content = this.wrapper.getFirst('.db_infoboxContent');//getElement('.db_infoboxContent');
		this.linkStyle = this.getSpanStyle();
		this.linkHoverColour = this.extractValue('.linkColourHover', 'black');
		this.needClick = this.extractValue('.needClick', '0');
		this.effect = new Fx.Morph(this.content, {duration: 400, wait: false, transition: Fx.Transitions.Sine.easeOut});
		
		this.marginTop = 20;
		this.padding = 0;
		if(this.parent.getProperty('class') == 'db_css'){this.marginTop = 0;this.padding = 10;}
	},
	addColour: function(){
		this.content.setStyle('display', 'block');
		this.contentSize = this.content.scrollHeight;
		this.content.setStyle('display', 'none');
		this.content.setStyle('height', 0);
		this.openAction(0, 0, 0);
	},
	
	rollOver: function(){
		this.link.setStyle('color', this.linkHoverColour);
		this.heading.setStyle('color', this.linkHoverColour);
		if(this.needClick == "0") { this.open();}
	},
	rollOut: function(){
		this.link.set('style', this.linkStyle);
		this.heading.setStyle('color', '');
		if(this.needClick == "0") { this.close();}
	},
	click: function(){
		if(this.needClick == "1") {
			if(this.isOpen) {this.close();
			} else {this.open();}
		}
	},
	open: function(){
		this.heading.addClass('hover');
		this.content.setStyle('display', 'block');
		this.openAction(this.contentSize, this.marginTop, this.padding);
		this.isOpen = true;
	},
	close: function() {
		this.heading.removeClass('hover');
		this.openAction(0, 0, 0);
		this.isOpen = false;
	},
	openAction: function(contentSize, marginTop, padding){
		this.effect.start({
			'height': contentSize,
			'margin-top': marginTop,
			'padding-top':padding,
			'padding-bottom':padding
		})
	}
});
var menu = new Class({
	Extends: siteBlock, 
	initialize: function(wrapper){
		this.parent(wrapper);
		this.getType();
		this.extract();
		this.addColour();
		this.stacksOverflowFix();
		this.addRollover();
		this.addDropdown();
	},
	getType: function() {
		this.parent = this.wrapper.getParent();
		this.grandParent = this.parent.getParent();
		var superClass = this.parent.getProperty('class');
		if(superClass != "nav_css") { this.isRound = true;
		} else { this.isRound = false;}
	},
	extract: function(){
		this.linkHoverColour = this.extractValue('.linkColourHover', 'red');
		this.effectType = this.extractValue('.effect', '1');
		this.effectSpeed = this.extractValue('.effectSpeed', '500');
		this.effectSpeed = parseFloat(this.effectSpeed);
		this.showSub = this.extractValue('.showSubMenu', '1');	
		this.showSplit = this.extractValue('.spliton', '1');
		this.splitColour = this.extractValue('.splitBorder', "black");
		if(!this.isRound) {
			this.linkBackground = this.extractValue('.linkColourBackground', "white");
			this.linkBackgroundHover = this.extractValue('.linkColourBackgroundHover', "white");
			this.linkColourBorder = this.extractValue('.linkColourBorder', "black");
			this.linkColourBorderHover = this.extractValue('.linkColourBorderHover', "red");
			this.popOutColour = this.extractValue('.popOutColour', "white");
		}
		
		this.superId = this.getId(this.grandParent);
		this.style = this.getSpanStyle();
	},
	addColour: function(){
		//do not add style together as if they return null, it will stuff up.
		//var cssToAdd = "#" + this.superId + " .navcontainer a{color: " + this.linkColour + "; font-family:" + this.font + "; visibility: visible;}";
		var cssToAdd = "#" + this.superId + " .navcontainer a{visibility: visible;}";
		cssToAdd += "#" + this.superId + " .navcontainer a{" + this.style + "}";
		if(this.isRound) {
			if(this.showSplit == '1') {
				cssToAdd += "#" + this.superId + " .navcontainer a{border-right: 1px solid " + this.splitColour + ";}";
			}
		} else {
			cssToAdd += "#" + this.superId  + " .nav_css .navcontainer a{background-color: " + this.linkBackground + "; border-bottom-color: " + this.linkColourBorder + ";}";
			cssToAdd += "#" + this.superId  + " .nav_css .navcontainer a:hover{background-color: " + this.linkBackgroundHover + "; border-bottom-color: " + this.linkColourBorderHover + ";}";
			cssToAdd += "#" + this.superId  + " .nav_css .navcontainer ul ul{background-color: " + this.popOutColour + "}";
			cssToAdd += "#" + this.superId  + " .nav_css .navcontainer ul ul a{background-color: " + this.popOutColour + "}";
		}
		addCss(cssToAdd);
	},
	//if its stack, gets first parent .stacks_out and adds overflow: visible; to it.
	//+ IE fix
	stacksOverflowFix: function() {
		var p = this.grandParent.getParents('.stacks_out');
		if(p.length > 0) {
			p[0].setStyle('overflow', 'visible');
			if((navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) || (navigator.userAgent.toLowerCase().indexOf('msie 7') != -1)) {
				p[0].setStyle('z-index', '3000');//for IE
			}
			if(!this.isRound) {p[0].setStyle('height', '50px');}//bit sneaky.
		}
	},
	addRollover: function(){
		var li = this.wrapper.getElements('li');
		for (j=0; j < li.length; j++)
		{
			this.rolloverEffect(li[j]);
		}
	},
	rolloverEffect: function(object){
		var a = this.findLinkInLi(object);
		var hoverColour = this.linkHoverColour;
		var s = this.style;
		object.addEvent('mouseenter', function(){
			a.setStyle('color', hoverColour);
		});
		object.addEvent('mouseleave', function(){
			//use set rather than setProperty as IE does not like the former
			a.set('style', s);
		});
	},
	addDropdown: function() {
		if(this.showSub == '1') {
			var mainUl = this.wrapper.getElements('ul');
			mainUl = mainUl[0];
			var menuHeading = mainUl.getChildren('li');
			for (j=0; j < menuHeading.length; j++)
			{
				var menu = menuHeading[j].getElements('ul');
				if(menu.length > 0){
					this.dropdownSetup(menuHeading[j], menu[0], j);
				}
			}
		}
	},
	dropdownSetup: function(menuHeading, menu, j) {
		var extra = 0;
		var factor = 1;
		if(this.isRound){extra = 40; factor=1.2;}
		if(typeof(GLOBAL_extra) != 'undefined'){extra = GLOBAL_extra;}
		if(typeof(GLOBAL_factor) != 'undefined'){factor = GLOBAL_factor;}

		menu.setStyle('display', 'block');
		var menuSize = menu.scrollHeight*factor + extra;
		menu.setStyle('height', 0);
		
		//This wraps the submenu li items in a div so that it can slide down with the menu.
		var subItems = menu.getChildren('li');
		var idOfWrap = 'mySecondElement' + j + this.superId;
		var mySecondElement = new Element('div', {id: idOfWrap, 'class': 'menuWrap'});
		mySecondElement.inject(menu, 'top');
		mySecondElement.adopt(subItems);
		cssToAdd = "#" + this.superId + " .navcontainer #" + idOfWrap + " {height: " + (menuSize) +"px;}";
		addCss(cssToAdd);
		
		this.dropdownEffect(menuHeading, menu, menuSize);
	},
	dropdownEffect: function(menuHeading, menu, menuSize){
		var menufxs = this.switchEffect(menu, this.effectType, this.effectSpeed);
		menuHeading.addEvent('mouseenter', function(){
			menufxs.start({
				'height': menuSize
			});
		});
		menuHeading.addEvent('mouseleave', function(){
			menufxs.start({
				'height': 0
			});
		});
	},
	switchEffect: function(object, effect, effectSpeed) {
		switch(effect){
			case '1': var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.linear}); break;
			case '2': var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.Quad.easeOut}); break;
			case '3': var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.Cubic.easeOut}); break;
			case '4': var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.Quart.easeOut}); break;
			case '5': var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.Quint.easeOut}); break;
			case '6': var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.Sine.easeOut}); break;
			case '7': var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.Expo.easeOut}); break;
			case '8': var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.Elastic.easeOut}); break;
			case '9': var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.Back.easeOut}); break;
			case '10': var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.Bounce.easeOut}); break;
			default: var menufxs = new Fx.Morph(object, {duration: effectSpeed, wait: false,  transition: Fx.Transitions.linear});
		}
		return menufxs;
	}
});

window.addEvent('domready', function(){ var newInstance = new Page();}); 