var HIDE_POPUP_DELAY = 0;  //     

var ActiveMenu = null;

var ITEM_INFO_ATTR = 'rel';

   
var MenuPosition = {
down: function( d,f,w,s )
{
    var xL = f.pos.x + f.dim.width - s.pos.x;
    var xR = s.pos.x + s.dim.width - f.pos.x;
    var yU = f.pos.y - s.pos.y;
    var yD = s.pos.y + s.dim.height - f.pos.y - f.dim.height; 
    
    var newD = '';
    if (d == 'auto') d = 'rd';
    d = d.split('');
    for (var i=0; i<d.length; i++) {
        switch (d[i]) {
            case 'r': case 'l':
                var res = xR > xL ? 'r':'l'; 
                res = (w.dim.width<=xR && d[i]=='r') ? 'r' : res;
                res = (w.dim.width<=xL && d[i]=='l') ? 'l' : res;
                newD += res;
                if (d.length==1) newD += 'd';
            break;
            
            case 'u': case 'd':
                var res = yD > yU ? 'd':'u'; 
                res = (w.dim.height<=yU && d[i]=='u') ? 'u' : res;
                res = (w.dim.height<=yD && d[i]=='d') ? 'd' : res;
                newD += res;
                if (d.length==1) newD += 'r';
            break;
        }
    }
    d = newD;
    

    var res = { x: 0, y: 0 };
    switch (d) {
        case "ld": case "dl":
            res.x = f.pos.x + f.dim.width - w.dim.width;
            res.y = f.pos.y + f.dim.height;
        break;
        case "lu": case "ul":
            res.x = f.pos.x + f.dim.width - w.dim.width;
            res.y = f.pos.y - w.dim.height;
        break;
        case "ru": case "ur":        
            res.x = f.pos.x;      
            res.y = f.pos.y - w.dim.height; 
        break;            
        case "rd": case "dr": default:          
            res.x = f.pos.x;      
            res.y = f.pos.y + f.dim.height;
        break;
    }
    return res;
},
// ^^^ down

sub: function( d,f,w,s )
{
    var xL = f.pos.x + f.dim.width - s.pos.x;
    var xR = s.pos.x + s.dim.width - f.pos.x;
    var yU = f.pos.y + f.dim.width - s.pos.y;
    var yD = s.pos.y + s.dim.height - f.pos.y; 
    
    var newD = '';
    if (d == 'auto') d = '';
    d = d.split('');    
    for (var i=0; i<d.length; i++) {
        switch (d[i]) {
            case 'r': case 'l':
                var res = xR > xL ? 'r':'l';
                res = (w.dim.width<=xR && d[i]=='r') ? 'r' : res;
                res = (w.dim.width<=xL && d[i]=='l') ? 'l' : res;
                newD += res;
                if (d.length==1) newD += 'd';
            break;
                
            case 'u': case 'd':
                var res = yD > yU ? 'd':'u'; 
                res = (w.dim.height<=yU && d[i]=='u') ? 'u' : res;
                res = (w.dim.height<=yD && d[i]=='d') ? 'd' : res;
                newD += res;
                if (d.length==1) newD +='r';
            break;
        }
    }
    d = newD;
    
    var res = { x: 0, y: 0 };
    switch (d) {
        case "ld": case "dl":
            res.x = f.pos.x - w.dim.width;
            res.y = f.pos.y;
        break;
        case "lu": case "ul":
            res.x = f.pos.x - w.dim.width;
            res.y = f.pos.y + f.dim.height - w.dim.height;
        break;
        case "ru": case "ur":        
            res.x = f.pos.x + f.dim.width;      
            res.y = f.pos.y + f.dim.height - w.dim.height;
        break;            
        case "rd": case "dr": default:          
            res.x = f.pos.x + f.dim.width;
            res.y = f.pos.y; 
        break;
    }
    return res;
}
// ^^^ sub
}
// ^^^ MenuPosition





function MenuObject( from,who, hide, parent, type, direction )
{
    this.fromElem = from;
    this.whoElem  = who;

    parent = (ActiveMenu && parent) ? ActiveMenu.getObject(parent) : null;
    if (parent) {
        if (parent.childObj) {
            parent.childObj.hide();
        }
        parent.childObj = this;
        this.parentObj = parent;
    } else {
        this.parentObj = null;
        if (ActiveMenu) ActiveMenu.hide();
        ActiveMenu = this;
    }
    this.childObj = null;
    
    this.type = type ? type.toLowerCase() : 'down';
    this.hideFlag = hide || false;
    this.direction = direction ? direction.toLowerCase() : 'auto';
    
    this.timer = null;
    
}
// ^^^ MenuObject

