$(document).ready(function(){

    $('.light, #body h2, .profile-news h2, .in-focus h2, .law-news h2').each(function(){
        Cufon.replace(this, {
            hover: true,
            fontFamily: 'light'
        });
        $(this).css({
            visibility: "visible"
        });
    });

    /**   FIRST ELEMENT MARGIN FIX  **/
    $('.c-right').children().eq(0).css('margin-top','0');

    /**   show leftmenu-separator-line   **/
    if ( $('#content .c-left > div').size() === 3 ){
        $('#leftmenu-separator-line').show();
    }

    /** sub menu events */
    $('#laws ul.sub').hide();
    $('#laws > li:not(".active") > a').bind({
        click: function(){
            $(this).siblings('ul.sub').toggle();
        }
    })
    $('#laws ul.sub').find('.active').parent('ul').show();

    /** fix layout first element padding & margin */
    $('.layout #body > *').eq(1).css({
        'margin-top': 0,
        'padding-top': 0
    });

    /** fix left-menu first li element **/
    $('#left-menu li:first a:first').css({
        'margin-top': 0,
        'padding-top': 0
    });

    /** fix left-menu sub borders **/
    $('#left-menu > ul > li.active').each(function(){
        if ( $(this).find('ul').size() == 0 ){
            $(this).css('border-bottom', 'none');
        }
    })

    /** more arrows */
    var $newImg = $('.more .arrow img').clone();
    var distance = 7;
    $('.more').bind({
        mouseenter: function(){
            $(this).css('cursor','pointer');
            $(this).find('.arrow').append($newImg);
            $(this).find('.arrow img').eq(1).animate({
                'right': '+=' + distance
            }, {
                'queu': false,
                'duration':200
            }, function(){

                });
        },
        mouseleave: function(){

            $(this).find('.arrow img').eq(1).animate({
                'right': '-=' + distance
            }, {
                'queu': false,
                'duration':200
            }, function(){

                });
        },

        click: function(event){
            event.preventDefault();
            location.href = $(this).find('a').eq(0).attr('href');
        }
    })

    /** clickbox */
    $('.in-focus .item, #home-block .law-news .items .item, .profile-news .item').bind({
        mouseenter: function(){
            $(this).css('cursor','pointer').addClass('select');
        },
        mouseleave: function(){
            $(this).removeClass('select');
        },
        click: function(event){
            event.preventDefault();
            location.href = $(this).find('a').attr('href');
        }
    });

    /** newsletter */
    $('.email input').val('@');

    /** home newsletter */
    $('.newsletter-box').bind({
        click: function(){
            $(this).addClass('active');
            $(this).find('.form').show();
            $(this).find('.submit').hide();
            $('#asubmit').show();
        }
    })

    $('#asubmit').bind('click',function(event){
        event.preventDefault();
        $('.newsletter-box form').submit();
    })

})

