var mkm = {};

/**
 * this function replaces the mailto links which have the form (href tag):
 * nospam:account__AT__sub__DOT__domain__DOT__tld
 * and the id: nospam_X
 * where X is a number (starting from 1 and continous)
 * innerHTML will be set to the email address 
 */
mkm.antiSpam = function () {
    var i = 0,
        a = null,
        old = '',
        newHref = '';
    while (true) {
        i += 1;
        a = document.getElementById('nospam_' + i);
        if (!a) {
            return;
        }
        // replace link's href attribute
        old = a.href;
        newHref = old.replace('nospam:', '');
        newHref = newHref.replace(/__AT__/, '@');
        newHref = newHref.replace(/__DOT__/g, '.'); // g --> replace all occurances
        a.href = 'mailto:' + newHref;
        a.innerHTML = newHref;
    }
}


mkm.contactFromIsOpen = false;
mkm.contactController = function () {
    if (mkm.contactFromIsOpen) {
        $('#contact').slideUp('slow');
    } else {
        $('#contact').slideDown('slow', function() {
            window.scrollTo(0, 0);
            $('#cf_field_1').focus();
        });
    }
    mkm.contactFromIsOpen = !mkm.contactFromIsOpen;
};


mkm.scrollDocumentUp = function () {
    var scroll_top = $(window).scrollTop(),
        new_scroll_top = (scroll_top > 20 ? scroll_top - 20 : 0);

    $(window).scrollTop(new_scroll_top);
    if (new_scroll_top) {
        window.setTimeout(mkm.scrollDocumentUp, 10);
    } else {
        mkm.searchForUsClick();
    }
};


mkm.searchForUsClick = function () {
    var scroll_top = $(window).scrollTop();

    if (scroll_top) {
        mkm.scrollDocumentUp(mkm.searchForUsClick);
        return;
    }
    if (!mkm.contactFromIsOpen) {
        mkm.contactController();
    }
};


// this function is run on page init
mkm.init = function () {
    mkm.antiSpam();
    // stop event propagation on contact form input fields
    $('#cf_field_1').bind('keydown', function(event) {
        event.stopPropagation();
    });
    $('#cf_field_2').bind('keydown', function(event) {
        event.stopPropagation();
    });
    $('#cf_field_3').bind('keydown', function(event) {
        event.stopPropagation();
    });
    // slide down and up for contact form
    $('#write_us').click(mkm.contactController);
    $('#search_for_us').click(mkm.searchForUsClick);
    // check if we should open the contact form
    if ("#usermessageb" === window.location.hash) {
        mkm.contactFromIsOpen = true;
        $('#contact').removeClass('hidden');
        //window.scrollTo(0, 0);
        window.setTimeout(mkm.scrollDocumentUp, 100);
    }
};


// make the function run when the page is loaded
if (window.addEventListener) {
    window.addEventListener("load", mkm.init, false);
} else {
    if (window.attachEvent) {
        window.attachEvent("onload", mkm.init);
    } else {
        // try to run now
        mkm.init();
    }
}


