function mousePageXY(e)
{
  var x = 0, y = 0;

  if (!e) e = window.event;

  if (e.pageX || e.pageY)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }

  return {"x":x, "y":y};
}

function setCookie (name, value, expires, path, domain, secure)
{
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name)
{
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
    offset = cookie.indexOf(search);
    if (offset != -1) {
    offset += search.length;
    end = cookie.indexOf(";", offset)
    if (end == -1) {
    end = cookie.length;
    }
    setStr = unescape(cookie.substring(offset, end));
    }
    }
    return(setStr);
}





var img_0 = new Image();
img_0.src = "/common/img/border_left.png";

var img_1 = new Image();
img_1.src = "/common/img/border_mid.png";

var img_2 = new Image();
img_2.src = "/common/img/city_sel_down.png";

var img_3 = new Image();
img_3.src = "/common/img/border_right.png";



//////////////////////////////////
////////////
$(document).ready(
        function()
        {
            //Привязка событий
            $("#top_menu td.top").bind("mouseover", ShowSubMenu );
        }
    );

////
var ShaderElement     = null;
var CitySelectElement = null;
var CityList          = null;
function Show_Felial_Select( inEvnt)
{
    if(ShaderElement == null)
    {
        var CityLabel     = $("a.city_select").eq(0);
        var rect          = CityLabel.get()[0].getBoundingClientRect();
        CitySelectElement = $("<table cellpadding='0px' cellspacing='0px' style='position:fixed; top:" + (rect.top - 4) + "px; left:" + (rect.left -4) + "px; width:150px; height:30px; z-index:1100; table-layout:fixed; '><tr>  <td width='7px' style='background:url("+img_0.src+");'></td>  "
            +"<td width='*'style='background:url("+img_1.src+"); position:relative; cursor:pointer; padding:0px;'> <div style='height:24px; padding-top:6px;position:relative;' >" + CityLabel.text()
            +"<div style='position:absolute; right:0px; top:4px; height:21px; width:15px; background:#fff url("+img_2.src+") center center no-repeat;'></div> </div></td>  <td width='7px' style='background:url("+img_3.src+");'></td></table>");

        ShaderElement = $("<div style='position:fixed; top:0px; left:0px; width:100%; height:100%; z-index:1000; background:#000;'></div>");



        $("body").eq(0).append(ShaderElement);
        $("body").eq(0).append(CitySelectElement);
        $("body").eq(0).bind("scroll",BodyScrollLock);

        var divDOM = ShaderElement.get()[0];
        if(divDOM.attachEvent )
        {
        	 divDOM.attachEvent("onmousewheel", BodyScrollLock);
        }
        else if(divDOM.addEventListener)
        {
        	divDOM.addEventListener('DOMMouseScroll',BodyScrollLock, false);
        	divDOM.addEventListener('mousewheel', BodyScrollLock, false);
        	divDOM.addEventListener('onmousewheel', BodyScrollLock, false);
        }

        ShaderElement.bind("click", Hide_Felial_Select );
        ShaderElement.css("opacity","0");
        ShaderElement.animate( {"opacity":"0.3"},500 );

        CityList = $("<div style='position:fixed; display:none;  top:" + (rect.top + 20) + "px; left:" + (rect.left -2) + "px;  border:2px solid #555; background:#fff; width:135px; border-radius:0px 0px 4px 4px;  padding:3px; z-index:1200;' >"


            +"<a href='http://msk.asbank.ru/' class='city_select_href'>Москва</a>"
            +"<a href='http://www.asbank.ru' class='city_select_href'>Оренбург</a>"
            +"<a href='http://pnz.asbank.ru' class='city_select_href'>Пенза</a>"
            +"<a href='http://spb.asbank.ru/' class='city_select_href'>Санкт-Петербург</a>"
            +"</div>");

        CitySelectElement.find("td").eq(0).append(CityList);
        var cont = CitySelectElement.find("td").eq(1);

        //cont.click(function(inEvnt){CityList.slideDown(400); } );

        CityList.slideDown(400);
    }
    evt = (window.event)?event:inEvnt;
	if(evt)
	{
		evt.returnValue    = false;
		if(evt.preventDefault)evt.preventDefault();
	}
}

function Hide_Felial_Select( inEvnt)
{
    CitySelectElement.remove();
    CityList.remove();
    ShaderElement.remove();

    ShaderElement = null;
}



var isIE6 = false;
if (window.navigator.userAgent.indexOf ("MSIE") >= 0)
{       
    uaVers=window.navigator.userAgent.substr(window.navigator.userAgent.indexOf("MSIE")+5,3);
    if( parseFloat(uaVers) < 7 )
    {
        isIE6 = true;
    }        
}
            
 var LastChildMenu = null;
 var LastIndexChildMenu = -1;
 var bindBalans = 0;

function ShowSubMenu( inEvnt)
{

    newIndex = $(this).attr("data-index");
    if(newIndex == 0) return;
    if( newIndex == LastIndexChildMenu ) return;
    LastIndexChildMenu = newIndex;

    if( LastChildMenu != null)
    {
        LastChildMenu.stop(true,true);
        LastChildMenu.fadeOut(500);
    }
    else
    {
        $(document).bind("mousemove", MouseMoveOnShowMenu);
        bindBalans++;
    }

    var docRec     = $("#top_bar").get()[0].getBoundingClientRect();
    var rect       = $(this).get()[0].getBoundingClientRect();
    LastChildMenu  = $("#children_content_" + newIndex);
    
    
    if(isIE6)
    {
        var to = document.getElementsByTagName( 'body' )[0].scrollTop;
        var le = document.getElementsByTagName( 'body' )[0].scrollTop;
        LastChildMenu.css("position","absolute");
        if(rect.left < (docRec.right +  le ) - 200) LastChildMenu.css({"top":(rect.bottom + to )+"px","left":(rect.left + le) + "px"});
        else LastChildMenu.css({"top":(rect.bottom + to ) + "px", "left" :(docRec.right + le - 200) + "px"});
        LastChildMenu.show();
    }
    else
    {
        LastChildMenu.css("position","fixed");
        if(rect.left < docRec.right - 200) LastChildMenu.css({"top":rect.bottom +"px","left":rect.left + "px"});
        else LastChildMenu.css({"top":rect.bottom + "px", "left" :(docRec.right - 200) + "px"});
    }
    LastChildMenu.fadeIn(500);
    

}



function MouseMoveOnShowMenu(inEvnt)
{
    if( LastChildMenu != null)
    {
        var rect       = LastChildMenu.get()[0].getBoundingClientRect();
        var mousePos   = mousePageXY(inEvnt);
        if(mousePos.y > rect.bottom + 5 || mousePos.y < rect.top - 35 || mousePos.x < rect.left - 5  || mousePos.x > rect.right + 5)
        {
            
            LastIndexChildMenu = -1;
            LastChildMenu.stop(true,true);
            if(isIE6)
            {
                LastChildMenu.hide();
            }
            else
            {
                LastChildMenu.fadeOut(500);
            }
            LastChildMenu = null;
            $(document).unbind("mousemove", MouseMoveOnShowMenu);
            bindBalans--;
            
            
        }
    }
    else
    {

    }
}

function BodyScrollLock(inEvnt)
{
    var Evt  = window.event ? window.event: inEvt;
	if(Evt.stopPropagation)
	Evt.stopPropagation();
	if(Evt.preventDefault)
	Evt.preventDefault();
	Evt.cancelBubble = true;
	Evt.cancel = true;
	Evt.returnValue = false;
    return false;
}








