/* Variables */

var start_question = '<typ hier kort en bondig uw vraag>';

/* Set Defaults */

if (typeof(a_page) == "undefined") { var a_page = ''; }
a_page = a_page.split('.almere.nl/')[1];
if (typeof(a_page) == "undefined") { var a_page = ''; }
a_page = a_page.split('/')[0];

var a_color = new Object();

switch(a_page) {
	case 'home':                   a_color.dark = '24889a'; a_color.light = '6ab9c8'; a_color.back = 'c8e1e6'; break;
	case 'de_stad':                a_color.dark = 'cb651d'; a_color.light = 'd89457'; a_color.back = 'f2d8c6'; break;
	case 'leven_en_werken':        a_color.dark = 'af3a2e'; a_color.light = 'b87069'; a_color.back = 'ebcecb'; break;
	case 'vrije_tijd_en_toerisme': a_color.dark = '419a3b'; a_color.light = '68b862'; a_color.back = 'cfe6ce'; break;
	case 'ondernemen':             a_color.dark = '94871e'; a_color.light = 'b1a544'; a_color.back = 'e4e1c7'; break;
	case 'direct_regelen_en_info': a_color.dark = 'a84c75'; a_color.light = 'c4799b'; a_color.back = 'e9d2dc'; break;
	default:                       a_color.dark = '24889a'; a_color.light = '6ab9c8'; a_color.back = 'c8e1e6'; // home
}

/* Create Holder */

var a_hld = document.createElement('div');
a_hld.setAttribute('id','a_hld');
a_hld.style.width = '232px';
a_hld.style.height = '296px';
a_hld.style.background = '#' + a_color.back;
a_hld.style.position = 'relative';
a_hld.style.textAlign = 'left';

document.getElementById('ally').appendChild(a_hld);

/* Create DIV's */

var a_str = '';
a_str += '<div id="div_avatar" style="position:absolute;left:25px;top:11px;z-index:1;"></div>';
a_str += '<div id="div_nav" style="position:absolute;top:267px;width:232px;height:29px;background:#' + a_color.dark +';z-index:2;"></div>';
a_str += '<div id="div_text" style="position:absolute;top:247px;width:232px;left:0px;z-index:3;background:#fff;font-family:arial;font-size:12px;color:#000;opacity:.80;filter: alpha(opacity=80); -moz-opacity: 0.80;"></div>';
a_str += '<div id="div_text_content" style="position:absolute;top:247px;width:188px;left:3px;z-index:4;font-family:arial;font-size:12px;color:#555;padding:4px 4px 5px 4px;overflow:hidden;"></div>';
a_str += '<div id="div_text_loader" style="position:absolute;top:247px;width:218px;left:208px;z-index:5;"></div>';
a_str += '<div id="div_dialog" style="position:absolute;background:#fff;z-index:6;">';
a_str += '  <div id="div_dialog_cc" style="position:relative;font-family:Arial;font-size:12px;line-height:20px;padding:6px 15px 30px 15px;"></div>';

a_str += '  <div id="div_dialog_ul" style="position:absolute;background:url(http://artificialindustry.net/p/almere.nl/assets/dialog_ul.png) no-repeat;width:12px;height:12px;left:-12px;top:-12px;"></div>';
a_str += '  <div id="div_dialog_ur" style="position:absolute;background:url(http://artificialindustry.net/p/almere.nl/assets/dialog_ur.png) no-repeat;width:16px;height:12px;top:-12px;"></div>';
a_str += '  <div id="div_dialog_br" style="position:absolute;background:url(http://artificialindustry.net/p/almere.nl/assets/dialog_br.png) no-repeat;width:16px;height:16px;"></div>';
a_str += '  <div id="div_dialog_bl" style="position:absolute;background:url(http://artificialindustry.net/p/almere.nl/assets/dialog_bl.png) no-repeat;width:12px;height:16px;left:-12px;"></div>';

