String.prototype.get = function(p){
    return (match = this.match(new RegExp("[?|&]?" + p + "=([^&]*)"))) ? match[1] : false;
}


var outputdata;
var noentrytext="Es wurde kein Eintrag gefunden";
var showdetail=false;

function showpage(whichpage){
  ztncntrchilds=container.childNodes;
  for(var i=ztncntrchilds.length-1; i>-1; i--){
    container.removeChild(ztncntrchilds[i]);
  } 
  var json = transport.responseText.evalJSON(true);
  outputdata=json.data;  
  
  if (showdetail==false){

    var firstentry=(whichpage*entriesperpage-entriesperpage) < 0 ? 0 : whichpage*entriesperpage-entriesperpage;

    var lastentry=(whichpage*entriesperpage) > outputdata.length ? outputdata.length : whichpage*entriesperpage;
   
    for (var i=firstentry; i<lastentry; i++){
      if (typeof outputdata[i].tagsastext == 'undefined'){
        outputdata[i].tagsastext=outputdata[i].ntagname.join(', ');
        outputdata[i].position=i;
        outputdata[i].leftright= (i % 2 == 0) ? "left" : "right";
      }
      container.innerHTML+=theboxtemplate.evaluate(outputdata[i]);
    }
    ztncntrchilds=container.childNodes;
    for(var i=0; i<ztncntrchilds.length; i++){
      new Effect.Appear(ztncntrchilds[i], { queue: 'end', duration: 0.5 });
    }
    if (outputdata.length==0){
      container.innerHTML+=noentrytext;
    }
    if (typeof onlistcomplete == 'function') onlistcomplete();
    makepaginator(whichpage,entriesperpage,json.tot);
  }else{
    showdetails(showdetail);
  }

}

function zalert(thetext){
  if (window.location.search.get('debug')){
    alert(thetext);
  }
}

var transport;
function loadzpcdata(){
  zalert("daturl");
  new Ajax.Request(dataurl, {
    onSuccess: function(trsp) {
      transport=trsp;
      showpage(1);
    }
  });
}

var theboxtemplate="";
var theboxdetailtemplate="";
var templatefile="";
var detailtemplatefile="";
var requesttemplatefile="";
var container="";
var page=1;
var entriesperpage=1;
var onlistcomplete=false;
var ondetailcomplete=false;
function initzpcoffer(tpf,dtpf,dootpf,rtf,ctr,dtu,pg,epp,shd,olc,odc){  
  var paginatorelements=document.getElementsByClassName('zpccounter');
  $$('.zpccounter').each(Element.hide);
  $$('.zpccountertext').each(Element.hide);  
  if (typeof tpf == 'object'){  
    params=tpf;
    templatefile=params.templatefile;  
    detailtemplatefile=params.detailtemplatefile;  
    detailotherofferstemplatefile=params.detailotherofferstemplatefile;  
    requesttemplatefile=params.requesttemplatefile;
    container=params.container;
    dataurl=params.dataurl;
    page=(typeof params.page !='undefined') ? params.page : 1;
    if (typeof params.showdetail !='undefined'){
      showdetail=params.showdetail;
      
    }else if (window.location.search.get('showdetail')){
      showdetail=window.location.search.get('showdetail');
    }
    if (typeof params.onlistcomplete !='undefined'){
      onlistcomplete=params.onlistcomplete;    
    }
    if (typeof params.ondetailcomplete !='undefined'){
      ondetailcomplete=params.ondetailcomplete;    
    }
    entriesperpage=(typeof params.entriesperpage !='undefined') ? params.entriesperpage : 100;
  }else{
    templatefile=tpf;  
    detailtemplatefile=dtpf;  
    detailotherofferstemplatefile=dootpf;  
    requesttemplatefile=rtf;
    container=ctr;
    dataurl=dtu;
    page=pg;
    if (typeof shd !='undefined'){
      showdetail=shd;      
    }else if (window.location.search.get('showdetail')){
      showdetail=window.location.search.get('showdetail');
    }
    if (typeof olc !='undefined'){
      onlistcomplete=olc;    
    }
    if (typeof odc !='undefined'){
      ondetailcomplete=odc;    
    }
    entriesperpage=epp;
  }  
  if (window.location.search.get('rand')){
    if (window.location.search.get('rand')=='false') dataurl+='&rand=false';
  }
  //load template:
  new Ajax.Request(detailtemplatefile, {
    onSuccess: function(transport) {
      var transporttext=transport.responseText.replace( /[\n\r\t]/g ,'' );
      theboxdetailtemplate = new Template(transporttext);   
    }
  });
  new Ajax.Request(detailotherofferstemplatefile, {
    onSuccess: function(transport) {
      var transporttext=transport.responseText.replace( /[\n\r\t]/g ,'' );
      theboxdetailotherofferstemplate = new Template(transporttext);   
    }
  });
  new Ajax.Request(templatefile, {
    onSuccess: function(transport) {
      var transporttext=transport.responseText.replace( /[\n\r\t]/g ,'' );
       
      theboxtemplate = new Template(transporttext);  
      
      loadzpcdata();             
      zalert("initzpcoffer");
    }
  });
 
  
}
function zpcloadpage(tpg){
  //ztncntrchilds=container.childNodes;
  /*for(var i=0; i<ztncntrchilds.length; i++){
    new Effect.Fade(ztncntrchilds[i], { queue: 'end', duration: 0.5 });
  }*/
  loadzpcdata();
}

