//------------------------------------------------- Peiling ----------------------------------------------------------------------
function voted(id) {   
  cookarr = document.cookie.split(';');
  for (x=0;x<cookarr.length;x++) {
    c = cookarr[x].split('=');
      if (c.length >= 2) {
      try {
        nm = strip(c[0]);
        nm2 = nm.split("");
        if (nm == id) 
          return true;
      } catch(e) {alert(e);}
    }
  }
  return false;
}

 
// ------------------------------------------------ AJAX maillist subscription ---------------------------------------------------
send_maillist_subscription = function () {
  intraxxion.submitform('newsletter.htm/action',getElement('digibrief'), maillist_subscription);
};
maillist_subscription = function (data) {
  resp = JSON.parse(data.responseText);
  maillistMessage(resp.message);
};
maillistMessage = function(msg) {         
  cmnew = DIV({'id':'digibrief_message','style':'padding-bottom:4px;display:block'}, SPAN(null, msg));
  swapDOM('digibrief_message', cmnew);
  callLater(7, hideMaillistMessage );
};
hideMaillistMessage = function() {
  getElement('digibrief_message').style.display = 'none';
};

// ------------------------------------------------ AJAX spotlight search --------------------------------------------------------
var old = '';

dolivesearch = function () {
  res = $('spotlightresults');
  qry = $('spotlightquery').value;
  if (qry != old) {
    if (qry == '' || qry == 'Zoeken binnen deze site...') {
      res.innerHTML = '';
      res.style.display = 'none';
    } else
      intraxxion.submitform('DesignerTemplates/spotlightresults.html', $('livesearch'), displayresults);
  }
  old = qry;
  setTimeout('dolivesearch()', 700) ;
};
initsearch =function () {
  dolivesearch();
};

displayresults = function (data) {
  $('spotlightresults').style.display = 'block';
  $('spotlightresults').innerHTML = data.responseText;
};

// ------------------------------------------------ Page initialization --------------------------------------------------
connect(window, 'onload', 
  function(e) {
      animation  = new ImageAnimation('json_fetch_animation_images', $('fotodia'), {'height':'60','width':'480'});
  }
);
      
var vandaag = new Date();

connect(window, 'documentReady',
    function(evt) {
        
        try {
            initsearch();
        } catch(e) {
            ;
        }
        //------- Kalender --------
        if ($('calendar')) {
            var maand, jaar
            var dag = vandaag.getDate();
            
            if (location.search === "") {
                maand = vandaag.getMonth()+1;
                jaar = vandaag.getFullYear();
            } else {
                jaar = location.search.substring(6,10);
                maand = location.search.substring(17,19);
            }
            
          connect($('calprev'),'onclick', function(e) {
            el = e.src().parentNode.id.split(',');
            location.href = "?year="+el[0]+"&month="+el[1]            
          });
          connect($('calnext'),'onclick', function(e) {
              el = e.src().parentNode.id.split(',');
              location.href = "?year="+el[0]+"&month="+el[1]          
          });
          if (parseInt(maand,10) == parseInt(vandaag.getMonth())+1) {
              forEach($$('.calweday').concat($$('.calday')), setVandaag);
          }
          d = loadJSONDoc('fetch_evenementen_maand?year='+jaar+'&month='+maand+'&day=1');
          d.addCallbacks(
            function(data) {
                forEach(data, setEvenement);
            }, 
            function(err) {
                log(err);
            }
          );
        }
    }
);

// ------------------------------------------------ Kalender --------------------------------------------------------
function setVandaag(o) {
    
   if ( parseInt(vandaag.getDate(),10) == parseInt(scrapeText(o),10)) {
     addElementClass(o,'current');              
   }
}

function setEvenement(d) {
    var maand, jaar;
    if (location.search === "") {
        maand = vandaag.getMonth()+1;
        jaar = vandaag.getFullYear();
    } else {
        jaar = location.search.substring(6,10);
        maand = location.search.substring(17,19);
    }
    forEach($$('.calweday').concat($$('.calday')), 
        function(ob) {
            if (parseInt(d,10) == parseInt(scrapeText(ob),10)) {
                addElementClass(ob,'event');
                connect(ob,'onclick', function(e) { 
                    toon_dag_agenda(jaar, maand, scrapeText(ob))
                }
            );
            }
        }
    );
}

function toon_dag_agenda(jaar,maand,dag) {
    d = doSimpleXMLHttpRequest("DesignerTemplates/days_events.html?datum="+jaar+'-'+maand+'-'+dag);
    d.addCallbacks(
        function(data) {
          $('maincontent').innerHTML = data.responseText;
        }, 
        function(e) {
            log(e.message);
        }
    );
}

function opfris() {
    loc = document.URL.split("?");
    location.href = loc[0]
}
// ------------------------------------------------ ImageAnimationBuffer --------------------------------------------------------

