/**
 *  author:		Gerard http://www.photocyprus.com/
 *	version:	1.1 - 2008-12-28
 *
 */


if (typeof(avgo) == "undefined")
	_a = avgo = {};

if (typeof(_a.VoteAjax) == "undefined")
	_a.VoteAjax = {};
//else
//	alert("VoteAjax is already set!");

//alert("VoteAjax is set.");

_a.VoteAjax = function (id1,id2,id3,id4,id5, param){
	//alert("VoteAjax.");
//}
//_a.VoteAjaxOLD = function (id1,id2,id3,id4,id5, param){
	// no DOM - give up!
	//
	if (!document.getElementById)
		return 0;
	var p = this;
	// get field via DOM
	//
	this.btn1 = _a.DOM.gE(id1);
	if (this.btn1){
		this.btn1.onclick 	= function(ev){ return p.doAjaxRequest(1); };
	}
	this.btn2 = _a.DOM.gE(id2);
	if (this.btn2){
		this.btn2.onclick 	= function(ev){ return p.doAjaxRequest(2); };
	}
	this.btn3 = _a.DOM.gE(id3);
	if (this.btn3){
		this.btn3.onclick 	= function(ev){ return p.doAjaxRequest(3); };
	}
	this.btn4 = _a.DOM.gE(id4);
	if (this.btn4){
		this.btn4.onclick 	= function(ev){ return p.doAjaxRequest(4); };
	}
	this.btn5 = _a.DOM.gE(id5);
	if (this.btn5){
		this.btn5.onclick 	= function(ev){ return p.doAjaxRequest(5); };
	}
	// parameters object
	//
	this.oP = param ? param : {};
	// defaults	
	//
	var k, def = {
			meth:"get", 
			responseField:"responseField",
			className:"ajax"
		};
	for (k in def){
		if (typeof(this.oP[k]) != typeof(def[k]))
			this.oP[k] = def[k];
	}
	this.responseField = _a.DOM.gE(this.oP["responseField"]);
	if (!this.responseField){
		alert("no response field '"+this.oP["responseField"]+"'");
		return 0;
	}
	this.buttonsDiv = _a.DOM.gE(this.oP["buttonsDiv"]);
	if (!this.buttonsDiv){
		alert("no buttonsDiv '"+this.oP["buttonsDiv"]+"'");
		return 0;
	}
};

//_a.VoteAjax.prototype.setAjax = function (val){
//	var pointer = this;
//	pointer.doAjaxRequest(val) ;
//	alert("setAjax return false");
//	return false;
//};





_a.VoteAjax.prototype.doAjaxRequest = function (input){
	//alert("doAjaxRequest");
	var pointer = this;
	//
	// create ajax request
	//
	if (typeof(this.oP.script) == "function")
		var url = this.oP.script(encodeURIComponent(input))+"&toto=1234";
	else
		var url = this.oP.script+encodeURIComponent(input)+"&toto=1234";
	if (!url)
		return false;
	//alert("url="+url);
	var meth = this.oP.meth;
	var onSuccessFunc = function (req) { pointer.setResponse(req) };
	var onErrorFunc = function (status) { alert("AJAX error: "+status); };
	var myAjax = new _a.Ajax();
	myAjax.makeRequest( url, meth, onSuccessFunc, onErrorFunc );
	return false;
};

_a.VoteAjax.prototype.setResponse = function (req){
	//alert("setResponse");
	try{
		if (this.oP.json){
			var jsondata = eval('(' + req.responseText + ')');
			var results = jsondata.results[0];
			// traverse json
			//alert("jsondata="+jsondata.results[0].response);
			this.responseField.innerHTML=results.response;
			this.responseField.style.display = "block";
			this.buttonsDiv.style.display = "none";
//			alert("done");
		} else {
			var xml = req.responseXML;
			// traverse xml
			var results = xml.getElementsByTagName('results')[0].childNodes;
			this.responseField.innerHTML=results[0].response;
			this.responseField.style.display = "block";
			this.buttonsDiv.style.display = "none";
		}
		if (typeof(this.oP.callback) == "function"){
//			alert("will callback="+this.oP.callback);
			this.oP.callback( results[0].response );
		}
	}catch(err){}
};


// AJAX PROTOTYPE _____________________________________________


if (typeof(_a.Ajax) == "undefined")
	_a.Ajax = {};
_a.Ajax = function (){
	this.req = {};
	this.isIE = false;
};
_a.Ajax.prototype.makeRequest = function (url, meth, onComp, onErr){
	if (meth != "POST")
		meth = "GET";
	this.onComplete = onComp;
	this.onError = onErr;
	var pointer = this;
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest)	{
		this.req = new XMLHttpRequest();
		this.req.onreadystatechange = function () { pointer.processReqChange() };
		this.req.open("GET", url, true); //
		this.req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
		if (this.req)
		{
			this.req.onreadystatechange = function () { pointer.processReqChange() };
			this.req.open(meth, url, true);
			this.req.send();
		}
	}
};

_a.Ajax.prototype.processReqChange = function(){
	
	// only if req shows "loaded"
	if (this.req.readyState == 4) {
		// only if "OK"
		if (this.req.status == 200)
		{
			this.onComplete( this.req );
		} else {
			this.onError( this.req.status );
		}
	}
};


// DOM PROTOTYPE _____________________________________________
if (typeof(_a.DOM) == "undefined")
	_a.DOM = {};
/* create element */
_a.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 */
_a.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 */
_a.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 */
_a.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(_a.Fader) == "undefined")
	_a.Fader = {};





_a.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);
};




_a.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();
	}
};



_a.Fader.prototype._tween = function(t,b,c,d)
{
	return b + ( (c-b) * (t/d) );
};
