/**
 * Behaviour Class.
 */
Behaviour = {
    /**
     * @access public
     * @static
     */
    list: [],


    /**
     * @access public
     * @static
     */
    register: function( sheet ) {
        Behaviour.list.push( sheet );
	},

    /**
     * @access public
     * @static
     */
    start: function() {
        Behaviour.addLoadEvent( function() {
            Behaviour.apply();
        } );
    },

    /**
     * @access public
     * @static
     */
    apply: function() {
        for ( h = 0; sheet = Behaviour.list[h]; h++ ) {
            for ( selector in sheet ) {
                list = document.getElementsBySelector( selector );

                if ( !list ) {
                    continue;
                }

                for ( i = 0; ele = list[i]; i++ ) {
                    sheet[selector]( document.getElementById( ele.id ) );
                }
            }
        }
    }
};


/**
 * Returns an array of element objects from the current document
 * matching the CSS selector. Selectors can contain element names,
 * class names and ids and can be nested.
 *
 * @access public
 * @static
 */
document.getElementsBySelector = function( selector ) {
    // attempt to fail gracefully in lesser browsers
    if ( !document.getElementsByTagName ) {
        return new Array();
    }

    // split selector in to tokens
    var tokens = selector.split( ' ' );
    var currentContext = new Array( document );

    for ( var i = 0; i < tokens.length; i++ ) {
        token = tokens[i].replace( /^\s+/, '' ).replace( /\s+$/, '' );

        // token is an ID selector
        if ( token.indexOf( '#' ) > -1 ) {
            var bits    = token.split( '#' );
            var tagName = bits[0];
            var id      = bits[1];
            var element = document.getElementById( id );

            if ( tagName && element.nodeName.toLowerCase() != tagName ) {
                // tag with that ID not found, return false
                return new Array();
            }

            // set currentContext to contain just this element
            currentContext = new Array( element );
            continue; // Skip to next token
        }

        // token contains a class selector
        if ( token.indexOf( '.' ) > -1 ) {
            var bits      = token.split( '.' );
            var tagName   = bits[0];
            var className = bits[1];

            if ( !tagName ) {
                tagName = '*';
            }

            // get elements matching tag, filter them for class selector
            var found = new Array;
            var foundCount = 0;

            for ( var h = 0; h < currentContext.length; h++ ) {
                var elements;

                if ( tagName == '*' ) {
                    elements = currentContext[h].all? currentContext[h].all : currentContext[h].getElementsByTagName( '*' ); // IE workaround
                } else {
                    elements = currentContext[h].getElementsByTagName( tagName );
                }

                for ( var j = 0; j < elements.length; j++ ) {
                    found[foundCount++] = elements[j];
                }
            }

            currentContext = new Array;
            var currentContextIndex = 0;

            for ( var k = 0; k < found.length; k++ ) {
                if ( found[k].className && found[k].className.match( new RegExp( '\\b' + className + '\\b' ) ) ) {
                    currentContext[currentContextIndex++] = found[k];
                }
            }

            continue; // Skip to next token
        }

        if ( token.match( /^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/) ) {
            var tagName      = RegExp.$1;
            var attrName     = RegExp.$2;
            var attrOperator = RegExp.$3;
            var attrValue    = RegExp.$4;

            if ( !tagName ) {
                tagName = '*';
            }

            // grab all of the tagName elements within current context
            var found = new Array;
            var foundCount = 0;

            for ( var h = 0; h < currentContext.length; h++ ) {
                var elements;

                if ( tagName == '*' ) {
                    elements = currentContext[h].all? currentContext[h].all : currentContext[h].getElementsByTagName( '*' ); // IE workaround
                } else {
                    elements = currentContext[h].getElementsByTagName( tagName );
                }

                for ( var j = 0; j < elements.length; j++ ) {
                    found[foundCount++] = elements[j];
                }
            }

            currentContext = new Array;
            var currentContextIndex = 0;
            var checkFunction; // this function will be used to filter the elements

            switch ( attrOperator ) {
                case '=': // equality
                    checkFunction = function( e ) {
                        return ( e.getAttribute(attrName) == attrValue );
                    };

                    break;

                case '~': // match one of space seperated words
                    checkFunction = function( e ) {
                        return ( e.getAttribute( attrName ).match( new RegExp( '\\b' + attrValue + '\\b' ) ) );
                    };

                    break;

                case '|': // match start with value followed by optional hyphen
                    checkFunction = function( e ) {
                        return ( e.getAttribute( attrName ).match( new RegExp( '^' + attrValue + '-?' ) ) );
                    };

                    break;

                case '^': // match starts with value
                    checkFunction = function( e ) {
                        return ( e.getAttribute( attrName ).indexOf( attrValue ) == 0 );
                    };

                    break;

                case '$': // match ends with value - fails with "Warning" in Opera 7
                    checkFunction = function( e ) {
                        return ( e.getAttribute( attrName ).lastIndexOf( attrValue ) == e.getAttribute( attrName ).length - attrValue.length );
                    };

                    break;

                case '*': // match ends with value
                    checkFunction = function( e ) {
                        return ( e.getAttribute( attrName ).indexOf( attrValue ) > -1 );
                    };

                    break;

                default:
                    // just test for existence of attribute
                    checkFunction = function( e ) {
                        return e.getAttribute( attrName );
                    };
            }

            currentContext = new Array;
            var currentContextIndex = 0;

            for ( var k = 0; k < found.length; k++ ) {
                if ( checkFunction( found[k] ) ) {
                    currentContext[currentContextIndex++] = found[k];
                }
            }

            continue; // skip to next token
        }

        // if we get here, token is JUST an element (not a class or ID selector)
        tagName = token;
        var found = new Array;
        var foundCount = 0;

        for ( var h = 0; h < currentContext.length; h++ ) {
            var elements = currentContext[h].getElementsByTagName( tagName );

            for ( var j = 0; j < elements.length; j++ ) {
                found[foundCount++] = elements[j];
            }
        }

        currentContext = found;
    }

    return currentContext;
};