a_str += '  <div id="div_dialog_uc" style="position:absolute;background:url(http://artificialindustry.net/p/almere.nl/assets/dialog_uc.png) repeat-x;height:12px;left:0px;top:-12px;"></div>';
a_str += '  <div id="div_dialog_cr" style="position:absolute;background:url(http://artificialindustry.net/p/almere.nl/assets/dialog_cr.png) repeat-y;width:16px;top:0px;"></div>';
a_str += '  <div id="div_dialog_bc" style="position:absolute;background:url(http://artificialindustry.net/p/almere.nl/assets/dialog_bc.png) repeat-x;height:16px;top:0px;"></div>';
a_str += '  <div id="div_dialog_cl" style="position:absolute;background:url(http://artificialindustry.net/p/almere.nl/assets/dialog_cl.png) repeat-y;width:12px;left:-12px;top:0px;"></div>';

a_str += '  <div id="div_dialog_p"  style="position:absolute;background:url(http://artificialindustry.net/p/almere.nl/assets/dialog_p.png) no-repeat;width:42px;height:16px;"></div>';
a_str += '  <div id="div_dialog_close" style="position:absolute;width:105px;height:20px;"></div>';

a_str += '</div>';

a_hld.innerHTML = a_str;

document.getElementById('div_dialog').style.visibility = 'hidden';
document.getElementById('div_text').style.visibility = 'hidden';
document.getElementById('div_text_content').style.visibility = 'hidden';
document.getElementById('div_text_loader').style.visibility = 'hidden';

/* Create Avatar */

var div_avatar = document.createElement('div');

var id = 'ally_flash';
var width = 182;
var height = 257;
var objectURL = 'http://artificialindustry.net/p/almere.nl/assets/ally-4.swf?color='+a_color.back+'&v=2';
//var objectURL = 'http://www.arjenboutsema.nl/zooi/test.swf?color='+a_color.back+'&v=2';
//var objectURL = 'ally-4.swf?color='+a_color.back+'&v=2';

document.getElementById('div_avatar').innerHTML = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' id='" + id + "' width='" + width + "' height='" + height + "' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\'><param name='movie' value='" + objectURL + "' /><param name='quality' value='high' /><param name='allowScriptAccess' value='always' /><param name='wmode' value='transparent' /><embed src='" + objectURL + "' quality='high' width='" + width + "' height='" + height + "' name='" + id + "' play='true' loop='false' quality='high' allowScriptAccess='always' wmode='transparent' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>";
// opaque voor gegarandereerde werking

/* Create Loader */

var div_avatar = document.createElement('div');

var id = 'ally_loader';
var width = 19;
var height = 19;
var objectURL = 'http://artificialindustry.net/p/almere.nl/assets/loader.swf?v=1';
//var objectURL = 'http://www.arjenboutsema.nl/zooi/test.swf?color='+a_color.back+'&v=2';
//var objectURL = 'loader.swf?&v=2';

document.getElementById('div_text_loader').innerHTML = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' id='" + id + "' width='" + width + "' height='" + height + "' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\'><param name='movie' value='" + objectURL + "' /><param name='quality' value='high' /><param name='allowScriptAccess' value='always' /><param name='wmode' value='transparent' /><embed src='" + objectURL + "' quality='high' width='" + width + "' height='" + height + "' name='" + id + "' play='true' loop='false' quality='high' allowScriptAccess='always' wmode='transparent' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>";
// opaque voor gegarandereerde werking

/* Create Navigation */

var a_str = '';
a_str += '<form action="javascript:void();" onsubmit="return a_submit();">';
a_str += '<input value="'+start_question+'" type="text" id="a_input" style="position:absolute;left:2px;top:3px;width:192px;height:16px;padding:3px;border:1px solid #000;font-family:arial;font-size:12px;font-weight:normal;" onkeydown="hide_question();" onfocus="q_focus();">';
a_str += '<input type="submit" value="" alt="Stel de vraag" style="position:absolute;left:205px;top:3px;width:25px;height:24px;border:0px;background:url(http://artificialindustry.net/p/almere.nl/assets/button_a.gif);cursor:pointer;cursor:hand;" onmouseover="a_submit_on(this);" onmouseout="a_submit_off(this);" onmousedown="a_submit_off(this);" onmouseup="a_submit_on(this);">';
a_str += '</form>';
document.getElementById('div_nav').innerHTML = a_str;

function a_submit_on(object) { object.style.background = 'url(http://artificialindustry.net/p/almere.nl/assets/button_b.gif)'; }
function a_submit_off(object)  { object.style.background = 'url(http://artificialindustry.net/p/almere.nl/assets/button_a.gif)'; }