$(window).bind('load', function(){

    if ( $('#gallery img').size() > 0 ){
        $('#gallery').cycle({
            fx: 'fade',
            speed: 500,
            timeout: 5000
        })
    }

    if ( $('.in-focus .items').size() > 0 ){
        $('.in-focus .items').cycle({
            fx: 'scrollUp',
            speed: 500,
            timeout: 5000,
            pause: 1
        })
    }

    if ( $('.law-news .items').size() > 0 ){
        $('.law-news .items').cycle({
            fx: 'scrollUp',
            speed: 500,
            timeout: 5000,
            delay: 1000,
            pause: 1
        })
    }

    if ( $('.profile-news .items').size() > 0 ){
        $('.profile-news .items').cycle({
            fx: 'scrollUp',
            speed: 1000,
            timeout: 5000,
            pause: 1
        })
    }


    /** solve homepage left content arrow height and position */
    $('.body-wrapper .arrow a').height( $('.body-wrapper .body').outerHeight() );
    $('.body-wrapper .arrow img').css({
        'margin-top': ($('.body-wrapper .arrow a').height() - $('.body-wrapper .arrow img').height()) / 2
    });

    /** solve content left right boxes height */
    var actualContentHeight = $('#content').height();
    $('.layout .left-block, .layout .right-block, .profile-news').css('min-height', actualContentHeight);

    /** solve home boxes */
    $('#home-block .body-wrapper').height( $('#home-block').height() );
    
    /** solve right-box height */
    //var bBodyHeight = $('#body').height();
    //$('.right-block.first').height( bBodyHeight );

    /** homepage services cycle **/
    /* $('#items').cycle({
        fx: 'scrollLeft',
        speed: 400,
        timeout: 0,
        next: '#more'
    })*/

    /** mac firefox fix */
    if ($.client.os == "Mac" && $.client.browser == "Firefox"){
        $('#lang').css({
            'top': '42px'
        });
        $('#top-menu ul li.active').css({
            'padding-bottom': '8px'
        });
        $('#bottom-menu ul li a').css({
            'padding-bottom': '5px'
        });
    }


    Cufon.refresh();
})



/*

highlight v3

Highlights arbitrary terms.

<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>

MIT license.

Johann Burkard
<http://johannburkard.de>
<mailto:jb@eaio.com>

*/

jQuery.fn.highlight = function(pat) {
    function removeAccents( str ){
        str = str.replace(/[àáâãäåāăąǎǻ]/ig, "a");
        str = str.replace(/[çćĉċč]/ig, "c");
        str = str.replace(/[ďđ]/ig, "d");
        str = str.replace(/[èéêëēĕėęě]/ig, "e");
        str = str.replace(/[ĝğġģ]/ig, "g");
        str = str.replace(/[ĥħ]/ig, "h");
        str = str.replace(/[ĩīĭįìíîï]/ig, "i");
        str = str.replace(/[ĵ]/ig, "j");
        str = str.replace(/[ķ]/ig, "k");
        str = str.replace(/[ĺļľŀł]/ig, "l");
        str = str.replace(/[ñńņňŉŋ]/ig, "n");
        str = str.replace(/[òóôõöōŏőơǒǿ]/ig, "o");
        str = str.replace(/[ŕŗř]/ig, "r");
        str = str.replace(/[śŝşš]/ig, "s");
        str = str.replace(/[ţťŧ]/ig, "t");
        str = str.replace(/[ùúûüũūŭůűųǔǖǘǚǜ]/ig, "u");
        str = str.replace(/[ŵ]/ig, "w");
        str = str.replace(/[ýþÿŷ]/ig, "y");
        str = str.replace(/[źżž]/ig, "z");
        return str;
    }
    pat = removeAccents( pat );
    function innerHighlight(node, pat) {
        var skip = 0;
        if (node.nodeType == 3) {
            var textWithoutAccent = removeAccents( node.data.toLowerCase() );
            var pos = textWithoutAccent.toLowerCase().indexOf( pat );

            if (pos >= 0) {
                var spannode = document.createElement('span');
                spannode.className = 'highlight';
                var middlebit = node.splitText(pos);
                var endbit = middlebit.splitText(pat.length);
                var middleclone = middlebit.cloneNode(true);
                spannode.appendChild(middleclone);
                middlebit.parentNode.replaceChild(spannode, middlebit);
                skip = 1;
            }
        }
        else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
            for (var i = 0; i < node.childNodes.length; ++i) {
                i += innerHighlight(node.childNodes[i], pat);
            }
        }
        return skip;
    }
    return this.each(function() {
        innerHighlight(this, pat.toLowerCase());
    });
};

jQuery.fn.removeHighlight = function() {
    return this.find("span.highlight").each(function() {
        this.parentNode.firstChild.nodeName;
        with (this.parentNode) {
            replaceChild(this.firstChild, this);
            normalize();
            }
    }).end();
};