var imageBuffer = null;
var imageindex = 0;
var lastimage = 0;

ImageAnimationBuffer = function(url,ob) {
  bindMethods(this);
  this.images = []
  this.url = url;       
  this.complete = false;
};

ImageAnimationBuffer.prototype = {
  init: function() {
    if (!this.complete)
      this.load();
  },
  load: function(optionsurl) {
    d = loadJSONDoc(this.url);
    d.addCallbacks(this.loadReady, this.loadFailed);    
  },
  loadReady: function(data) { 
    for (i=0; i < data.length;i++) {
      my_img = new Image()
      my_img.src = data[i];  
      this.images.push(my_img);
    }
    lastimage = data.length-1;
    imageindex = 0;
    this.complete = true;
    //if (document.all)
      //this.ob.bufferComplete();
    //else
    //alert('SBC');
    //signal(this, 'onBufferComplete');
    this.ob.bufferComplete();
  },
  loadFailed: function(err) {
    log(err);
  },
  getImages: function() {
    return this.images;
  }
}

ImageAnimation = function(url, element, size) {
  bindMethods(this);
  this.element = element;
  this.size = size;
  this.active1 = 'img1_2';
  this.active2 = 'img2_2';
  this.active3 = 'img3_2';
  this.active4 = 'img4_2';
  this.idx = 0;
  bufferCompleted = false;
  this.init(url);
};
ImageAnimation.prototype = {
  init: function(url) {
    this.imageBuffer = new ImageAnimationBuffer(url);
    this.imageBuffer.ob = this;
    this.imageBuffer.init();
    //connect(this.imageBuffer, 'onBufferComplete', this, this.bufferComplete)
  },
  bufferComplete: function() {
    bufferCompleted = true;
    //alert('BC');
    //this.element.style.width=this.size.width+'px';
    //this.element.style.height=this.size.height+'px';
    this.element.style.display='block';
    //overlay = DIV({'id':'overlay','style':'width:'+this.size.width+'px;height:22px;background:#387262;position:absolute;left:0;z-index:10'});
    //logo = DIV({'id':'overlay','style':'width:275px;height:69px;background:transparent url(DesignerImages/Veldeke_logo_fc.gif) no-repeat;position:absolute;left:8px;top:40px;z-index:10'});
    img1_1 = DIV({'id':'img1_1','style':'width:120px;height:60px;display:block;position:absolute;left:0'});
    img1_2 = DIV({'id':'img1_2','style':'width:120px;height:60px;display:none;position:absolute;left:0;'});
    
    img2_1 = DIV({'id':'img2_1','style':'width:120px;height:60px;display:block;position:absolute;left:120px'});
    img2_2 = DIV({'id':'img2_2','style':'width:120px;height:60px;display:none;position:absolute;left:120px'});
    
    img3_1 = DIV({'id':'img3_1','style':'width:120px;height:60px;display:block;position:absolute;left:240px'});
    img3_2 = DIV({'id':'img3_2','style':'width:120px;height:60px;display:none;position:absolute;left:240px'});
    
    img4_1 = DIV({'id':'img4_1','style':'width:120px;height:60px;display:block;position:absolute;left:360px'});
    img4_2 = DIV({'id':'img4_2','style':'width:120px;height:60px;display:none;position:absolute;left:360px'});

    connect(this.element,'onclick',
      function(e) {
        location.href='home.htm';
      }
    );
    appendChildNodes(this.element,
      //overlay, logo,
      img1_1, img1_2,
      img2_1, img2_2,
      img3_1, img3_2,
      img4_1, img4_2); 
    
    // Set initial images  
    this.idx = Math.floor(Math.random() * this.imageBuffer.images.length);
    img1_1.style.background = 'url('+this.imageBuffer.images[this.idx].src+') no-repeat';
    this.idx = Math.floor(Math.random() * this.imageBuffer.images.length);
    img2_1.style.background = 'url('+this.imageBuffer.images[this.idx].src+') no-repeat';
    this.idx = Math.floor(Math.random() * this.imageBuffer.images.length);
    img3_1.style.background = 'url('+this.imageBuffer.images[this.idx].src+') no-repeat';
    this.idx = Math.floor(Math.random() * this.imageBuffer.images.length);
    img4_1.style.background = 'url('+this.imageBuffer.images[this.idx].src+') no-repeat';
    this.active1 = 'img1_1';
    this.active2 = 'img2_1';
    this.active3 = 'img3_1';
    this.active4 = 'img4_1';
    //setOpacity(overlay,0.90);
    
    callLater(2.5, this.swap_image_1);   
  },
  swap_image_1: function() {
    this.idx = Math.floor(Math.random() * this.imageBuffer.images.length);
    img = this.imageBuffer.images[this.idx];
    fade($(this.active1),{'duration':1.5});
    if (this.active1 == 'img1_1') {
      this.active1 = 'img1_2';
    } else {
      this.active1 = 'img1_1';
    }
    $(this.active1).style.background = 'url('+img.src+') no-repeat';
    appear($(this.active1),{'duration':1.5});    
    callLater(2.5, this.swap_image_2); 
  },
  swap_image_2: function() {
    this.idx = Math.floor(Math.random() * this.imageBuffer.images.length);                              
    img = this.imageBuffer.images[this.idx];
    fade($(this.active2),{'duration':1.5});
    if (this.active2 == 'img2_1') {
      this.active2 = 'img2_2';
    } else {
      this.active2 = 'img2_1';
    }
    $(this.active2).style.background = 'url('+img.src+') no-repeat';
    appear($(this.active2),{'duration':1.5});    
    callLater(2.5, this.swap_image_3); 
  },
  swap_image_3: function() {
    this.idx = Math.floor(Math.random() * this.imageBuffer.images.length);                              
    img = this.imageBuffer.images[this.idx];
    fade($(this.active3),{'duration':1.5});
    if (this.active3 == 'img3_1') {
      this.active3 = 'img3_2';
    } else {
      this.active3 = 'img3_1';
    }
    $(this.active3).style.background = 'url('+img.src+') no-repeat';
    appear($(this.active3),{'duration':1.5});    
    callLater(2.5, this.swap_image_4); 
  },
  swap_image_4: function() {
    this.idx = Math.floor(Math.random() * this.imageBuffer.images.length);                              
    img = this.imageBuffer.images[this.idx];
    fade($(this.active4),{'duration':1.5});
    if (this.active4 == 'img4_1') {
      this.active4 = 'img4_2';
    } else {
      this.active4 = 'img4_1';
    }
    $(this.active4).style.background = 'url('+img.src+') no-repeat';
    appear($(this.active4),{'duration':1.5});    
    callLater(2.5, this.swap_image_1); 
  }  
}





