// POPUP PARAMS
// POPUP PARAMS
var M_POPUP = 0;
var M_DROPDOWN = 1;

var WIDTH = 0;
var TIMEOUT = 1;
var HIDE_SELECTED = 2;

document.is_popup = {

	current: false,
	wmm: false,
	interval: false,
	sinterval: false,
	timeout: 300,
	hovertime: 250,
	mode: M_POPUP,
	parent: false,
	ddown: true,
	parent_class: '',
	ev: 0,

	submenus: [],
	tmpparams: false,

	// Reconfigurable with params
	width: "200px",

	init: function()
	{
		if(document.layers)
		{
			if( window.onmousemove )
				this.wmm = window.onmousemove;

			window.captureEvents(Event.MOUSEMOVE);
			window.onmousemove = document.is_popup.icheck;
		}
		else
		{
			if( document.onmousemove )
				this.wmm = document.onmousemove;

			document.onmousemove = document.is_popup.icheck;
		}

		this.submenus = new Array();
	},

	show: function( event, object, use_menu )
	{
		thisp = document.is_popup;
        if( object || thisp.over_object )
            var el = object || thisp.over_object;
        else
        {
            if( typeof( event ) != "object" && thisp.ev )
                var el = thisp.ev;
            else
                var el = event.currentTarget || event.srcElement;
        }
		var cp = thisp.pareseParams( arguments );
        if( use_menu || thisp.over_menu )
            var menu = document.getElementById( use_menu || thisp.over_menu );
        else
            var menu = document.getElementById( el.id + "_popup" );
		if( !menu ) { return; }
		if( thisp.current ) { thisp.close(); }

		if( thisp.sinterval )
		{
			window.clearTimeout( thisp.sinterval );
			thisp.sinterval = false;
		}

    if( el.className.length > 0 )
    {
  		if( !el.old_classes )
  			el.old_classes = el.className;
  		if ( el.className.indexOf( "DDover" ) == -1 )
  			el.className = el.className + " DDover";
    }
		thisp.mode = M_DROPDOWN;
		thisp.current = menu;
		thisp.parent = el;
		if( el.old_classes )
			thisp.parent_class = el.old_classes;
		else
			thisp.parent_class = el.className;

//		el.className += "_active";

		// Find parent coords
		var x = Utils.findPosX( el );
		var y = Utils.findPosY( el );
		if( Utils.browser.IE )
			x = Utils.getX( el ) - 2;

		// Open Up or down / left or right ( client height pos )
		var ch = Utils.bodyHeight();
		var cw = Utils.bodyWidth();

		y += Utils.getHeight( el );

		menu.style.left = x + 'px';
		menu.style.top = y + 'px';

		menu.style.display = "block";

		thisp.readmenuitems();

		if( cp[WIDTH] )
			menu.style.width = cp[WIDTH];
		else
		{
			if( menu.lang == "AUTOWIDTH" )
				menu.style.minWidth = (Utils.getWidth( thisp.parent ) ) + "px";
			else
				menu.style.width = (Utils.getWidth( thisp.parent ) ) + "px";
		}

    // Menu Width / Heihgt
		var mw = Utils.getWidth( menu );
		var mh = Utils.getHeight( menu );

		// Reposition menu if needed
		// TO DO: popup || drop down ( different behaviour )

		// Drop up if needed
		//if( y + mh > ch ) { ddown = false; y = y - mh + Utils.getHeight( el ); }
		// Drop left if needed
		if( x + mw > cw ) { x = x - mw + Utils.getWidth( el ); }
		//if( navigator.userAgent.toLowerCase().indexOf('msie') > -1 )
			//x++;
		menu.style.left = x+'px';
		menu.style.top = y+'px';
	},

	pareseParams: function( params )
	{
		if( params.length <= 3 )
			return [];
		var args = [];

		// Parse all arguments over 3
		for( i=3;i<params.length;i+=2 )
		{
			if( params[i+1] )
				args[params[i]] = params[i+1];
			else
				break;
		}

		return args;
	},

	close: function()
	{
		thisp = document.is_popup;
		if( !thisp.current )
			return;

		if( thisp.parent.old_classes )
			thisp.parent.className = thisp.parent.old_classes;

    	for(i=0; i<thisp.submenus.length; i++)
			thisp.submenus[i].menu.submenu.style.display = "none";
		thisp.submenus.length = 0;

		if( thisp.mode == M_DROPDOWN && thisp.parent )
			thisp.parent.className = thisp.parent_class;

		thisp.current.style.display = "none";
		thisp.current = false;
		if( thisp.interval ) { window.clearTimeout( thisp.interval ); thisp.interval = false; }
	},

	icheck: function(e)
	{
		thisp = document.is_popup;
		if( thisp.wmm )
			thisp.wmm();

		if( !thisp.current ) { return; }

		// Run submenu checks
		for(i=0; i<thisp.submenus.length; i++ )
		{
			if( thisp.submenus[i].icheck( e ) )
				return;
		}

		var ax = parseInt( thisp.current.style.left );
		var ay = parseInt( thisp.current.style.top );
		var aw = parseInt( thisp.current.offsetWidth );
		var ah = parseInt( thisp.current.offsetHeight );

		var c = Utils.getCursorPosition(e);

		var ayo = 0;
		// Calc offset if DD, based on parent height
		ayo = Utils.getHeight( thisp.parent );
		// Out of the box ?
		if( c.x < ax || c.x > ax+aw )
		{
			if( !thisp.interval ) { thisp.interval = window.setTimeout( document.is_popup.close, thisp.timeout ); }
			return;
		}

		if( c.y < ay-( thisp.ddown ? ayo : 0 ) || c.y > ay+ah+( !thisp.ddown ? ayo : 0 ) )
		{
			if( !thisp.interval ) { thisp.interval = window.setTimeout( document.is_popup.close, thisp.timeout ); }
			return;
		}

		if( thisp.interval ) { window.clearTimeout( thisp.interval ); thisp.interval = false; }

	},

	over: function( event, object, use_menu )
	{
        thisp = document.is_popup;

        if( object )
            thisp.over_object = object;
        else
            delete ( thisp.over_object );
        if( use_menu )
            thisp.over_menu = use_menu;
        else
            delete ( thisp.over_menu );

        if( !thisp.sinterval )
		{
			thisp.ev = event.currentTarget || event.srcElement;
			thisp.sinterval = window.setTimeout( document.is_popup.show, thisp.hovertime );
		}
	},

	out: function()
	{
		thisp = document.is_popup;

		var el = thisp.parent;

		/*if( el.old_classes )
		{
			el.className = el.old_classes;
			el.old_classes = false;
		}*/

        delete( thisp.over_menu );
        delete( thisp.over_object );

        if( !thisp.current && thisp.sinterval )
		{
			window.clearTimeout( thisp.sinterval );
			thisp.sinterval = false;
			thisp.ev = false;
		}
	},

	readmenuitems: function()
	{
		thisp = document.is_popup;
		if( !thisp.current )
			return false;
		var mis = thisp.current.getElementsByTagName( 'li' );
                var re = thisp.parent.lang;
                //alert(thisp.parent.innerHTML);
		//var re = thisp.parent.innerHTML;
		var subs = false;
		var link = false;
		for( i=0; i<mis.length; i++ )
		{
			//if( mis[i].parentNode != thisp.current	)
				//continue;
			// Get Subs:
			mis[i].onmouseover = function() { this.className = "DDover" };
			mis[i].onmouseout = function() { this.className = "" };
			subs = mis[i].getElementsByTagName( 'ul' );
			if( subs.length > 0 )
			{
				// Prepare second level menu
				sm = new is_internal_menu();
				subs[0].style.display = "none";
				subs[0].style.position = "absolute";
				//subs[0].style.marginLeft = ( Utils.getWidth( thisp.parent ) - 1 ) + "px";
				subs[0].style.zindex = 100;
				mis[i].submenu = subs[0];
				mis[i].smid = thisp.submenus.length;
				sm.menu = mis[i];
				sm.ispid = mis[i].smid;
				mis[i].className = "inner_menu";
				subs[0].className = "DropDown";
				mis[i].onmouseover = function() { if( document.is_popup.submenus.length > this.smid ) document.is_popup.submenus[ this.smid ].over(); };
				mis[i].onmouseout = function() { if( document.is_popup.submenus.length > this.smid ) document.is_popup.submenus[ this.smid ].out(); };
				// Disable on clock for both elements
				mis[i].onclick = function() { if( document.is_popup.submenus.length > this.smid ) document.is_popup.submenus[ this.smid ].open(); };
				mis[i].childNodes[0].onclick = function() { if( document.is_popup.submenus.length > this.smid ) document.is_popup.submenus[ this.smid ].open(); }
				thisp.submenus.push( sm );
				if( mis[i].parentNode != thisp.current )
				{
					// Attach to parent
					for( p=0; p<thisp.submenus.length; p++ )
					{
						if( thisp.submenus[p].menu == mis[i] )
						{
							thisp.submenus[p].submenus.push( p );
							break;
						}
					}
				}
				//sm.readmenuitems();
				delete( sm );
			}

        link = mis[i].getElementsByTagName( 'a' );
        if( link[0].lang == re )
          mis[i].style.display = "none";
        else
          mis[i].style.display = "";
       }
	},

	setParentParams: function( element )
	{
		if( !element )
			return;
		thisp = document.is_popup;
		if( !thisp.current )
			return false;

		thisp.parent.innerHTML = element.innerHTML;
		thisp.parent.lang = element.lang;
	}
}

