function $_( n ) {
    if ( !n ) return false;
    if ( !document.getElementById(n) ) return false;
    return document.getElementById(n);
}

function ready( func ) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function bubbling_no( e ) {
    e = bubbling_ie( e ); /* for ie */
    if ( !e.cancelBubble ) e.cancelBubble = true;
    if ( e.stopPropagation ) e.stopPropagation();
    return false;
}
function bubbling_ie( e ) {
    if ( !e ) var e = window.event;
    return e;
}

function bounds( el, flag ) {
    var left = el.offsetLeft;
    var top = el.offsetTop;
    for (var parent = el.offsetParent; parent; parent = parent.offsetParent) {
        if (parent.className.indexOf( 'wrapper-w2' ) != -1 && flag != 1) return { left: left, top: top, width: el.offsetWidth, height: el.offsetHeight };
        left += parent.offsetLeft;
        top += parent.offsetTop;
    }
    return { left: left, top: top, width: el.offsetWidth, height: el.offsetHeight };
}

function page_size() {
    var de = document.documentElement;
    var w = window.innerWidth || self.innerWidth || ( de&&de.clientWidth ) || document.body.clientWidth;
    var h = window.innerHeight || self.innerHeight || ( de&&de.clientHeight ) || document.body.clientHeight;
    
    pagesize = new Array( w,h ) 
    return pagesize;
}

function menu_fix() {
    if ( $_('last_item') ) {
        var pagesize = page_size();
        if ( pagesize[0] < 1120 ) {
            $_('last_item').className += ' setleft';
        } else {
            $_('last_item').className = $_( 'last_item' ).className.replace( ' setleft','' );
        }
    }
}

function mail( name, dom, a, display ) {
    var m = 'mailto:';
    a = '.' + a;
    document.write( '<a href="'+m+name+'@'+dom+a+'">'+(display?display:name+'@'+dom+a)+'</a>' );
}

toggle = function() {
    var prethis, preel;
    
    function tposition( _this, el ) {
        var pagesize = page_size();
        var top = bounds( _this ).top;
        var left = bounds( _this ).left;
        var width = bounds( _this ).width;
        var elWidth = bounds( $_( el ) ).width;
        var height = bounds( _this ).height;
        
        $_( el ).style.top = top + height + 'px';
        $_( el ).style.left = left + 'px';
        
        if ( pagesize[0] < bounds( _this, 1 ).left + elWidth + 25 ) {
            $_( el ).style.marginLeft = width - elWidth + 'px';
            if( $_( 'x-bubble' ) ) {
                $_( 'x-bubble' ).style.right = width + 'px';
                $_( 'x-bubble' ).style.left = 'auto';
            }
        } else {
            $_( el ).style.marginLeft = 0;
            if( $_( 'x-bubble' ) ) {
                $_( 'x-bubble' ).style.right = 'auto';
                $_( 'x-bubble' ).style.left = 0;
            }
        }

    }
    
    function thide( _this, el ) {
        $_( el ).style.display = 'none';
        _this.className = _this.className.replace( ' active','' );
    }
    
    function tform( e, _this, el ) {
        if ($_( preel ) && preel != el) thide( prethis, preel );
        prethis = _this;
        preel = el;
        bubbling_no( e );
        if ($_( el )) {
            if ($_( el ).style.display == 'none') {
                _this.className = _this.className += ' active';
                $_( el ).style.display = 'block';   
                $_( el ).onclick = function( e ) { bubbling_no( e ); };
            } else if (!$_( preel ) || preel != el) {
                thide( _this, el );
            }
            
            tposition( _this, el );
            
            window.onresize = function() { tposition( _this, el ); };
            
            document.onclick = function() { thide( _this, el ); };
        }
        _this.blur();
    }
    return {
       form: tform,
       hide: thide,
       position: tposition
   }
}();

ready( menu_fix );
window.onresize = menu_fix;