var PTZ_With_ActiveX = 0;

var lastpan;
var lasttilt;
var timeoutID = -1;
var ptz_ch = 1;
var ptz_move_stop = 0 ;
var object_ptz_getposition = { channel:0, nX:0, nY:0 }

function tvs_ptzpanspeed(cmd)
{
    if(cmd <= 4){
    	document.Tvs.SetPTZControlSpeed(g_lChID, 0, 1);
    }
    else if(cmd == 5){
    	document.Tvs.SetPTZControlSpeed(g_lChID, 0, 2);
    }
    else if(cmd == 6){
    	document.Tvs.SetPTZControlSpeed(g_lChID, 0, 3);
    }
    else if(cmd == 7){
    	document.Tvs.SetPTZControlSpeed(g_lChID, 0, 5);
    }
    else if(cmd == 8){
    	document.Tvs.SetPTZControlSpeed(g_lChID, 0, 7);
    }
    else{
    	document.Tvs.SetPTZControlSpeed(g_lChID, 0, cmd);
    }
}

function tvs_ptztiltspeed(cmd)
{
    if(cmd <= 4){
    	document.Tvs.SetPTZControlSpeed(g_lChID, 1, 1);
    }
    else if(cmd == 5){
    	document.Tvs.SetPTZControlSpeed(g_lChID, 1, 2);
    }
    else if(cmd == 6){
    	document.Tvs.SetPTZControlSpeed(g_lChID, 1, 3);
    }
    else if(cmd == 7){
    	document.Tvs.SetPTZControlSpeed(g_lChID, 1, 5);
    }
    else if(cmd == 8){
    	document.Tvs.SetPTZControlSpeed(g_lChID, 1, 7);
    }
    else{
    	document.Tvs.SetPTZControlSpeed(g_lChID, 1, cmd);
    }
}

function ptz_pt(ch, p, t)
{
	//console.log("PTZ(" + p + ", " + t + ")");
	var stopflag = 0;
    pan = Math.floor(Math.abs(p / 7));
    tilt = Math.floor(Math.abs(t / 7));
    ptz_ch = ch;

    if(pan == lastpan && tilt == lasttilt){
        return;
    }
    lastpan = pan;
    lasttilt = tilt;
    
    if(pan == 0) p = 0;
    if(tilt == 0) t = 0;

    if(PTZ_With_ActiveX == 0){    
        url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MOVE="; 
        if(p < 0){ // left
            if(t < 0){ // up
                url += "leftup," + pan + "," + tilt + "&PTZ_TIMEOUT=5000";
            }
            else if(t > 0){
                url += "leftdown," + pan + "," + tilt + "&PTZ_TIMEOUT=5000";
            }
            else{
                url += "left," + pan + "&PTZ_TIMEOUT=5000";
            }
        }
        else if(p > 0){
            if(t < 0){ // up
                url += "rightup," + pan + "," + tilt + "&PTZ_TIMEOUT=5000";
            }
            else if(t > 0){
                url += "rightdown," + pan + "," + tilt + "&PTZ_TIMEOUT=5000";
            }
            else{
                url += "right," + pan + "&PTZ_TIMEOUT=5000";
            }
        }
        else{
            if(t < 0){ // up
                url += "up," + tilt + "&PTZ_TIMEOUT=5000";
            }
            else if(t > 0){
                url += "down," + tilt + "&PTZ_TIMEOUT=5000";
            }
            else{
                url += "stop";
                stopflag = 1;
            }
        }
        //console.log("url = " + url);
	    //data = ajaxGetAsync(url);
	    data = ajaxGet(url);
    }
    else{

        tvs_ptzpanspeed(pan);
        tvs_ptztiltspeed(tilt);
        if(p < 0){ // left
            if(t < 0){ // up
        	    document.Tvs.SendPTZCommand(g_lChID, 10);
            }
            else if(t > 0){
        	    document.Tvs.SendPTZCommand(g_lChID, 11);
            }
            else{
        	    document.Tvs.SendPTZCommand(g_lChID, 2);
            }
        }
        else if(p > 0){
            if(t < 0){ // up
        	    document.Tvs.SendPTZCommand(g_lChID, 12);
            }
            else if(t > 0){
        	    document.Tvs.SendPTZCommand(g_lChID, 13);
            }
            else{
        	    document.Tvs.SendPTZCommand(g_lChID, 3);
            }
        }
        else{
            if(t < 0){ // up
        	    document.Tvs.SendPTZCommand(g_lChID, 4);
            }
            else if(t > 0){
        	    document.Tvs.SendPTZCommand(g_lChID, 5);
            }
            else{
        	    document.Tvs.SendPTZCommand(g_lChID, 0);
                stopflag = 1;
            }
        }
    }

    if(stopflag){
        if (timeoutID >= 0)
        {
	        clearTimeout(timeoutID);
			timeoutID = -1;
			ptz_move_stop = 4 ;
        }
    }
    else{
        if (timeoutID >= 0)
        {
            clearTimeout(timeoutID);
            timeoutID = -1;
        }
        timeoutID = setInterval(ptz_timer, 490);
	}

}

