// Cookies needed for Font Resizer
function createCookie(name,value){ document.cookie = name+"="+value+"; path=/"; }
function eraseCookie(name){ createCookie(name,"",-1); }
function readCookie(name){
   var nameEQ = name + "=";
   var ca = document.cookie.split(';');
   for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   }
   return null;
}

// verberg en toon de backtotop-link als de site hoger is als het window
function inittotop(){
      $j('#top:not(.home) #columnwrapper').append('<a href="#" id="scrollup">Terug naar boven</a>');
      $j('#scrollup').hide().unbind('click').click(function(){
         $j.scrollTo( '#top', 800);    // onclick - scroll naar boven
         return false;
      });   
      if ($j("#top").height() > $j(window).height()){ // check hoogte
         $j('#scrollup').show();   
      }   
}

// timers voor carrousel op homepages   
var c=0;
var x=0;
var t;
var timer_is_on=0;
var speed = 5000;
function timedCount(){
   var aantal = $j('#brandthumbs a').length-1;
   var thislink = $j('#brandthumbs a:eq('+c+')');
   var thisurl = $j('#brandthumbs a:eq('+c+')').attr('href');
   var thistxt = $j('#brandthumbs a:eq('+c+')').html();
   thistxt = thistxt.substring(thistxt.indexOf('>')+1);
   // change other images back
   $j('#brandthumbs a.hover:not(this)').removeClass("hover");
   // change this image
   $j(thislink).addClass("hover");
   // change big image
   $j('#brandimg a:visible').stop().fadeOut('fast');   
   var id = '#'+thislink.attr('id').replace("thumb","image");  
   $j(id,'#brandimg').stop().fadeIn('fast');   
   $j('#brandbox a.mask').attr('href',thisurl).html(thistxt);
   // repeat na X images
   if (c==aantal){c=0;} else { c=c+1;};
   t=setTimeout("timedCount()",speed);

}
function doTimer(){
   if (!timer_is_on){
     timer_is_on=1;
     timedCount();
   }
}
function stopCount(){
   clearTimeout(t);
   timer_is_on=0;
} 






