
$(document).ready(function () {
    
    //Initiate Cufon
    Cufon.replace('.grid_12>h1, .site-ID, .popup h1, .article h1, .futura-bold', { fontFamily: 'Futura PT Bold' });
    Cufon.replace('.search-wrapper label, .tag-line, .m1a h1, .m1b h1, .m1c h1, .m2a h1, .m2b h1, .x-form h1', { fontFamily: 'CoopForged' });

    //Initiate ScrollTo
    $('a[href^=#]').bind('click', function(event) {
        event.preventDefault();
        $.scrollTo($(this).attr('href'), 'fast');
    });


    //Initiate Accordion
    if ($('.accordion').length) {
        $('.accordion').accordion();
    }

    //Initiate Defaultvalue
    /* Rel is not accepted according to w3c, Moved this to masterpage
    if($('input[defaultvalue]').length){
    $('input[defaultvalue]').defaultValue();
    } */

    // Submit on enter
    $('input.text').live('keypress', function(e) {
        if (e.keyCode == '13') {
            e.preventDefault();
            $(this).parents('*').each(function(i, parent) {
                var $el = $(parent).find('input[type=submit]:first');
                if ($el.length > 0) {
                    $el.trigger('click');
                    return false;
                }
            });
        }
    });

    //Toggle
    $('.toggle-container').hover(
        function(event) {
            $(this).children('.mouseover-container').show();
            $(this).children('.mouseout-container').hide();
        },
        function(event) {
            $(this).children('.mouseover-container').hide();
            $(this).children('.mouseout-container').show();
        }        
     );
});



function registerAutocomplete(queryCompletionUrl, textField, button, width) {

    var queryCompletionSearchForms = [{ textField: textField, button: button}];

    if (typeof (queryCompletionSearchForms) == 'undefined' || queryCompletionSearchForms.length == 0) return;

    $.each(queryCompletionSearchForms, function (index, selectors) {
        $(selectors.textField)
        .biositeAutocomplete(
            queryCompletionUrl,
            {
                cacheLength: 0,
                extraParams: { /* ilang: queryCompletionLanguage */
            },
            delay: 450,
            minChars: 3,
            selectFirst: false,
            width: width,
            dataType: 'jsonp',
            highlight: false,
            scroll: false,
            parse: function (data) {
                return $.map(data, function (row) {
                    return { data: row };
                });
            },
            formatItem: function (item) {
                if (item) {
                    return item.suggestionHighlighted;
                }
                return;
            }
        }
        )
        .result(function (event, item) {
            $(selectors.textField).val(item.suggestion);
            //$(selectors.button).click();
        });
    });
}