function ptz_preset_set(ch, i)
{    
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_PRESETSET=" + i; 
    data = ajaxGetAsync(url);
}

function ptz_preset_goto(ch, i)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_PRESETGOTO=" + i; 
	data = ajaxGetAsync(url);
	ptz_move_stop = 4 ;
}

function ptz_preset_clear(ch, i)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_PRESETCLR=" + i; 
    data = ajaxGetAsync(url);
}

function ptz_pattern_goto(i)
{    
	i += 130
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_PRESETGOTO=" + i; 
	data = ajaxGetAsync(url);
	ptz_move_stop = 4 ;
}

function ptz_swing_goto(i)
{
	i += 140
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_PRESETGOTO=" + i; 
	data = ajaxGetAsync(url);
	ptz_move_stop = 4 ;
}

function ptz_group_goto(i)
{
	i += 150
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_PRESETGOTO=" + i; 
	data = ajaxGetAsync(url);
	ptz_move_stop = 4 ;
}

function ptz_set_pattern_start(i)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_SETPATTERNSTART=" + i; 
    data = ajaxGetAsync(url);
}

function ptz_set_pattern_stop(i)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_SETPATTERNSTOP=" + i; 
	data = ajaxGetAsync(url);
	ptz_move_stop = 4 ;
}

function ptz_general_command(i)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_COMMAND=" + i; 
    data = ajaxGetAsync(url);
}

function ptz_drag_zoom(a, b, c, d)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_DRAGZOOM=" + a + "," + b + "," + c + "," + d; 
	data = ajaxGetAsync(url);
	ptz_move_stop = 4 ;
}

function ptz_det_clicktocenter(ch,x,y)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_DET_CENTERING=" + ch + "," + x + "," + y; 
	data = ajaxGetAsync(url);
	ptz_move_stop = 4 ;
}

function ptz_det_drag_zoom(ch, a, b, c, d)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_DET_DRAGZOOM=" + ch + "," + a + "," + b + "," + c + "," + d; 
	data = ajaxGetAsync(url);	
	ptz_move_stop = 4 ;
}

function __dcb_ptz_det_getposition(url, params, data, cb)
{
    if(data) {
        var token = data.split("=");		
        if(token.length != 2) {
            cb(object_ptz_getposition);
        }		
        if(token[0] == "PTZ_DET_GETPOSITION") {			
            var param = token[1].split(",");			
            if(param.length != 3) {
                cb(object_ptz_getposition);
            }
            object_ptz_getposition = { channel:param[0], nX:param[1], nY:param[2]  }		
        }
    }	

    cb(object_ptz_getposition);
}

function ptz_det_getposition(cb)
{
    //url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_DET_GETPOSITION=";
    url ="/httpapx/ReadParam?action=readparam&PTZ_DET_GETPOSITION=0"; 
    if (cb == undefined) {
        data = ajaxGet(url);
	
        if(data) {
            var token = data.split("=");		
            if(token.length != 2) {
                return object_ptz_getposition;
            }		
            if(token[0] == "PTZ_DET_GETPOSITION") {			
                var param = token[1].split(",");			
                if(param.length != 3) {
                    return object_ptz_getposition;
                }
                object_ptz_getposition = { channel:param[0], nX:param[1], nY:param[2]  }		
            }
        }	
	
	    return object_ptz_getposition ;
    }
    else {
        ajaxGetAsyncCb(url, undefined, __dcb_ptz_det_getposition, cb); 
    }
}

