$(function(){
    $('div.menu_item').hover(showMenu, hideMenu);
    $('div.submenuitem').hover(showSub, hideSub);
    slider(0);

    $(function(){
        $('#QapTcha').QapTcha({
            disabledSubmit:true
        }); 
    });

    var k = $('div[class^=scroller_item]').length;

    $("#scroller-content").css("width", k*946);

    $('a.lightbox').lightBox({
        fixedNavigation:true
    });

    $('a.rounds').click(function(){
        var id = $(this).attr('id');
        slide_home(id-1);
        return false;
    });

    $('a.videoclose').click(function(){
        $('#videotekst').hide();
        return false;
    });
    $('a.arrow_down').click(function(){
        nr++;
        arrowsImages(nr);
        return false;
    });
    $('a.arrow_up').click(function(){
        nr--;
        arrowsImages(nr);
        return false;
    });									
    $('a.arrow_left').click(function(){
        nav--;
        newsItems(nav);
        return false;
    });
    $('a.arrow_right').click(function(){
        nav++;
        newsItems(nav);
        return false;
    });
    $('a.arrow_left_cases').click(function(){
        nav--;
        casesItems(nav);
        return false;
    });
    $('a.arrow_right_cases').click(function(){
        nav++;
        casesItems(nav);
        return false;
    });

    arrowsImages(0);
    nav = $('#nav').val();
    newsItems(nav);
    casesItems(nav);

    $("form.frm").submit(function() {
        var ret = true;
        $('input.verplicht').each(function(i){
            if($(this).val() == ''){
                $(this).addClass('fail');
                ret = false;
            } else {
                $(this).removeClass('fail');
            }
        });
        $('input.email').each(function(i){
            if (!checkEmail($(this).val())){
                $(this).addClass('fail');
                ret = false;
            } else {
                $(this).removeClass('fail');
            }
        });
        return ret;
    });

});

function checkemail(str){
    if ((str.indexOf(".") > 2) && (str.indexOf("@") > 0)){
        return true;
    }
    else {
        return false;
    }
} 
						
function checkfrm(){
    if (document.getElementById('inp_3').value!=''  &&  
        document.getElementById('inp_1').value!=''){
        if ( checkemail(document.getElementById('inp_1').value)){
            document.frm.action = '';
            document.frm.submit();
        } else {
            alert('Het door u ingevulde email adres is incorrect');
        }
    } else {
        alert('Vul alle velden die gemarkeerd zijn met een * in aub');
    }
}

function showSub(){
	
    var id = $(this).attr('id');
    if($('div#subsub_'+id).length>0){
        $('div#subsub_'+id).show();
        $('div.subsubmenu').children('a.arrow').hide();
        $('div.subsubmenu_item').children('a.arrow').hide();
        $('div.submenuitem a').removeClass("suboverarrow");
        $('div#'+id+ ' a.arrow').addClass("suboverarrow");	
    //$(this).removeClass("arrow");			
		
    //} else {
    //	$(this).css("background-color","black");
    }
}

function hideSub(){	
    var id = $(this).attr('id');
    if($('div#subsub_'+id).length>0){	

        $('div.subsubmenu').hide();
        $('div#'+id+ ' a.arrow').removeClass("suboverarrow");		
    //$(this).addClass("arrow");		
    }
//	$(this).css("background-color","");
}

function checkEmail(email){
    if (email!=''){
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if (reg.test(email) == false){
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

var nr = 0;
var max = 6;
var nav = 0;

function casesItems(nav){
    var pages = parseInt($('#pages').val());
    nav = parseInt(nav);
    if((nav > 1) && (nav <= pages)){
        $('#image_left img').show();
    } else {
        $('#image_left img').hide();
    }
    if((nav < pages) && (nav >= 1)){
        $('#image_right img').show();
    } else {
        $('#image_right img').hide();
    }
    $.ajax({
        url: '/ajax.php?action=case&nav='+nav, 
        cache: true, 
        success: function(html){
            $('#navnummer').html(nav);
            $('#casesitemsmenu').html(html);
        }
    });
}

function newsItems(nav){
    var pages = parseInt($('#pages').val());
    nav = parseInt(nav);
    if((nav > 1) && (nav <= pages)){
        $('#image_left img').show();
    } else {
        $('#image_left img').hide();
    }
    if((nav < pages) && (nav >= 1)){
        $('#image_right img').show();
    } else {
        $('#image_right img').hide();
    }

    $.ajax({
        url: '/ajax.php?action=news&nav='+nav, 
        cache: true, 
        success: function(html){
            $('#navnummer').html(nav);
            $('#newsitemsmenu').html(html);
        }
    });
}

function arrowsImages(nr){
    var t = $('div[class^=images-scroller-item]').length;
    $("#images-scroller-content").animate({
        "marginTop": '-'+(nr*97)+'px'
    }, 300);
    if((nr >= 0) && ((t-max) > nr)){
        $('#bottomimages img').show();
    } else {
        $('#bottomimages img').hide();
    }

    if ((nr > 0)){
        $('#topimages img').show();
    } else {
        $('#topimages img').hide();
    }
}

var show = false;

function showMenu(){
    $('div#sub_'+$(this).attr('id')).show();
}

function hideMenu(){
    $('div#sub_'+$(this).attr('id')).hide();
}

function showMenu_old(){
    $('div#menu').clearQueue();
    if(!show){
        $('div#menu').animate({
            "height": '417px'
        }, 300);
    }
    show = true;

}

function hideMenu_old(){
    if(show){
        $('div#menu').delay(500).animate({
            "height": '43px'
        }, 300, function(){
            show = false;
        });
    }
}

var t = 5000;
var timer = null;

function slider(j){
    clearTimeout(timer);
    var k = $('div[class^=scroller_item]').length;
    $("#scroller-content").css("width", k*946);
    $('div[class^=scroller_item]').each(function(i){
        $(this).clearQueue();
        if(i>=j){
            $(this).delay((i-j)*t).queue(function(){
                $("#scroller-content").animate({
                    "marginLeft": '-'+(i*946)+'px'
                }, 300);
                $('img.imagerounds').attr("src", "/images/round.png");
                $('#round_'+(i+1)).attr("src","/images/round_act.png");
            });
        }
    });
    timer = setTimeout('slider(0)', t*(k-j));	
}

function slide_home(nr){
    var k = $('div[class^=scroller_item]').length;
    $('div[class^=scroller_item]').each(function(i){
        $(this).clearQueue();
    });
    $("#scroller-content").css("width", k*946);
    $("#scroller-content").animate({
        "marginLeft": '-'+(nr*946)+'px'
    }, 300);
    $('img.imagerounds').attr("src", "/images/round.png");
    $('#round_'+(nr+1)).attr("src","/images/round_act.png");
    clearTimeout(timer);
    timer = setTimeout('slider('+(nr+1)+')', 3000);	
}
