//====================== Browser checks
// convert all characters to lowercase to simplify testing
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var Opera = window.opera ? true : false;
var IE = (document.all && document.getElementById && !Opera) ? true:false;
var IE6    = (IE && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );

//====================== 
//====================== Initialiseer de volgende functies
jQuery(document).ready(function() {
  jQuery('.countdown').each(function() {
    var countTo = jQuery(this).find('.countdown_cb').text();
    setcountdown(countTo.split("-")[0],countTo.split("-")[1],countTo.split("-")[2]);
    taal = 'nl';
    countdown();
  if (jQuery(this).hasClass('countdownmetlink')) {
      var curlink = jQuery(this).parent().find('a').attr("href")
    jQuery(this).click(function() {
        location.href=curlink;
      }); 
  }
  });
});

function initStijlset(){
  //document.oncontextmenu = function() {return false;};
  if(IE)
	{
	  startList();
	  startNavList();
	}
  if(IE6)
	{
	  initzoekbuttons();
	}
  if (reloadopener==true)
	{
	  opener.location.reload(true);
	}
 }


var boxdirty = false;
//====================== Maak de zoekinputbox leeg
function emptysearchox(boxitem){
  if (boxdirty==false)
  {
	    boxitem.value='';
		boxitem.className="zoekbox_active";
		boxdirty=true;
  }
 }

var personenboxdirty = false;
function emptysearchoxpersonen(boxitem){
  if (personenboxdirty==false)
  {
	    boxitem.value='';
		boxitem.className="zoekbox_active";
		personenboxdirty=true;
  }
 }

var subhomeboxdirty = false;
function emptysearchoxsubhome(boxitem){
  if (subhomeboxdirty==false)
  {
	    boxitem.value='';
		boxitem.className="zoekbox_active";
		subhomeboxdirty=true;
  }
 }

 var showinfo_selected = false;
 function showinfo(){	 
	 if (showinfo_selected==false)
	 {
		 document.getElementById("show_informatie_item").className="selected";
		 document.getElementById("show_informatie").style.display="block";
     	 showinfo_selected=true;
	 }
	 else
	 {
	 	 document.getElementById("show_informatie_item").className="";
		 document.getElementById("show_informatie").style.display="none";
	 	 showinfo_selected=false;
	 }  
 }

//====================== voegt rollover functie toe aan class="zoekbutton"
function initzoekbuttons() {  
  var zoekbuttons = getElementsByClassName("zoekbutton");  
  for (var i=0; i<zoekbuttons.length; i++) { 
	  zoekbuttons[i].onmouseover = function() {		
		addClass(this,"over");
	  }
	  zoekbuttons[i].onmouseout = function() {
		removeClass(this,"over");
	  }
	}
  var verzendendivs = getElementsByClassName("verzenden");  
  for (var i=0; i<verzendendivs.length; i++) { 
	  var inputs = verzendendivs[i].getElementsByTagName("input");
      if (inputs.length > 0) {
		  var input = inputs[inputs.length-1];
		  }
		  input.onmouseover = function() {		
			addClass(this,"over");
		  }
		  input.onmouseout = function() {
			removeClass(this,"over");
		  }
	}  
   var knoppenfieldsets = getElementsByClassName("knoppen");  
   for (var i=0; i<knoppenfieldsets.length; i++) { 
	  var inputs = knoppenfieldsets[i].getElementsByTagName("input");
      if (inputs.length > 0) {
		  var input = inputs[inputs.length-1];
		  }
		  input.onmouseover = function() {		
			addClass(this,"over");
		  }
		  input.onmouseout = function() {
			removeClass(this,"over");
		  }
	}  
}


//====================== checkboxes laten resulteren in het Email to veld bij dienstrapportages
function selectdienstrapportto(el) {  
  if (el.checked)
  {
    document.getElementById("Formulier.$cc").value+=el.value+";";
  }
  else
  {
   document.getElementById("Formulier.$cc").value=document.getElementById("Formulier.$cc").value.replace(el.value+";","");
  }
}

/* general helper functions */

// returns array of elements with class classname
function getElementsByClassName(classname) {
  var a = [];
  var re = new RegExp('\\b' + classname + '\\b');
  var els = document.all?document.all:document.getElementsByTagName("*");
  for(var i=0,j=els.length; i<j; i++)
    if(re.test(els[i].className))a.push(els[i]);
  return a;
}

