/*
 *
 * Speech bubble razred
 *
 */

var speech_bubble = {
	xCord : 0,
	yCord : 0,
	bubble_data : '',
	timer_data : 0,
	div : Object,
	id_shown : '',
	id_over : '',
	
	/** Inicijalizacijska funkcija **/
	init : function () {
		if ( !document.getElementById ||
			!document.createElement ||
			!document.getElementsByTagName ) {
			return;
		}
		
		this.div = document.createElement('div');
		this.div.id = 'pojam_opis_wnd';
		document.getElementsByTagName('body')[0].appendChild(this.div);
		this.div.className = 'pojam_opis';
		this.div.style.width = '350px';
		this.div.style.height = '100px';
		addEvent(this.div, 'mouseover', function clear () {speech_bubble.on_over(e, speech_bubble.id_shown);});
		addEvent(this.div, 'mouseout', this.on_out);
	},
	
	/** Osvjezavanje koordinata **/
	update_coords : function (e) {
		getMouseXY(e, speech_bubble.div.style.width, speech_bubble.div.style.height);
		this.xCord = tempX;
		this.yCord = tempY;
	},
	
	/** Handleri za onmouseover i onmouseout eventove **/
	on_over : function (e, div_id) {
		if (div_id == this.id_shown) {
			clearTimeout(this.timer_data);
			getMouseXY(e, speech_bubble.div.style.width, speech_bubble.div.style.height);
			if (Math.abs(tempX - parseInt(this.div.style.left)) > 40 || 
				Math.abs(tempY - parseInt(this.div.style.top)) > 40) {

				if (browser == 'FF') {
					speech_bubble.update_coords(e);
					this.timer_data = setTimeout("speech_bubble.display('" + div_id + "')", 100);
				}
			}
		}
		else {
			speech_bubble.update_coords(e);
			speech_bubble.get_data(div_id);
			speech_bubble.div.innerHTML = '';
			this.timer_data = setTimeout("speech_bubble.display('" + div_id + "')", 100);
		}
		//this.id_over = div_id;
	},

	on_out : function () {
		this.timer_data = setTimeout("speech_bubble.hide()", 100);
	},
	
	/** AJAX temeljena funkcija za dohvat opisa pojmova **/
	get_data : function (div_id) {
		xmlHttp = GetXmlHttpObject();
		if (xmlHttp == null) {
			alert ("Vaš preglednik ne podržava AJAX!");
			return;
		}
		// funkcija koja obrađuje povratne podatke
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					var dekodirano = decode_hr_chars(xmlHttp.responseText);
					// ispis sadrzaja
					speech_bubble.div.innerHTML = dekodirano;
				}
			}
		}

		var url = "include/a_speech_bubble.asp?id=" + div_id;

		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	},
	
	/** Funkcija za prikaz podataka **/
	display : function (div_id) {
		var scrX = Number(speech_bubble.xCord);
		var scrY = Number(speech_bubble.yCord);
		var tp = parseInt(scrY - 50);
		var lt = parseInt(scrX + 20);

		this.div.style.left = lt + 'px';
		this.div.style.top = tp + 'px';

		this.id_shown = div_id;
		this.div.style.display = 'block';
	},
	
	/** Funkcija za skrivanje podataka **/
	hide : function () {
		if (String(this.id_over) == this.id_shown + '_lnk') {
			clearTimeout(this.timer_data);
			return;
		}
		clearTimeout(this.timer_data);
		this.div.style.display = 'none';
		this.id_shown = '';
		this.id_over = '';
	}
};

function s_bubble_loader() {
	speech_bubble.init();
}
addEvent(window, 'load', s_bubble_loader);