
try {
    document.execCommand("BackgroundImageCache", false, true);
} catch (e) { }

var sliderInterval;
var pos = 1;
$.moveSlider = function (toPage,speed){
   clearInterval(sliderInterval);
    if(controller=='albums'){
        newpx = (toPage*980)-980;
    }else{
        newpx = (toPage*963)-963;
    }
    newpxstr = -newpx.toString()+'px';
    anim = {'left' : newpxstr}
    $('#bannerimgs').animate(anim,speed,'swing',function(){
       if(typeof(slider_titles)!==undefined){
           $('#slidertitle').fadeOut(500,function(){
               $('#slidertitle').html(slider_titles[toPage].title);
               $('#slidertitle').fadeIn(500);
           });
        }
       $.startSlider();
    });
}
$.startSlider = function(){
    clearInterval(sliderInterval);
    sliderInterval = setInterval(function(){
        lastKey     = $('.slide:last').attr('rel');
        currentKey  = $('.slide.active').attr('rel');
        $('.slide').removeClass('active');
        if(currentKey==lastKey)
            currentKey = 1;
        else
            currentKey = parseInt(currentKey)+1;
         $('.slide').each(function(i,e){
             if($(e).attr('rel') == currentKey){
                 $(e).addClass('active');
                 $('#banneroverlaylink').attr('href',$(e).attr('href'));
             }
         });
        $.moveSlider(currentKey,800);
    },5000);
}
$.markInvalid = function(elementSelector){
    $(elementSelector).css({'border':'1px dashed red', 'background-color':'#FAEBE7'});
}
$.showErrors = function(afterElement,aErrors){
    errorstring = '';
    $.each(aErrors,function(i,e){errorstring += "<li>"+e+"</li>";});
    afterElement.after('<ul class="errormsgs">'+errorstring+'<ul>');
}
$.isEmail = function (email){
    var result = email.toString().search(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/);
    if(result > -1){return true;} else {return false;}
}
$.hasVal = function(fieldSelector){
    return (($(fieldSelector).val()==$(fieldSelector).attr('title')) || $.trim($(fieldSelector).val())=='')?false:true;
}
$.saveSignup = function(direction){
    $.fancybox.showActivity();
    $.post(root+'/nieuwsbrief.html?ajax=1',$('#frm'+direction).serialize(),function(data){
        $.fancybox.hideActivity();
        alert(data.oPagina.status_update);
        window.location = root+'/';
    },'json');
}
var storage = (function() {
      try {
        return localStorage.getItem;
      } catch(e) {}
    }()) && localStorage;