// add & remove css classes
function addClass(el,clName) {
  if (el) {
    if (el.className.indexOf(clName) < 0)
      el.className += " "+clName;
  }
}
function removeClass(el,clName) {
  if (el)
    el.className = el.className.replace(clName,"");
}

$(document).ready(function(){	
    if (twitterfeedwidget == true)
	{
		searchtwitterfeed(1);
	}
});

function searchtwitterfeed(count) {
 if ($('div.TweetDiv').length == 0 && count<100)
 {
    count++;	
	setTimeout("searchtwitterfeed('"+count+"')", 100);
 }
 else
 {
   $("div.TweetDiv:odd").css("background-color", "#e7e7e7");  
   $("div.TweetDiv:even").css("background-color", "#fff");  
 }
}


var url;
function setTwitLink(title,longUrl,orgUrl,summ) {
  var lv_tweet = title + ' - ' + summ;
  var tweet = lv_tweet.replace("%2c",",");
  if ($.browser.msie && $.browser.version.substr(0,1)<7) { 
    var tw_link = 'http:/'+'/twitter.com/home?status=' + encodeURIComponent( tweet + ' ' + orgUrl );
    window.open(tw_link);
  } 
  else {
    $.getJSON("http://api.bit.ly/v3/shorten?login=vrhaaglanden&apiKey=R_4660871dbf44987f2ae02ed6c60ca9c1&longUrl=" + longUrl + "&format=json&callback=?",
      function(data) {
        url = data.data.url;
        var limit = 140 - ( url.length + 1 );
        if( tweet.length > limit ) {
            tweet = tweet.substr( 0, ( limit - 3 ) ) + '...';
        }
        var tw_link = 'http:/'+'/twitter.com/home?status=' + encodeURIComponent( tweet + ' ' + url );
  
        //document.write( '<a href="' + link + '" target="_blank">Test twitter</a>' );
        window.open(tw_link);
        return false;
    });
  }
}

function submitpersbericht(message){
  $("div.knoppen").html("<div class='versturenbezig'><img src='/views/shared/images/extra/busy.gif'/>"+message+"</div>");
  setTimeout("submitpersberichtreal()", 200);
}

function submitpersberichtreal(){
  $("form.formulier").submit();
}

function setcountdown(theyear,themonth,theday){
  yr=theyear;mo=themonth;da=theday;
}

var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
function countdown(){
  var today = new Date();
  var todayy = today.getYear();
  if (todayy < 1000) {
    todayy += 1900;
  }
  var todaym = today.getMonth();
  var todayd = today.getDate();
  var todayh = today.getHours();
  var todaymin = today.getMinutes();
  var todaysec = today.getSeconds();
  var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec;
  futurestring = montharray[mo - 1] + " " + da + ", " + yr;
  dd = Date.parse(futurestring) - Date.parse(todaystring);
  dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
  dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
  dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
  dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
  //if on day of occasion
  if(dday<=0&&dhour<=0&&dmin<=0&&dsec<=1&&todayd==da){
    return;
  }
  //if passed day of occasion
  else if (dday<=-1){
  return;
  }
  //else, if not yet
  else{
     if (taal == 'nl') {
      if (dday == '1') d = 'dag';
      else d = 'dagen';
      if (dhour == '1') u = 'uur';
      else u = 'uren';
      if (dmin == '1') m = 'minuut';
      else m = 'minuten';
      if (dsec == '1') s = 'seconde';
      else s = 'seconden';
      jQuery('.countdown').html("<div class='countdown_cb'>"+dday+" "+d+", "+dhour+" "+u+",</div><div class='countdown_cs'>"+dmin+" "+m+" en "+dsec+" "+s+"</div>");

    }
    else if (taal == 'uk') {
      if (dday == '1') d = 'day';
      else d = 'days';
      if (dhour == '1') u = 'hour';
      else u = 'hours';
      if (dmin == '1') m = 'minute';
      else m = 'minutes';
      if (dsec == '1') s = 'second';
      else s = 'seconds';
      jQuery('.countdown').html("<div class='countdown_cb'>"+dday+" "+d+", "+dhour+" "+u+",</div><div class='countdown_cs'>"+dmin+" "+m+" and "+dsec+" "+s+"</div>");

    }

  }
  setTimeout("countdown()", 1000);
}

