function RepeaterControl(){
    
    this.HeaderTemplate = "";
    this.ItemTemplate = "";
    this.SelectedItemTemplate = "";
    this.FooterTemplate = "";
    this.AlternatingItemTemplate = "";
    this.SeparatorTemplate = "";
    this.HostControlID = null;
    
    this.DataSource = null;
}

RepeaterControl.prototype.DataBind = function(){
    var htmlString = "";
    htmlString += this.HeaderTemplate;
    
    
    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);
			}
		}
		
		// 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;
    
    $(this.HostControlID).innerHTML = htmlString;
}

// Pager Control Code

function PagerControl(pageSize,step){
    this.PageSize = pageSize;
    this.Step = step;
    this.PageCount = 0;
    this.CurrentPage = 1;
    this.HeaderTemplate = "<ul class=\"paging\">";
    this.ItemTemplate = "<li><a href=\"#\" onclick=\"{#reloadfunction}({#page}, event)\" >{#title}</a> </li>";
    this.SelectedItemTemplate = "<li><span>{#title} </span></li>";
    this.FooterTemplate = "</ul>";
    this.HostControlID = null;
}

PagerControl.prototype.RenderPager = function(reloadFunction){
    var result = "";
    
    if(this.PageCount > 1){
        result += this.HeaderTemplate;
        var startPoint = Math.floor((this.CurrentPage/this.Step)) * this.Step;
        if((this.CurrentPage % this.Step) == 0) {
            startPoint -= this.Step;
        }
        
        if(startPoint >= this.Step){
            result += this.ItemTemplate.replace("{#page}",startPoint).replace("{#title}","&laquo; Previous ").replace("{#reloadfunction}",reloadFunction);
        }
        
        for(var i=startPoint +1;i<=this.PageCount && i<=(startPoint + this.Step);i++){
            if(i != this.CurrentPage){
                result += this.ItemTemplate.replace("{#page}",i).replace("{#title}",i).replace("{#reloadfunction}",reloadFunction);
            }else{
                result += this.SelectedItemTemplate.replace("{#title}",i);
            }
        }
		
//		if (startPoint < (this.PageCount - this.Step)) result += this.ItemTemplate.replace("{#page}",(parseInt(this.CurrentPage, 10)+1)).replace("{#title}","Next &rsaquo;").replace("{#reloadfunction}",reloadFunction);
        
        if(startPoint < (this.PageCount - this.Step)){
            result += this.ItemTemplate.replace("{#page}",(startPoint + this.Step +1)).replace("{#title}","Last &raquo;").replace("{#reloadfunction}",reloadFunction);
        }
        result += this.FooterTemplate;
    }
    
    $(this.HostControlID).innerHTML = result;
}

// Search Code
 PINT.Search = {  
        dataReceived : function(request){
            // bind repeater.
            repeaterControl.DataSource = request.responseXML.documentElement.getElementsByTagName('ContentItem');
            repeaterControl.DataBind();
            
            // bind pager.
            var pagerinfo = request.responseXML.documentElement.getElementsByTagName('PagerData');
            pagerControl.CurrentPage = pagerinfo[0].getAttribute('CurrentPage');
            pagerControl.PageCount = pagerinfo[0].getAttribute('PageCount');
            pagerControl.RenderPager("PINT.List.loadData")
            
            document.getElementById("total").innerHTML ='We found '+ pagerinfo[0].getAttribute('TotalCount') + ' ';
            
            if(pagerinfo[0].getAttribute('PageCount') != 0)
            {
                document.getElementById("countTop").innerHTML = "Page " + pagerinfo[0].getAttribute('CurrentPage') + " of " + pagerinfo[0].getAttribute('PageCount');
                document.getElementById("countBottom").innerHTML = "Page " + pagerinfo[0].getAttribute('CurrentPage') + " of " + pagerinfo[0].getAttribute('PageCount');
                document.getElementById("PagingBottom").innerHTML = document.getElementById("PagingTop").innerHTML;
            }
            PINT.widget.lightbox.panelLoading.hide();
        },

        reportError : function(t) {
             alert('Error ' + t.status + ' -- ' + t.statusText);
        },
        
        creativeDataReceived : function(request){
            var rpt =request.request.options['control'];
            var style =request.request.options['style'];
            var off =request.request.options['off'];
            
            // create and bind repeater
            var repeaterControl = new RepeaterControl();
            repeaterControl.ItemTemplate = style;
            repeaterControl.HostControlID = rpt;
            repeaterControl.DataSource = request.responseXML.documentElement.getElementsByTagName('ContentItem');
            repeaterControl.DataBind();
            
            if(off == "true")
            {
                PINT.widget.lightbox.panelLoading.hide();
            }
        }
        
}