var a_str = '';
a_str += '<input type="button" value="" style="position:absolute;left:0px;top:0px;width:105px;height:20px;border:0px;background:url(http://artificialindustry.net/p/almere.nl/assets/close_a.gif);cursor:pointer;cursor:hand;" onmouseover="a_close_on(this);" onmouseout="a_close_off(this);" onmousedown="a_close_off(this);" onmouseup="a_close_on(this);" onclick="a_close(this);">';
document.getElementById('div_dialog_close').innerHTML = a_str;

function a_close_on(object) { object.style.background = 'url(http://artificialindustry.net/p/almere.nl/assets/close_b.gif)'; }
function a_close_off(object)  { object.style.background = 'url(http://artificialindustry.net/p/almere.nl/assets/close_a.gif)'; }

function a_close(object)  { document.getElementById('div_dialog').style.visibility = 'hidden'; }

function q_focus() {
	if (document.getElementById('a_input').value == start_question) { document.getElementById('a_input').value = ''; }
}

/*  Post and return */

var a_q;
var request;
var aObj;

function a_submit() {
	a_q = document.getElementById('a_input').value;
	document.getElementById('a_input').value = '';

	if(a_q == start_question) { a_q = ''; }

	if(a_q != '') {
		show_question(a_q);
		a_hide();

		request = 'http://artificialindustry.net/p/almere.nl/ally-json.php?u='+_uid+'&t=AA01&r=x&q=' + a_q;
		aObj = new JSONscriptRequest(request);
		aObj.buildScriptTag();
		aObj.addScriptTag();


	}
	document.getElementById('a_input').focus();
	return false;
}

var _uid = '';
var a_welcome_dropped = false;

function a_callback(jsonData) {
	if (a_welcome_dropped) {
		document.getElementById('div_text_loader').style.visibility = 'hidden';
		_uid = jsonData.data.uid;
		a_show(jsonData.data.answer);
		aObj.removeScriptTag();
		jsRequest(jsonData);
	} else {
		a_welcome_dropped = true;
	}
}

function a_welcome() {

	a_q = 'custom_welcome';

	request = 'http://artificialindustry.net/p/almere.nl/ally-json.php?u='+_uid+'&t=AA01&r=x&q=' + a_q;
	aObj = new JSONscriptRequest(request);
	aObj.buildScriptTag();
	aObj.addScriptTag();

}


/* Flash Communication */

var initialized = false;

function isReady() {
	//alert("yo");
	return initialized;
}

//function init() {
initialized = true;
//}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function jsRequest(value) {
	thisMovie("ally_flash").jsRequest(value);
}
/*
function flashRequest(value) {
	//alert("yo");
	jsonRead(value);
		//alert("yo");
}
*/

/* Show answer */

function a_show(a_answer) {

	// reset vars
	document.getElementById('div_dialog').style.width = '150px';
	document.getElementById('div_dialog_cc').style.width = '150px';

	// dump text
	document.getElementById('div_dialog_cc').innerHTML = a_add_astyle(returnHTML(a_answer));

	// set width/height ratio
	resize_dialog();
}

function a_hide() {
	document.getElementById('div_dialog').style.visibility = 'hidden';
}

function a_add_astyle(string) {
	string = (string.replace(/<a /g, '<a style="text-decoration:underline;font-weight:normal;color:#' + a_color.dark + ';hover:#' + a_color.light + '" '));
	return string;
}

function returnHTML(html) {
	var htmlNode = document.createElement("DIV");
	htmlNode.innerHTML = html;
	if(htmlNode.innerText) {
		return htmlNode.innerText; // Other
	} else {
		return htmlNode.textContent; // FF
	}
}

/* Resize dialog */

