﻿
var playing_path = 0;
var going_home = 0;
var selected_path_id = -1;
var played_path_id = -1;

var DEFAULT_SPEED = 2;
var DEFAULT_TURNING_SPEED = 5;

var MOVEMENT_INTERVAL = 300;

var FWD_BCK_PULSES = 5;
var LFT_RGT_PULSES = 5;
var ROTATE_PULSES = 1;

var head_pos = 'cam_down';

var pulse_count = 0;

var pulse_timeout_id = -1;

var lastSelected = -1;

function doNothing(sText)
{

}

var roboscreen = 0;
function sendPhoto(){
		jQuery.get('/plugins/robot_plugin/robot_image.php', function(data) {
			if (data == '0') {
				alert('Вы исчерпали лимит');
			} else if (data == '-1') {
				alert('Произошла какя-то ошибка');
			} else {
				roboscreen++;
				var imgss = '<a href="' + data + '" class="fancybox-nobg">'
				imgss = imgss + '<img id="new'+roboscreen+'" height=90 src="'+ data +'" />';
				imgss = imgss + '</a>';
			if (!document.getElementById("roboscrul")){ return; }
	 
                    var saveRoot = document.getElementById("roboscrul");

					var pdiv = document.createElement("li");
					pdiv.innerHTML = imgss;
                    saveRoot.appendChild(pdiv);
                    saveRoot = null;
			}
		});
		return;
	roboscreen++;

var currentTime = new Date();
var shours = currentTime.getHours();
var sminutes = currentTime.getMinutes();
var smonth = currentTime.getMonth() + 1;
var sday = currentTime.getDate();
var syear = currentTime.getFullYear();
var ssecond = currentTime.getSeconds();

if (sminutes < 10){
	sminutes = "0" + sminutes;
}
	
	var imnums = syear +"_"+ smonth +"_"+ sday +"_"+ shours +"_"+ sminutes+"_"+ ssecond; //Math.floor (100000 * Math.random ());
	var imgss = '<a href="/robo/screenshots/Jpeg/CamImg'+imnums+'.jpg" class="fancybox-nobg">'
	var imgss = imgss + '<img id="new'+roboscreen+'" height=90 src="/robo/screenshots/Jpeg/CamImg'+imnums+'.jpg" />';
	var imgss = imgss + '</a>';
	
	if (!document.getElementById("roboscrul")){ return; }
	 
                    var saveRoot = document.getElementById("roboscrul");

					var pdiv = document.createElement("li");
					pdiv.innerHTML = imgss;
                    saveRoot.appendChild(pdiv);
                    saveRoot = null;
	
		//if (document.getElementById("screen1")){
		//	replaceHtmlAd2("screen1",imgss);
		//} else {
		//	document.write(imgss);
		//}
	
    //alert('Photo Sent');
}

function pulseMove(drive_cmd,speed){
    sendCmd('/robo/rev.cgi', 'Cmd=nav&action=18&drive=' + drive_cmd + '&speed=' + speed, doNothing);
    
    if(drive_cmd == 1 || drive_cmd == 2){
        max_pulse_count = FWD_BCK_PULSES;
    } else if(drive_cmd == 5 || drive_cmd == 6) {
        //max_pulse_count = ROTATE_PULSES;
        return;
    } else {
        max_pulse_count = LFT_RGT_PULSES;
    }
        
    if(pulse_count < max_pulse_count){
        pulse_timeout_id = setTimeout('pulseMove(' + drive_cmd + ', ' + speed + ')', MOVEMENT_INTERVAL);
        pulse_count++;
    } else {
        pulse_count = 0;
    }
}

