// JavaScript Document general scripts for every page
/* create html5 tags ie */
/************************/
document.createElement('header');
document.createElement('nav');
document.createElement('article');
document.createElement('section');
document.createElement('footer');
document.createElement('aside');
document.createElement('time');
document.createElement('date');

$(document).ready(function() {

    //check if ie6
    function is_ie6() {
        return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined) && $.browser.msie && ($.browser.version == "6.0"));
    }

    if (is_ie6()) {

        //hide site temporary for ie6 users
        //document.write('<h1>OOPS!</h1><p>This website is not optimized for Internet Explorer 6.<br />Download a  more recent version on <a href="http://windows.microsoft.com/nl-BE/internet-explorer/products/ie/home">http://windows.microsoft.com/nl-BE/internet-explorer/products/ie/home</a></p><p>The Jobat team</p>')

        $('#map_belgium img').each(function() {
            var imgsrc = $(this).attr('src');
            $(this).attr('src', imgsrc.substring(0, imgsrc.length - 3) + 'gif')
        })

        $('#map_belgium img').click(function() {
            var imgsrc = $(this).attr('src');
            $(this).attr('src', imgsrc.substring(0, imgsrc.length - 3) + 'gif')
        })

        $('#ie6 body.language #topcenter a.button img.icon').each(function() {
            var imgsrc = $(this).attr('src');
            $(this).attr('src', imgsrc.substring(0, imgsrc.length - 3) + 'gif')
        })

        $('.pagination ul').attr("width", $('.pagination ul').width())

        $('#footerwrapper footer ul#logos li img').each(function() {
            var imgsrc = $(this).attr('src');
            var currentimg = $(this);
            $.ajax({
                url: 'http://' + window.location.hostname + imgsrc.substring(0, imgsrc.length - 3) + 'gif',
                type: 'HEAD',
                error: function() {
                    //file not exists
                },
                success: function() {
                    //file exists
                    currentimg.attr('src', imgsrc.substring(0, imgsrc.length - 3) + 'gif')
                }
            });


        })
    }

    /* check if adhese imu loaded */
    /******************************/
    if ($('#rList div#banner, #rList div#IMU').length > 0 && $('.adhese_300x250').length == 0) {
        $('#rList div#banner, #rList div#IMU').parent().addClass('imuNotLoaded')
    }



    /* read cookie */
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }

    /* tabs */
    /********/
    if ($(".tabs").length != 0) {
        //$('.tabs').tabs({ fx: { opacity: 'toggle'} });
        $('.tabs').tabs().bind('tabsselect', function(event, ui) {
            $('.search-block').removeClass('article-search')
        });

    };


    //toggle search-history
    if (!($("#searchwrapper").hasClass('searchwrapper-home'))) {
        var newTopSearch = (parseInt(78) - parseInt($("#searchwrapper").height()));

        function mobileSearch(newTopSearch) {
            $("#searchwrapper").css("top", newTopSearch + "px").show();
            $(".mobile_search_btn").click(function() {
                if (!$(this).hasClass("sel")) {
                    $("#searchwrapper").animate({
                        top: '0px'
                    }, 600, 'easeOutExpo');
                    $(this).addClass("sel");
                    $("#headerwrapper header #topright").fadeOut();
                } else {
                    $("#searchwrapper").animate({
                        top: newTopSearch
                    }, 600, 'easeOutExpo');
                    $(this).removeClass("sel");
                    $("#headerwrapper header #topright").fadeIn();

                }
                return false
            })
        }
        mobileSearch(newTopSearch);
    }

    // toggle aside simple-block

    /* check if profilebox is open or closed */
    if (readCookie("JobatMainPageBoxes") != null) {
        var profilebox = readCookie("JobatMainPageBoxes").split('&');
        profilebox = profilebox[0].substring(profilebox[0].indexOf('|') + 1, profilebox[0].length);
    }
    if (profilebox == 0) {
        $('aside .simple-block h3.profile').next().hide()
    }

    $('aside .simple-block h3').click(function() {
        $(this).toggleClass('open');
        $(this).parent().next('.simple-block-content').fadeToggle();
        if ($(this).hasClass('profile')) {
            $(this).next().fadeToggle()
        }
        SaveOpenCloseProfile();
        return false;
    });

    //run through aside simple blocks
    $('aside .simple-block').each(function() {
        if (!($('h3', this).hasClass("open"))) {
            $('.simple-block-content', this).css("display", "none")
        } else {
            $('.simple-block-content', this).css("display", "block")
        }
    })


    //handled in taggingV2.js
    //select job-result
    /*$('.jobs-results strong.star').click(function() {
    $(this).toggleClass('sel');
    return false;
    })*/

    //slideTo
    function goToByScroll(id) {
        $('html,body').animate({ scrollTop: $(id).offset().top }, 1200, "easeOutExpo");
    }
    $('.criteria-bar a.refine').click(function() {
        goToByScroll(".col1");
        return false

    })
    $('a.criteria-top').click(function() {
        goToByScroll(".criteria-bar");
        return false
    })
    $('a.top').click(function() {
        goToByScroll("#skywrapper");
        $(this).fadeOut();
        return false
    })


    /* detect mobile */
    /**********************/
    var detectMobile = function() {
        if ($(window).width() <= 463/* && $('body:not(.mobile)')*/) {
            $('body').addClass("mobile");


            //toggle categories more
            //$('.categories .article-content nav').hide();

            //main-doormat blocks collapse
            //$('#main-doormat div .article-content').hide();

            //joblist
            if ($('.jobs-list').length > 0) {
                $('ul.jobs-list li h3').removeClass("open");
                $('ul.jobs-list li div').hide();
                //$('.jobs-list ul').children('li:not(.top10)').show();
            }

            //footer lists collapse
            $('footer ul ul').hide();

            //joblist
            $('ul.jobs-list li h2').unbind('click')
            $('ul.jobs-list li h2').click(function() {
                $(this).parent().next('div').toggle();
                $(this).toggleClass('open');
                $(this).parent().children('div').fadeToggle();
                return false;
            })

            //toggle ul in main-doormat
            $('#main-doormat h3').unbind('click')
            $('#main-doormat h3').click(function() {
                if ($(this).parent().next('.article-content').is(':hidden') && $('body').hasClass('mobile')) {
                    $('#main-doormat .article-content').hide();
                    $('#main-doormat h3 a').removeClass('sel');
                    $(this).addClass('sel');
                    $(this).parent().next('.article-content').fadeIn();
                    return false;
                } else {
                    return true;
                }
            })

            //toggle ul in doormat
            $('#doormat h3').unbind('click')
            $('#doormat h3').click(function() {
                if ($('body').hasClass('mobile')) {
                    $(this).toggleClass('sel');
                    $(this).next().fadeToggle();
                    return false;
                }
            })

            //toggle ul in footer
            $('footer h3 a').unbind('click')
            $('footer h3 a').click(function() {
                if ($(this).parent().next('ul').is(':hidden') && $('body').hasClass('mobile')) {
                    $('#doormat ul').hide();
                    $('#doormat h3 a').removeClass('sel');
                    $(this).addClass('sel');
                    $(this).parent().next('ul').fadeIn();
                    return false;
                } else {
                    return true;
                }
            })




        } else if ($('body').hasClass('mobile')) {
            $('body').removeClass("mobile");

            //joblist
            $('ul.jobs-list li h2').unbind('click')
            if ($('.jobs-list').length > 0) {
                $('ul.jobs-list li div').show();
                //$('.jobs-list ul').children('li:not(.top10)').hide();
            }

            //nav
            $('#navwrapper nav').show()
            $('.toggleNav').remove();

            //footer lists expand
            $('footer ul ul').show();

            //toggle ul in main-doormat
            $('#main-doormat h3').unbind('click')

            //toggle ul in doormat
            $('#doormat h3').unbind('click')

            //toggle ul in footer
            $('footer h3 a').unbind('click')

        }
    }
    detectMobile();

    $(window).resize(function() {
        detectMobile();
    });

    if ($('body').hasClass('mobile')) {
        //if ($(window).width() <= 463){
        //nav mobile
        $('#navwrapper nav').hide()
        var captionMenu;
        if ($('#navwrapper nav li.sel a span').text() != '') {
            captionMenu = $('#navwrapper nav li.sel a span').text()
        } else {
            captionMenu = 'Menu'
        }

        $('#navwrapper').append('<a class="toggleNav" href="#"><span>' + captionMenu + '</span></a>')
        $('#navwrapper a.toggleNav').click(function() {
            $(this).toggleClass('open');
            $(this).prev('nav').slideToggle();
            return false;
        })

        //toggle ul in main-doormat
        /* $('#main-doormat h3 a').click(function() {
        if ($(this).parent().next('.article-content').is(':hidden') && $('body').hasClass('mobile')) {
        $('#main-doormat .article-content').hide();
        $('#main-doormat h3 a').removeClass('sel');
        $(this).addClass('sel');
        $(this).parent().next('.article-content').fadeIn();
        return false;
        } else {
        return true;
        }
        })*/




    }



    /* search-block */
    /****************/
    if ($(".emptyonclick").length != 0) {
        var language = readCookie("UserLanguage");  //this isn't getting read in properly.
        if (language == null) language = ReadCookieEktron("SiteLanguageName");
        //alert(language);
        $('.emptyonclick').emptyonclick(language);
    };


    /* forms general */
    /*****************/
    //focus input fields
    $('input').not('.btn input').focus(function() {
        $(this).addClass("focusField");
        $(this).prev('label').addClass("focusLabel")
    });
    $('input').blur(function() {
        $(this).removeClass("focusField");
        $(this).prev('label').removeClass("focusLabel")
    });

    //focus textarea
    $('textarea').focus(function() {
        $(this).addClass("focusField");
    });
    $('textarea').blur(function() {
        $(this).removeClass("focusField");
    });

    //focus select
    $('select').focus(function() {
        $(this).addClass("focusField");
    });
    $('select').blur(function() {
        $(this).removeClass("focusField");
    });


    /* register form */
    /*****************/
    //init
    $("existingpassword").hide();
    $("#nopassword").attr("checked", "checked");
    $("#yespassword").removeAttr("checked");

    $('#nopassword').change(function() {
        if ($(this).is(':checked')) {
            $('.newpassword').show()
            $('.existingpassword').hide()
        } else {
            $('.newpassword').hide()
            $('.existingpassword').show()
        }
    })
    $('#yespassword').change(function() {
        if ($(this).is(':checked')) {
            $('.existingpassword').show()
            $('.newpassword').hide()
        } else {
            $('.existingpassword').hide()
            $('.newpassword').show()
        }
    })


    /*nyroModal*/
    /***********/
    $('#showPopup').click(function(e) { /* use id on input button */
        e.preventDefault();
        $.nmManual('#formSoll'); /* link to iframe */
    });

    $('#register').click(function(e) { /* use id on input button */
        e.preventDefault();
        $.nmManual('#formRegister'); /* link to iframe */
    });


    /*pagintion*/
    /***********/
    if ($(".pagination").length > 0) {
        $('.pagination a.next,.pagination a.prev').parent('li').css("width", "auto")
    };

    /*dymean*/
    /********/
    if ($(".dymean").length > 0 && $(".dymean").is(':empty')) {
        $(".dymean").css("margin-top", "0")
    };







});
