
function apiFisgona(name,marco,url){this.name=name;this.ajaxURL=url;this.date=0;this.reload=2000;this.fromColorBack='86FF7F';this.toColorBack='FFFFFF';this.fromColorFore='000000';this.toColorFore='000000';this.fromColorBackOut='FF7F7F';this.toColorBackOut='FFFFFF';this.fromColorForeOut='000000';this.toColorForeOut='000000';this.timeFromTo=20;this.timeToFrom=20;this.limit=30;this.rangeColorBack=false;this.rangeColorFore=false;this.rangeColorBackOut=false;this.rangeColorForeOut=false
this.cache=new Object;this.numCache=0;this.dataPos=new Object;this.interval=false;this.resetRangeColor();this.marco=document.getElementById(marco);if(this.marco){this.rowTemplate=this.marco.childNodes[0].cloneNode(true);for(var x=0;x<this.rowTemplate.childNodes.length;x++){foo=this.rowTemplate.childNodes[x].getAttribute('rel');if(foo!=''){this.dataPos[foo]=x;}}}else{alert('No se ha podido cargar el molde de la fisgona');}}
apiFisgona.prototype={start:function(){if(this.numCache==0){while(this.marco.hasChildNodes()){this.marco.removeChild(this.marco.firstChild);}}
this.getData();this.interval=setInterval(this.name+'.getData()',this.reload);},stop:function(){clearInterval(this.interval);},getData:function(){var ajax=(window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();var obj=this;ajax.onreadystatechange=function(){if(ajax.readyState==4){if(ajax.status==200){obj.showData(eval('('+ajax.responseText+')'));}}};ajax.open("post",this.ajaxURL,true);ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");ajax.send('orderBy='+this.date);},showData:function(data){var newRow=false;var num=data.length;var randName=false;if(this.date!=0){var ini=0;}else{var ini=(this.timeFromTo-1);}
for(var x=0;x<num;x++){if(data[x]['orderBy']>=this.date){if(this.cache[data[x]['uniqueID']]){newRow=this.cache[data[x]['uniqueID']].cloneNode(true);randName=this.cache[data[x]['uniqueID']].id+'_'+data[x]['orderBy']+'_'+Math.random();this.cache[data[x]['uniqueID']].setAttribute('id',randName);this.cache[data[x]['uniqueID']]=newRow;this.iniFromTo(randName,0,true);}else{newRow=this.rowTemplate.cloneNode(true);newRow.setAttribute('id',data[x]['uniqueID']);this.cache[data[x]['uniqueID']]=newRow;this.numCache++;this.clearLast();}
this.date=data[x]['orderBy'];this.includeData(data[x]['uniqueID'],ini);delete data[x]['orderBy'];delete data[x]['uniqueID'];for(cual in data[x]){this.setData(newRow,cual,data[x][cual]);}}}},setData:function(obj,key,data){obj.childNodes[this.dataPos[key]].innerHTML=data;},includeData:function(cual,ini){if(this.marco.hasChildNodes()){this.marco.insertBefore(this.cache[cual],this.marco.firstChild);}else{this.marco.appendChild(this.cache[cual]);}
setTimeout(this.name+'.iniFromTo("'+cual+'", '+ini+')',0);},iniFromTo:function(key,pos,out){if(out!==true){out=false;}
if(out){var obj=document.getElementById(key);var colorBack=this.rangeColorBackOut;var colorFore=this.rangeColorForeOut;}else{var obj=this.cache[key];var colorBack=this.rangeColorBack;var colorFore=this.rangeColorFore;}
if(colorBack[pos]&&colorFore[pos]&&obj){obj.style.background='#'+colorBack[pos];obj.style.color='#'+colorFore[pos];pos++;setTimeout(this.name+'.iniFromTo("'+key+'", '+pos+', '+((out)?'true':'false')+')',50);}else if(out){if(obj){obj.parentNode.removeChild(obj);}else{this.clearLast(false);}}},clearLast:function(resta){if(resta!==false){resta=true;}
if(this.numCache>this.limit||resta==false){var obj=this.marco.lastChild;if(this.cache[obj.id]){delete this.cache[obj.id];}
this.marco.removeChild(obj);if(resta){this.numCache--;}}},setTime:function(time){this.reload=time;},setFromToBack:function(from,to){this.fromColorBack=from;this.toColorBack=to;this.resetRangeColor();},setFromToFore:function(from,to){this.fromColorFore=from;this.toColorFore=to;this.resetRangeColor();},setToFromBack:function(from,to){this.fromColorBackOut=from;this.toColorBackOut=to;this.resetRangeColor();},setToFromFore:function(from,to){this.fromColorForeOut=from;this.toColorForeOut=to;this.resetRangeColor();},setTimeFromTo:function(time){this.timeFromTo=time;this.resetRangeColor();},setTimeToFrom:function(time){this.timeToFrom=time;this.resetRangeColor();},setDate:function(date){this.date=date;},setLimit:function(limit){this.limit=limit;},resetRangeColor:function(){this.rangeColorBack=this.hexFromTo(this.timeFromTo,this.fromColorBack,this.toColorBack);this.rangeColorFore=this.hexFromTo(this.timeFromTo,this.fromColorFore,this.toColorFore);this.rangeColorBackOut=this.hexFromTo(this.timeToFrom,this.fromColorBackOut,this.toColorBackOut);this.rangeColorForeOut=this.hexFromTo(this.timeToFrom,this.fromColorForeOut,this.toColorForeOut);},hexFromTo:function(Num,Hex1,Hex2){var Partes1=this.hex2RGB(Hex1);var Partes2=this.hex2RGB(Hex2);var SumR=(Partes2[0]-Partes1[0])/(Num-1);var SumG=(Partes2[1]-Partes1[1])/(Num-1);var SumB=(Partes2[2]-Partes1[2])/(Num-1);var ArrayReturn=new Array();ArrayReturn[0]=Hex1;for(x=1;x<(Num-1);x++){Partes1[0]+=SumR;Partes1[1]+=SumG;Partes1[2]+=SumB;ArrayReturn[x]=this.RGB2Hex(Math.round(Partes1[0]),Math.round(Partes1[1]),Math.round(Partes1[2]));}
ArrayReturn[(Num-1)]=Hex2;return ArrayReturn;},hex2RGB:function(Hex){if(typeof(Hex)=='string'){Hex=parseInt(Hex,16);}
var R=Hex>>16;var G=(Hex&0x00FF00)>>8;var B=(Hex&0x0000FF);return[R,G,B];},RGB2Hex:function(R,G,B){return this.dec2Hex(R)+this.dec2Hex(G)+this.dec2Hex(B);},dec2Hex:function(Dec){var hexchars="0123456789ABCDEF";Dec=Dec||0;Dec=parseInt(Dec,10);if(isNaN(Dec))Dec=0;Dec=Math.round(Math.min(Math.max(0,Dec),255));return hexchars.charAt((Dec-Dec%16)/16)+hexchars.charAt(Dec%16);}}