function move(move_id){
    var drive_cmd = 0;
    var speed = DEFAULT_SPEED;
            
    switch(move_id){
        case 'rot_left':
            drive_cmd = 5;
            speed = DEFAULT_TURNING_SPEED;
            break;
        case 'rot_right':
            drive_cmd = 6;
            speed = DEFAULT_TURNING_SPEED;
            break;
        case 'move_forward':
            drive_cmd = 1;
            break;
        case 'move_left':
            drive_cmd = 3;
            break;
        case 'move_right':
            drive_cmd = 4;
            break;
        case 'move_back':
            drive_cmd = 2;
            break;   
        case 'move_fwd_left':
            drive_cmd = 7;
            break;
        case 'move_fwd_right':
            drive_cmd = 8;
            break;
        case 'move_bck_left':
            drive_cmd = 9;
            break;
        case 'move_bck_right':
            drive_cmd = 10;
            break;
        case 'cam':
            if(head_pos == 'cam_down') { head_pos = 'cam_mid'; drive_cmd = "13"; 
            } else if(head_pos == 'cam_mid') { head_pos = 'cam_up'; drive_cmd = "11"; 
            } else if(head_pos == 'cam_up') { head_pos = 'cam_down'; drive_cmd = "12"; }
            break;
    }
    if(drive_cmd){
            if(pulse_timeout_id != -1){
                clearTimeout(pulse_timeout_id);
                pulse_count = 0;
            }
            pulseMove(drive_cmd, speed);
    }
}

function stop(){
    sendCmd("/robo/rev.cgi", "Cmd=nav&action=17", doNothing);
    sendCmd("/robo/rev.cgi", "Cmd=nav&action=33", doNothing);
}

function trim(string) {
	return string.replace(/^\s+|\s+$/g,"");
}

function updateVideo(value){
    sendCmd("/robo/ChangeResolution.cgi","ResType=" + value, doNothing);
}

function say() {
	var sayMenu = jQuery('div.robot-say');
	if (sayMenu != null) {
		if (sayMenu.css('display') == 'none') {
			sayMenu.show();
			jQuery('div.robot-remote').hide();
			catchKeys = false;
			jQuery('#say_length').html('0/280');
		}
	} else {
		alert('ERROR 13854019824091');
	}
	
}
function robot_say_submit(input, button) {
		input.attr('disabled', 'disabled');
		button.attr('disabled', 'disabled');
		jQuery.get('/plugins/robot_plugin/robot_say.php?say=' + input.val(), function(data) {
			if (data == '0') {
				alert('Вы исчерпали лимит');
			} else {
				input.removeAttr('disabled');
				button.removeAttr('disabled');
				input.val('');
				jQuery.sound.play('http://esquire.ru/plugins/robot_plugin/wavs/' + data, {
					timeout: 40000
				});
			}
		});
		say_close();
}
function say_close() {
	jQuery('div.robot-say').hide();
	jQuery('div.robot-remote').show();
	catchKeys = true;
}


function selectBtn(ele){
	/*
    var imgEle = ele.firstChild;
    if(lastSelected != -1){
        lastSelected.style.border = "1px solid #404040";
    }
    imgEle.style.border = "1px solid white";
    lastSelected = imgEle;
	*/
}

function request(url, postData, rtn_function){
    var xhr = null;

    try {xhr = new XMLHttpRequest();}
    catch (e) {xhr = null;}
    if (xhr == null)
    {
        try {xhr = new ActiveXObject("Msxml2.XMLHTTP");}
        catch (e) {xhr = null;}
    }
    if (xhr == null)
    {
        try {xhr = new ActiveXObject("Microsoft.XMLHTTP");}
        catch (e) {xhr = null;}
    }
    if(xhr == null){
        return;
    }	            
	
    xhr.open ("POST", url, true);
    xhr.onreadystatechange = function ()
    {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
            rtn_function (xhr.responseText);
        }
    }
    xhr.send (postData);

    return xhr;
}


var on = "0"
function headlight() {
	if ( on == "0" ){ on = "1"; } else { on = "0"; }
	sendCmd("/robo/rev.cgi","Cmd=nav&action=19&LIGHT="+on);
}

function parseVars2(str, sep, equ){
  var seperated = str.split(sep);
  var results = new Array();
  for(i = 0; i < seperated.length; i++){
    results[i] = seperated[i].split(equ);
  }
  return results;
}

function replaceHtmlAd2(el, html){
var oldEl = (typeof el === "string" ? document.getElementById(el) : el);  
var newEl = oldEl.cloneNode(false);  
newEl.innerHTML = html;  
oldEl.parentNode.replaceChild(newEl, oldEl);  
return newEl;  
}
