////////////////////////////////////// functions //////////////////////////////////////

var sitefunctions = {
   // font resizer
   textresize : function(){
      var fontsizeContainer = $j("#FontSize"); // div waar de links in staan
      $j(fontsizeContainer).css('display','inline-block');
      var $cookie_name = "SmartInstant-LetterFormaat";
      var originalFontSize = '0.7em'; // gelijk aan css waarde!
      var smallFontSize = '0.6em';
      var bigFontSize = '0.8em';
      var $getSize = $j.cookie($cookie_name);
      if($getSize) {
            $j("#top,#footer").css({fontSize : $getSize });
      }
      // reset link - reset cookie
      $j(".FontSizeReset", fontsizeContainer).click(function(){
            $j("#top,#footer").css("font-size", '');
            $j.cookie($cookie_name, null,{path: '/'});
             sitefunctions.resetpage();
            return false;
      });
      // +link
      $j(".FontSizeInc", fontsizeContainer).click(function(){
         $j("#top,#footer").css("font-size", bigFontSize);
         $j.cookie($cookie_name, bigFontSize,{path: '/'});
         sitefunctions.resetpage();
         return false;
      });
      // -link
      $j(".FontSizeDec", fontsizeContainer).click(function(){
         $j("#top,#footer").css("font-size", smallFontSize);
         $j.cookie($cookie_name, smallFontSize,{path: '/'});
         sitefunctions.resetpage();
         return false;
      });
  },
  // functies die herhaald moeten worden na font-resizen
  resetpage : function(){
      setmenu();
	  equalHeight();
      // location.reload();  // hele pagina refreshen
  }
};

function setmenu(){
// vertical menu
          $j('#navigation .vertical>ol>li').hover(function(){
            $j(this).addClass("active");
            $j('ol', this).slideDown(200);
         },function(){
            $j(this).removeClass('active');
            $j('ol', this).stop().hide().removeAttr("style");
         });
// horzontal menu
         var wtop = 0;
         $j('#navigation .horizontal>ol>li').each(function(){
            // reset
            $j('li:first',this).attr('style','');
            wtop = $j(this).parent().width();         //breedte #menu
            //verplaats submenu's zodat ze onder de link komen te staan
            var xli = $j(this).position();            // x-offset menulink
            margeL = xli.left;
            var wa = 0;                               // breedte alle links in submenu
            $j('ol li a',this).each(function(){
               wa += $j(this).outerWidth();
            });
            var wli = $j(this).outerWidth()/2;        // halve breedte menulink
            var x = xli.left + wli;                   // midden menulink
            var m = x - (wa/2);                        // marge = midden menulink - helft breedte alle links
            margeL = Math.max(0,m);                     // marge altijd > 0
            if((wa+margeL)>=wtop){                      // marge altijd < breedte submenu
               margeL  = wtop - wa;
               margeL  = Math.max(0,margeL);
            };
            if ((wa + margeL)>wtop){margeL = wtop - wa;}// marge + submenu altijd <= aan breedte menu
            if ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined)&&m>0){margeL=margeL/2};  //IE6 check + css-bugfix
            $j('ol li:first',this).css('marginLeft',margeL );
            $j('ol',this).hide().css({width:wtop});      // css positioned alles top:-1000px om de breedtes te kunnen berekenen
         });
         $j('#navigation .horizontal>ol>li').hover(function(){
            $j(this).addClass("active");
            $j('ol', this).css('top','auto').slideDown(200);
         },function(){
            $j(this).removeClass('active');
            $j('ol', this).stop().hide().removeAttr("style").css({width:wtop});
         });

// doormat menu
         wtop = $j('#navigation .doormat').width();
         htop = $j('#navigation .doormat ol:first').outerHeight();
		 $j('body').addClass('doormatmenu');
		 $j('#doormat').remove();
         $j('#navigation .doormat').prepend('<div id="doormat" style="top:'+htop+'px"></div>');
		 $j('#doormat').hide();
         $j('#navigation .doormat .submenu').each(function(){
            var xli = $j(this).parent().position();
            var ws =  $j(this).outerWidth();
            if ((xli.left+ws)>wtop){
               $j(this).parent().addClass('popleft');
            }
         })
         // var h = 100;
         $j('#navigation .doormat>ol>li').hover(function(){
            $j(this).addClass("active");
            var sub = $j('.submenu',this);
			if(sub.length){
	            h = sub.css('marginTop','-1000px').show().outerHeight();
	            $j('#doormat').stop().addClass('open').css('paddingBottom','').show().animate({height:(h)},'fast',function(){
	               sub.css('marginTop','');
	            });
			}
         },function(){
            $j(this).removeClass('active');
            $j('.submenu',this).hide();
            $j('#doormat:visible').stop().animate({height:'0',paddingBottom:'0'},function(){
				$j(this).removeClass('open').hide();
			});
         });
         $j('#navigation .doormat').mouseleave(function(){
            $j('#menu .active').removeClass('active').find('.submenu').hide();
            $j('#doormat:visible').stop().animate({height:'0',paddingBottom:'0'},function(){
				$j(this).removeClass('open').hide();
			});
         });
         $j('#navigation .subinfo li li a').hover(function(){
            $j(this).next('.submenuinfo').css('height',h).show();
         },function(){
            $j(this).next('.submenuinfo').hide();
         })
}

