var FADE_SPEED=5;//1-10

function getXML(url) {
 var xmldoc=null;
 if(window.XMLHttpRequest) {
  try {
   xmldoc=new XMLHttpRequest();
   xmldoc.open("GET",url,false);
   xmldoc.send("");
   return xmldoc.responseXML;
  } catch(e) {  }
 }
 if(document.implementation && document.implementation.createDocument) {
  xmldoc=document.implementation.createDocument("","",null);
 } else {
  xmldoc=new ActiveXObject("MSXML2.DOMDocument");
 }
 xmldoc.async=false;
 xmldoc.load(url);
 return xmldoc;
}
function setAlpha(obj,pct) {
 if(!obj) { return; }
 if(pct<0) { setAlpha(obj,0);return; }
 if(pct>100) { setAlpha(obj,100);return; }
 if(obj.filters) {
  if(obj.currentStyle) {
   if(obj.currentStyle.hasLayout!=null) {
    if(!obj.currentStyle.hasLayout) { obj.style.zoom=1; }
   }
  }
  if(obj.filters.alpha) {
   obj.filters.alpha.opacity = pct;
  } else {
   obj.style.filter = "alpha(opacity=" + pct + ")";
  }
 }
 obj.style.opacity = pct / 100;
}
function getAlpha(obj) {
 if(!obj) { return 100; }
 if(obj.filters && obj.filters.alpha && obj.filters.alpha.opacity) { return obj.filters.alpha.opacity; }
 if(obj.style.opacity) { return obj.style.opacity*100; }
 return 100;
}
function getRenderedStyle(oElm, strCssRule){
 var strValue = "";
 if(document.defaultView && document.defaultView.getComputedStyle){
  var compStyle = document.defaultView.getComputedStyle(oElm, "");
  if(compStyle != null) { strValue = compStyle.getPropertyValue(strCssRule); }
 } else if(oElm.currentStyle){
  strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
   return p1.toUpperCase();
  });
  strValue = oElm.currentStyle[strCssRule];
  if(strValue=="auto") {
   if(oElm.getAttribute("offset"+strCssRule)) { strValue=oElm.getAttribute("offset"+strCssRule); }
  }
 }
 return strValue;
}
function parseValue(val) {
 var v=parseFloat(val);
 if(isNaN(v)) { return 0; }
 return v;
}

var flippers=[];
var cont=[];

function Flipper() {
 return({'freq':null,'cont':[],'tmr':null,'elemID':null,'currInd':0});
}

function Flipper_Init(flipObj,srcFile,flipFrequency) {
 if(flipObj==null) { return; }
 if(flipFrequency==null) { flipFrequency=5000; }

 var flipInd=flippers.length;
 flippers[flipInd]=new Flipper();
 if(flipObj.id.length==0) { flipObj.id="flipper_"+Math.random(); }
 flippers[flipInd].elemID=flipObj.id;
 flippers[flipInd].freq=flipFrequency;

 var xmlDoc=getXML(srcFile);
 var template=null;
 var elm=xmlDoc.getElementsByTagName("template");
 if(elm.length) { template=elm[0].childNodes[0].nodeValue; }
 if(template!=null) {
  var elm=xmlDoc.getElementsByTagName("item");
  for(var i=0,li=elm.length;i<li;i++) {
   flippers[flipInd].cont[flippers[flipInd].cont.length]=template;
   while(flippers[flipInd].cont[flippers[flipInd].cont.length-1].indexOf("[@")!=-1) {
    var posA=flippers[flipInd].cont[flippers[flipInd].cont.length-1].indexOf("[@");
    var posB=flippers[flipInd].cont[flippers[flipInd].cont.length-1].indexOf("]",posA);
    var itm=elm[i].getElementsByTagName(flippers[flipInd].cont[flippers[flipInd].cont.length-1].substring(posA+2,posB));
    if(itm.length) {
     itm=itm[0].childNodes[0].nodeValue;
    } else {
     itm='';
    }
    flippers[flipInd].cont[flippers[flipInd].cont.length-1]=flippers[flipInd].cont[flippers[flipInd].cont.length-1].substring(0,posA)+itm+flippers[flipInd].cont[flippers[flipInd].cont.length-1].substring(posB+1,flippers[flipInd].cont[flippers[flipInd].cont.length-1].length)
   }
  }
  flippers[flipInd].cont.sort(function() { return (Math.round(Math.random())-0.5); });
  Flipper_Flip(flipInd);
 }
}
function Flipper_Flip(flipInd) {
 if(flipInd<flippers.length) {
  if(flippers[flipInd].tmr!=null) { clearTimeout(flippers[flipInd].tmr); }
  flippers[flipInd].currInd++;
  if(flippers[flipInd].currInd>=flippers[flipInd].cont.length) { flippers[flipInd].currInd=0; }
  var sObj=document.getElementById(flippers[flipInd].elemID);
  var newElm=sObj.cloneNode(false);
  newElm.innerHTML=flippers[flipInd].cont[flippers[flipInd].currInd];
  newElm.id="flipper_"+Math.random();
  sObj.style.border="1px solid transparent";
  newElm.style.border="1px solid transparent";
//  newElm.style.marginTop=("-"+(parseValue(getRenderedStyle(sObj, "height"))+parseValue(getRenderedStyle(sObj, "padding-top"))+parseValue(getRenderedStyle(sObj, "padding-bottom"))+parseValue(getRenderedStyle(sObj, "margin-top"))+parseValue(getRenderedStyle(sObj, "margin-bottom"))+parseValue(getRenderedStyle(sObj, "border-top-width"))+parseValue(getRenderedStyle(sObj, "border-bottom-width")))+"px");
  newElm.style.marginTop="-"+sObj.offsetHeight+"px";
  setAlpha(newElm, 0);
  setAlpha(sObj, 100);
  flippers[flipInd].fadeLvl=100;
  flippers[flipInd].fadeId=newElm.id
  for(var i=1;i<=10;i++) {
   setTimeout(function() { setAlpha(newElm,getAlpha(newElm)+10);setAlpha(sObj,getAlpha(sObj)-10); },(10-FADE_SPEED)*10*i);
  }
  setTimeout(function() { newElm.parentNode.removeChild(newElm);sObj.innerHTML=newElm.innerHTML;setAlpha(sObj,100); },(10-FADE_SPEED)*100+10);
  sObj.parentNode.appendChild(newElm,sObj);
  flippers[flipInd].tmr=setTimeout(function() { Flipper_Flip(flipInd); }, flippers[flipInd].freq);
 }
}
