(function ($) {
    "use strict";
    $(function () {
        var hostname = window.location.hostname,
            body = $('body'),
            anchors = $('a'),
            footer_newsletter = $('#link-newsletter'),
            popup_search_link = $('#popup-search > a'),
            popup_search_box = $('#pu-search'),
            popup_subscribe_link = $('#popup-subscribe'),
            popup_subscribe_button = $('#pu-subscribe form input[type="image"]'),
            popup_subscribe_text = $('#pu-subscribe form input[name="email"]'),
            popup_subscribe_box = $('#pu-subscribe');
        body
            .click(function () {
                $('#pu-search:visible').fadeOut();
            });
        anchors
            .each(function () {
                var anchor = $(this),
                    host = anchor.attr('hostname');
                if (host !== hostname && anchor.attr('id') !== 'submitBtn') { // filter out paypal button
                    anchor.attr('target', '_blank');
                }
            });
        footer_newsletter
            .click(function (e) {
                e.stopPropagation();
                popup_subscribe_link.click();
            });
        popup_search_box
            .click(function (e) {
                e.stopPropagation();
                $('> div > p', this).hide();
                $('> div > form', this)
                    .show();
                $('> div > form > div > input[name="s"]', this)
                    .focus();
            });
        popup_search_link
            .click(function () {
                if (popup_search_box.is(':visible')) {
                    popup_search_box.fadeOut();
                } else {
                    $('> div > form', popup_search_box).hide();
                    $('> div > form > div > input[name="s"]', popup_search_box).val('');
                    $('> div > p', popup_search_box).show();
                    popup_search_box.fadeIn();
                }
                return false;
            });
        popup_subscribe_link
            .attr('href', '')
            .fancybox({
                'autoDimensions': false,
                'width': 500,
                'height': 180,
                'href': '/ajax/subscribe/'
            });
        popup_subscribe_button
            .live('click', function () {
                var email = $('#pu-subscribe input[type="text"][name="email"]').val();
                $.post(
                    '/ajax/subscribe/', 
                    { email: email }, 
                    function (data, status_text) {
                        if (status_text === 'success') {
                            var new_popup = $(data),
                                old_popup = $('#pu-subscribe'),
                                container = old_popup.parent();
                            new_popup.hide();
                            old_popup.fadeOut(400);
                            container.append(new_popup);
                            new_popup.fadeIn(400);
                        }
                    }
                );
                return false;
            });
        popup_subscribe_text
            .live('focus', function () {
                var el = $(this),
                    default_val = el.data('default');
                if (default_val === undefined) {
                    default_val = el.val();
                    el.data('default', default_val);
                }
                if (el.val() === default_val) {
                    el.val('');
                }
            })
            .live('blur', function () {
                var el = $(this),
                    default_val = el.data('default');
                if (default_val === undefined) {
                    default_val = el.val();
                    el.data('default', default_val);
                }
                if (el.val() === '') {
                    el.val(default_val);
                }
            });
    });
}(jQuery));

