(function($)
{
    $.fn.buildheijmans = function(_options)
    {
        var options = {
            minHeight: 500,
            serviceOpen: false,
            twitterUserNames: ["HeijmansNL"],
            twitterQuery: "",
            twitterCount: 4,
            twitterOn: true
        };

        if (_options) $.extend(options, _options);
        //options.serviceOpen=false;
        var minHeight = 500;
        /* default width uit css */
        var menuWidth = 719;
        var menuHolderWidth = 971;
        if ($(".menu").length > 0) {
            menuWidth = parseInt($(".menu").css('width'));
            if (isNaN(parseInt($(".menu").css('width')))) menuWidth = $(".menu").width;
        }
        if ($(".menuHolderWidth").length > 0) {
            menuHolderWidth = $(".menuHolderWidth").width();
        }
        $(".menu #ulmenu > li").each(function()
        {
            if ($('ul', this).length > 0)
            {
                var thisPos = $(this).position().left;
                var thisWidth = $(this).outerWidth() + 1;
                thisWidth /= 2;
                var ulWidth = $('ul', this).outerWidth() + 10;
                var ulWidth2 = ulWidth / 2;
                var leftPos = thisPos - ulWidth2 + thisWidth;
                if (leftPos <= 0)leftPos = 0;
                $('ul', this).css({'left':leftPos,'width':ulWidth});
                if ((leftPos + ulWidth) > menuWidth || ulWidth > menuWidth)$('ul', this).css({'left':(menuWidth - ulWidth)});
            }

        });
        $(".menu #ulmenu > li").hover(
                function()
                {
                    if (!$(this).hasClass('selected'))
                    {
                        $(this).addClass('show-submenu');
                        $('#ulmenu li.selected ul').hide();
                    }
                },
                function()
                {

                    $(this).removeClass('show-submenu');
                    $('#ulmenu li.selected ul').show();
                }
                );
        var ec_info = $('.rightcolumn .ecinfo').text();
        if (ec_info != "")
        {
            $("#ul_rightcolumn").show();
            $("#ul_rightcolumn > li").hover(
                    function()
                    {
                        $(this).addClass('show-submenu');
                    },
                    function()
                    {
                        $(this).removeClass('show-submenu');
                    }
                    );
        }

        //de schaduwrandjes moeten goede hoogte krijgen
        function resizeContentHeight()
        {
            var h = $('.content-left').outerHeight(true);
            var hh = $('.content').outerHeight(true);
            var deHoogte = 0;
            if (h < hh)deHoogte = hh;
            else deHoogte = h;
            if (h > hh) {
                if (deHoogte < options.minHeight)
                {
                    $('.content').css("min-height", options.minHeight);
                }
                else $('.content').css("min-height", deHoogte);
            }
        }

        $('#service-hideshow').click(
                function()
                {
                    if ($('#id_serviceblock').is(':hidden'))
                    {
                        $('#id_serviceblock').slideDown("", resizeContentHeight);

                        $('#imgid_servicedicht').hide();
                        $('#imgid_serviceopen').show();
                        $('#imgid_servicedriehoekdicht').hide();
                        $('#imgid_servicedriehoekopen').show();
                    }
                    else
                    {
                        $('#id_serviceblock').slideUp("", resizeContentHeight);
                        $('#imgid_serviceopen').hide();
                        $('#imgid_servicedicht').show();

                        $('#imgid_servicedriehoekopen').hide();
                        $('#imgid_servicedriehoekdicht').show();
                    }
                }
                );
        /* zet speciale class om interesse */
        $('.menu li > a[href=/interesse]').addClass('a-interesse');
        $('.menu li ul li a[href=/interesse]').removeClass('a-interesse');
        /* als li menuItem GEEN name='servicebox' heeft , dan gaat de service box dicht */
        /* name='servicebox' voor menuItem zetten in xsl/common/serviceMenu */
        /* SERVICE menu standaard closed , dus geen check om het te verbergen */
        if (!options.serviceOpen) {
            if (!$('li[name=servicebox]').hasClass('selected'))$('#id_serviceblock').hide();
        }

        $('#tweet-hideshow').toggle(
                function()
                {
                    $('#id_tweet').slideUp("", resizeContentHeight);
                    $('#imgid_twitteropen').hide();
                    $('#imgid_twitterdicht').show();
                    $('.tweettop').css('height', '35px');
                    $('#toon-twitter').text("[toon alle tweets]");
                },
                function()
                {
                    $('#id_tweet').slideDown("", resizeContentHeight);
                    $('#imgid_twitteropen').show();
                    $('#imgid_twitterdicht').hide();
                    $('.tweettop').css('height', '53px');
                    $('#toon-twitter').text("[verberg alle tweets]");
                }
                );

        $('a#online-brochure').click(function() {
            var deId = $(this).attr('id');
            try
            {
                _gaq.push(['_trackEvent', 'online brochure bekeken', deId]);
                log("Tel link");
            }
            catch(err)
            {
                log("popup-async-google-error " + err);
            }
        })

        /*
         *   zet google teller op social links wordt nog niet gebruikt
         *   maar kan gebruikt worden voor 'normale' target=_self links
         * */
        function recordOutboundLink(link, category, action)
        {
            try
            {
                _gaq.push(['myTracker._trackEvent', ' + category + ', ' + action + ']);
                setTimeout('document.location = "' + link.href + '"', 100)
            }
            catch(err)
            {
            }
        }

        //twitter
        function tweetGeladen()
        {
            $(".tweet .tweet_text a").attr('rel', 'external');
            resizeContentHeight();
        }

        /* tweet script moet geladen zijn */
        if ($.isFunction($(".tweet").tweet)) {
            $(".tweet").tweet({
                //username: ["smitendouma","uwthuis"],
                //username: "",
                //query: ["from:smitendouma Roderveld","from:uwthuis Roderveld"],
                //query: ["from:smitendouma&phrase=#","from:uwthuis&phrase=#"],
                //query: "#roderveld4",
                username: options.twitterUserNames,
                query: options.twitterQuery,
                avatar_size: 40,
                count: options.twitterCount,
                intro_text: null,                       // [string]   do you want text BEFORE your your tweets?
                outro_text: null,                       // [string]   do you want text AFTER your tweets?
                join_text:  null,
                auto_join_text_default: "",
                auto_join_text_ed: "",
                auto_join_text_ing: "",
                auto_join_text_reply: "",
                auto_join_text_url: "",
                loading_text: "loading tweets..."
            }).bind('loaded', tweetGeladen);
        }

        resizeContentHeight();
    };
})(jQuery);