function sendfriend(lan, pageurl){
	var u = escape(top.window.location.href);
	window.open('/smartinstant2.net?id=2894','sendfriend','width=1024,height=768');
	return false;
}
function openWindow(url){
	window.open(url,'_blank','toolbar=no,menubar=no,resizable=yes,location=no,status=yes');
}

function equalHeight(){
   // geef blokken binnen .si_equalheight dezelfde hoogte
   $j("#top .si_equalheight .sml_box").css('height','');
   $j("#top .si_equalheight").each(function(){
      var hoogte = 0;
      $j('.sml_box',this).not(".no_equalheight").each(function(){
         hoogteN = $j(this).height();
         if (hoogteN > hoogte){hoogte = hoogteN;};
      })
      $j('.sml_box',this).not(".no_equalheight").height(hoogte);
   })
}

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}


////////////////////////////////////// onLoad //////////////////////////////////////

var $j = jQuery.noConflict();
$j(document).ready(function(){

   // font resizer
   $j('#FontSize').show().append(' <span>Tekst</span> <a class="FontSizeDec" href="#" title="Kleiner lettertype">A-</a> <a class="FontSizeReset" href="#" title="Normaal lettertype">A&nbsp;</a> <a class="FontSizeInc" href="#" title="Groter lettertype">A+</a>');
   sitefunctions.textresize();

   // initieer menu
   setmenu();
   equalHeight();

   // javascriptfunctiens alleen via javascript tonen
   $j('#print').html('Print').addClass('printbutton').click(function(){ print(); return false;});

  // uitklapmenu voor nested ul's (sitemap)
  $j('#top .uitklap ul').hide().parent().addClass('parent').prepend('<a href="#" class="openchild"></a>');
  $j('#top .uitklap li.parent .openchild').toggle(function(){
      $j(this).parent().addClass('open').find('ul:first').animate({height:'show',opacity:'show'},'fast');
	  return false;
  },function(){
      $j(this).parent().removeClass('open').find('ul:first').slideUp('fast');
	  return false;
  });

  // uitklaplijst voor ul.collapslist
  $j('.collapslist .itemdetails').hide().prev('.listlink').addClass('collapslink').toggle(function(){
    $j(this).parents('li').addClass('open').find('.itemdetails').slideDown();
  },function(){
    $j(this).parents('li').removeClass('open').find('.itemdetails').slideUp();
  });

   // list met top-thumbnail
   function equalHeightList(el){
	   // geef blokken binnen .imagetoplist dezelfde hoogte
	   $j("#top .imagetoplist "+el).css('height','');
	   $j("#top .imagetoplist").each(function(){
	      var hoogte = 0;
	      $j(el,this).each(function(){
	         hoogteN = $j(this).height();
	         if (hoogteN > hoogte){hoogte = hoogteN;};
	      })
	      $j(el,this).css({minHeight:hoogte,display:"block"});
	   })
	}
	equalHeightList('.itemcontent a.listlink');
	equalHeightList('.itemdetails');

	$j('.list:not(.collapslist) li').each(function(){
		var href = $j('.listlink',this).attr('href');
		if(href){
			$j(this).hover(function(){
				$j(this).addClass('hover');
			},function(){
				$j(this).removeClass('hover');
			}).click(function(){

				window.location.href = href;
			})
		}
	})


	
    // foto's met lightbox
   var thumbobject = $j('#top')
   if ($j('.lightbox', thumbobject).length){
      // maak achtergrond en container DIV
      $j('body').append('<div id="black"></div><div id="imagecontainer"></div>');
      var toph = $j('#top').outerHeight(true) + $j('#footer').outerHeight(true);      // hoogte website
      var windowh = $j(window).height();   // hoogte browserwindow
      var windoww = $j(window).width();    // breedte van browserwindow
      $j(".lightbox", thumbobject).css('cursor','pointer').click(function(){
         windowh = $j(window).height();   // hoogte browserwindow
         windoww = $j(window).width();    // breedte van browserwindow
         var scrollh = $j(window).scrollTop();  // verticale scrollafstand
         var ictop = windowh/2 + scrollh - 25;         // top-positie van ImageContainer
         if (toph > windowh){var blackh=toph;}else{var blackh=windowh;}
         var org = $j(this);
         var imageurl = org.attr('src');
         $j('#black').css('height',blackh).css('opacity','0.8').fadeIn(function(){
            $j('#imagecontainer').css({height:'10px',width:'10px',marginLeft:'-5px',top:ictop}).fadeIn('fast',function(){
               loadlightbox(imageurl,org);
            });
         }).click(function(){
            CloseBox();
         });
         return false;
      });
      // open en laad de lightbox
      function loadlightbox(ajaxurl,org){
         var imagecontainer = $j('#imagecontainer')
         imagecontainer.empty();
         var pos = ajaxurl.lastIndexOf('?');
         if (pos>0){
            ajaxurl = ajaxurl.substring(0,pos);
         }
         var myImage = new Image();
         myImage.onload = getWidthAndHeight;
         myImage.src = ajaxurl;
         function getWidthAndHeight() {
            this.onload = null;
            var maxw = windoww - 50;
            var maxh = windowh - 90;
            imagew = this.width;
            imageh = this.height;
            if (imagew > maxw){
              imageh=(maxw/imagew)*imageh;
              imagew=maxw;
            }
            if (imageh > maxh){
              imagew=(maxh/imageh)*imagew;
              imageh=maxh;
            }
            var imageboxh = imageh + 40;
            var imageboxw = imagew + 20;
            var margl = -imagew/2;
            var margh = -imageh/2;
            imagecontainer.animate({height:imageboxh,width:imageboxw,marginLeft:margl,marginTop:margh},function(){
               // plaats title attr als bijschrift
               var text = org.attr('title');
               var alttext = org.attr('alt');
               if (text==""){text=alttext;}
               if (text){
                  imagecontainer.append('<div id="ajaxtekst">'+text+'</div>');
               }
               var texth = imageboxh + $j('#ajaxtekst',imagecontainer).outerHeight();
               imagecontainer.animate({height:texth});
               var ofst = imagecontainer.offset();
               var t = ofst.top;
               if (t<20){
                  t = imageh+margh+20;
                  imagecontainer.animate({top:t});
               }
               imagecontainer.prepend('<img src="'+ajaxurl+'?hid=img;mxw='+maxw+';mxh='+maxh+'" alt="" />');
               // navigatievlakken
               var navwidth =imagew/4;
               var index = $j(".lightbox").index(org);
               var next = $j('.lightbox:eq('+(index+1)+')');
               var righturl = next.attr('src');
               if (righturl){
                  imagecontainer.append('<div id="right"><a href="#">volgende &raquo;</a></div>');
                  $j('#right',imagecontainer).hover(function(){
                     $j(this).addClass('hover');
                  },function(){
                     $j(this).removeClass('hover');
                  }).css({width:navwidth, paddingTop:imageh, left: 3*navwidth+10}).click(function(){
                     loadlightbox(righturl,next);
                  });
               };
               if (index>0){
                  var prev = $j('.lightbox:eq('+(index-1)+')');
                  var lefturl = prev.attr('src');
                  if (lefturl){
                     imagecontainer.append('<div id="left"><a href="#">&laquo; vorige</a></div>');
                     $j('#left',imagecontainer).hover(function(){
                        $j(this).addClass('hover');
                     },function(){
                        $j(this).removeClass('hover');
                     }).css({width:navwidth, paddingTop:imageh}).click(function(){
                        loadlightbox(lefturl,prev);
                     });
                  };
               }
               $j('#left a, #right a', imagecontainer).click(function(){
                  $j(this).parent().trigger('click');  return false;
               });
               imagecontainer.append('<a id="close" href="#">x</a>');
               $j('#close',imagecontainer).show().css('opacity','0.5').click(function(){
                  CloseBox(); return false;
               }).hover(function(){ $j(this).css('opacity','1');},function(){ $j(this).css('opacity','0.5');});
               // keybindings
               $j(document).unbind('keydown').keydown(function(e){
                  switch(e.keyCode) {
                     case 37: // User pressed "left" arrow
                         $j('#left').trigger('click');
                     break;
                     case 39: // User pressed "right" arrow
                        $j('#right').trigger('click');
                     break;
                     case 27: // User pressed "Esc"
                        CloseBox();
                     break;
                  }
               });
               $j('#ajaximage',imagecontainer).fadeIn();
               $j('#ajaxtekst',imagecontainer).slideDown();
            });
         }
      }
      function CloseBox(){
         $j('#imagecontainer').empty().hide().attr('style','');
         $j('#black').fadeOut();
      }
   }
   // end lightbox

   // vraaggeleiding homepage
   	$j('#siohomebutton').click(function(){
		$j('.siohome_fcvg_fc').hide();
		$j('.siohome_fcvg_vg').show();
		return false;
	});
   // end vraaggeleiding homepage

});