function ptz_setmask(i, a, b, c, d)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_SETMASK=" + i + "," + a + "," + b + "," + c + "," + d; 
    data = ajaxGetAsync(url);
}

function ptz_mask_command(id, i)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_MASKCOMMAND=" + id + "&PTZ_MASKID=" + i; 
    data = ajaxGetAsync(url);
}

function ptz_zoomhome()
{
	ptz_end(1);
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=1&PTZ_ABSOLUTEPOSITION=-1,-1,0,-1"; 
    data = ajaxGetAsync(url);
}

function ptz_end(force)
{
	if (timeoutID >= 0)
	{
		clearTimeout(timeoutID);
		timeoutID = -1;
        if(PTZ_With_ActiveX == 0){    
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ptz_ch + "&PTZ_MOVE=stop"; 
            data = ajaxGetAsync(url);
        }
        else{        
           	document.Tvs.SendPTZCommand(g_lChID, 0);
       	}
	}
	else{
	    if(force){
            if(PTZ_With_ActiveX == 0){    
                url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ptz_ch + "&PTZ_MOVE=stop"; 
                data = ajaxGetAsync(url);
            }
            else{
           	    document.Tvs.SendPTZCommand(g_lChID, 0);
           	}
	    }
    }
}

function ptz_timer() 
{
    if(PTZ_With_ActiveX == 0){    
        url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ptz_ch + "&PTZ_MOVE=continue"; 
	    data = ajaxGetAsync(url);
	}
	else{
        document.Tvs.SendPTZCommand(g_lChID, 1);
	}
}

function ptz_cmd(ch, cmd)
{
	switch(cmd)
	{
/*		case 'left':
			tvs_sendPtzCmd(2);
			break;
		case 'right':
			tvs_sendPtzCmd(3);
			break;
		case 'up':
			tvs_sendPtzCmd(4);
			break;
		case 'down':
			tvs_sendPtzCmd(5);
			break;
*/			
		case 'zoom_in':
            if(PTZ_With_ActiveX == 0){    
                url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MOVE=zoomin,-1"; 
        	    data = ajaxGetAsync(url);
        	}
        	else{
           	    document.Tvs.SendPTZCommand(g_lChID, 6);
           	}
			break;
		case 'zoom_out':
            if(PTZ_With_ActiveX == 0){    
                url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MOVE=zoomout,-1"; 
        	    data = ajaxGetAsync(url);
        	}
        	else{
           	    document.Tvs.SendPTZCommand(g_lChID, 7);
           	}
			break;
		case 'focus_far':
            if(PTZ_With_ActiveX == 0){    
                url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MOVE=focusfar,-1"; 
        	    data = ajaxGetAsync(url);
        	}
        	else{
           	    document.Tvs.SendPTZCommand(g_lChID, 8);
           	}
			break;
		case 'focus_near':
            if(PTZ_With_ActiveX == 0){    
                url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MOVE=focusnear,-1"; 
        	    data = ajaxGetAsync(url);
        	}
        	else{
           	    document.Tvs.SendPTZCommand(g_lChID, 9);
           	}
			break;
/*			
		case 'left_up':
			tvs_sendPtzCmd(10);
			break;
		case 'left_down':
			tvs_sendPtzCmd(11);
			break;
		case 'right_up':
			tvs_sendPtzCmd(12);
			break;
		case 'right_down':
			tvs_sendPtzCmd(13);
			break;
*/

		case 'light_on':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_LIGHT=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'light_off':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_LIGHT=0"; 
        	data = ajaxGetAsync(url);
			break;
		case 'power_on':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_POWER=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'power_off':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_POWER=0"; 
        	data = ajaxGetAsync(url);
			break;
		case 'home':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MOVE=home"; 
        	data = ajaxGetAsync(url);
			break;
		case 'iris_close':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MOVE=irisclose"; 
        	data = ajaxGetAsync(url);
			break;
		case 'iris_open':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MOVE=irisopen"; 
        	data = ajaxGetAsync(url);
			break;
		case 'iris_auto':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_IRISAUTO=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'focus_auto':
            if(PTZ_With_ActiveX == 0){    
                url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_FOCUSAUTO=1"; 
        	    data = ajaxGetAsync(url);
        	}
        	else{
           	document.Tvs.SendPTZCommand(g_lChID, 28);
        	}
			break;
		case 'focus_manual':
			break;

		case 'menu_on':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MENUON=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'menu_off':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MENUOFF=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'menu_enter':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MENUENTER=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'menu_esc':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_MENUESC=1"; 
        	data = ajaxGetAsync(url);
			break;
			/*
		case 'ss_on':
			tvs_sendPtzCmd(112);
			break;
		case 'ss_off':
			tvs_sendPtzCmd(113);
			break;
		case 'ni_on':
			tvs_sendPtzCmd(114);
			break;
		case 'ni_off':
			tvs_sendPtzCmd(115);
			break;
		case 'st_on':
			tvs_sendPtzCmd(116);
			break;
		case 'st_off':
			tvs_sendPtzCmd(117);
			break;
*/
		case 'onepushaf':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_ONEPUSHAF=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'onepushwb':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_ONEPUSHWB=1"; 
        	data = ajaxGetAsync(url);
			break;

		case 'lsc_scan':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_LSCSCAN=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'awc_set':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_AWCSET=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'lens_offset_adjust':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_LENSOFFSETADJUST=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'lens_iris_adjust':
            url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_LENSIRISADJUST=1"; 
        	data = ajaxGetAsync(url);
			break;
		case 'ventilation':
			url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_VENTILATION=1";
			data = ajaxGetAsync(url);
			break;
		case 'calibration':
			url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_CALIBRATION=1";
			data = ajaxGetAsync(url);
			break;
		case 'wiper_1h':
			url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_WIPER=1h";
			data = ajaxGetAsync(url);
			break;
		case 'wiper_2h':
			url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_WIPER=2h";
			data = ajaxGetAsync(url);
			break;
		case 'wiper_3h':
			url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_WIPER=3h";
			data = ajaxGetAsync(url);
			break;
	}
}