Behaviour.register( {
    '#searchparameter': function( ele ) {
        ele.onfocus = function() {
            if ( !arguments.callee.done ) {
                ele.value = '';
                ele.style.color = '#000000';

                arguments.callee.done = true;
            }
        }
    },
    '#language': function( ele ) {
        ele.onchange = function() {
            document.location.href = ele.value;
        }
    },
    '#language-submit': function( ele ) {
        ele.style.display = 'none';
    },
    '#nav-main': function( ele ) {
        // helper function to hide submenus
        function getChildByTagName( ele, tagName ) {
            if ( ele.childNodes && ( ele.childNodes.length > 0 ) ) {
                for ( var i = 0; i < ele.childNodes.length; i++ ) {
                    if ( ele.childNodes[i].nodeName == tagName ) {
                        return ele.childNodes[i];
                    }
                }
            }

            return false;
        }

        function hideSubmenus() {
            for ( i = 0; i < nav_root.childNodes.length; i++ ) {
                var node = nav_root.childNodes[i], sub;

                if ( ( node.nodeName == 'LI' ) && ( sub = getChildByTagName( node, 'UL' ) ) ) {
                    sub.className = sub.className.replace( ' hovered', '' ).replace( ' active', '' );
                }
            }
        }

        nav_root = getChildByTagName( ele, 'UL' );
        li_nodes = document.getElementsBySelector( '#nav-main ul li' );

        document.getElementById( 'content-wrap' ).onmouseover = function() {
            hideSubmenus();
            active_menu.className += ' active';
        }

        for ( i = 0; i < li_nodes.length; i++ ) {
            var node = li_nodes[i], sub;

            if ( node.nodeName == 'LI' ) {
                if ( ( sub = getChildByTagName( node, 'UL' ) ) && ( sub.className.indexOf( 'active' ) > 0 ) ) {
                    active_menu = sub;
                }

                node.onmouseover = function() {
                    hideSubmenus();
                    getChildByTagName( this, 'UL' ).className += ' hovered';
                }
            }
        }
    },
	'#showdeladdress': function (ele) {
		ele.onclick = function() {
            document.order.submit();
        }
	},
	'#deladr-submit': function ( ele ) {
        ele.style.display = 'none';
    },
	'#agb': function (ele) {
		ele.onclick = function() {
			if (ele.checked == true)
				document.order.agb2.checked = true;
			else document.order2.agb2.checked = false;
		}
	},
	'#agb2': function (ele) {
		ele.onclick = function() {
			if (ele.checked == true)
				document.order.agb.checked = true;
			else document.order.agb.checked = false;
		}
	}
} );


var active_menu = '';
var nav_root    = null;

FastInit.addOnLoad( function() {
    Behaviour.apply();
} );
