var goldibild = new Array();

var Base64 = {
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
        input = Base64._utf8_encode(input);
        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
        }
        return output;
    },
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length) {
            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = Base64._utf8_decode(output);
        return output;
    },
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}

function bot_getrandom(min, max) {
  if( min > max ) { return( -1 ); }
	if( min == max ) { return( min ); }
	return(min + parseInt(Math.random() * (max-min+1)));
}

function bot_minimize() {
  //$.ajax({ 'url':'api_ajax.php?action=bot_size&mode=2' });
  $('#bot_border_div').slideUp(1000);
  window.setTimeout("$('#bot_minimized_div').fadeIn(400)", 800);
}

function bot_maximize() {
  if (document.getElementById('bot_border_div').style.display != "block"){
    //$.ajax({ 'url':'api_ajax.php?action=bot_size&mode=1' });
    window.setTimeout("$('#bot_border_div').slideDown(1000);", 400);
    $('#bot_minimized_div').fadeOut(400);
  }
}

function bot_getBotAuswertung(num) {
  var teile = Base64.decode(num);
  var teil = teile.split("~");
  if (teil['2'] != '' && teil['2'] != "0" && teil['2'] != undefined) {
  	if (teil['2'] != undefined){
  		if (teil['2'] != '0'){
    		location = "index.php" + teil['2'];
    	}
    }
  }
  else {
    document.getElementById('bot_ausgabe_div').innerHTML += '<br><font style="color: blue; font-weight: bold;">GOldi:</font> ' + teil[0];
    document.getElementById('bot_ausgabe_div').scrollTop = document.getElementById('bot_ausgabe_div').scrollHeight;
    document.getElementById('bot_input').value = '';
    bot_animation(teil['1'], 0);
  }
}

function bot_startBotAuswertung() {
  var bot_frage = document.getElementById('bot_input').value;
  if (bot_frage != "") {
    document.getElementById('bot_ausgabe_div').innerHTML += '<br><font style="color: red; font-weight: bold;">Sie:</font> ' + bot_frage;
    AjaxRequest.get( {
      'url':'api_ajax.php?action=bot_auswertung&frage='+Base64.encode(bot_frage)
     ,'onSuccess':function(req){
     		bot_getBotAuswertung(req.responseText);
     	}
    } );
  }
  else {
    document.getElementById('bot_ausgabe_div').innerHTML += '<br><font style="color: blue; font-weight: bold;">GOldi:</font> ...';
    document.getElementById('bot_ausgabe_div').scrollTop = document.getElementById('bot_ausgabe_div').scrollHeight;
		bot_animation('2,1', 0);
  }
}

function bot_onloadpage_emotion(num) {
 if (num != "none") {
   bot_animation(num, 0);
 }
}

function bot_onloadpage() {
  document.getElementById('bot_ausgabe_div').scrollTop = document.getElementById('bot_ausgabe_div').scrollHeight;
  AjaxRequest.get( {
      'url':'api_ajax.php?action=bot_emotion_mem'
     ,'onSuccess':function(req){  bot_onloadpage_emotion(req.responseText); }
  } );
  goldibild[00]=new Image(); goldibild[00].src="conf/layout/bot/bot_ani_00.gif";
  goldibild[10]=new Image(); goldibild[10].src="conf/layout/bot/bot_ani_10.gif";
	goldibild[11]=new Image(); goldibild[11].src="conf/layout/bot/bot_ani_11.gif";
	goldibild[20]=new Image(); goldibild[20].src="conf/layout/bot/bot_ani_20.gif";
	goldibild[21]=new Image(); goldibild[21].src="conf/layout/bot/bot_ani_21.gif";
	goldibild[30]=new Image(); goldibild[30].src="conf/layout/bot/bot_ani_30.gif";
	goldibild[31]=new Image(); goldibild[31].src="conf/layout/bot/bot_ani_31.gif";
	goldibild[40]=new Image(); goldibild[40].src="conf/layout/bot/bot_ani_40.gif";
	goldibild[50]=new Image(); goldibild[50].src="conf/layout/bot/bot_ani_50.gif";
	goldibild[51]=new Image(); goldibild[51].src="conf/layout/bot/bot_ani_51.gif";
	goldibild[60]=new Image(); goldibild[60].src="conf/layout/bot/bot_ani_60.gif";
	goldibild[70]=new Image(); goldibild[70].src="conf/layout/bot/bot_ani_70.gif";
	goldibild[80]=new Image(); goldibild[80].src="conf/layout/bot/bot_ani_80.gif";
	goldibild[81]=new Image(); goldibild[81].src="conf/layout/bot/bot_ani_81.gif";
	goldibild[90]=new Image(); goldibild[90].src="conf/layout/bot/bot_ani_90.gif";
	goldibild[91]=new Image(); goldibild[91].src="conf/layout/bot/bot_ani_91.gif";
	goldibild[100]=new Image(); goldibild[100].src="conf/layout/bot/bot_ani_100.gif";
	goldibild[101]=new Image(); goldibild[101].src="conf/layout/bot/bot_ani_101.gif";
	goldibild[102]=new Image(); goldibild[102].src="conf/layout/bot/bot_ani_102.gif";
	goldibild[103]=new Image(); goldibild[103].src="conf/layout/bot/bot_ani_103.gif";
	goldibild[114]=new Image(); goldibild[114].src="conf/layout/bot/bot_ani_114.gif";
	goldibild[120]=new Image(); goldibild[120].src="conf/layout/bot/bot_ani_120.gif";
	goldibild[121]=new Image(); goldibild[121].src="conf/layout/bot/bot_ani_121.gif";
	goldibild[122]=new Image(); goldibild[122].src="conf/layout/bot/bot_ani_122.gif";
	goldibild[130]=new Image(); goldibild[130].src="conf/layout/bot/bot_ani_130.gif";
}

