Fx.Morph = Fx.Styles.extend({
	
	start: function(className){

		var to = {};


		$each(document.styleSheets, function(style){
			var rules = style.rules || style.cssRules;
			$each(rules, function(rule){


				if (!rule.selectorText.test('\.' + className + '$')) return;
				Fx.CSS.Styles.each(function(style){
					if (!rule.style || !rule.style[style]) return;
					var ruleStyle = rule.style[style];
					to[style] = (style.test(/color/i) && ruleStyle.test(/^rgb/)) ? ruleStyle.rgbToHex() : ruleStyle;
				});
			});
		});


		return this.parent(to);
	}

});




sfHover = function() {
	var sfEls = document.getElementById("menulist_root").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}







// There are certain things we need to wait for the page to load before we can run them
// Here is where they are called
var load_method = (window.ie ? 'load' : 'domready');
window.addEvent(load_method, function(){
//window.addEvent('domready', function(){
	

	// Chris' funky menu stuff
	
	
	sfHover();
	format_main_menu();
	format_side_menu();	
	split_menus();
	right_click_menu();
	
	
	/*
	// Extra Salsa fun bits
	moveFixedContactBar();
	contactus_bar();

	setFooterBoxWidths();
	salsa_footer();
	salsa_boxes();*/






	var szNormal = 196, szSmall  = 161, szFull   = 301;
	var boxes = $$(".module-slidebox");
	var fx = new Fx.Elements(boxes, {wait: false, duration: 700, transition: Fx.Transitions.Back.easeOut});
	
	Fx.CSS.Styles = ["backgroundColor", "color","top"];
	
	boxes.each(function(box, i) {
	
		var myMorph = new Fx.Morph(box, {wait: false,duration: 300});
		var myMorph1 = new Fx.Morph(box.getElementsByTagName('h2')[0], {wait: false,duration: 500});
	
		box.addEvent("mouseenter", function(event) {
				var o = {};
						o[i] = {width: [box.getStyle("width").toInt(), szFull]}
						boxes.each(function(other, j) {
							if(i != j) {
								var w = other.getStyle("width").toInt();
								if(w != szSmall) o[j] = {width: [w, szSmall]};
							}
						});
			fx.start(o);
	
			//new Event(event).stop();

			myMorph.start('boxmorph');
			//myMorph1.start('headingmorph');
	
		});
	
		box.addEvent("mouseleave", function(event) {
			var o = {};
			boxes.each(function(box, i) {
				o[i] = {width: [box.getStyle("width").toInt(), szNormal]}
			});
			fx.start(o);
		
			//new Event(event).stop();
		
			myMorph.start('boxmorphorig');
			//myMorph1.start('headingmorphorig');
		})
	});


	
	


	var myMorph3 = new Fx.Morph('contact_bar', {wait: false,duration: 800});
	
	var isVisible = false;
	var headerHeight = 117;
	
	window.onscroll = function()
	{
		
		// Thanks to Johan Sundström (http://ecmanaut.blogspot.com/) and David Lantner (http://lantner.net/david) 
		// for their help getting Safari working as documented at http://www.derekallard.com/blog/post/conditionally-sticky-sidebar
		if( window.XMLHttpRequest ) { // IE 6 doesn't implement position fixed nicely...
			if (!isVisible && (document.documentElement.scrollTop > headerHeight || self.pageYOffset > headerHeight)) {
				
				$('contact_bar').style.position = 'fixed';
				myMorph3.start('contact_bar_open');
				isVisible = true;
				
			} else if (isVisible && (document.documentElement.scrollTop < headerHeight || self.pageYOffset < headerHeight)) {
				myMorph3.start('contact_bar_closed');
				isVisible = false;
			}
		}
	}
	
	
	
	
	
	/*
	
	var move = new Fx.Style('footer-bottom-wrapper', 'top');
	var isUp = false;

	$('footer-content').addEvent('click', function(){
		var footer_height = $('footer-wrap').getStyle('height').toInt();
	//	move.start(isUp ? footer_height-50 : footer_height-170);
		isUp = !isUp;
	});		
	
	*/
	
	
	
	


	
	moveFixedContactBar();
	setFooterBoxWidths();

	
	


});









function expandArea(area) {

	var boxes = $$('.module-slidebox');

	boxes.each(function(box) {
		box.fireEvent('mouseleave');
	});


	if(area != 'end') {
	
	$$(area)[0].fireEvent('mouseenter');

	}
}



// mad
function setFooterBoxWidths() {	
	var move = new Fx.Style('footer-bottom-wrapper', 'top');
	var isUp = false;
	
	/*
	
	$('footer-content').addEvent('click', function(){
		var footer_height = $('footer-wrap').getStyle('height').toInt();
		move.start(isUp ? footer_height-50 : footer_height-170);
		isUp = !isUp;
	});	
	
	*/
	
	var footer_boxes = $$('.moduletable-footer-content-block');	

	if(footer_boxes.length > 0) {

		var footer_width = $('footer-modules').getStyle('width').toInt();
		var margin_right = footer_boxes[0].getStyle('margin-right').toInt();
		
		var box_size = (footer_width - (margin_right*(footer_boxes.length-1))) / footer_boxes.length;
	
		footer_boxes.each(function(footer,i) {
		
			if(i == (footer_boxes.length-1)) {
				footer.setStyle('margin-right','0px');
			}
		
			footer.setStyle('width',box_size +'px');
			footer.setStyle('display','block');
			
			//new Fx.Style(div, 'opacity', {duration: 1000} ).start(1);
		});
	}
	
	// Set the height of the footer box, and the flickr bottom stuff.
	var footer_height = $('footer-wrap').getStyle('height').toInt();
//alert(footer_height);
	
	//if(footer_height>1)	var flickr_bar = $('footer-bottom-wrapper').setStyle('top',footer_height-50 + 'px');	
	
}




