/**
 *  author:		Giovanni Caprilli
 *	version:	0.1
 */


if (typeof(gis) == "undefined")
	_i = gis = {};


if (typeof(_i.inform) == "undefined") _i.inform = {};
else alert("inform is already set!");

_i.inform = function () {
	// no DOM - give up!
	if (!document.getElementById) {
		alert("fallito DOM");
		return 0;
	}
	
	this.myAjax = new _i.Ajax();
	//myAjax.makeRequest( url, meth, onSuccessFunc, onErrorFunc );
	//no AJAX - no party!
	if (this.myAjax.XHR==null) {
		alert("fallito XHR");
		return 0;
	}
	// init variables
	this.rID 	= 0;
	//if all OK assign action listeners to recipe icons
	this.assignListeners();
}

_i.inform.prototype.assignListeners = function () {
	elements = document.getElementsByTagName("img");
	var p = this;
	for (var i = 0; i < elements.length; i++) {
		if (1 != elements[i].nodeType) {
				continue;
		}
		if (/^ricetta_/.test(elements[i].id)) {
			elements[i].onmouseover = function(ev){ return p.itemOver(this); }; //this.itemOver;
			elements[i].onmouseout = function(ev){ return p.itemOut(); };
		}
	}
}

_i.inform.prototype.itemOver = function (img) {
	clearTimeout(this.toID);
	var pointer = this;
	this.toID = setTimeout(function () { pointer.createInfoBox(img) }, 1000);
	//this.createInfoBox(img);
	//this.createInfoBox(this.id);
}
_i.inform.prototype.itemOut = function () {
	clearTimeout(this.toID);
	this.clearInfoBox();
}

_i.inform.prototype.createInfoBox = function(img) {

	// get rid of old list
	// and clear the list removal timeout
	_i.DOM.remE("ib");
	this.killTimeout();
	
	// create holding div
	var div = _i.DOM.cE("div", {id:"ib", className:"info_box"}, "<div class=\"cx\"><br /><img src=\"./img/ajax-loader.gif\"/><br /><br />LOADING</div>",1);	
	
	// Posizionamento relativo all'oggetto di riferimento
	var pos = _i.DOM.getPos(img);
	div.style.left 		= (pos.x - 130) + "px";
	div.style.top 		= ( pos.y - 30) + "px";

	// add DIV to document
	document.getElementsByTagName("body")[0].appendChild(div);

	this.doAjaxRequest(img.id);
};


_i.inform.prototype.killTimeout = function()
{
	clearTimeout(this.toID);
};

_i.inform.prototype.resetTimeout = function()
{
	clearTimeout(this.toID);
	var pointer = this;
	this.toID = setTimeout(function () { pointer.clearInfoBox() }, 1000);
};

_i.inform.prototype.clearInfoBox = function () {
	//this.killTimeout();
	var ele = _i.DOM.gE("ib");
	var pointer = this;
	if (ele)
	{
		var fade = new _i.Fader(ele,1,0,250,function () { _i.DOM.remE("ib") });
		//_i.DOM.remE("ib");
	}
};

_i.inform.prototype.doAjaxRequest = function (img_id) {
	// check that saved input is still the value of the field
	var pointer = this;
	
	// create ajax request
	url="./ricette/info_ricetta.php?id="+img_id;
	
	var meth = "GET";
	
	var onSuccessFunc = function (req) { pointer.showResult(req) };
	var onErrorFunc = function (status) { alert("AJAX error: "+status); };

	
	this.myAjax.makeRequest( url, meth, onSuccessFunc, onErrorFunc );
};

_i.inform.prototype.showResult = function (req) {
	html = req.responseText;
	
	var ele = _i.DOM.gE("ib");
	if (ele) {
		ele.innerHTML = html;
	}
}

// AJAX PROTOTYPE _____________________________________________


if (typeof(_i.Ajax) == "undefined")
	_i.Ajax = {};

_i.Ajax = function () {
	this.XHR = this.getXMLHttpRequest();
	this.isIE = false;
};