function resize_dialog() {

	// set width/height ratio
	document.getElementById('div_dialog_cc').style.width = (document.getElementById('div_dialog_cc').clientWidth + (document.getElementById('div_dialog_cc').clientHeight/3)) + 'px';

	// resize if needed
	while(document.getElementById('div_dialog_cc').clientHeight > 200) {
		document.getElementById('div_dialog_cc').style.width = (document.getElementById('div_dialog_cc').clientWidth+5) + 'px';
		document.getElementById('div_dialog').style.width = document.getElementById('div_dialog_cc').clientWidth + 'px';
	}

	// set dialog params
	document.getElementById('div_dialog').style.width = document.getElementById('div_dialog_cc').clientWidth + 'px';
	if (document.getElementById('div_dialog_cc').clientHeight < 60) {
		document.getElementById('div_dialog').style.height = '60px';
		document.getElementById('div_dialog_cc').style.top = (document.getElementById('div_dialog').clientHeight - document.getElementById('div_dialog_cc').clientHeight)/2 + 'px';
	} else {
		document.getElementById('div_dialog').style.height = document.getElementById('div_dialog_cc').clientHeight + 'px';
		document.getElementById('div_dialog_cc').style.top = '0px';
	}

	// move dialog
	document.getElementById('div_dialog').style.left = (20 - document.getElementById('div_dialog').clientWidth) + 'px';
	document.getElementById('div_dialog').style.top = (12 + (200-document.getElementById('div_dialog').clientHeight)*.4) + 'px';

	// snap the corners
	document.getElementById('div_dialog_ur').style.left = document.getElementById('div_dialog').clientWidth + 'px';
	document.getElementById('div_dialog_br').style.left = document.getElementById('div_dialog').clientWidth  + 'px';
	document.getElementById('div_dialog_br').style.top  = document.getElementById('div_dialog').clientHeight + 'px';
	document.getElementById('div_dialog_bl').style.top  = document.getElementById('div_dialog').clientHeight + 'px';

	// snap sides
	document.getElementById('div_dialog_uc').style.width  = document.getElementById('div_dialog').clientWidth  + 'px';
	document.getElementById('div_dialog_cr').style.left   = document.getElementById('div_dialog').clientWidth  + 'px';
	document.getElementById('div_dialog_cr').style.height = document.getElementById('div_dialog').clientHeight + 'px';
	document.getElementById('div_dialog_bc').style.top    = document.getElementById('div_dialog').clientHeight + 'px';
	document.getElementById('div_dialog_bc').style.width  = document.getElementById('div_dialog').clientWidth  + 'px';
	document.getElementById('div_dialog_cl').style.height = document.getElementById('div_dialog').clientHeight + 'px';

	// set pointer
	document.getElementById('div_dialog_p').style.left = (document.getElementById('div_dialog').clientWidth) + 'px';
	document.getElementById('div_dialog_p').style.top = (Math.round(document.getElementById('div_dialog').clientHeight*.66)) + 'px';

	// set close
	document.getElementById('div_dialog_close').style.left = '12px';
	document.getElementById('div_dialog_close').style.top = (document.getElementById('div_dialog').clientHeight - 22) + 'px';

	// show dialog
	document.getElementById('div_dialog').style.visibility = 'visible';
}

/* Show / hide question */

function show_question(text) {
	text = removeHTMLTags(text);
	if(text != '') {
		document.getElementById('div_text_content').innerHTML = text + '&nbsp;';
		document.getElementById('div_text').style.visibility = 'visible';
		document.getElementById('div_text_content').style.visibility = 'visible';
	} else {
		document.getElementById('div_text_content').innerHTML = '&nbsp;';
		document.getElementById('div_text').style.visibility = 'hidden';
		document.getElementById('div_text_content').style.visibility = 'hidden';
	}
	document.getElementById('div_text_content').style.top =  (267-document.getElementById('div_text_content').clientHeight) + 'px';
	document.getElementById('div_text').style.top =  (267-document.getElementById('div_text_content').clientHeight) + 'px';
	document.getElementById('div_text').style.height =  (document.getElementById('div_text_content').clientHeight) + 'px';

	document.getElementById('div_text_loader').style.visibility = 'visible';
	document.getElementById('div_text_loader').style.top =  (270-document.getElementById('div_text_content').clientHeight) + 'px';

}

function removeHTMLTags(text) {
	var strInputCode = text;

	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1) { return (p1 == 'lt')? '<' : '>'; });

	var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, '');

	return strTagStrippedText;
}

function hide_question() {
	if (document.getElementById('a_input').value != '') {
	document.getElementById('div_text').style.visibility = 'hidden';
	document.getElementById('div_text_content').style.visibility = 'hidden';
	//document.getElementById('div_text_loader').style.visibility = 'hidden';
	}
}

/* JSONrequest */

function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl;
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

JSONscriptRequest.scriptCounter = 1;

JSONscriptRequest.prototype.buildScriptTag = function () {

    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}

JSONscriptRequest.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);
}

JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}

/* Start the welcome */

a_welcome();