varNavCache = {};
function setStorage(key, val){
   if(storage)
      localStorage.setItem(key, val);	
   else
     varNavCache[key] = val;        
}
function getStorage(key){
   return (storage)?localStorage.getItem(key):varNavCache[key];   
}
$.currentElement = null;
$(document).ready(function(){
    if(storage){
        setTimeout(function(){
            $('.nav a').each(function(i,e){
                var currEl = $(e); 
                if(getStorage(currEl.attr('href'))==undefined){
                    $.post(currEl.attr('href'),{ajax:1},function(data){ 
                        setStorage(currEl.attr('href'), data.subnav);                                        
                    },'json');     
                }
            });        
        },10); 
    }
    $('.nav a').live('mouseover',function(e){        
        currEl = $(this);                                      
        if(getStorage(currEl.attr('href'))!=undefined){
            $('div#subnav').html(getStorage(currEl.attr('href')));
        }else{
            $.post(currEl.attr('href'),{ajax:1},function(data){ 
                setStorage(currEl.attr('href'),data.subnav);                
                $('div#subnav').html(data.subnav);
            },'json');                        
        }                                                                                                                                                  
    });    
    $('.sendinforequest').live('click',function(e){
        $('#robot-check').val(1);
        hasErrors = 0;
        if($.trim($('input[name=naam]').val())==''){
            $.markInvalid('input[name=naam]');
            hasErrors = 1;
        }        
        if($.trim($('input[name=email]').val())==''){
            $.markInvalid('input[name=email]');
            hasErrors = 1;
        }   
        if(!$.isEmail($('input[name=email]').val())){
            $.markInvalid('input[name=email]');
            hasErrors = 1;
        }              
        if(hasErrors){
            e.preventDefault();
            return;
        }    
    });     
        
    $('.clearform').live('click',function(e){
        e.preventDefault();
        $(':input').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
    });   

  
   // Automatisch text in input velden overschrijven met lege waarde
   $('.focusblur').live('focus',function(e){
      if($(this).val()==$(this).attr('title'))
          $(this).val('');
   }).live('blur',function(e){
      if($.trim($(this).val())=='')
          $(this).val($(this).attr('title'));
   });
   // Error kader verwijderen bij focus
    $('input[type=text],input[type=radio],input[type=password],select').focus(function(){
        if(!$(this).hasClass('nofocusborder'))
            $(this).css({'background-color':'white','border':'1px solid #9b9b9b'});        
        $('.errormsgs').fadeOut(500,function(){
            $(this).remove();    
        });
    });
    // Nieuwsbrief validatie
    $('#newssignup').live('click',function(e){
        e.preventDefault();
        var errors = new Array();
        if(!$.isEmail($('#signup_email').val())){
            $.markInvalid('#signup_email');
            errors.push('Geef alstublieft een geldig e-mailadres op');
        }else if(!$.hasVal('#signup_email')){
            $.markInvalid('#signup_email');
            errors.push('Geef alstublieft een e-mailadres op');
        }if(!$.hasVal('#signup_naam')){
            $.markInvalid('#signup_naam');
            errors.push('Geef alstublieft uw naam op');
        }
        if(errors.length)
            return $.showErrors($(this),errors);
        $.saveSignup('in');
    });
    $('#newssignout').live('click',function(e){
        e.preventDefault();
        var errors = new Array();
        if(!$.isEmail($('#signout_email').val())){
            $.markInvalid('#signout_email');
            errors.push('Geef alstublieft een geldig e-mailadres op');
        }else if(!$.hasVal('#signout_email')){
            $.markInvalid('#signout_email');
            errors.push('Geef alstublieft een e-mailadres op');
        }
        if(errors.length)
            return $.showErrors($(this),errors);
        $.saveSignup('out');
    });

    var sliderpageWidth = 974;
    var sliderSegments  = Math.ceil($('#thumbs .single').length/3);
    var srollerwidth    = sliderSegments*sliderpageWidth;
    $('#thumbs').css({width:srollerwidth+'px'});
   
    $('.specialsphoto').click(function(e){
       window.location = root+'/onbijt-en-lunch/';
    });
    $('.slide').click(function(e){
        $('#banneroverlaylink').attr('href',$(this).attr('href'));
       clearInterval(sliderInterval);
       setTimeout(function(){
           $.startSlider();
       },10000);
       e.preventDefault();
       $.moveSlider(parseInt($(this).attr('rel')),1200);
       $('.slide').removeClass('active');
       $(this).addClass('active');
    });
   $.startSlider();
   
    $('a.move#prev').live('click',function(e){
        e.preventDefault();
        pos = pos -1;
        anim = {left: -(pos*sliderpageWidth)+sliderpageWidth+'px'};
        $('#prev').css({display:((pos==1)?'none':'block')});
        $('#next').css({display:((sliderSegments==pos)?'none':'block')});
        $('#thumbs').animate(anim,400,'swing',function(){            
        });        
    });
    $('a.move#next').live('click',function(e){
        e.preventDefault();
        pos = pos +1
        anim = {left: -(pos*sliderpageWidth)+sliderpageWidth+'px'};
        $('#prev').css({display:((pos==1)?'none':'block')});
        $('#next').css({display:((sliderSegments==pos)?'none':'block')});
        $('#thumbs').animate(anim,400,'swing',function(){
        });
    });
});