// onload
var $j = jQuery.noConflict();  
$j(document).ready(function(){

  // FONT RESIZER
  // edit configuration
  var container = $j('#top'); // outer div, containing the site
  var baseFontSize = '0.7em'; // set base font-size same as css
  var maxSize = 1.1;            // max fontsize in em
  var minSize = 0.3;          // min fontsize in em
  var stepSize = 0.1;         // increase/decrease by steps of 0.2
  function resetpage(){ // functies die uitgevoerd worden na het resizen
   // location.reload();  // refreshen om divs te herpositioneren
  }
  // end configuration
  var fontsizeContainer = $j('#FontSize').show(); // show de fontsize-links
  var sFontSize = readCookie('font');
  if (sFontSize) {
      container.css('fontSize', sFontSize+ 'em'); 
      if (sFontSize*(1+stepSize) > maxSize){ $j(".FontSizeInc",fontsizeContainer).css('opacity','0.1');  }
      if (sFontSize*(1-stepSize) < minSize){ $j(".FontSizeDec",fontsizeContainer).css('opacity','0.1');  }
  } else { // set base fontsize
      $j('#top').css('fontSize',baseFontSize);
  }
  // reset FontSize
  $j(".FontSizeReset",fontsizeContainer).click(function(){
    container.css('fontSize', baseFontSize);
    $j("a",fontsizeContainer).css('opacity','');
    eraseCookie("font");
    resetpage();
    return false;
  });
  // Increase Font Size
  $j(".FontSizeInc",fontsizeContainer).click(function(){
    var newFontSize = parseFloat(container.css('fontSize'), 10) * (1+stepSize);
    if (newFontSize > maxSize){
       $j(".FontSizeInc",fontsizeContainer).css('opacity','0.1');
    } else {
       $j("a",fontsizeContainer).css('opacity','');
       container.css('fontSize', newFontSize+ 'em');
       createCookie("font", newFontSize);
       resetpage();
    }
    return false;
  });
  // Decrease Font Size
  $j(".FontSizeDec",fontsizeContainer).click(function(){
    var newFontSize = parseFloat(container.css('fontSize'), 10) * (1-stepSize);
    if (newFontSize < minSize){
       $j(".FontSizeDec",fontsizeContainer).css('opacity','0.1');
    } else {
       $j("a",fontsizeContainer).css('opacity','');
       container.css('fontSize', newFontSize+ 'em');
       createCookie("font", newFontSize);
       resetpage();
    }
    return false;
  });

   // menu
   $j('#menu>li').hover(function(){
      $j(this).addClass("hover");
      $j('ul', this).show();
      $j('#menu').css('borderBottom','2px solid #FFFFFF');
   },function(){
      $j(this).removeClass("hover");
      $j('ul', this).stop().hide().removeAttr("style");
      $j('#menu').css('borderBottom','');
   });

   // foto's in kaders zetten
   $j('#contentcolumn .text img').not('.left_noframe, .right_noframe').each(function(){
      var c = $j(this).attr('class');
      $j(this).wrap('<div class="foto_klein align_'+c+'"></div>').after('<div class="mask png"></div>');
   });

  // verberg tekst van h2 tot h2 en geef de h2 een openklik fuctie
  $j(".text.openklaptitels h2:not(.show)").each(function() {
      var coll = $j(this);
      $j(this).nextAll().each(function() {
      if ($j(this).is('h2') || $j(this).is('.show')) {
        return false;
      }
      coll = coll.add(this);
      });
      if(coll.length > 1){  // alleen als er wat na de h2 staat
         coll.not('h2').wrapAll('<div class="hide"></div>')
         $j(this).addClass('showtext').toggle(function(){
            $j(this).addClass('open').next('.hide').show();
            inittotop();
         },function(){
            $j(this).removeClass('open').next('.hide').hide();
            inittotop();
         }).next('.hide').hide();
      }
  });
  
   // klaplist
   $j("#contentcolumn .klaplist .item").hide().parent().find('h2:first').toggle(function(){
      $j(this).addClass('open').parent().find('.item:first').show();
      inittotop();
   },function(){
      $j(this).removeClass('open').parent().find('.item:first').hide();
      inittotop();
   }).hover(function(){
      $j(this).addClass('hover');
   },function(){
      $j(this).removeClass('hover');
   });
   
   // brandbox home   
   var brandbox = $j('#top.home #brandbox');
   if(brandbox.length){
      // kopieer de thumbs naar #brandimg
      var ingangen = $j('#brandthumbs').html();
      ingangen = ingangen.replace(/th.jpg/g,".jpg").replace(/thumb/g,"image"); ;
      $j('#brandimg').html(ingangen);
      // verander links in images
      $j('#brandimg a',brandbox).each(function(){
         var src='url('+$j('img',this).remove().attr('src')+')';
         $j(this).css('backgroundImage',src);
      });
      
      // geef hover aan links
      $j('#brandthumbs a',brandbox).each(function(){
         var src='url('+$j('img',this).attr('src')+')';
         var url=$j(this).attr('href');
         var txt=$j(this).html();
         txt = txt.substring(txt.indexOf('>')+1);
         $j(this).css('backgroundImage',src);
         $j('img',this).attr('src','/design/images/pixel.gif');
         var id = '#'+$j(this).attr('id').replace("thumb","image");  
         $j(this).hover(function(){
            $j('#brandthumbs a.hover:not(this)',brandbox).removeClass('hover');
            $j(this).addClass('hover');
            $j('#brandimg a:visible',brandbox).fadeOut('fast');   
            $j(id,brandbox).fadeIn('fast');
            $j('a.mask',brandbox).html(txt).attr('href',url);
            stopCount();
         },function(){
            t=setTimeout("doTimer()",speed);
         });
      });

      
      // start de carroussel op de homepage
      if ($j('#top.home #brandbox').length>0){
         $j('#brandthumbs',brandbox).fadeIn('slow',function(){
           $j('#brandthumbs a',brandbox).fadeIn('slow');
         });
         doTimer();   
      }
   }
   
   // paginafuncties weergeven
   $j('#top:not(.home) #contentcolumn_extrainfo').append('<div id="paginafuncties"><a href="javascript:print()" class="btn_print">Print</a><a href="http://app.readspeaker.com/proreader/proreader.php?cid=5036&amp;lang=nl_nl&amp;url=" target="prwin" onclick=\'window.open(this.href+escape(document.location.href)+"&amp;selhtml="+escape(selectedString), this.target, "width=190, height=120, resizable=1, scrollbars=1, screenX=0, screenY=0, left=0, top=0"); return false;\' class="btn_read">Lees voor</a></div>');         
   
   // externe links in new window
   $j('#columnwrapper a[href^="http://"]').attr({
    target: "_blank", 
    title: "Opens in a new window"
  });

   // externe links in new window not for http://www.siza.nl
   $j('#columnwrapper a[href^="http://www.siza.nl"]').attr({
    target: "", 
    title: ""
  });

   // externe links in new window not for http://siza.nl
   $j('#columnwrapper a[href^="http://siza.nl"]').attr({
    target: "", 
    title: ""
  });
   
   // interne links in new window
   $j('a.new-window').click(function(){
     window.open(this.href);
     return false;
   });


   // klaplist #adressen
   $j("#contentcolumn_extrainfo #adressen .contact").hide().prev('a.plaats').addClass('opener').toggle(function(){
      $j(this).addClass('open').next('.contact').show();
      inittotop();
   },function(){
      $j(this).removeClass('open').next('.contact').hide();
      inittotop();
   });   

   // mouseover foto's bij productsoort
   $j('#productsoort .locaties').after('<div class="productplaats"><div class="mask png"></div></div>').next('.productplaats').hide().find('.png').ifixpng();
   $j('#productsoort .locaties a:not(.lokatiefoto)').hover(function(){
      var pic = $j(this).next('.locatie_foto').attr('href');
      if(pic){
         $j(this).parent().parent().parent().parent().next('.productplaats').show().prepend('<img src="'+pic+'" alt="" />');
      }
   },function(){
      $j(this).parent().parent().parent().parent().next('.productplaats').hide().find('img').remove();
   });

   // toon de backtotop-link als de site hoger is als het window
   inittotop();
  
  //hover op smallbanners
  $j('#extrainfo .smallbanner').hover(function(){
   $j(this).css('opacity','0.9');
  },function(){
   $j(this).css('opacity','1');
  });
    
  // icon van h1 afhankelijk van linktext in menu1...
  var icon=$j('#menu .menu1 ul li.active a').html();
  if(icon){
     icon= 'url(/images/icon/'+icon.split(' ').join('').toLowerCase()+'.gif)';
    $j('h1').css('backgroundImage',icon);
  }
   // IE6 specific javascript
   var isIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
   if (isIE6) {
   
      // menuFix voor IE6: hide selectboxen
      $j('#gerichtzoeken').height($j('#gerichtzoeken').height());
      $j('#menu>li:gt(0)').hover(function(){
         $j('#gerichtzoeken *').hide();
      },function(){
         $j('#gerichtzoeken *').show();
      });   
      
      // IE6 png-fix
      $j('.png').ifixpng(); 

   }                   
      
});   
