/*
 * $Id: mv.js 4480 2008-06-11 10:57:39Z mkolibabka $
 */

if (typeof mv == "undefined") {
    var mv = new Object();
    mv.isMac = (navigator.appVersion.toLowerCase().indexOf("mac") != -1);
}

mv.MVA = function(id) {
    this.initialized = false;
    this._init_cb = undefined;
    this._init_cb_args = undefined;
    this._menu_cbs = {};
    this._menu_poi_cbs = {};
    this._m_id_cntr = 0;
    this._id = id;
    this.type_table = {};
    if (document.getElementById) {
        this._mva = document.getElementById(id);
        if (this._mva.localName == "OBJECT") {
            this._mva = this._mva.getElementsByTagName("embed")[0];
        }
    } else {
        this._mva = document[id];
    }
    if (typeof this._mva == 'undefined')
        return;
    mv.MVA._mva_objs[id] = this;
    if (typeof this._mva.ei_initialized == 'function' &&
        this._mva.ei_initialized())
        this.initialized = true;
};

mv.MVA._mva_objs = new Object();

mv.dispatch_flash_call = function() {
    var callee = mv.MVA._mva_objs[arguments[0]];
    if (callee != undefined) {
        if (typeof callee[arguments[1]] != "function")
            return;
        try {
            if (arguments.length > 2)
                callee[arguments[1]].apply(callee, arguments[2]);
            else
                callee[arguments[1]]();
        } catch (err) {
            // just ignore errors
            //alert([err.name, err.message]);
        }
    }
}