function ptz_begin(ch, command)
{
    ptz_ch = ch;
    if(timeoutID < 0){
    	ptz_cmd(ch, command);
    	timeoutID = setInterval(ptz_timer, 490);
    }
}

function ptz_preset_set_semocon(ch, num, action)
{
    url ="/httpapx/SendPTZ?action=sendptz&PTZ_CHANNEL=" + ch + "&PTZ_PRESETSET_SEMOCON=" + num +
		"," + action;
    data = ajaxGetAsync(url);
}

function ptz_semocon_ir_command(cmd, val)
{
	if(cmd == "bright")
		url ="/httpapx/SendPTZ?action=sendptz&PTZ_SEMOCON_IR_BRIGHT=" + val; 
	else if(cmd == "dark")
		url ="/httpapx/SendPTZ?action=sendptz&PTZ_SEMOCON_IR_DARK=" + val; 
	else if(cmd == "wide")
		url ="/httpapx/SendPTZ?action=sendptz&PTZ_SEMOCON_IR_WIDE=" + val; 
	else if(cmd == "narrow")
		url ="/httpapx/SendPTZ?action=sendptz&PTZ_SEMOCON_IR_NARROW=" + val; 
	else if(cmd == "stop")
		url ="/httpapx/SendPTZ?action=sendptz&PTZ_SEMOCON_IR_STOP=0"; 
	else if(cmd == "continue")
		url ="/httpapx/SendPTZ?action=sendptz&PTZ_SEMOCON_IR_CONTINUE=0";
    data = ajaxGetAsync(url);
}

function ptz_semocon_timer()
{
	if(PTZ_With_ActiveX == 0){
		ptz_semocon_ir_command("continue", "0");
	}
}

function ptz_begin_semocon_ir(ch, cmd, val)
{
    ptz_ch = ch;
    if(timeoutID < 0){
    	ptz_semocon_ir_command(cmd, val);
    	timeoutID = setInterval(ptz_semocon_timer, 490);
    }
}

function ptz_end_semocon_ir()
{
	if (timeoutID >= 0)
	{
		clearTimeout(timeoutID);
		timeoutID = -1;
        if(PTZ_With_ActiveX == 0){
			ptz_semocon_ir_command("stop", "0");
        }
	}
}