function bot_close_helpful() {
  document.getElementById('bot_helpful_div').style.display = 'none';
  document.getElementById('bot_ausgabe_div').style.display = 'block';
  document.getElementById('bot_form').style.display = 'block';
  document.getElementById('bot_helpful_link').style.display = 'block';
  document.getElementById('bot_helpful_div').innerHTML = '<span style="float: left;"><font style="color: blue; font-weight: bold;">GOldi:</font> Dies wären meine alternativen Fragen:</span> <span style="float: right;"><a onclick="bot_close_helpful(); return false" href="javascript:void(0)">Schließen</a></span><span style="clear: both;"><br></span>';
}

function bot_show_helpful(res) {
  var res = Base64.decode(res);
  if (res == "0") { res = "Hierzu gibt es leider keine alternativen Fragen!"; }
  document.getElementById('bot_ausgabe_div').style.display = 'none';
  document.getElementById('bot_form').style.display = 'none';
  document.getElementById('bot_helpful_link').style.display = 'none';
  document.getElementById('bot_helpful_div').style.display = 'block';
  document.getElementById('bot_helpful_div').innerHTML += res;
}

function bot_helpful() {
	AjaxRequest.get( {
      'url':'api_ajax.php?action=bot_helpful'
     ,'onSuccess':function(req){  bot_show_helpful(req.responseText); }
    } );
}

function bot_animation(ani, posi) {
  var aniarray = ani.split(';');
  if (posi < aniarray.length) {
	  if (aniarray[posi] == '') { aniarray[posi] = "0,0"; }
		var aniarray2 = aniarray[posi].split(',');
		var firstpart = aniarray2[0];
		if (aniarray2.length == 2) {
		  var secondpart = aniarray2[1];
		}
		else {
		  switch (aniarray2[0]) {
        case "0":  var secondpart = 0; break;
        case "1":  var secondpart = bot_getrandom(0, 1); break;
				case "2":  var secondpart = bot_getrandom(0, 1); break;
				case "3":  var secondpart = bot_getrandom(0, 1); break;
				case "4":  var secondpart = 0; break;
				case "5":  var secondpart = bot_getrandom(0, 1); break;
				case "6":  var secondpart = 0; break;
				case "7":  var secondpart = 0; break;
				case "8":  var secondpart = bot_getrandom(0, 1); break;
				case "9":  var secondpart = bot_getrandom(0, 1); break;
				case "10": var secondpart = bot_getrandom(0, 3); break;
				case "11": var secondpart = 4; break;
				case "12": var secondpart = bot_getrandom(0, 2); break;
				case "13": var secondpart = 0; break;
				default:   var secondpart = 0; break;
			}
		}
    var newimg = firstpart+secondpart;
		if (newimg == 0) { newimg = '00'; }
    switch (newimg) {
      case "00":  var time = 2000;  break;
      case "10":  var time = 2880;  break;
			case "11":  var time = 3600;  break;
			case "20":  var time = 20000; break;
			case "21":  var time = 1920;  break;
			case "30":  var time = 11200; break;
			case "31":  var time = 9600;  break;
			case "40":  var time = 2880;  break;
			case "50":  var time = 2880;  break;
			case "51":  var time = 2880;  break;
			case "60":  var time = 2880;  break;
			case "70":  var time = 2880;  break;
			case "80":  var time = 2880;  break;
  		case "81":  var time = 2880;  break;
			case "90":  var time = 2880;  break;
			case "91":  var time = 2880;  break;
			case "100": var time = 2880;  break;
			case "101": var time = 4380;  break;
			case "102": var time = 3000;  break;
			case "103": var time = 3840;  break;	
			case "81":  var time = 2880;  break;
			case "114": var time = 4380;  break;
			case "120": var time = 3000;  break;
			case "121": var time = 3840;  break;
			case "122": var time = 3840;  break;
			case "130": var time = 9500;  break;
			default:    var time = -1;    break;
		}
    posi++;
		document.getElementById('bot_face').src = goldibild[newimg].src;
    window.setTimeout("bot_animation('" + ani + "', '" + posi + "');", time);
	}
}
