var close_menu = false;
var sub_menu = null;
var close_sub = false;
var sub_sub = null;
jQuery.fx.interval = 25;

(function ($){
  var check=false, isRelative=true;

  $.elementFromPoint = function(x,y)
  {
    if(!document.elementFromPoint) return null;

    if(!check)
    {
      var sl;
      if((sl = $(document).scrollTop()) >0)
      {
       isRelative = (document.elementFromPoint(0, sl + $(window).height() -1) == null);
      }
      else if((sl = $(document).scrollLeft()) >0)
      {
       isRelative = (document.elementFromPoint(sl + $(window).width() -1, 0) == null);
      }
      check = (sl>0);
    }

    if(!isRelative)
    {
      x += $(document).scrollLeft();
      y += $(document).scrollTop();
    }

    return $(document.elementFromPoint(x,y));
  }	

})(jQuery);


function logg(str)
{
	document.getElementById('out').innerHTML=str+'<br />';
}

var bcheck = false;

function backup_check(e)
{
	if ($.elementFromPoint(e.pageX,e.pageY).closest('ul.menuh').length)
	{
		if (bcheck)
		{
			window.clearTimeout(bcheck);
			bcheck = false;
		}
	}
	else
	{
		if (!bcheck)
		{
			bcheck = window.setTimeout('bcheck=false;closemenu();',800);
		}
	}
}

$(document).ready(function()
{
	$(document).bind('mousemove',backup_check);
	$('ul.menuh > li').bind('mouseover',function(e)
	{
		if ((this==e.target.parentNode&&e.target.tagName.toLowerCase()=='a')||this==e.target)
		{
			if ($(this).children('ul').length)
			{
				if (sub_menu!=$(this))
				{
					closemenu();
				}
				sub_menu = $(this);
				var off = $(this).offset();
				$(this).children('ul').removeClass('invisible').offset({left:off.left-10,top:off.top+35}).top;
			}
			clear_timeout('close_menu');
		}
	}).bind('mouseout',function(e)
	{
		if ((this==e.target.parentNode&&e.target.tagName.toLowerCase()=='a')||this==e.target)
		{
			if (close_menu)
			{
				window.clearTimeout(close_menu);
			}
//			close_menu = window.setTimeout('closemenu()',800);
			e.stopPropagation();
		}
	});
	$('ul.menuv > li').bind('mouseover',function(e)
	{
		if ((this==e.target.parentNode&&e.target.tagName.toLowerCase()=='a')||this==e.target)
		{
			if ($(this).children('ul').length)
			{
				if (sub_sub!=$(this))
				{
					closesub();
				}
				sub_sub = $(this);
				var off = $(this).offset();
				$(this).children('ul').removeClass('invisible').offset({left:off.left+145,top:off.top});
			}
			clear_timeout('close_menu');
		}
		else
		{
		}
	}).bind('mouseout',function(e)
	{
		if ((this==e.target.parentNode&&e.target.tagName.toLowerCase()=='a')||this==e.target)
		{
			if (close_menu)
			{
				window.clearTimeout(close_menu);
			}
//			close_menu = window.setTimeout('closemenu()',800);
			e.stopPropagation();
		}
	});
	$('ul.submenuv > li').bind('mouseover',function(e)
	{
		clear_timeout('close_menu');
	}).bind('mouseout',function(e)
	{
			if (close_menu)
			{
				window.clearTimeout(close_menu);
			}
//		close_menu = window.setTimeout('closemenu()',800);
		e.stopPropagation();
	});
	
	if ($('ul.landing').length)
	{
		$('ul.landing a img.tab').bind('mouseover',function(e){
			ttt = 250;//(578-(jwidth($(this).css('width'))-48))/2;
			e.preventDefault();									
			var t = $('ul.landing .panel_open');
			t.css('width','622px');
			t.find('img.panel').css('width','228px');
			t.removeClass('panel_open');
			
			t = $('ul.landing a').not($(this).parent());
			t.each(function(i,e){
				$(this).stop();
				$(this).children('.panel').stop();
				$(this).removeClass('isopen');
//				var t = (jwidth($(this).css('width'))-48)/2;
				$(this).animate({width:48},ttt,'linear');
				$(this).children('.panel').animate({width:0},ttt,'linear');
			});
			
			$(this).parent().stop();
			$(this).parent().children('.panel').stop();
			$(this).parent().addClass('isopen');
			$(this).parent().animate({width:622},ttt,'linear');
			$(this).parent().children('.panel').animate({width:228},ttt,'linear');
		});
	}
});

var ttt = 0;
function jwidth(str)
{
	return str.substr(0,str.length-2)/1;
}

function clear_timeout(to)
{
	window.setTimeout('if ('+to+') { window.clearTimeout('+to+'); '+to+' = false;}',1);
}

function closemenu()
{
	closesub();
	if (sub_menu)
	{
		sub_menu.children('ul').addClass('invisible');
		sub_menu = null;
	}
}

function closesub()
{
	if (sub_sub)
	{
		sub_sub.children('ul').addClass('invisible');
		sub_sub = null;
	}
}
