/*
* block-link.js
* A valid, standards-compliant, unobtrusive method for creating links that contain block-level elements
* by David Benton
* http://www.dbenton.com
* Many thanks to those who did the bulk of the work. Their names are listed with their contributions below.
*/

/*
	setOpacity()
	Peter-Paul Koch
	http://www.quirksmode.org/js/opacity.html
*/
function setOpacity(obj, value)
{
	obj.style.opacity = value/10;
	obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

/*
	jscss()
	http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
*/
function jscss(a,o,c1,c2)
{
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
	  var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}

/*
	getStyle()
	Robert Nyman
	http://www.robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/
*/

function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

/*
    getElementsByClassName()
	Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function blockLink() {
	
	//Thanks Mar. <http://mar.anomy.net/entry/2006/11/23/00.06.40/>
	var is_ie/*@cc_on = {
		// quirksmode : (document.compatMode=="BackCompat"),
		version : parseFloat(navigator.appVersion.match(/MSIE (.+?);/)[1])
	}@*/;
	
	var linkareas = getElementsByClassName(document, "*", "linkarea");
	for (var i = 0; i < linkareas.length; i++) {
		
		var links = linkareas[i].getElementsByTagName("a");
		
		if (links.length > 0) {
			
			var linksource = getElementsByClassName(linkareas[i], "a", "linksource");
			
			if (linksource.length == 0) {
				linksource = links;
			}
			
			linkareas[i].style.position = 'relative';
			
			var overlaylink = document.createElement('a');
			overlaylink.href = linksource[0].href;
			overlaylink.className = 'blocklink';
			overlaylink.style.width = linkareas[i].offsetWidth + 'px';
			overlaylink.style.height = linkareas[i].offsetHeight + 'px';
			overlaylink.style.top = '0';
			overlaylink.style.left = '0';
			overlaylink.style.padding = '0';
			overlaylink.style.margin = '0';
			overlaylink.style.position = 'absolute';
			overlaylink.style.zIndex = '10';
			if (is_ie)	{
				overlaylink.style.backgroundColor = '#FFFFFF'; /*IE needs this (heaven knows why), but it causes problems in iCab*/
				setOpacity(overlaylink, 0);
			}
			
			overlaylink.onmouseover = function() { if ('function' == typeof jscss) { jscss( 'add', this.parentNode, 'linkarea-hover' );} }
			overlaylink.onmouseout = function() { if ('function' == typeof jscss) { jscss( 'remove', this.parentNode, 'linkarea-hover' );} }
			
			var existing_link = getElementsByClassName(linkareas[i], "a", "blocklink");
			
			if (existing_link.length > 0) {
				existing_link[0].parentNode.removeChild(existing_link[0]);
			}
			
			linkareas[i].insertBefore(overlaylink, linkareas[i].lastChild.nextSibling);
		}
	}	
}

/*
DropDown Class
by David Benton
based on the script at:
http://www.robinwinslow.me.uk/scratch/fademenu/FadeMenu.html
*/

var DropDown = new Class({
    
	initialize: function dropdownMenu(elem,options) {
        
		// Retrieve the element properly
        this.parentElem = $(elem);
        
		if($type(this.parentElem) != 'element') {throw new Error('invalid element or id passed as first argument');}
        // Add the options
        
		this.options = new Abstract({ // Defaults
            fadeInSpeed: 0,
            fadeOutSpeed: 0,
            fadeOutDelay: 0
        });
        
		if($type(options) == 'object') {this.options.extend(options);}
        
		// Retrieve the child items
        this.menuItems = this.parentElem.getChildren().filterByTag('li');
        
		// Setup all sub menus
        for(var i=0; i<this.menuItems.length; i++) {
           
		    // Find this item's sub menu
            var subMenu = this.menuItems[i].getElement('ul');
            
			// If we have a sub menu, set it up
            if(
                $type(subMenu) == 'element' // Make sure it's an element
                && !($defined(this.ignoreClass) && subMenu.className != this.ignoreClass) // And that it doesn't have the "ignoreClass"
            ) {
                this.menuItems[i].subMenu = subMenu;
                this.setupSubMenu(subMenu);
                subMenu.create(this.options);
            }
        }
    },
	
    setupSubMenu: function(subMenu) {
        if($type(subMenu) == 'element') {
            
			new Abstract(subMenu).extend({
                
				create: function(options) {
                    
					// Get options
                    this.options = options;
                    
					// Retrieve elements
                    this.parentElem = this.getParent();
                    
					// Make sure the element and all it's items are hidden
                    //this.menuItems().setOpacity(0);
                    this.setOpacity(0);
                    
					// Create new events for fadein and fadeout
                    this.addEvent('fadein',this.showMenu);
                    this.addEvent('fadeout',this.hideMenu);
                    
					// Add mouseenter and mouseleave events to the sub menu
                    var thisMenu = this;
                    this.parentElem.addEvent('mouseenter',function(evt) {thisMenu.latestEvent = 'mouseenter'; thisMenu.fireEvent('fadein',evt)});
                    this.parentElem.addEvent('mouseleave',function(evt) {thisMenu.latestEvent = 'mouseleave'; thisMenu.fireEvent('fadeout',evt,thisMenu.options.fadeOutDelay)});
                    
                    // If recursive, run fadeMenu on this list
                    if(this.options.fadeRecursive) {new FadeMenu(this,this.options)};
                },
				
                showMenu: function(evt) {
                    
					if(this.latestEvent == 'mouseleave') {return false;}
                    
					this.parentElem = this.getParent();
					this.parentElem.addClass('active');

					this.setOpacity(1); // Make sure the list is visible
                    //this.fadeInItems();
                },
                
				hideMenu: function(evt) {
                    
					if(this.latestEvent == 'mouseenter') {return false;}
                    
					this.parentElem = this.getParent();
					this.parentElem.removeClass('active');

					this.setOpacity(0);
                },
               
			    fadeOutItems: function() {
                    // Get the list element
                    var thisMenu;
                    if(this.element) {thisMenu = this.element.getParent();}
                    else {thisMenu = this;}
                    // If there are still menu items left, fade out the last one
                    if(thisMenu.latestEvent == 'mouseleave') {thisMenu.setOpacity(0);}
                },
                
				menuItems: function() {return this.getChildren().filterByTag('li')}
            });
        }
    }
});

/*
addLoadEvent()
by Simon Willison
http://simonwillison.net/2004/May/26/addLoadEvent/
*/
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}