// ------------------------------------------------ OUWE LEEM ------------------------------------------------

//function show_photo(url) {
///*
//  newwindow = window.open(url,'name','height=600,width=800,top=0,left=0');
//  if (window.focus) {
//    newwindow.focus()
//  }
//*/
//  window.location.href=url;
//}
//
//function get_page(art_no)
//{
//  my_form = document.getElementById('myfrm');
//  document.getElementById('start').value=parseInt(art_no);
//  my_form.submit();
//}
//    
//    numericorder = function(a, b) { 
//      return a -b; }
//
//function ch2() {
//  var ch = 0;
//  var cw = 0;
//    thecontent = document.getElementById('content');
//    thetop = document.getElementById('top');
//    if (self.innerHeight) // all except Explorer
//    {
//    cw = self.innerWidth;
//    ch = self.innerHeight;
//    }
//    else if (document.documentElement && document.documentElement.clientHeight)
//   // Explorer 6 Strict Mode
//    {
//    cw = document.documentElement.clientWidth;
//    ch = document.documentElement.clientHeight;
//    }
//    else if (document.body) // other Explorers
//    {
//    cw = document.body.clientWidth;
//      ch = document.body.clientHeight;
//  } 
//  newheight = ch - thetop.offsetHeight - document.getElementById('footer').offsetHeight +20 ;
//  if (document.all)
//    newheight = parseInt(newheight) - 10;
//  
//    h_height = hilites_height() ;
//  //alert(h_height+' : '+ thecontent.offsetHeight); 
//
//  if (h_height > 0) {
//    if (thecontent.offsetHeight <= h_height) {
//      if (document.all) {
//        thecontent.style.height = h_height - 50 + 'px';
//       // alert('IE');
//      }
//      else
//        thecontent.style.height = h_height + 50 + 'px';
//    }
//  }
//
//  if (thecontent.offsetHeight < newheight) {
//    thecontent.style.height = newheight+'px';
//  }
//}
//
//function ch3() {
//  // Roep het 2 keer aan vanwege safari
////  ch2();
////  ch2();
//  try {
//    initsearch();
//  } catch(e) {log(e);}
//  
//  return
//}
//
//window.onload=ch3
////window.onresize=ch2;
//
///* Find hilites*/
//function hilites_height() {
//    mc = document.getElementById("maincontent");
//    count = 0;
//    
//  for (i=0; i< mc.childNodes.length; i++) {
//      node = mc.childNodes[i];
//    if (node.className == 'boxcontainer') {
//        count = count + 1;
//          
//          
//    }
//  }
//  rows = parseInt(count / 3) ;
//  if (count % 3 > 0) {
//    rows = rows + 1;
//  }
//  return (rows*380);
//}
