//PNG fix for header
(function ($) {
    if (!$) return;
    $.fn.extend({
        fixPNGKath: function(sizingMethod, forceBG) {
                if (!($.browser.msie)) return this;
                var emptyimg = "empty.gif"; //Path to empty 1x1px GIF goes here
                sizingMethod = sizingMethod || "scale"; //sizingMethod, defaults to scale (matches image dimensions)
                this.each(function() {
                        var isImg = (forceBG) ? false : jQuery.nodeName(this, "img"),
                                imgname = (isImg) ? this.src : this.currentStyle.backgroundImage,
                                src = (isImg) ? imgname : imgname.substring(5,imgname.length-2);
                        this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
                        if (isImg) this.src = emptyimg;
                        else this.style.backgroundImage = "url(" + emptyimg + ")";
                });
                return this;
        }
    });
})(jQuery);

//slideswitch
function slideSwitch(id) {
    var $active = $('#'+id+' div.active');

    if ( $active.length == 0 ) $active = $('#'+id+' div:last');

    var $next =  $active.next().length ? $active.next()
        : $('#'+id+' div:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}


var menuTimeout = 0;

//menu functions
function initMenu(){

    //fancy effect
    $("#menu a:not(.openSub), .menu_themaMenu_1 a").click(function(){clickedMenu(this)});

    //open - close menu
    $("#menu .openSub").click(function(){openSubMenu(this);return false;});
    $("#menu>ul>li>a>span").hover(function(event){openSubMenu(this.parentNode)} , function(){menuTimeout = setTimeout(function(){closeSubMenu()},500)});
    $(".submenu ul").hover(function(){$('.submenu .active').addClass('activeTemp') ;clearTimeout(menuTimeout)}, function(){menuTimeout = setTimeout(function(){closeSubMenu()},500) });
}

function clickedMenu(which){
    $(which).addClass('clicked') ;
}

function openSubMenu(which){
   
       //clear all timers
       if(menuTimeout > 0) clearTimeout(menuTimeout);
       //close all subs but this parent's child
       $(".submenu ul:not(#"+which.id+"Sub):visible").hide();
       //show hidden child
       if($("#"+which.id+"Sub")){
            $("#"+which.id+"Sub").show();
        };

       $('.temp').removeClass('temp') ;
       $('.active').addClass('activeTemp') ;
       $(which).addClass('temp') ;
}


function closeSubMenu(){
   $('.clicked').removeClass('clicked') ;
   $('.temp').removeClass('temp') ;
   $('.activeTemp').removeClass('activeTemp') ;
   $(".submenu ul:visible").hide();
   $(".submenu .active").parents("ul:first").show();
}



//newsletter
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}


//doc ready function
function docReady(){
    initMenu();

    $("#home25").click(function(){window.location = '/'}) ;
    $("#over_kathleen32").click(function(){window.location = '/over/wie'}) ;
    $("#visie33").click(function(){window.location = '/visie/verder_dan_morgen'}) ;

    $('#silhouette').fixPNGKath("crop",false);

    $(window).scroll(function () {
        var scrollTop = $('body').scrollTop() + $('html').scrollTop();
        var opacity = 1;

        if(scrollTop > 0 ){
            if(scrollTop < 101) {
                opacity =  1 - (scrollTop / 100) ;
                $('#scroller').css('display', 'none');
            }
            else{
                opacity = 0;
                $('#scroller').css('display', 'block');
            }

        }
        else{
            $('#scroller').css('display', 'none');
        }
        $('#silhouetteContainer').css('opacity', opacity);
    });

    $('#silhouetteContainer').css('display', 'block');

    $('.scroller').click( function(){
        $.scrollTo( $('#top') , 500);
        return false;
    });


    $("#nlForm").submit(function() {
        if(!isValidEmailAddress($("#nlAdres").val())){
            $("#nlAdres").effect("shake", {
                times:3,
                distance:10
            }, 100, function(){
                $("#nlAdres").focus()
            });
        }
        else{
            $.ajax({
                type: "POST",
                url: "/submitmail/",
                data: "email="+$("#nlAdres").val(),
                success: function(msg){
                    $("#nlForm").hide("slide", {
                        direction: "right"
                    }, 1000);
                    $("#nlFormRespons").show();
                }
            });

        }
        return false;
    });
}