mv.MVA.prototype = {
    _POI_CB_NAME: 'pm_update_cb',
    _POI_CB_MENU_NAME: 'pm_menu_selected',
    _POI_CB_POI_MENU_NAME: 'pm_poi_menu_selected',
    mv_init: function() {
        if (typeof this._init_cb == 'function') {
            try {
                if (this._init_cb_args != undefined)
                    this._init_cb.apply(null, this._init_cb_args);
                else
                    this._init_cb();
            } catch (err) {
                // just ignore errors
                alert([err.name, err.message]);
            }
            this._init_cb = undefined;
            this._init_cb_args = undefined;
        }
        this._init_mac_mw();
        this.initialized = true;
    },
    _init_mac_mw: function() {
        if (!mv.isMac || (typeof this._mva._ext_mw_event == 'undefined') ||
            !window.addEventListener)
            return;
        var isAWK = (navigator.userAgent.indexOf('AppleWebKit') != -1);
        var __mva = this._mva;
        var eh = function(e) {
            if (__mva != e.target) {
                e.returnValue = true;
                return;
            }
            var d = 0;
            if (e.wheelDelta) {
                d = e.wheelDelta / 120;
                if (window.opera)
                    d = -d;
            } else if (e.detail) {
                d = -e.detail / 3;
            }
            if (isAWK)
                d /= 3;
            if (d != 0) {
                try {
                    __mva._ext_mw_event(d);
                } catch (err) {
                }
            }
            if (e.preventDefault)
                e.preventDefault();
            e.returnValue = false;
        };
        window.addEventListener('DOMMouseScroll', eh, false);
        window.onmousewheel = document.onmousewheel = eh;
    },
    _version: function() {
        return this._mva.ei_version();
    },
    set_coords: function(x, y, s, t, no_ani) {
        this._mva.mv_set_coords(x, y, s, t, no_ani);
    },
    get_coords: function() {
        return this._mva.mv_get_coords();
    },
    get_scale: function() {
        return this._mva.mv_get_scale();
    },
    get_vp_ref: function() {
        return this._mva.mv_get_vp_ref();
    },
    fit_bbox: function(l, t, r, b, part, a_o) {
        if (part == undefined)
            part = 1.0;
        this._mva.mv_fit_bbox(l, t, r, b, part, a_o);
    },
    pan: function(dx, dy, t, no_ani) {
        this._mva.mv_pan(dx, dy, t, no_ani);
    },
    set_init_cb: function(cb_func, cb_args) {
        if (typeof cb_func != 'function')
            return;
        this._init_cb = cb_func;
        this._init_cb_args = cb_args;
        if (this.initialized)
            this.mv_init();
    },
    set_cb: function(cb_type, cb_func, cb_args) {
        if (!this.initialized)
            return;
        if (typeof cb_func != 'function')
            return;
        var do_request = false;
        if (typeof this[cb_type] != 'function') {
            do_request = true;
        } else {
            delete this[cb_type];
            this[cb_type] = undefined;
        }

        if (typeof cb_args != 'undefined') {
            this[cb_type] = function() {
                cb_func.apply(null, cb_args);
            }
        } else {
            this[cb_type] = function() {
                cb_func.apply(null, arguments);
            }
        }
        if (do_request)
            this._mva._request_cbs(cb_type);
    },
    clear_cb: function(cb_type) {
        if (typeof this[cb_type] != 'function')
            return;

        this._mva._cancel_cbs(cb_type);

        delete this[cb_type];
        this[cb_type] = undefined;
    },
    route_on: function(rid, l, t, r, b) {
        this._mva.mv_route_on(rid, l, t, r, b);
    },
    route_off: function() {
        this._mva.mv_route_off();
    },
    pois_set: function(type, pois, scale, bbox, nc) {
        this._mva.poi_set_pois(type, pois, scale, bbox, nc);
    },
    blockpoppoi:function(miliseconds){
      this._mva.blockpoppoi(miliseconds);
    },
    simulate_poi_mouse_click: function(type, poi, x, y, s) {
        this._mva.simulate_poi_mouse_click(type, poi, x, y, s);
    },
    simulate_poi_mouse_click_and_set_coords: function(type, poi, x, y, s) {
        this._mva.simulate_poi_mouse_click_and_set_coords(type, poi, x, y, s);
    },
    simulate_poi_mouse_over: function(type, poi) {
        this._mva.simulate_poi_mouse_over(type, poi);
    },
    simulate_poi_mouse_out: function(type, poi) {
        this._mva.simulate_poi_mouse_out(type, poi);
    },
    pois_clear: function(type, scale) {
        this._mva.poi_clear_pois(type, scale);
    },
    pois_get: function(type) {
        return this._mva.poi_get_pois(type);
    },
    
       //mvfa.pois_request_updates('poi-2',5.0,0.4,makeTourInfo);
    pois_request_updates: function(type, size_fc, shift_th, cb_func) {
        
    	
    	//alert('typ: ' + type);
    	
    	if (!this.initialized)
            return;
        if (typeof cb_func != 'function')
            return;
        var do_request = false;
        
        if (typeof this[this._POI_CB_NAME] != 'function') {
            do_request = true;
        } else {
            delete this[this._POI_CB_NAME];
            this[this._POI_CB_NAME] = undefined;
        }

        
        this.type_table['t_' + type] = cb_func;
        
        this[this._POI_CB_NAME] = function() {
        	//alert('arg: '+arguments[0]);
        	var f = this.type_table['t_' + arguments[0]];
        	//cb_func.apply(null, arguments);
        	f.apply(null, arguments);
        }
        if (do_request)
        	this._mva._request_cbs(this._POI_CB_NAME);
        	this._mva.poi_request_updates(type, size_fc, shift_th);
        
        
    },
    pois_cancel_updates: function(type) {
        
    	//alert('cancel: ' + type);
    	
    	if (typeof this[this._POI_CB_NAME] != 'function')
            return;
        
        this._mva.poi_cancel_updates(type);
        //this._mva._cancel_cbs(this._POI_CB_NAME);

        //delete this[this._POI_CB_NAME];
        //this[this._POI_CB_NAME] = undefined;
    },
    pois_s_add: function(type, data, scale) {
        this._mva.poi_add_poi(type, data, scale);
    },
    pois_s_remove: function(type, pid) {
        this._mva.poi_remove_poi(type, pid);
    },
    pois_s_get: function(type, pid) {
        return this._mva.poi_get_poi(type, pid);
    },
    pois_enable: function(type, do_enable) {
        this._mva.poi_enable(type, do_enable);
    },
    vector_route_data_response: function(response){
      this._mva.vector_route_data_response(response);
    },
    switch_agregate_route_pois: function(type, pid, visible){
      this._mva.switch_agregate_route_pois(type, pid, visible);
    },
    enable_vector_route: function(mode){
        this._mva.enable_vector_route(mode);
    },
    hide_alert: function(){
      this._mva.hide_alert();
    },
    show_alert: function(to, te, ldts){
      this._mva.show_alert(to, te, ldts);
    },
    /*  === context menu handling methods ===  */
    pois_menu_enable: function(type, is_poi, mi_id, do_enable) {
        this._mva.poi_menu_enable(type, is_poi, mi_id, do_enable);
    },
    __menu_cb_helper: function(type, mid, pt) {
        var callee = this._menu_cbs[mid];
        if (callee != undefined) {
            callee(type, pt);
        }
    },
    __poi_menu_cb_helper: function(type, pid, mid) {
        var callee = this._menu_poi_cbs[mid];
        if (callee != undefined) {
            callee(type, pid);
        }
    },
    pois_menu_add: function(type, is_poi, caption, cb_func, mi_id) {
        if (!this.initialized)
            return;
        if (typeof cb_func != 'function')
            return;
        if (typeof mi_id != 'string')
            mi_id = "__mid" + (is_poi ? "_p" : "") + this._m_id_cntr++;
        /* rc ahead, although unlikely... */
        var mi_added = this._mva.poi_menu_add(type, is_poi, mi_id, caption);
        if (!mi_added)
            return undefined;
        if (is_poi)
            this._menu_poi_cbs[mi_id] = cb_func;
        else
            this._menu_cbs[mi_id] = cb_func;
        var cb_type = (is_poi ? this._POI_CB_POI_MENU_NAME :
                       this._POI_CB_MENU_NAME);
        if (typeof this[cb_type] != 'function') {
            this[cb_type] = (is_poi ? this.__poi_menu_cb_helper :
                             this.__menu_cb_helper);
            this._mva._request_cbs(cb_type);
        }
        return mi_id;
    },
    pois_menu_remove: function(type, is_poi, mi_id) {
        var cb_type = (is_poi ? this._POI_CB_POI_MENU_NAME :
                       this._POI_CB_MENU_NAME);
        if (typeof this[cb_type] != 'function')
            return;
        var cb_obj = is_poi ? this._menu_poi_cbs : this._menu_cbs;
        this._mva.poi_menu_remove(type, is_poi, mi_id);
        if (typeof cb_obj[mi_id] != 'function')
            return;
        delete cb_obj[mi_id];
        var cb_left = false;
        for (var i in cb_obj) {
            if (typeof cb_obj[i] != 'undefined')
                cb_left = true;
        }
        if (!cb_left) {
            this._mva._cancel_cbs(cb_type);
            delete this[cb_type];
            this[cb_type] = undefined;
        }
    }
};
