
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*974)-974;
    }
    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 id=\"error_list\">"+e+"</li>";});
    afterElement.after('<ul class="errormsgs">'+errorstring+'<ul>');
}
$.alertErrors = function(aErrors){
    errorstring = '';
    $.each(aErrors,function(i,e){errorstring += e+"\n";});
    alert(errorstring);    
}
$.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/nieuwsbrief.html?ajax=1',$('#frm'+direction).serialize(),function(data){
        $.fancybox.hideActivity();
        alert(data.oPagina.status_update);
        window.location = root+'/';
    },'json');
}
var instant = {};

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];   
}
$(document).ready(function(){            
    setTimeout(function(){
        $('#logo .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);            
    $('.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');
    });    
    $('#logo .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');                        
        }                                                                                                                                                  
    });

    $('#bannerpages').css('width',(($('#bannerpages a').length*19)).toString()+'px');
    $('.centerabs').css('width',(($('.centerabs a').length*20)).toString()+'px');
    $.bindFancybox();
    $('#bannerimgs').css('width',($('#bannerimgs img').length*1050).toString()+'px');

   // 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(){
        });
    });

    $('#subscribe').click(function(e){
        $('#errorlistcnt').html('');
        e.preventDefault();
        errors = new Array();
        if($.trim($('#fldcount').val())==''){
            $.markInvalid('#fldcount');            
            errors.push('Geef alstublieft aan met hoeveel personen u komt');
        }
        if($.trim($('#fldname').val())==''){
            $.markInvalid('#fldname');
            errors.push('Geef alstublieft uw naam op');
        }
        if($.trim($('#fldgeslacht').val())==''){
            $.markInvalid('#fldgeslacht');
            errors.push('Geef alstublieft uw naam op');        
        }
        if($.trim($('#fldadres').val())==''){
            $.markInvalid('#fldadres');
            errors.push('Geef alstublieft uw adres op');
        }
        if($.trim($('#fldpostcode').val())==''){
            $.markInvalid('#fldpostcode');
            errors.push('Geef alstublieft uw postcode op');
        }
        if($.trim($('#fldwoonplaats').val())==''){
            $.markInvalid('#fldwoonplaats');
            errors.push('Geef alstublieft uw woonplaats op');
        }
        if($.trim($('#fldbankgiro').val())==''){
            $.markInvalid('#fldbankgiro');
            errors.push('Geef alstublieft uw rekeningnummer op');
        }
        if($.trim($('#fldmobiel').val())==''){
            $.markInvalid('#fldmobiel');
            errors.push('Geef alstublieft uw telefoonnummer op');
        }
        if($.trim($('#fldemail').val())==''){
            $.markInvalid('#fldemail');
            errors.push('Geef alstublieft uw e-mailadres op');
        }else if(!$.isEmail($('#fldemail').val())){
            $.markInvalid('#fldemail');
            errors.push('Geef alstublieft een geldig e-mailadres op');            
        }        
        if(errors.length>0)
            $.alertErrors(errors);
        else
            $('#frmsubscribe').submit();
    });
});
