jQuery.noConflict();

jQuery(document).ready(function() {

    var KPC = (function() {
        // tell the world that javascript is in town.
        jQuery('body').addClass('js-on');

        jQuery('a.popup').hover(
		      function() {
		          var url = jQuery(this).attr('href').split('#')[1];
		          jQuery('#' + url).toggle().bgiframe();
		      },
		      function() {
		          var url = jQuery(this).attr('href').split('#')[1];
		          jQuery('#' + url).toggle();
		      }
		    );

        /*
        jQuery('input[type="submit"]').addClass('IEsubmit');
        jQuery('input[type=radio]').addClass('IEradio');
        jQuery('input[type=checkbox]').addClass('IEcheckbox');
        */

        jQuery("ul.navigation li").hover(function() {
            jQuery(this).addClass("hover");
        }, function() {
            jQuery(this).removeClass("hover");
        });


        // support browsers that don't support the first-child pseudo property
        jQuery('fieldset p *:first-child, li ul.meta li:first-child, ul.bc li:first-child').addClass('first');

        // also for last-child
        jQuery('#menu ul li:last-child').addClass('last');

        // do we need this?
        jQuery('fieldset fieldset.iv label + label').addClass('iv');

        // no print button on trainingen on the end
        jQuery('.printsavemail').append('<li class="print"><button title="Afdrukken">print</button></li>');
        // but in front!
        //jQuery('#content.trainingen .util').prepend('<li class="print"><button title="Afdrukken">print</button></li>');

        // go and print when you click the print button
        jQuery('.print button').click(function() {
            window.print();
            return false;
        });

        // Changed by JRH Gemini Issue:  KPC-7992
        // only add the buttons if there is more than 1 Employee.
        jQuery(".extras .carousel").each(function(i) {
            if (jQuery(this).children("ul").children("li").size() > 1) {
                jQuery(this).children("ul").after('<p class="buttons"><button class="prev" alt="vorige"></button><button class="next" alt="volgende"></button></p>');
            }
        }
        );
        //if (jQuery('.extras .carousel ul li').size() > 1) {
        // build navigation for the carousel in .extras
        //jQuery('.extras .carousel ul').after('<p class="buttons"><button class="prev">vorige</button><button class="next">volgende</button></p>');
        //}

        // Display homepage carousel items
        jQuery("#carousel-default").css("display", "block");
        jQuery("#carousel-po").css("display", "block");
        jQuery("#carousel-so").css("display", "block");
        jQuery("#carousel-vo").css("display", "block");
        jQuery("#carousel-bo").css("display", "block");


        // init carousel in .extras
        jQuery(".extras .carousel li").addClass("visible");

        jQuery(".extras .carousel").each(function(i) {
            if (jQuery(this).children("ul").children("li").size() > 1) {
                jQuery(this).jCarouselLite({
                    btnNext: ".next",
                    btnPrev: ".prev",
                    visible: 1
                });
            }
        });

        // init carousel on subhome
        var carCount = {
            itemsdisplay: 3,
            items: '.subhome .carousel > ul > li',
            margin: 20, // margin on the list items. hard coded. change when css changes
            interval: '15000', // #lectric.kpc carousel timer...
            init: function() {

                var l = jQuery(carCount.items).size(); //nr of items in caroussel
                var w = jQuery(carCount.items).width();
                var w = w + carCount.margin; //total width of listitem

                jQuery(".next").click(function() {
                    jQuery(".subhome .carousel > ul").stop();
                });

                jQuery(".prev").click(function() {
                    jQuery(".subhome .carousel > ul").stop();
                });

                jQuery('.subhome .carousel > ul').animate({
                    left: -carCount.itemsdisplay * w
                }, carCount.itemsdisplay * carCount.interval);

                carCount.carousel();

                //the carousel script adds elements to the carousel, but it starts with the wrong element.
                //removing the first elements fixes this problem.
                /* 20090421 AAK: somehow this doesn't work anymore, so it is removed */
                /*for (var i = 0; i < 3; i++) {
                jQuery('.subhome .carousel ul li:first').remove();
                }*/

            },
            carousel: function() {
                jQuery(".subhome .carousel").jCarouselLite({
                    btnNext: ".next",
                    btnPrev: ".prev",
                    visible: carCount.itemsdisplay
                });
            }
        }

        var itemCount = jQuery(carCount.items).size();
        if (itemCount > 3) {
            jQuery('.subhome .carousel ul').before('<p class="buttons"><button class="prev" alt="vorige"></button><button class="next" alt="volgende"></button></p>');
        }
        else {

            jQuery('.subhome .carousel ul').before('<p class="buttons"><button class="prev" alt="vorige" style="visibility:hidden"></button><button class="next" alt="volgende" style="visibility:hidden"></button></p>');

            if (itemCount <= 2) {
                //add li element
                jQuery('.subhome .carousel ul li:last').after('<li></li>');
            }

            if (itemCount == 1) {
                //add another li element
                jQuery('.subhome .carousel ul li:last').after('<li></li>');
            }
        }

        carCount.init();

        // set active state on navigation bar on home.
        //jQuery('.home .navigation li').click(function() {
        //jQuery('.home .navigation li').removeClass('active');
        //jQuery(this).addClass('active');
        //});
        jQuery('.logohome').click(function() {
            jQuery('.home .navigation li').removeClass('active');
        });

        jQuery('.home .carousel li a').click(function() {
            var tar = jQuery(this).attr('href');
            window.location = tar;
        });

        /* toevoeging mnp tbv homepage */
        jQuery('.home .navigation li').click(function() {
            //var tar = jQuery(this).attr('href');
            var tar = jQuery(this).find('.navlink').attr('href');
            window.location = tar;
        });

        /**
        when the link in .mail is clicked we get the href
        then we build an overlay  and a mask div
        we put the content of the element .mail-overlay in the overlay div
        we append a close button to the h1 of mail-overlay.
        we prevent the default action
        **/
        jQuery('.util .mail a').click(function() {
            var url = jQuery(this).attr('href');
            jQuery('body').append('<div id="overlay">overlay</div><div id="mask"></div>');
            jQuery('#overlay').load(url + ' .mail-overlay');
            jQuery('.mail-overlay h1').append('<span class="IEsubmit" id="close"><input type="submit" value="close" /></span>');
            return false;
        });

        jQuery.listen('click', '#close', function() {
            jQuery('#overlay, #mask').remove();
        });

        jQuery.listen('click', '.newwindow', function() {
            var dest = this.getAttribute('href');
            window.open(dest);
            return false;
        });

        jQuery.listen('click', 'a.popupwindow', function() {

            var dest = this.href;
            var width = GetElementValueFromClassName(this.className, "w");
            var height = GetElementValueFromClassName(this.className, "h");

            var title = 'test'; /* OR GetElementValueFromClassName(this.className, "t"); */
            // unless GetElementValueFromClassName(this.className, "t") has been given
            if (GetElementValueFromClassName(this.className, "t") != null && GetElementValueFromClassName(this.className, "t") != "") {
                title = GetElementValueFromClassName(this.className, "t");
            }

            var scrollbar = '1'; /* OR GetElementValueFromClassName(this.className, "s"); */
            // unless GetElementValueFromClassName(this.className, "s") has been given
            if (GetElementValueFromClassName(this.className, "s") != null && GetElementValueFromClassName(this.className, "s") != "") {
                scrollbar = GetElementValueFromClassName(this.className, "s");
            }

            var resizable = '1'; /* OR GetElementsValueFromClassName(this.className,"r"); */
            // unless GetElementValueFromClassName(this.className, "r") has been given
            if (GetElementValueFromClassName(this.className, "r") != null && GetElementValueFromClassName(this.className, "r") != "") {
                resizable = GetElementValueFromClassName(this.className, "r");
            }

            var location = '0'; /* OR GetElementsValueFromClassName(this.className,"l"); */
            // unless GetElementValueFromClassName(this.className, "l") has been given
            if (GetElementValueFromClassName(this.className, "l") != null && GetElementValueFromClassName(this.className, "l") != "") {
                location = GetElementValueFromClassName(this.className, "l");
            }

            var menubar = '0'; /* OR GetElementsValueFromClassName(this.className,"m"); */
            // unless GetElementValueFromClassName(this.className, "m") has been given
            if (GetElementValueFromClassName(this.className, "m") != null && GetElementValueFromClassName(this.className, "m") != "") {
                menubar = GetElementValueFromClassName(this.className, "m");
            }

            var status = '0'; /* OR GetElementsValueFromClassName(this.className,"st"); */
            // unless GetElementValueFromClassName(this.className, "m") has been given
            if (GetElementValueFromClassName(this.className, "st") != null && GetElementValueFromClassName(this.className, "st") != "") {
                status = GetElementValueFromClassName(this.className, "st");
            }



            var toolbar = '0'; /* OR GetElementsValueFromClassName(this.className,"to"); */
            // unless GetElementValueFromClassName(this.className, "to") has been given
            if (GetElementValueFromClassName(this.className, "to") != null && GetElementValueFromClassName(this.className, "to") != "") {
                toolbar = GetElementValueFromClassName(this.className, "to");
            }

            popUp(dest, title, width, height, scrollbar, resizable, location, menubar, status, toolbar);
            return false;
        });

        /*
        * open hier een nieuw venster met de url uit de poplink functie.
        * 
        * class="popup w:800 h:400"
        */
        function popUp(winURL, title, width, height, scrollbar, resizable, location, menubar, status, toolbar) {
            if (title == "") { title = 'KPC'; }
            if ((width == "") || (width == "0")) { width = 780; }
            if ((height == "") || (height == "0")) { height = 580; }
            if (scrollbar == "") { scrollbar = 'auto'; }
            if (resizable == "") { resizable = '0'; }
            if (location == "") { location = '0'; }
            if (menubar == "") { menubar = '0'; }
            if (status == "") { status = '0'; }
            if (toolbar == "") { toolbar = '0'; }

            winleft = ((screen.width - width) / 2);
            wintop = (((screen.height - height) - 80) / 2);

            window.open(winURL, 'KPC', "width=" + width + ", height=" + height + ", left=" + winleft + ", top=" + wintop + ", scrollbars=" + scrollbar + ", resizable=" + resizable + ", location=" + location + ", menubar=" + menubar + ", status=" + status + ", toolbar=" + toolbar);
        }

        jQuery(function() {
            jQuery('.populate').each(function() {

                var e = jQuery(this);

                if (e.attr('title').length) {
                    e.val(e.attr('title'));
                }
                if (e.attr('title') == e.val()) e.addClass('inputLabel');

                e.focus(function() {
                    var e = jQuery(this);
                    if (e.attr('title') == e.val()) {
                        e.val('').removeClass('inputLabel');
                    }
                }).blur(function() {
                    var e = jQuery(this);
                    if (e.attr('title').length & !e.val().length) {
                        e.val(e.attr('title')).addClass('inputLabel');
                    }
                });

                jQuery('label[@for=' + jQuery(this).attr('id') + ']').addClass('hiddenlabel');

            });
        });

        jQuery('form').submit(function() {
            var els = this.elements;
            for (var i = 0; i < els.length; i++) {
                if (els[i].title == els[i].value)
                    els[i].value = '';
            }
        });

        // fix margin linklist style, in case a p tag is preceeded
        var listElements = jQuery('.extras ul.linkpijlrood');

        jQuery.each(listElements, function(index, value) {

            var prevElement = jQuery(this).prev();
            if (prevElement.length > 0) {
                var domEl = jQuery(prevElement).get(0);
                var tagName = domEl.tagName.toLowerCase();

                if (tagName == 'p') {
                    // add negative margin
                    jQuery(this).css("margin-top", "-10px");
                }
            }
        });
        jQuery('.textBlock').each(function() {
            var temp = jQuery(this).parent().html();
            jQuery(this).parent().parent().html('<td colspan="4">' + temp + '</td>');
        });

        //search boxes
        window.InitSearchBox && InitSearchBox();
    })();
});