MenuObject.prototype.getObject = function( obj, from )
{
    obj = $(obj);
    from = $(from);
    if (obj) {
        if (this.whoElem.id == obj.id && (!from || this.fromElem == from))
            return this; 
        else if (this.childObj) 
            return this.childObj.getObject(obj,from);
    }
    return null;
}
// ^^^ getObject

MenuObject.prototype.dropdown = function()
{
    if (this.visible()) {
        if (this.hideFlag) {
            this.hide();
        } else {
            this.cancelHide();
        }
    } else {
    
        //     from
        var fromPos = Position.positionedOffset(this.fromElem);
        //var fromPos = Position.offsetParent(this.fromElem);
        var f = { pos : 
                    { x : fromPos[0],
                      y : fromPos[1]
                    },
                  dim : Element.getDimensions(this.fromElem)
                };
        //   who
        var w = { dim : Element.getDimensions(this.whoElem) };
        
        //         
        var s = { pos : {},
                  dim : {}
                };
        //  
        /*if (document.all) { // ie, opera
            s.dim = { height : document.body.offsetHeight, width : document.body.offsetWidth };
        } else if (document.layers) { // ns
            s.dim = { height : window.innerHeight, width : window.innerWidth };
        } else { // mozilla
            s.dim = { height : document.body.clientHeight, width : document.body.clientWidth };
        }*/

        if (self.innerWidth) { // mozilla,opera
    		s.dim = { height : self.innerHeight, width : self.innerWidth };
	    } else if (document.documentElement && document.documentElement.clientWidth) { // ie
            s.dim = { height : document.documentElement.clientHeight, width : document.documentElement.clientWidth };	    
	    } else if (document.body) {
            s.dim = { height : document.body.clientHeight, width : document.body.clientWidth };
        } else if (document.layers) { // ns
            s.dim = { height : window.innerHeight, width : window.innerWidth };
        }
        
        
        

        //   
        Position.prepare();
        s.pos.x = Position.deltaX;
        s.pos.y = Position.deltaY;
        var maxX = s.pos.x + s.dim.width;
        var maxY = s.pos.y + s.dim.height;

        //   ,      
        var menuPos = MenuPosition[this.type](this.direction, f, w, s);
        
        this.whoElem.style.left = menuPos.x; 
        this.whoElem.style.top  = menuPos.y;
        this.show();
    }
}    
// ^^^ dropdown

MenuObject.prototype.visible = function()
{
    return Element.visible(this.whoElem);
}
// ^^^ visible

MenuObject.prototype.show = function()
{
    Element.show(this.whoElem);
}
// ^^^ show

MenuObject.prototype.hide = function()
{
    if (this.childObj) this.childObj.hide();
    Element.hide(this.whoElem);
    if (this.whoElem.onHide) this.whoElem.onHide();
    if (ActiveMenu == this) ActiveMenu = null;
}
// ^^^ hide

MenuObject.prototype.wanaHide = function( who,delay )
{
    this.timer = setTimeout( function() {who.hide()}, delay );
    if (this.parentObj) this.parentObj.wanaHide(this.parentObj, delay);
}
// ^^^ wanaHide


MenuObject.prototype.cancelHide = function()
{
    clearTimeout( this.timer );
    if (this.parentObj) this.parentObj.cancelHide();
}
// ^^^ cancelHide

function dropdownMenu( from, who, params )
{
    from = $(from);
    who  = $(who);
    if (!from || !who) return;
    if (!params) params = {};
   
    var menu = null;
    if (ActiveMenu) {
        menu = ActiveMenu.getObject(who, from);
    }
    if (!menu) {
        menu = new MenuObject(from,who, params.hide, $(params.parent), params.type, params.direction);
    }
    
    menu.dropdown();
}
// ^^^ dropdownMenu


function wanaHide( who, delay )
{
    if (!ActiveMenu) return;
    if (typeof(delay) == "undefined") delay = HIDE_POPUP_DELAY;
    
    who = ActiveMenu.getObject(who);
    if (who) who.wanaHide( who, delay );
    

}
// ^^^ wanaHide


function cancelHide( who )
{
    if (!ActiveMenu) return;
    
    who = ActiveMenu.getObject(who);
    if (who) who.cancelHide();

    
}
// ^^^ cancelHide


