/*---------------------------------------------------  
 *  RepeterControl for AJAX
 *  (c) 2005 Jigar Desai <desaijm@hotmail.com>
 *  Date Change: 12/31/2005
 *  requires: prototype.js http://prototype.conio.net/
/*--------------------------------------------------*/

function RepeaterControl(){
    
    this.HeaderTemplate = "";
    this.ItemTemplate = "";
    this.SelectedItemTemplate = "";
    this.FooterTemplate = "";
    this.AlternatingItemTemplate = "";
    this.SeparatorTemplate = "";
    this.HostControlID = null;
    
    this.DataSource = null;
    this.speed = 5000;
    this.name = "";
    this.timer = 0;
    
}

RepeaterControl.prototype.DataBind = function(){
    var htmlString = "";
    htmlString += this.HeaderTemplate;
    
    //debugger;
    for(var count=0;count<this.DataSource.length;count++){
        var node = this.DataSource[count];
        
        var row = this.ItemTemplate
        
        if(this.AlternatingItemTemplate != "" && (count+1)% 2 == 0 ){
            row = this.AlternatingItemTemplate;
        }
       
        for (var i = 0; i < node.attributes.length; i++){
        
			var re = new RegExp("{@" + node.attributes[i].nodeName + "}","g");
			row = row.replace(re,node.attributes[i].nodeValue);
		}
		
		for (var i = 0; i < node.childNodes.length; i++){
			
		    if(node.nodeType != 3 && node.childNodes[i].firstChild != null){
			    var re = new RegExp("{\\$" + node.childNodes[i].nodeName + "}","g");
			    row = row.replace(re,node.childNodes[i].firstChild.nodeValue);
			    
			}
			else
				if(node.nodeType != 3 ){
			    var re = new RegExp("{\\$" + node.childNodes[i].nodeName + "}","g");
			    row = row.replace(re,"");
			    
			}
		}
		
		// eval javascript
		
		evalRe = new RegExp("{Eval(.*?)}","g");
		
		myArray = row.match(evalRe);
		
		if(myArray != null){
		    for(var i=0;i<myArray.length;i++){
		    
		        var result = eval(myArray[i].replace(evalRe,"$1"));
		        row = row.replace(myArray[i],result);
		    }
		}
         
        htmlString += row;
        if(this.SeparatorTemplate != "" && count + 1 != this.DataSource.length){
            htmlString += this.SeparatorTemplate;
        }
        
        
    }
    
    htmlString += this.FooterTemplate;
    htmlString=htmlString.replace(/<li class='fuenteNormal'><\/li/gi,"<\/li");
    $(this.HostControlID).innerHTML = htmlString;
    try {
    
		el('tt').value = htmlString;
	}catch(x) { };
}

function RepeaterControlShow_play()       
{
  with(this)
  {
  
     clearTimeout(timer);
    timer = setTimeout(name+'.play()', speed);
	DataBindUnoAleatorio();
  }
}

RepeaterControl.prototype.play = RepeaterControlShow_play;

RepeaterControl.prototype.DataBindUnoAleatorio = function(){
    var htmlString = "";
    htmlString += this.HeaderTemplate;
    //debugger;
    count=Math.floor(Math.random()*this.DataSource.length);
    
        var node = this.DataSource[count];
        
        var row = this.ItemTemplate
        
        if(this.AlternatingItemTemplate != "" && (count+1)% 2 == 0 ){
            row = this.AlternatingItemTemplate;
        }
       
        for (var i = 0; i < node.attributes.length; i++){
        
			var re = new RegExp("{@" + node.attributes[i].nodeName + "}","g");
			row = row.replace(re,node.attributes[i].nodeValue);
		}
		
		for (var i = 0; i < node.childNodes.length; i++){
			
		    if(node.nodeType != 3 && node.childNodes[i].firstChild != null){
			    var re = new RegExp("{\\$" + node.childNodes[i].nodeName + "}","g");
			    row = row.replace(re,node.childNodes[i].firstChild.nodeValue);
			    
			}
			else
				if(node.nodeType != 3 ){
			    var re = new RegExp("{\\$" + node.childNodes[i].nodeName + "}","g");
			    row = row.replace(re,"");
			    
			}
		}
		
		// eval javascript
		
		evalRe = new RegExp("{Eval(.*?)}","g");
		
		myArray = row.match(evalRe);
		
		if(myArray != null){
		    for(var i=0;i<myArray.length;i++){
		    
		        var result = eval(myArray[i].replace(evalRe,"$1"));
		        row = row.replace(myArray[i],result);
		    }
		}
         
        htmlString += row;
        if(this.SeparatorTemplate != "" && count + 1 != this.DataSource.length){
            htmlString += this.SeparatorTemplate;
        }
        
    htmlString += this.FooterTemplate;
    
    htmlString=htmlString.replace(/<li class='fuenteNormal'><\/li/gi,"<\/li");
    
    $(this.HostControlID).innerHTML = htmlString;
    try {
    
		el('tt').value = htmlString;
	}catch(x) { };
}
