/*
    Developed by Denis Rusakov (denis.rusakov@gmail.com)
*/
/*
function load_script(file)
{
    if(!file)
        return false;
    document.open();
    document.write('<script src="'+file+'" type="text/javascript"></script>');
    document.close();
    return true;
}
load_script('/js/_debug.js');
*/
function Point(x,y) {
    this.x=this.y=0;
    this.set=function(x,y) {this.x=((x)?x:0); this.y=((y)?y:0);}
    this.moove=function(x,y) {this.x+=x; this.y+=y;}
    this.toString=function() {return 'x='+this.x+' y='+this.y;}
    this.set(x,y);
    return this;
}
function get_position(obj) {
    var pt=Point();
    var node=obj;
    do{
        pt.x+=node.offsetLeft;
        pt.y+=node.offsetTop;
    }while(node=node.offsetParent) 
    return pt;
}
function get_e_position(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

function popup_layer(id,onmouseout_hide)
{
    var win_popup='document.__popup_layers_'+id;
    if(eval(win_popup))
        return eval(win_popup);
    this.win;
    this.style='';
    this.st_class='';
    this.pos={x:0,y:0,w:0,h:0};
    this.set_data=function(child) {
        if(!this.win) return false;
        while(ch=this.win.firstChild)
            this.win.removeChild(ch);
        this.win.appendChild(child);
        return true;
    }
    this.set_style=function(style) {
        this.style=style;
    }
    this.set_style_class=function(style) {
        this.st_class=style;
    }
    this.init=function(onmouseout_hide) {
        if(this.win) return true;
        var b=document.getElementById('footer');
        if(!b) return false;
        this.win=document.createElement('div');
        //~ this.set_data(document.createTextNode('TEST...'));
        if(onmouseout_hide==undefined || onmouseout_hide==1)
            this.do_hide=1;
            this.win.onmouseout=function(e){
                (new popup_layer(id)).hide(e,1);
            };
        b.appendChild(this.win);
        return true;
    }
    this.show=function(xx,yy,ww,hh,onmouseout_hide) {
        if(!this.init(onmouseout_hide))
            return false;
        this.pos={x:xx,y:yy,w:ww,h:hh};
        this.win.className=this.st_class;
        this.win.style.cssText=this.style+'position:absolute;z-index:10;display:block;left:'+xx+'px;top:'+yy+'px;'+((ww)?'width:'+ww+'px;':'')+((hh)?'height:'+hh +'px;':'');
        return true;
    }
    this.show_hint=function() {
    }
    this.hide=function(e,f)
    {
        if(!this.win)
            return false;
        /*if(f){
            var c=get_e_position(e);
            if(!(c.x<this.pos.x || c.x-this.pos.x>this.pos.w || c.y<this.pos.y || c.y-this.pos.y>this.pos.h))
                return true;
        }*/
        this.win.style.cssText='display:none;';
        return true;
    }
    this.init(onmouseout_hide);
    eval(win_popup+'=this;');
    return this;
}

function popup_icq(obj)
{
    var inst=(new Date()).getTime();
    var p=new popup_layer(inst,0);
    if(!p)
        return false;
    p.set_style_class('managers_info');
    var data=document.createElement('div');
    //~ var h=document.createElement('strong');
    //~ h.appendChild(document.createTextNode('Менеджер по направлениям:'));
    //~ data.appendChild(h);
    var c=document.createElement('span');
    c.appendChild(document.createTextNode(obj.title));
    data.appendChild(c);
    p.set_data(data);
    obj.title="";
    obj.onmouseover=function(e){
        var pt=get_position(obj);
        pt.moove(0,16);
        var p=new popup_layer(inst);
        p.show(pt.x,pt.y,180,0);
    };
    obj.onmouseout=function(e){
        (new popup_layer(inst)).hide(e);
    };
    obj.onmouseover();
    return p;
}

function popup_img(obj,h,w)
{
    obj.setAttribute('_w',obj.width);
    obj.setAttribute('_h',obj.height);
    var inst=(new Date()).getTime();
    var p=new popup_layer(inst,0);
    var i=document.createElement('img');
    i.setAttribute('src',obj.src);
    i.onmouseout=function(e) {
        (new popup_layer(inst)).hide(e);
    }
    p.set_data(i);
    obj.onmouseover=function(e){
        var pt=get_position(obj);
        var p=new popup_layer(inst);
        p.show(pt.x,pt.y,0,0);
    };
    obj.onmouseover();
    return true;
}
function resize_images(obj) {
    if(!obj) return false;
    w_max=h_max=150;
    w_n=obj.width;
    h_n=obj.height;
    if(w_max && w_n > w_max) {
        h_n=Math.floor(obj.height*w_max/obj.width);
        w_n=w_max;
    }
    if(h_max && h_n > h_max) {
        w_n=Math.floor(obj.width*h_max/obj.height);
        h_n=h_max;
    }
    if(w_n==obj.width && h_n==obj.height)
        return true;
        obj.width=w_n;
        obj.height=h_n;
    obj.onmouseover=function(e) {
        popup_img(this,h_n,w_n);
    }
}