function menuMouseOver()
{
    var elm = $(arguments[0]);
    var box = $(arguments[1]);
    if (!elm || !box) return;

    dropdownMenu( elm, box, arguments[2] );

//    Element.addClassName(elm, 'submenuOver');

//    box.onHide = function() { Element.removeClassName (elm, 'submenuOver') };
}
// ^^^ menuMouseOver


function menuMouseOut()
{
    var box = $(arguments[0]);
    wanaHide( box );
}
// ^^^ menuMouseOut





/////////    

function showShops( cityId, goodId, url )
{
    var metroListElem = $('city'+cityId+'idg'+goodId);
    var metroListStr = '';
    if (metroListElem) {
        var inputs = metroListElem.getElementsByTagName('input');
        for (var i=0; i < inputs.length; i++) {
            if (inputs[i].type != 'checkbox') continue;
            if (inputs[i].checked) {
                if (inputs[i].value == 'all') break;
                metroListStr += inputs[i].value + ',';
            }
        }
        if (metroListStr.length) {
            metroListStr = '/metro_list/' + metroListStr.substr(0,metroListStr.length-1);
        }
    }
    location.href = url + metroListStr;
}



function closeForm( who, save )
{
    who = $(who);
    if (!who) return false;

    while( who.tagName.toUpperCase() != 'BODY' ) {
        var entry = ActiveMenu.getObject( who );
        if (entry) break;
        who = who.parentNode;
    }  

    if (!save) { //   ,      
        var form = $A(who.childNodes).find( function(node) { return node.tagName && node.tagName.toUpperCase() == 'FORM'; } );
        Form.getInputs(form, 'checkbox').each( function(checkbox) {
            checkbox.checked = false;
        });
    }
    wanaHide(who,0);
} 
// ^^^ closePopup

    
    
function checkboxSelectAll( switchBox )
{
    switchBox = $(switchBox);
    if (!switchBox) return;
    
    var form = switchBox.parentNode; 
    while (form.tagName.toUpperCase() != 'FORM') {
        if (form.tagName.toUpperCase() == 'BODY') return;
        form = form.parentNode;
    }

    Form.getInputs(form, 'checkbox').each( function(checkbox) {
        checkbox.checked = switchBox.checked;
    });
}
// ^^^ checkboxSelectAll

function mouseDownHandler(e)
{
    if (!ActiveMenu) return true;
    e = e || window.event;
    if (e) {
        var clicked = e.target || e.srcElement;
        
        var elem = clicked;
        var menu2hide = null;
        
        while (elem && elem.tagName != 'BODY') {
            menu2hide = ActiveMenu.getObject(elem);
            if (menu2hide) break;
            elem = elem.parentNode;
        }
        
        if (!menu2hide) {
            menu2hide = ActiveMenu;
        } else {
            menu2hide = menu2hide.childObj;
        }
        
        if (menu2hide && menu2hide.fromElem != clicked) {
            menu2hide.hide();
        }
    }
}

if (window.captureEvents) { //  Firefox, Opera, Netscape Navigator
    window.captureEvents(Event.MOUSEDOWN);
    window.onmousedown = mouseDownHandler;
} else { //  Internet Explorer
    document.onmousedown = mouseDownHandler;
}

///////////

function dropdownPopup(ddp, add)
{
    //alert(ddp.who);
    var doModify = !Element.visible(ddp.who) || (!ActiveMenu || (ddp.parent ? ActiveMenu.getObject(ddp.parent).fromElem : ActiveMenu.fromElem ) != $(ddp.from));
    dropdownMenu( ddp.from, ddp.who, {hide:ddp.hide, parent:ddp.parent, type:ddp.type, direction:ddp.direction} );
    if (doModify) {
        additionModify( ddp.from, ddp.who, ddp.type, add );
    }
}