function makepaginator(pg,epp,tote){ //Aktuelle Seite, Einträge pro seite, Anzahl Einträge
  var paginatorcode='';
  var numberlinks=5;
  var firstpage=pg-2;
  if (firstpage<1) firstpage=1;
  var lastpage=pg+2;
  if (Math.ceil(tote/epp)>1){
    if (lastpage>Math.ceil(tote/epp)) lastpage=Math.ceil(tote/epp);
    if (firstpage>1) {
      paginatorcode+='<a href="#" onclick="showpage(1);return false;" class="zpcpaginatorlink"><span>|<</span></a>';
    }
    if (pg>1){
      paginatorcode+='<a href="#" onclick="showpage('+(pg-1)+');return false;" class="zpcpaginatorlink"><span><</span></a>';
    }
    for (var i=firstpage; i<=lastpage; i++){
      if (pg==i){
        paginatorcode+='<a href="#" onclick="return false;" class="zpcpaginatorlinkactive"><span>'+i+'</span></a>';
      }else{
        paginatorcode+='<a href="#" onclick="showpage('+i+');return false;" class="zpcpaginatorlink"><span>'+i+'</span></a>';
      }
    }
    
    if (pg<Math.ceil(tote/epp)){
      paginatorcode+='<a href="#" onclick="showpage('+(pg+1)+');return false;" class="zpcpaginatorlink"><span>></span></a>';
    }
    if (lastpage<Math.ceil(tote/epp)) {
      paginatorcode+='<a href="#" onclick="showpage('+Math.ceil(tote/epp)+');return false;" class="zpcpaginatorlink"><span>>|</span></a>';
    }
  }
  var paginatorelements=document.getElementsByClassName('zpcpaginator');
  for(var i=0; i<paginatorelements.length; i++){
    paginatorelements[i].innerHTML=paginatorcode;
  }
  var paginatorelements=document.getElementsByClassName('zpccounter');
  for(var i=0; i<paginatorelements.length; i++){
    paginatorelements[i].innerHTML=tote;
  }
  $$('.zpccounter').each(Element.show);
  $$('.zpccountertext').each(Element.show);
  
  
}

function showdetails(dsid){
  //ztncntrchilds=container.childNodes;
  /*for(var i=0; i<ztncntrchilds.length; i++){
    new Effect.Fade(ztncntrchilds[i], { queue: 'end', duration: 0.5 });
  }*/
  loaddetailsdata(dsid);
}
function closedetails(){
  showdetail=false;
  zpcloadpage(page);
}
var outputdetdata;
function loaddetailsdata(dsid){
  var paginatorelements=document.getElementsByClassName('zpcpaginator');
  for(var i=0; i<paginatorelements.length; i++){
    paginatorelements[i].innerHTML="";
  }
  new Ajax.Request(dataurl+'&details='+dsid, {
    onSuccess: function(transport) {
      ztncntrchilds=container.childNodes;
      for(var i=ztncntrchilds.length-1; i>-1; i--){
        container.removeChild(ztncntrchilds[i]);
      }
      
      var json = transport.responseText.evalJSON(true);
     
      outputdetdata=json.data;
      var arrpos=0;
      for (var i=0; i<outputdata.length; i++){
        if(outputdata[i].id==dsid){
          outputdetdata = Object.extend(outputdetdata, outputdata[i]);

        }
      }
      
      otheroffersdata=outputdetdata.otheroffers;
      otheroffershtml="";
      //theboxdetailotherofferstemplate
      for (var i=0; i<otheroffersdata.length; i++){
        otheroffershtml+=theboxdetailotherofferstemplate.evaluate(otheroffersdata[i]);
      }  
      outputdetdata.otheroffershtml=otheroffershtml;
      container.innerHTML+=theboxdetailtemplate.evaluate(outputdetdata);
      ztncntrchilds=container.childNodes;
      if (typeof ondetailcomplete == 'function') ondetailcomplete();
      for(var i=0; i<ztncntrchilds.length; i++){
        new Effect.Appear(ztncntrchilds[i], { queue: 'end', duration: 0.5 });
      }
      
    }
  });
}

function showrequest(dsid){
  loadrequesttemplate(dsid);
}
function closerequest(){
  zpcloadpage(page);
}
function loadrequesttemplate(dsid){
  new Ajax.Request(requesttemplatefile, {
    onSuccess: function(transport) {
      var transporttext=transport.responseText.replace( /[\n\r\t]/g ,'' );
      theboxrequesttemplate = new Template(transporttext);  
      
      ztncntrchilds=container.childNodes;
      for(var i=ztncntrchilds.length-1; i>-1; i--){
        container.removeChild(ztncntrchilds[i]);
      } 
      container.innerHTML+=theboxrequesttemplate.evaluate(outputdetdata);
      //container.innerHTML+=transport.responseText
      ztncntrchilds=container.childNodes;
      for(var i=0; i<ztncntrchilds.length; i++){
        new Effect.Appear(ztncntrchilds[i], { queue: 'end', duration: 0.5 });
      }
    }
  });

}

function sendform(){

  
$('requestform').request({
  method: 'GET',
  onSuccess: function(transport){ 
    alert('Form data saved!') 
  }
})
}