_i.Ajax.prototype.getXMLHttpRequest = function () {
	var browserUtente = navigator.userAgent.toUpperCase(); // informazioni sul nome del browser
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
		XHR = new XMLHttpRequest();
	else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
		if(browserUtente.indexOf("MSIE 5") < 0) // la versione 6 di IE ha un nome differente,per l'oggetto ActiveX
			XHR = new ActiveXObject("Msxml2.XMLHTTP");
		else // le versioni 5 e 5.5 invece sfruttano lo stesso nome
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return XHR;
}

_i.Ajax.prototype.makeRequest = function (url, meth, onComp, onErr) {
	
	if (meth != "POST")
		meth = "GET";
	this.XHR = this.getXMLHttpRequest();
	this.onComplete = onComp;
	this.onError = onErr;
	
	var pointer = this;

	//if (this.XHR) {
		this.XHR.onreadystatechange = function () { pointer.processReqChange() };
		this.XHR.open(meth, url, true);
		this.XHR.send();
	//}
};


_i.Ajax.prototype.processReqChange = function() {
	
	// only if req shows "loaded"
	if (this.XHR.readyState == 4) {
		// only if "OK"
		if (this.XHR.status == 200)
		{
			this.onComplete( this.XHR );
		} else {
			this.onError( this.XHR.status );
		}
	}
};




// DOM PROTOTYPE _____________________________________________


if (typeof(_i.DOM) == "undefined")
	_i.DOM = {};



/* create element */
_i.DOM.cE = function ( type, attr, cont, html )
{
	var ne = document.createElement( type );
	if (!ne)
		return 0;
		
	for (var a in attr)
		ne[a] = attr[a];
	
	var t = typeof(cont);
	
	if (t == "string" && !html)
		ne.appendChild( document.createTextNode(cont) );
	else if (t == "string" && html)
		ne.innerHTML = cont;
	else if (t == "object")
		ne.appendChild( cont );

	return ne;
};



/* get element */
_i.DOM.gE = function ( e )
{
	var t=typeof(e);
	if (t == "undefined")
		return 0;
	else if (t == "string")
	{
		var re = document.getElementById( e );
		if (!re)
			return 0;
		else if (typeof(re.appendChild) != "undefined" )
			return re;
		else
			return 0;
	}
	else if (typeof(e.appendChild) != "undefined")
		return e;
	else
		return 0;
};



/* remove element */
_i.DOM.remE = function ( ele )
{
	var e = this.gE(ele);
	
	if (!e)
		return 0;
	else if (e.parentNode.removeChild(e))
		return true;
	else
		return 0;
};



/* get position */
_i.DOM.getPos = function ( e )
{
	var e = this.gE(e);

	var obj = e;

	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	
	var obj = e;
	
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;

	return {x:curleft, y:curtop};
};










// FADER PROTOTYPE _____________________________________________



if (typeof(_i.Fader) == "undefined")
	_i.Fader = {};





_i.Fader = function (ele, from, to, fadetime, callback)
{	
	if (!ele)
		return 0;
	
	this.e = ele;
	
	this.from = from;
	this.to = to;
	
	this.cb = callback;
	
	this.nDur = fadetime;
		
	this.nInt = 50;
	this.nTime = 0;
	
	var p = this;
	this.nID = setInterval(function() { p._fade() }, this.nInt);
};




_i.Fader.prototype._fade = function()
{
	this.nTime += this.nInt;
	
	var ieop = Math.round( this._tween(this.nTime, this.from, this.to, this.nDur) * 100 );
	var op = ieop / 100;
	
	if (this.e.filters) // internet explorer
	{
		try
		{
			this.e.filters.item("DXImageTransform.Microsoft.Alpha").opacity = ieop;
		} catch (e) { 
			// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
			this.e.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+ieop+')';
		}
	}
	else // other browsers
	{
		this.e.style.opacity = op;
	}
	
	
	if (this.nTime >= this.nDur)
	{
		clearInterval( this.nID );
		if (this.cb != undefined)
			this.cb();
	}
};



_i.Fader.prototype._tween = function(t,b,c,d)
{
	return b + ( (c-b) * (t/d) );
};