function additionModify( from, who, type, params )
{
    from = $(from);
    who  = $(who);
    type = type || 'down';

    var fromPos  = Position.positionedOffset(from);
    var fromTop  = fromPos[1];
    var fromLeft = fromPos[0]; 

    var whoTop  = parseInt( who.style.top  );
    var whoLeft = parseInt( who.style.left );

    var vertical   = fromTop  <= whoTop  ? 'b':'t';
    var horisontal = fromLeft <= whoLeft ? 'r':'l';
        
    var postfix = (type == 'down') ? (vertical + horisontal) : (horisontal + vertical);
      

    //  
    if (params.picIdBase) {
        var base = params.picIdBase;
        var imgBox = $(base + postfix);
        if (imgBox) {
            var otherBoxes = who.getElementsByTagName( imgBox.tagName );
            for (var i=0; i<otherBoxes.length; i++) {
                if (otherBoxes[i].id && otherBoxes[i].id.indexOf(base)==0)
                    otherBoxes[i].style.display = 'none';
            }
            imgBox.style.display = '';
            //   -   valign
            var parent = imgBox.parentNode;
            switch (postfix) { //       sub
                case 'lt': case 'rt':
                    parent.style.verticalAlign = 'bottom';
                break;
                case 'lb': case 'rb':
                    parent.style.verticalAlign = 'top';
                break;
            }
        }
     }

    //   
    var x = params.xOffset || 0;
    var y = params.yOffset || 0;
    
    switch (postfix) {
        case 'lt': case 'rt':
            who.style.top = (whoTop + y) + 'px';
        break;

        case 'lb': case 'rb':
            who.style.top = (whoTop - y) + 'px';
        break;

        case 'tl': case 'bl':
            who.style.left = (whoLeft + x) + 'px';
        break;

        case 'tr': case 'br':
            who.style.left = (whoLeft - x) + 'px';
        break;
    }

}
// ^^^ additionModify



function checkBoxSwitch( input, box )
{
    input = $(input);
    box = $(box);
    if (!input || !box) return;
    
    if (input.checked) {
        Element.show(box);
    } else {
        Element.hide(box);
    }
}


//      
function selectItem( who, params, toFocus )
{
    who = $(who);
    if (!who) return;

    
    //        ( )
    var target = $(params.target);
    if (target) {
        target.innerHTML = who.innerHTML;
    }

    // -     ITEM_INFO_ATTR 
    var hidden = $(params.hidden);
    if (hidden) {
        hidden.value = who.getAttribute(ITEM_INFO_ATTR);
    }

    //    ,     
    var menu2hide = null;
    while (who && who.tagName != 'BODY') {
        menu2hide = ActiveMenu.getObject(who);
        if (menu2hide) break;
        who = who.parentNode;
    }
        
    if (!menu2hide) {
        menu2hide = ActiveMenu;
    }
        
    menu2hide.hide();
    
    toFocus = $(toFocus);
    if (toFocus) toFocus.focus();
}
// ^^^ selectItem



//  
function popitup( url, name, params )
{
    var wnd = window.open(url,name,params);
    if (!wnd.opener) {
        wnd.opener = self;
    }
    if (window.focus) {
        wnd.focus()
    }
    return;
}
// ^^^ popitup



function popupListenMp3( cp, ddp, add )
{
///////////
    
    var body = $(cp.popBody);
    var child = $(cp.popChild);
    var nodes = body.childNodes;
    for (var n=0; n<nodes.length; n++) {
        Element.hide(nodes[n]);
    }
    body.appendChild( child );
    Element.show( child );
///////////
    $('reflectFrame').src = '';

    var frame = $('codeFrame');
    frame.src = frame.getAttribute('base_src') + '/rnd/' + Math.round(Math.random()*1000);
    
    //alert(ddp.who);
    
    dropdownPopup(ddp, add);
    
    //dropdownPopup('POP_Help');
    var inputs = $(ddp.who).getElementsByTagName('input');
    for (var i=0; i<inputs.length; i++) {
        if (inputs[i].name != 'code') continue;
        inputs[i].value = '';
        if (inputs[i].focus) {
            inputs[i].focus();
            //    !!!!
            var fuckingopera = inputs[i];
            setTimeout( function() {fuckingopera.focus()}, 1 );
        }
        break;
    }
}
// ^^^popupListenMp3


function switchElems()
{
    for (var a=0; a<arguments.length; a++) {
        elem = $(arguments[a]);
        if (!elem) continue;
        if (elem.style.display != 'none') {
            elem.style.display = 'none';
        } else {
            elem.style.display = '';
        }
    }
}
// ^^^ switchElems



function mmOver(obj)
{
    obj = $(obj);
	if (obj.className != "clicked_1")
    	obj.className = "over";
}

function mmOut(obj)
{
    obj = $(obj);
	if (obj.className != "clicked_1")
	    obj.className = "";
}

function mmClick(obj)
{
    obj = $(obj);
	var box = obj.id + 'Box';
	hide_all();
	obj.className = "clicked_1";
	document.getElementById(box).style.display='block';
}

function hide_all()
{
    var container = $('buttonsContainer');
    var divs = container.childNodes;
    for (var d=0; d<divs.length; d++) {
        if (divs[d].tagName.toLowerCase() != 'div') continue;
        divs[d].className = '';
        $(divs[d].id+'Box').style.display = 'none';
    }
}