/** INTERNAL MENUS **/
is_internal_menu = Class.create();
is_internal_menu.prototype = {
	initialize: function() {
		this.menu = false;
		this.ispid = 0;
		this.smenu = false;
		this.hmenu = false;
		this.submenus = new Array();
	},

	over: function()
	{
		//alert( this.menu.innerHTML +" -||- "+ this.smenu + " -- " + this.menu.submenu.style.display );
		if( !this.smenu && this.menu.submenu.style.display == "none")
		{
			this.smenu = window.setTimeout( "if( document.is_popup.submenus.length > " + this.ispid + " ) document.is_popup.submenus[ " + this.ispid + " ].open()", document.is_popup.hovertime );
		}
		else if ( this.hmenu )
		{
			window.clearTimeout( this.hmenu );
			this.hmenu = false;
		}
	},
	out: function()
	{
		if( this.smenu )
		{
			window.clearTimeout( this.smenu );
			this.smenu = false;
		}
		else if( !this.hmenu && this.menu.submenu.style.display == "block" )
		{
			this.hmenu = window.setTimeout( "if( document.is_popup.submenus.length > " + this.ispid + " ) document.is_popup.submenus[ " + this.ispid + " ].close()", document.is_popup.hovertime );
		}
	},

	open: function()
	{
		this.smenu = false;
		document.innermenu = false;
		// Relaclulate menu width;
		var mw = Utils.getWidth( this.menu );
		var mh = Utils.getHeight( this.menu );

		this.menu.submenu.style.width = mw + "px";
		this.menu.submenu.style.marginTop = "-" + (mh-10) + "px";
		this.menu.submenu.style.marginLeft = (mw - 10) + "px";
		this.menu.submenu.style.display = "block";
		var x = Utils.findPosX( this.menu.submenu );
		var y = Utils.findPosY( this.menu.submenu );
		var cw = Utils.windowWidth();
		var ch = Utils.windowHeight();
		var csh = Utils.windowScrollHeight();
		mh = Utils.getHeight ( this.menu.submenu );

		if( x + mw > cw )
			this.menu.submenu.style.marginLeft = "-" + mw + "px";
		if( y + mh > ch+csh )
		{
			//var p = y;
			var p = Math.abs( ( (ch+csh) - y ) - mh );
			p += 20;
			this.menu.submenu.style.marginTop = "-" + (p) + "px";
		}
	},

	close: function()
	{
		this.hmenu = false;
		document.innermenu = false;
		this.menu.submenu.style.display = "none";
	},

	icheck: function( e )
	{
        if( !document.is_popup.submenus[ this.ispid ] )
            return false;

        pthis = document.is_popup.submenus[ this.ispid ].menu;

		if( pthis.submenu.style.display != "block" )
			return false;

		if( this.submenus.length > 0 )
		{
			// Any open submenus ?
			for( i=0; i<this.submenus.length; i++ )
			{
				//if( document.is_popup.submenus[ this.submenus[i] ].icheck() )
				return true;
			}
		}

		//var ax = parseInt( thisp.submenu.style.left );
		//var ay = parseInt( thisp.submenu.style.top );
		var ax = Utils.findPosX( pthis.submenu );
		var ay = Utils.findPosY( pthis.submenu );
		var aw = parseInt( pthis.submenu.offsetWidth );
		var ah = parseInt( pthis.submenu.offsetHeight );

		var c = Utils.getCursorPosition(e);

		var px = Utils.findPosX( pthis );
		var py = Utils.findPosY( pthis );
		var pw = Utils.getWidth( pthis );
		var ph = Utils.getHeight( pthis );

		// Out of the box ?
		if( c.x < ax || c.x > ax+aw )
		{
			// Are we in the parent item ?
			if( ( c.x < px || c.x > px + pw ) || ( c.y < py || c.y > py + ph ) )
			{
				this.out();
				return false;
			}
		}

		if( c.y < ay || c.y > ay+ah )
		{
			this.out();
			return false;
		}

		if( this.hmenu )
		{
			window.clearTimeout( this.hmenu );
			this.hmenu = false;
		}
		return true;
	}
}


document.is_popup.init();
