﻿String.prototype.endsWith = function (str) { return (this.match(str + "$") == str) }
String.prototype.startsWith = function (s) { return this.indexOf(s) == 0; };
String.prototype.contains = function (str) { return this.indexOf(str) != -1; };

Cufon.replace('h1', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('.frutiger', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('#primary-nav tr td a span', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('h2', { fontFamily: 'FrutigerBoldCondensed' })('h3.frutiger', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('.text-section h2', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('.page-text h3', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('.text-section h4', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('.text-section h5', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('.text-section h6', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('h2.georgia', { fontFamily: 'GeorgiaRegular' });
Cufon.replace('.people-search-results h3', { fontFamily: 'FrutigerCondensed' });
Cufon.replace('a.submit-btn', { fontFamily: 'FrutigerCondensed' });
Cufon.replace('.subscription-table-header div', { fontFamily: 'FrutigerCondensed' });
Cufon.replace('#updates-section div h2', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('#services-section div h2', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('h2.vacancytype-heading', { fontFamily: 'FrutigerBoldCondensed' });
Cufon.replace('h2.articletype-heading', { fontFamily: 'FrutigerBoldCondensed' });

$(document).ready(function () {

    $("input.submit").after(function () { return $('<a></a>').attr({ "href": "", "class": "submit-btn btn forinput", "id": $(this).attr('id') }).html($(this).attr('value')); }).remove();
    
    //Initialize global functions
    checkAndLoad();

    $.preloadCssImages();

    bind_form_events();

    //$('select').JellyfishCustomSelect();

    //$('input:checkbox').checkbox();

    //autosubmit
    wireUpAutoSubmit();
    

    $('a.print-page').click(function (e) {
        e.preventDefault();
        window.print();
    });

    // add a "rel" attrib if Opera 7+
    if (window.opera) {
        if ($("a.bookmark-page").attr("rel") != "") { // don't overwrite the rel attrib if already set
            $("a.bookmark-page").attr("rel", "sidebar");
        }
    }

    $("a.bookmark-page").click(function (event) {
        event.preventDefault();
        var url = window.location;
        var title = document.title;

        if (window.sidebar != null) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(title, url, "");
        } else if (window.external != null) { // IE Favorite
            window.external.AddFavorite(url, title);
        } else if (window.opera != null) { // Opera 7+
            return false;
        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
            alert('Press "CTRL + D" or "CMD + D" (for Mac) to add this page to your bookmarks.');
        }
    });


    $(window).scroll(function () {
        checkAndLoad();
    });

    $(window).resize(function () {
        checkAndLoad();
    });

    $('.view-sitemap-link').href = ("#page-bottom");

    $(".view-sitemap-link").click(function (event) {

        $('html, body').animate({ scrollTop: $('a#page-bottom').offset().top }, 800);
    });


    //Initialize customization of people search filters    
    switchToCustomRadios("#name-radio-switch", "anchor-radio-switch", true);
    switchToCustomRadios("#people-letters", "anchor-radio-switch2", false);  
});

//ovelay functions
function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}
function checkAndLoad() {
    if (isScrolledIntoView($('.footer-bottom'))) {
        $('.view-sitemap-link').css({ display : 'none'});
        $('.floating-toolbar').css({position: 'relative',top: null, bottom: null});
    }
    else {
        $('.floating-toolbar').css({position : 'fixed', top: null, bottom: 0});
        $('.view-sitemap-link').css({ display: 'block' });
    }
}

function show_ajax_loader(container) {
    $(container).append('<div class="ajax-loader"></div>');
}

function hide_ajax_loader(container) {
    $(container).find('.ajax-loader').remove();
}

function bind_form_events() {

    var focussed_form;
    $('input').focus(focus_form);
    $('input').blur(unfocus_form);    
    $('select').focus(focus_form);
    $('select').blur(unfocus_form);
    $('a.btn').focus(focus_form);
    $('a.btn').blur(unfocus_form);
    $('.select_bg').focus(focus_form);

    $(document).keypress(function (e) {
        if (e.keyCode == 13) {
            submit_form();
        }
    });

    $('a.btn.forinput').click(function (e) {
        e.preventDefault();
        $(this).closest('form').trigger('submit');
    });
}

function focus_form() {
    focussed_form = $(this).closest('form');
}

function unfocus_form() {
    focussed_form = null;
}

function submit_form() {
    $(focussed_form).trigger('submit');
}

//auto submit function
function wireUpAutoSubmit() {
    $(".autosubmit").each(function (index) {
        $(this).change(function () {            
            $(this).closest('form').trigger('submit');
        })
    });
}

//people search
function switchToCustomRadios(container, anchor_switch_class, appendslash) {

    var defaultvalue = "";
    var selectedvalue = "";
    var label_count = $(container + ' label').size();
    var anchor_switch_selector = "." + anchor_switch_class;

    $(container).find('label').each(function (i) {

        var input = $('input', this);
        var anchor = '<a href="" rel="' + $('input', this).attr('value') + '" class="' + anchor_switch_class + '">' + $(this).text() + "</a>";

        if (appendslash) {
            if (i < label_count - 1) anchor += " / ";
        }

        $(this).removeAttr('class');
        $(this).html('');
        $(this).append(input);
        $(this).append(anchor);
        $(input).next("a").addClass($(input).attr('class'));

        if ($('input', this).attr('checked')) {
            $(input).next('a').addClass('selected');
            selectedvalue = $(input).attr('value');
        }

        $(input).css({ position: 'absolute', zIndex: -1, visibility: 'hidden' });
    });

    $(container + " a" + anchor_switch_selector).click(function (e) {
        e.preventDefault();
        $(this).blur();

        if (!$(this).prev('input').is(':disabled')) {
            $(this).prev('input').attr('checked', true);
            $(container + " .selected").removeClass("selected");
            $(this).addClass("selected");
        }
    });
}
