jQuery.noConflict();
jQuery(document).ready(function ($) {

    $('a.btn, a.more, a.arrow, a.bird, a.twitter, a.facebook, a.linkedin, a.googleplus, .phonenumber, a.btnSubmit, span.fax, a.mail').append('<span class="icon" />');

    $('.threeCols .col:nth-child(3n+1)').addClass('firstItemRow');
    $('ul.threeCols').each(function () {
        $(this).find('li:nth-child(3n)').each(function () {
            $(this).addClass('third');
        });
    });

    $('.scrollBox').each(function () {
        $(this).find('.scrollable').scrollable({
            onSeek: function () {
                var getCurrentItem = this.getIndex() + 1;
                this.getRoot().next('.seperator').find('.current').html(getCurrentItem);
            }
        });

        var scrollBoxApi = $(this).find('.scrollable').data("scrollable");
        var getSizeItems = scrollBoxApi.getSize();
        $(this).find('.size').html(getSizeItems);
        var getCurrentItem = scrollBoxApi.getIndex() + 1;
        $(this).find('.current').html(getCurrentItem);
    });


    /* fallbacks */
    $('.no-boxshadow #showcase').prepend('<span class="shadowTop"></span>');
    $('.no-boxshadow .colRight').prepend('<div class="shadowLeft"></div>');

    if ($('.lt9').length) {
        $('#sitemap > ul li:first-child').addClass('first-child');
        $('#sitemap > ul li:last-child').addClass('last-child');
    }

    if ($('.lt8').length) {
        $('.tabs .showcaseCaption .btn').mousedown(function () {
            $(this).addClass('active');
        });
        $('.tabs .showcaseCaption .btn').mouseup(function () {
            $(this).removeClass('active');
        });
    };

    if ($('.ie6').length) {
        $('#contact li:first-child').css('borderLeft', 'none');
        $('#crew ul:first-child').css('padding-left', '0');
    }

    /* crew */
    var $crewList = $('#crew');

    var reOrderLists = function () {
        $crewList.find('li').removeClass('second third active').hide();
        var colTwo = $crewList.find('.colTwo ul').html();
        var colThree = $crewList.find('.colThree ul').html();
        $crewList.find('ul:first-child').append(colTwo, colThree);
        $crewList.find('.colTwo, .colThree').remove();
        $crewList.find('.changeBg').removeClass('changeBg');
    }

    var createLists = function () {
        //orderAlphabetically();
        orderByDiscipline();
        i = 0;
        $crewList.find('li.active').each(function () {
            if (i == 1) { $(this).addClass('second'); }
            if (i == 2) { $(this).addClass('third'); }
            i++;
            if (i > 2) { i = 0; }
        });
        $crewList.find('div.colOne').after($('<div class="colTwo" />').append($crewList.find('li.active.second')).wrapInner('<ul />'));
        $crewList.find('div.colTwo').after($('<div class="colThree" />').append($crewList.find('li.active.third')).wrapInner('<ul />'));
        $crewList.find('li.active').show();
        window.location.hash = 'selectBox';
        $('#main').equalHeight({ 'children': '.content, .colRight' });
    }

    var resetLists = function () {
        $('#selectBox').find('select').val('all');
        orderByDiscipline();
        //orderAlphabetically();
        $crewList.find('li:nth-child(3n-1)').addClass('second');
        $crewList.find('li:nth-child(3n)').addClass('third');
        $crewList.find('div.colOne').after($('<div class="colTwo" />').append($crewList.find('li.second')).wrapInner('<ul />'));
        $crewList.find('div.colTwo').after($('<div class="colThree" />').append($crewList.find('li.third')).wrapInner('<ul />'));
        $crewList.find('li').show();
    }

    var orderAlphabetically = function () {
        var abclist = $crewList.find('ul');
        var listitems = abclist.children('li').get();
        listitems.sort(function (a, b) {
            var compA = $(a).text().toUpperCase();
            var compB = $(b).text().toUpperCase();
            return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
        })
        $.each(listitems, function (idx, itm) { abclist.append(itm); });
    }

    var orderByDiscipline = function () {
        var teamManagement = $('.teamManagement').get();
        var teamConceptStrategie = $('.teamConceptStrategie').get();
        var teamAccountManagement = $('.teamAccountManagement').get();
        var teamProjectManagement = $('.teamProjectManagement').get();
        var teamFunctioneelOntwerp = $('.teamFunctioneelOntwerp').get();
        var teamUserExperienceDesign = $('.teamUserExperienceDesign').get();
        var teamDesignCopy = $('.teamDesignCopy').get();
        var teamFrontEndOntwikkeling = $('.teamFrontEndOntwikkeling').get();
        var teamApplicatieOntwikkeling = $('.teamApplicatieOntwikkeling').get();
        var teamTesting = $('.teamTesting').get();
        var teamHosting = $('.teamHosting').get();
        var teamOfficeManagement = $('.teamOfficeManagement').get();
        $crewList.find('ul').append(teamManagement).append(teamConceptStrategie).append(teamAccountManagement).append(teamProjectManagement).append(teamFunctioneelOntwerp).append(teamUserExperienceDesign).append(teamDesignCopy).append(teamFrontEndOntwikkeling).append(teamApplicatieOntwikkeling).append(teamTesting).append(teamHosting).append(teamOfficeManagement);
    }

    $crewList.find('li').each(function () {
        crewRel = $(this).attr('class');
        $(this).find('.iconCrewGroup').attr('rel', crewRel);
    });

    if ($crewList.length) {
        resetLists();
    }

    $crewList.find('li h2').live({
        mouseenter: function () {
            $(this).prev('.crewImage').find('img').addClass('hover');
            $crewList.find('li').stop().css({ opacity: 1 });
            $crewList.find('li').not($(this).closest('li')).animate({ opacity: 0.2 });
        },
        mouseleave: function () {
            $(this).prev('.crewImage').find('img').removeClass('hover');
            $crewList.find('li').not($(this).closest('li')).stop().css({ opacity: 1 });
        },
        click: function () {
            if ($(this).hasClass('active')) {
                $(this).removeClass('active');
                $(this).find('.arrowDown').removeClass('active').addClass('inactive');
                $(this).closest('.contentBox').find('.crewDetail').slideUp(function () {
                    $(this).siblings('h3').slideDown(function () {
                        if ($('.ie').length) {
                            $(this).siblings('h2').find('.arrowUp').removeClass('arrowUp').addClass('arrowDown');
                        }
                        $('#main').equalHeight({ 'children': '.content, .colRight' });
                    });
                });
                return false;
            } else {
                $(this).addClass('active');
                $(this).find('.arrowDown').removeClass('inactive').addClass('active');
                $crewList.find('.crewDetail h2').click(function () {
                    return false;
                });
                $(this).closest('h2').next('h3').slideUp(500, function () {
                    $(this).closest('.contentBox').find('.crewDetail').slideDown(500, function () {
                        if ($('.ie').length) {
                            $(this).siblings('h2').find('.arrowDown').removeClass('arrowDown').addClass('arrowUp');
                        }
                        $('#main').equalHeight({ 'children': '.content, .colRight' });
                    });
                });
                return false;
            }
        }
    });

    $crewList.find('li .iconCrewGroup').live({
        mouseenter: function () {
            thisTeam = $(this).attr('rel');
            $crewList.find('li').stop().css({ opacity: 1 });
            $crewList.find('li:not(.' + thisTeam + ')').animate({ opacity: 0.2 });
            $crewList.find('.' + thisTeam).find('.triangle').addClass('changeBg');
        },
        mouseleave: function () {
            $crewList.find('li').stop().css({ opacity: 1 });
            $crewList.find('.' + thisTeam).find('.triangle').removeClass('changeBg');
        }
    });

    $crewList.find('li:not(.active) .iconCrewGroup').live('click', function () {
        thisTeam = $(this).attr('rel');
        reOrderLists();
        $crewList.find('.' + thisTeam).show().addClass('active');
        $('#selectBox').find('select').val(thisTeam);
        createLists();
    });

    $crewList.find('li.active .iconCrewGroup').live('click', function () {
        reOrderLists();
        resetLists();
        window.location.hash = 'selectBox';
        $('#main').equalHeight({ 'children': '.content, .colRight' });
    });

    $("#selectTeam").change(function () {
        if ($(this).find('option:selected:[value="all"]').length) {
            reOrderLists();
            resetLists();
            window.location.hash = 'selectBox';
            $('#main').equalHeight({ 'children': '.content, .colRight' });
        } else {
            thisTeam = $(this).val();
            reOrderLists();
            $crewList.find('.' + thisTeam).show().addClass('active');
            createLists();
        }
    });

    /* equal heights */
    $.fn.equalHeight = function (options) {
        var settings = {
            'children': this
        };

        if (options) {
            $.extend(settings, options);
        }

        if (settings['children'] == this) {
            $children = $(this);
        } else {
            $children = $(this).find(settings['children']);
        }

        var tallest = 0;
        $children.each(function () {
            $(this).css("height", "auto");

            var thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        return $(this).find(settings['children']).height(tallest);
    };
    $('.equalHeight').equalHeight({ 'children': '.gradientLeft' });
    $('#main').equalHeight({ 'children': '.content, .colRight' });

    /* Clickable div */
    $('.clickableBox').each(function () {
        var link = jQuery('a.boxtarget', $(this)).attr('href');
        $(this).click(function (e) {
            location.href = $(this).find("A").attr('href');
            if (e.target.nodeName != "A") {
                e.cancelBubble = true;
            }
        });
    });

    /* Vertical aligment */
    $('.verticalAligment').each(function () {
        var heightBox = $(this).height();
        var heightText = $(this).find('.contentText, hgroup').height();
        var remain = heightBox - heightText;
        var paddingTop = remain / 2;
        $(this).find('.contentText, hgroup').css("padding-top", paddingTop - 4).css("padding-bottom", 0);
    });

    /* form-labels inside fields */
    $("input.labelInForm,textarea.labelInForm").each(function () {
        defaultText = $("label[for='" + $(this).attr("id") + "']").text();
        if ($(this).val() == "") { $(this).val(defaultText); }

        $(this).bind("focus", { t: defaultText }, function (e) {
            if ($(this).val() == e.data.t) $(this).val("");
        });

        $(this).bind("blur", { t: defaultText }, function (e) {
            if ($(this).val() == "") $(this).val(e.data.t);
        });
    });

    if (typeof (parcMapConfig) == "object") {
        $('#map').mediabarMap(parcMapConfig);
    }

    $('#lang ul > li > a.current, #navigation ul > li.current > span > a, #showcase .showcaseTabs li a:not(.btn)').prepend('<div class="before" />');

    /* SHOWCASE SCRIPTS */

    if ($("#navigation .subNavigation ul").length) {
        var getCaption = $('#showcase .showcaseCaption').get();
        $('#showcase .showcaseTabs').removeClass().addClass("showImg").find('.caseCurrent').unwrap().unwrap().unwrap().unwrap().siblings().remove();
        $('#showcase .showImg .caseCurrent').prepend(getCaption);
        $('#showcase .showImg .caseCurrent .showcaseCaption').wrap('<div class="container" />')
        var setColorH3 = $(this).find('.showcaseCaption h3').css('color');
        $(this).find('.showcaseCaption h3 .deviation').css('color', setColorH3);
        $(this).find('.showcaseCaption .btn').remove();
        $('.showImg .caseCurrent').unwrap();
        if ($('#showcase .showImg a').attr("href") == "") {
            $('.caseCurrent').unwrap();
        }
    }

    if ($("#showcase .showcaseTabs").length) {

        $("#showcase .showcaseTabs").before(
            $("<div />").addClass("showcaseSlides").append($("<ul />"))
        );
        $("#showcase .showcaseTabs li a.showcaseImg").each(function () {
            $("#showcase .showcaseSlides ul").append($("<li />").append($(this)));
        });

        $("#showcase .showcaseTabs li").each(function (i) {
            $(this).addClass("item" + (i + 1));
            var setColorH3 = $(this).find('.showcaseCaption h3').css('color');
            $(this).find('.showcaseCaption h3 .deviation').css('color', setColorH3);
        });

        $("#showcase .showcaseTabs").after(
            $("<div />").addClass("showcaseTimer").append($("<div />").addClass("container clearfix").append($("<ul />")))
        );

        if (!$("#navigation .subNavigation").length && !$("#navigation li:first-child").hasClass('current')) {
            var getCaption = $('#showcase .showcaseCaption').get();
            $('.showcaseSlides').removeClass().addClass('showImg').find('.caseCurrent').unwrap().unwrap().unwrap().siblings().remove();
            $('#showcase .showImg .caseCurrent').prepend(getCaption);
            $('#showcase .showImg .caseCurrent .showcaseCaption').wrap('<div class="container" />')
            $('#showcase .showcaseTabs').remove();
            $('#showcase .showcaseTimer').remove();
            $('#main').addClass('mainUp');
        }


        for (i = 1; i <= 60; i++) {
            theSecond = $("<li />").html("&nbsp;");
            if (i == 1) $(theSecond).addClass("first");
            if (!(i % 3)) $(theSecond).addClass("smaller");

            $("#showcase .showcaseTimer ul").append($(theSecond))
        }

        var api = $("#showcase .showcaseTabs ul").tabs("#showcase .showcaseSlides ul>li .caseCurrent", {
            effect: 'fade', initialIndex: 0, api: true, rotate: true, tabs: 'a:not(.btn)'
        });

        var timervalue = 0;
        var tabsTimer;
        startTabsTimer = function (firstLoop) {
            if ($("#showcase .showcaseTimer ul li.done").length == 60) {
                api.next();
                resetTabsTimer();
                $('#showcase li:last-child .showcaseCaption').hide();
                $('#showcase li:first-child .showcaseCaption').show();
                return;
            }

            if (!(timervalue % 12) && firstLoop == false) {
                api.next();
                $("#showcase .showcaseTabs li h4 a").each(function () {
                    if ($(this).hasClass('current')) {
                        $("#showcase .showcaseTabs li .showcaseCaption").hide();
                        $(this).parent().next('.showcaseCaption').show();
                    }
                });
            }

            $("#showcase .showcaseTimer ul li:eq(" + timervalue + ")").addClass("animationStart");
            $("#showcase .showcaseTimer ul li:eq(" + timervalue + ")").animate({
                backgroundColor: "#ea0101",
                borderLeftColor: "#960008",
                borderRightColor: "#960008"
            }, function () {
                $(this).addClass("done").removeClass("animationStart");
            })
            timervalue++;
            tabsTimer = setTimeout("startTabsTimer(false)", 1000);
        }

        resetTabsTimer = function () {
            clearTimeout(tabsTimer);
            $("#showcase .showcaseTimer ul li").stop().removeAttr("style").removeClass("animationStart").removeClass("done");
            timervalue = 0;
            startTabsTimer(true);
        }

        gotoTabsTimer = function (t) {
            curSeconds = $("#showcase .showcaseTimer ul li.done").length;
            timervalue = t;

            if (timervalue > curSeconds) {
                for (i = curSeconds; i < timervalue; i++) {
                    $("#showcase .showcaseTimer ul li:eq(" + i + ")").addClass("done");
                }
            }
            else {
                for (i = timervalue; i <= curSeconds; i++) {
                    $("#showcase .showcaseTimer ul li:eq(" + i + ")").stop().removeAttr("style").removeClass("animationStart").removeClass("done");
                }
            }

            startTabsTimer(true);
        }

        if (typeof (api) != "undefined") {
            api.onBeforeClick(function (e, tabIndex) {
                if (e.pageX != undefined) {
                    clearTimeout(tabsTimer);
                    $("#showcase .showcaseTimer ul li.animationStart").stop().removeAttr("style").removeClass("animationStart").addClass("done");
                    gotoTabsTimer((tabIndex * 12));
                }
            });
        }

        startTabsTimer(true);

        $('.showcaseCaption .btn').append('<span class="icon" /><span class="gradient" />');
        var setColorBtn = $('.showcaseCaption .btn').css('color');
        $('.showcaseCaption .btn .icon').css('border-left-color', setColorBtn);
        $("#showcase .showcaseTabs li h4 a").click(function () {
            if ($(this).hasClass('current')) {
                $("#showcase .showcaseTabs li .showcaseCaption").hide();
                $(this).parent().next('.showcaseCaption').show();
            }
        });
    }

    if (!$('.showImg').length) {
        $('#showcase').addClass('noHeader').append('<div class="showcaseBottom" />' + '<div class="hideShadow" />');
        $('.subNavigation').css('top', '101px');
    }

    if ($('#navigation .navigation  > ul > li:first-child').hasClass('current')) {
        $('#showcase').removeClass('noHeader').find('.showcaseBottom,').last().remove();
        $('#showcase .hideShadow').last().remove();
    }

    if ($('.noHeader') && !$('.showcaseSlides').length && !$('.subNavigation').length) {
        $('#main').addClass('mainUp');
    }

    $('body.rhmEditmode').find('.noHeader').removeClass();

});
