isDown=false;
posX=10; 
posY=10;
cur_tool = "zoomin";
map_link_pre = 'http://www.topplan.ru/';

function BrowserCheck() {
	var b = navigator.appName
        switch (b){
        case 'Microsoft Internet Explorer':    
            this.b = "ie";    
        break;
	case 'Opera':          
            this.b = "op"
        break;
        case 'Netscape':       
            this.b = "ns"  
        break;
	default : 
            this.b = b                  
        }
	this.v = parseInt(navigator.appVersion);
        this.ns = (this.b=="ns");
	this.ie = (this.b=="ie");
        this.op = (this.b=="op");
}

is = new BrowserCheck()

function findPosY(obj){
    var curtop = 0;
    while (obj){
        curtop += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return curtop;
}

function findPosX(obj){
    var curleft = 0;
    while (obj){
        curleft += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return curleft;
}

function down(event){
    isDown=true;
    set_cursor(cur_tool);
    switch (cur_tool){
    case 'zoomin':
        sel = document.getElementById("sel_area").style;
        sel.pixelTop  = event.clientY+document.body.scrollTop; 
        sel.pixelLeft = event.clientX;
        sel.pixelHeight = 3;
        sel.pixelWidth  = 3;
        sel.display = "block";
        document.getElementById('marker').value = 0;
    break;
    case 'move':
    	posX = event.clientX ;
   	    posY = event.clientY+document.body.scrollTop;
        isDown=true;
        document.getElementById('marker').value = 0;
        set_cursor('move2');
    break;
    }
}

function up(event){
    var cx_dig = document.getElementById('cx_d').value;
    var cy_dig = document.getElementById('cy_d').value;
    var sc_dig = document.getElementById('sc_d').value;
    var img_w = document.getElementById('map_w').value;
    var img_h = document.getElementById('map_h').value;
    switch (cur_tool){
    case 'zoomin':
        if (isDown==true){
            isDown=false;
            var sel = document.getElementById("sel_area").style;
			var img = document.getElementById("im_container");
            var img_pos_x = findPosX(img);
            var img_pos_y = findPosY(img);

            var sel_x_center =  sel.pixelLeft - img_pos_x + ( sel.pixelWidth / 2);  //
            var center_x = Math.ceil((cx_dig - ((img_w/2) * sc_dig)) + (sel_x_center * sc_dig));
            
            var sel_y_center = img_h - (sel.pixelTop - img_pos_y + (sel.pixelHeight / 2)); //
            var center_y = Math.ceil((cy_dig - ((img_h/2) * sc_dig)) + (sel_y_center * sc_dig));

            var sc_x = (sel.pixelWidth / img_w) * sc_dig; //
            var sc_y = (sel.pixelHeight / img_h) * sc_dig; //

            scale_to = Math.max( sc_x , sc_y );
    	    var minsc = document.getElementById('maxs_d').value;
            if (scale_to < minsc)
	        scale_to = minsc;

            document.getElementById('cx_d').value = center_x;
            document.getElementById('cy_d').value = center_y;
            document.getElementById('sc_d').value = scale_to;
            sel.display = 'none';
			redraw_map();
        }
    break;
    case 'move':
        if (isDown==true){
            isDown=false;
            var im_cont = document.getElementById("im_container").style;
            var center_x = - Math.ceil((parseInt(im_cont.left) * sc_dig) - cx_dig);
            var center_y = Math.ceil(cy_dig - (parseInt(im_cont.top) * -sc_dig));
            document.getElementById('cx_d').value = center_x;
            document.getElementById('cy_d').value = center_y;
			redraw_map();
        }
    break;
    case 'zoomout':
        if (isDown==true){
            isDown=false;
            scale_to = sc_dig * 1.6;
            document.getElementById('sc_d').value = scale_to;
			redraw_map();
		}  
    break;
    }
}
function redraw_map(){
    var db_name = document.getElementById('db_d').value;
    var cx_dig = document.getElementById('cx_d').value;
    var cy_dig = document.getElementById('cy_d').value;
    var sc_dig = parseFloat(document.getElementById('sc_d').value);
    var img_w = document.getElementById('map_w').value;
    var img_h = document.getElementById('map_h').value;
 
    var minsc = document.getElementById('maxs_d').value;
    var mrk  = document.getElementById('marker').value;

    var maxx = document.getElementById('maxx_d').value;
    var minx = document.getElementById('minx_d').value;
    var maxy = document.getElementById('maxy_d').value;
    var miny = document.getElementById('miny_d').value;
	
	maxsc = Math.ceil(Math.min( (maxx - minx) / img_w , (maxy - miny) / img_h ));

	if (sc_dig > maxsc)
	    sc_dig = maxsc;

      	if (sc_dig < minsc)
            sc_dig = minsc;

	maxcxd = maxx - Math.ceil( (img_w/2) * sc_dig);
	mincxd = parseInt(minx) + Math.ceil( (img_w/2) * sc_dig);
	maxcyd = maxy - Math.ceil( (img_h/2) * sc_dig);
	mincyd = parseInt(miny) + Math.ceil( (img_h/2) * sc_dig);	

	if (cx_dig > maxcxd)
	    cx_dig = maxcxd;
	if (cx_dig < mincxd)
	    cx_dig = mincxd;

	if (cy_dig > maxcyd)
	    cy_dig = maxcyd;
	if (cy_dig < mincyd)
	    cy_dig = mincyd;

     document.getElementById('sc_marker').style.marginLeft = 322 - parseInt((sc_dig - minsc)/(maxsc - minsc) *320) ;

     document.getElementById('cx_d').value = cx_dig;
     document.getElementById('cy_d').value = cy_dig;
     document.getElementById('sc_d').value = sc_dig;

    set_cursor('wait');

    mapsrc =  '/maps/'+db_name+'/img_'+img_w+'x'+img_h+'_cx'+cx_dig+'cy'+cy_dig+'s'+sc_dig+'mark'+mrk+'.gif';

    document.getElementById('map_img').src  = mapsrc;
    document.getElementById('map_link').href = map_link_pre+'map/'+db_name+'/m'+mrk+'cx'+cx_dig+'cy'+cy_dig+'sd'+sc_dig;

}
function move(event){
    switch (cur_tool){
    case 'zoomin':
        sel = document.getElementById("sel_area").style;
        if (isDown==true){
            if ((event.clientX > sel.pixelLeft ) && ((event.clientY+document.body.scrollTop) > sel.pixelTop));
            sel.pixelHeight = (event.clientY+document.body.scrollTop) - sel.pixelTop-2;
            sel.pixelWidth  = event.clientX - sel.pixelLeft-2;
        }
    break;
    case 'move':
        if (isDown==true){
            im_cont = document.getElementById("im_container").style;
            im_cont.left = event.clientX - posX;
            im_cont.top  = (event.clientY+document.body.scrollTop) - posY;
        }
    break;
    }
}
function map_loaded(){
    im_cont = document.getElementById("im_container").style;
    im_cont.left=0;
    im_cont.top=0;
    set_cursor(cur_tool);
}

function tool_click(tool_name){
    document.getElementById(cur_tool).style.background = "none";
    document.getElementById(tool_name).style.background = "#DEDEDE";
    cur_tool = tool_name;
    set_cursor(tool_name);
}
function set_cursor(cur_name){ 
    im_frm = document.getElementById("im_frame").style;
    switch (cur_name){
    case 'zoomin':                             
        im_frm.cursor = (is.op)? 'crosshair' : 'url(/images/map/c_zoomin.cur), crosshair';
    break;
    case 'zoomout':
        im_frm.cursor = (is.op)? 'default' : 'url(/images/map/c_zoomout.cur), default';;
    break;
    case 'move':
        im_frm.cursor = (is.op)? 'move' : 'url(/images/map/c_hand1.cur), move';
    break;
    case 'move2':
        im_frm.cursor = (is.op)? 'move' : 'url(/images/map/c_hand2.cur), move';
    break;
    case 'wait':
        im_frm.cursor = (is.op)? 'wait' : 'url(/images/map/c_wait.cur), wait';
    break;

    }
}
function show_obj (obj_xx,obj_yy){

     var min_sc = document.getElementById('maxs_d').value;
     document.getElementById('cx_d').value = parseInt(obj_xx);
     document.getElementById('cy_d').value = parseInt(obj_yy);
     document.getElementById('sc_d').value = min_sc;
     document.getElementById('marker').value = 1;
     setTimeout("redraw_map()", 1);
}
function set_sc (sc_to){

     var min_sc = document.getElementById('maxs_d').value;
     var cur_sc = document.getElementById('sc_d').value;

     if ((sc_to != 'i') && (sc_to != 'o')){
         new_scale = parseInt( (parseInt(sc_to)/7) * maxsc);
         new_scale = parseInt( - min_sc -  new_scale)*(-1);
     } else if (sc_to == 'o') {
         new_scale = cur_sc * 3;
     } else if (sc_to == 'i') {
         new_scale = cur_sc / 3; //
     }
     if (new_scale > (maxsc / 2) ) //
         document.getElementById('marker').value = 0;

     document.getElementById('sc_d').value = new_scale;
     setTimeout("redraw_map()", 1);
}