function moveFixedContactBar() {
	var winW = null
	if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
				winW = window.innerWidth;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				winW = document.body.clientwidth;
			}
	}
	
	if(winW!=null){
		var left = (winW - $('contact_bar').getStyle('width').toInt()) / 2;
		$('contact_bar').setStyle('left',left+'px');
	}
}








	
// Chris' awesomely cool side menu hackeroo
function format_side_menu(){
	var current_item = $$('#column #current');
	if(current_item!=''){
		var item_parent = current_item[0].parentNode;

		if(item_parent.id!="menulist_root"){
			item_parent.className="current_item";
			while(item_parent.parentNode.className!="menu"){
				item_parent.parentNode.className+=" current_item";
				item_parent = item_parent.parentNode;
			}
		}
	} 
	// End Chris' awesomely cool side menu hackeroo
}
	
	
// Chris' awesomely cool main menu hackeroo
function format_main_menu(){		
	var current_item = $$('#navigation #current');
	
	if(current_item != '') {
	
	var item_parent = current_item[0].parentNode;
	
		if(item_parent.id!="menulist_root"){
			item_parent.className="current_item";
			while(item_parent.parentNode.className!="menu"){
				item_parent.parentNode.className+=" current_item";
				item_parent = item_parent.parentNode;
			}
		}
	
	}
	// End Chris' awesomely cool main menu hackeroo
}


// Chris' awesome cool main menu hideroo...er
// Any menu items that come after a spacer menu item will be hidden
function split_menus(){
	var spacers = $$('#navigation span span');
	if(spacers!=''){
		spacers.each(function(spacer){
			var containing_li = spacer.parentNode.parentNode;
			var next_sibling = containing_li.nextSibling;
			var containing_ul = containing_li.parentNode;
			containing_li.parentNode.removeChild(containing_li);
		
			while(next_sibling!=''){
				thisElement = next_sibling;
				try{
					next_sibling = next_sibling.nextSibling;
					thisElement.parentNode.removeChild(thisElement);
				} catch (err){
					next_sibling = '';
				}
			}
		});
	}
}




















function right_click_menu(){
	document.onclick = function(event){
		if(event!=null){
			if(event.button==2){
				var event = new Event(event);
				var pg = event.page;
				$('rc_menubox').setStyles({left: pg.x+'px', top: pg.y+'px'});
			} else {
				$('rc_menubox').setStyles({left:'-1000px'});
			}
		}
	}
}



function contactus_bar(){
	var myMorph3 = new Fx.Morph('contact_bar', {wait: false,duration: 800});
	
	
	var isVisible = false;
	var headerHeight = 117;
	
	window.onscroll = function()
	{
		
		// Thanks to Johan Sundström (http://ecmanaut.blogspot.com/) and David Lantner (http://lantner.net/david) 
		// for their help getting Safari working as documented at http://www.derekallard.com/blog/post/conditionally-sticky-sidebar
		if( window.XMLHttpRequest ) { // IE 6 doesn't implement position fixed nicely...
			if (!isVisible && (document.documentElement.scrollTop > headerHeight || self.pageYOffset > headerHeight)) {
				
	
				$('contact_bar').style.position = 'fixed';
	
				myMorph3.start('contact_bar_open');
	
				isVisible = true;
	
	
				
			} else if (isVisible && (document.documentElement.scrollTop < headerHeight || self.pageYOffset < headerHeight)) {
				
				myMorph3.start('contact_bar_closed');
	
				isVisible = false;
			}
		}
	}	
}







// Home page boxes
function salsa_boxes(){
	var szNormal = 196, szSmall  = 161, szFull   = 301;
	
	var boxes = $$(".module-slidebox");
	
	var fx = new Fx.Elements(boxes, {wait: false, duration: 700, transition: Fx.Transitions.Back.easeOut});
	
	
	Fx.CSS.Styles = ["backgroundColor", "color","top"];
	
	
	boxes.each(function(box, i) {
	
	
		var myMorph = new Fx.Morph(box, {wait: false,duration: 300});
		var myMorph1 = new Fx.Morph(box.getElementsByTagName('h2')[0], {wait: false,duration: 500});
	
		box.addEvent("mouseenter", function(event) {
				var o = {};
						o[i] = {width: [box.getStyle("width").toInt(), szFull]}
						boxes.each(function(other, j) {
							if(i != j) {
								var w = other.getStyle("width").toInt();
								if(w != szSmall) o[j] = {width: [w, szSmall]};
							}
						});
			fx.start(o);
	
			//new Event(event).stop();
	
			myMorph.start('boxmorph');
			//myMorph1.start('headingmorph');
	
		});
	
		box.addEvent("mouseleave", function(event) {
			var o = {};
			boxes.each(function(box, i) {
				o[i] = {width: [box.getStyle("width").toInt(), szNormal]}
			});
			fx.start(o);
		
			//new Event(event).stop();
		
		
			myMorph.start('boxmorphorig');
			//myMorph1.start('headingmorphorig');
		})
	
	});
	
}






// Allow the user to expand / collapse the footer
function salsa_footer(){
	var move = new Fx.Style('footer-bottom-wrapper', 'top');
	
	
	$('footer-content').addEvent('click', function(){
	
		var footer_height = $('footer-wrap').getStyle('height').toInt();
	
		move.start(isUp ? footer_height-50 : footer_height-170);
		isUp = !isUp;
	});	
	
}
