$(function() {        
    // Page controls
    if ($('*').is('#page_controls .text_size_select')) {
        $('#page_controls .text_size_select').remove();
        $('#page_controls .text_size').css('display', 'block');
    }
    
    // Font size
    function font_size()
    {
        if ($('*').is('#page_controls .text_size'))
        {
            var prev_font_size = '';
            
            $('.text_size li:eq(0) a:eq(0)').click(function () { return false; })
            
            $('.text_size li ul li:eq(0) a').click(function ()
            {
                $('#content').css('font-size', '0.75em'); // Set font size
                (getCookie('font-size') != null) ? prev_font_size = getCookie('font-size') : prev_font_size = 3; // Get previous font size
                setCookie('font-size', '1', (60*60*24*30*3), '/'); // Set cookie font-size to 1
                $(this).replaceWith($(this).text()); // Remove link from this node
                $('.text_size li ul li:eq(' + (prev_font_size - 1) + ')').wrapInner('<a rel="nofollow" href="font-size/' + prev_font_size + '">' + prev_font_size + '</a>'); // Make previous font size clickable
                $('.text_size li:eq(0) a:eq(0)').attr('href', 'font-size/1').html('1'); // Update font size number in 1st a element
                font_size();
                return false;
            });
            $('.text_size li ul li:eq(1) a').click(function ()
            {
                $('#content').css('font-size', '0.875em');
                (getCookie('font-size') != null) ? prev_font_size = getCookie('font-size') : prev_font_size = 3;
                setCookie('font-size', '2', (60*60*24*30*3), '/');
                $(this).replaceWith($(this).text());
                $('.text_size li ul li:eq(' + (prev_font_size - 1) + ')').wrapInner('<a rel="nofollow" href="font-size/' + prev_font_size + '">' + prev_font_size + '</a>');
                $('.text_size li:eq(0) a:eq(0)').attr('href', 'font-size/2').html('2');
                font_size();
                return false;
            });
            $('.text_size li ul li:eq(2) a').click(function ()
            {
                $('#content').css('font-size', '1em');
                (getCookie('font-size') != null) ? prev_font_size = getCookie('font-size') : prev_font_size = 3;
                setCookie('font-size', '3', (60*60*24*30*3), '/');
                $(this).replaceWith($(this).text());
                $('.text_size li ul li:eq(' + (prev_font_size - 1) + ')').wrapInner('<a rel="nofollow" href="font-size/' + prev_font_size + '">' + prev_font_size + '</a>');
                $('.text_size li:eq(0) a:eq(0)').attr('href', 'font-size/3').html('3');
                font_size();                
                return false;
            });
            $('.text_size li ul li:eq(3) a').click(function ()
            {
                $('#content').css('font-size', '1.25em');
                (getCookie('font-size') != null) ? prev_font_size = getCookie('font-size') : prev_font_size = 3;
                setCookie('font-size', '4', (60*60*24*30*3), '/');
                $(this).replaceWith($(this).text());
                $('.text_size li ul li:eq(' + (prev_font_size - 1) + ')').wrapInner('<a rel="nofollow" href="font-size/' + prev_font_size + '">' + prev_font_size + '</a>');
                $('.text_size li:eq(0) a:eq(0)').attr('href', 'font-size/4').html('4');
                font_size();
                return false;
            });
            $('.text_size li ul li:eq(4) a').click(function ()
            {
                $('#content').css('font-size', '1.5em');
                (getCookie('font-size') != null) ? prev_font_size = getCookie('font-size') : prev_font_size = 3;
                setCookie('font-size', '5', (60*60*24*30*3), '/');
                $(this).replaceWith($(this).text());
                $('.text_size li ul li:eq(' + (prev_font_size - 1) + ')').wrapInner('<a rel="nofollow" href="font-size/' + prev_font_size + '">' + prev_font_size + '</a>');
                $('.text_size li:eq(0) a:eq(0)').attr('href', 'font-size/5').html('5');
                font_size();
                return false;
            });
        }
    }
    
    font_size();
    
    // Suggest a page
    if ($('*').is('#page_controls .email')) {
        // Set up overlay
        var position = $('#page_controls .email').position(); // Get start pos
        $('#overlay_spe').overlay({
            start: {
                top: position.top,
                left: position.left
            },
            onLoad: function () {
                $('#overlay_spe #name').focus()
            },
            close: '.close a',
            closeOnClick: false
        });
        
        $('#page_controls .email a').click(function () {
            var api = $('#overlay_spe').overlay(); // get access to the overlay API
            api.load(); // call it's open() method
            return false;
        });
        
        // Prevent default event firing on close btn
        $('#overlay_spe .close a').click(function () { return false; });
                
        // Submit btn
        $('#overlay_spe input[type="image"]').hover(
            function () { $(this).attr('src', 'img/button_send_hover_' + getLang() + '.png'); },
            function () { $(this).attr('src', 'img/button_send_' + getLang() + '.png'); }
        );
        
        $('#overlay_spe input[type="image"]').click(function () {
            $(this).hide();
            var name = $('#overlay_spe input[name="name"]').val();
            var email = $('#overlay_spe input[name="email"]').val();
            var recipient_name = $('#overlay_spe input[name="recipient_name"]').val();
            var recipient_email = $('#overlay_spe input[name="recipient_email"]').val();
            var comment = $('#overlay_spe textarea').val();
            var suggest_url = $('#overlay_spe input[name="suggest_url"]').val();
            var lang = getLang();
            
            $.post('suggest/ajax_send_email', 
            {
                name: name,
                email: email,
                recipient_name: recipient_name,
                recipient_email: recipient_email,
                comment: comment,
                suggest_url: suggest_url,
                lang: lang
            },
            function(data, textStatus)
            {
                var split_arr = data.split('|');
                var status = split_arr[0];
                var message = split_arr[1];
                
                if (status == 'error') {
                    $('#overlay_spe .message').addClass('bg_error');
                    $('#overlay_spe .message').html(message);
                    $('#overlay_spe input[class="submit"]').fadeIn(200);
                    setTimeout(function() {
                        $('#overlay_spe .message').removeClass('bg_error');
                        $('#overlay_spe .message').html('&nbsp;');
                    }, 5000);
                }
                else if (status == 'ok') {
                    $('#overlay_spe .message').addClass('bg_ok');
                    $('#overlay_spe .message').html(message);
                    setTimeout(function() {
                        $('#overlay_spe .message').removeClass('bg_ok');
                        $('#overlay_spe .message').html('&nbsp;');
                        $('#overlay_spe input[name="name"]').val('');
                        $('#overlay_spe input[name="email"]').val('');
                        $('#overlay_spe input[name="recipient_name"]').val('');
                        $('#overlay_spe input[name="recipient_email"]').val('');
                        $('#overlay_spe textarea').val('');
                        $('#overlay_spe input[class="submit"]').fadeIn(200);
                    }, 3000);
                    
                    setTimeout(function () {
                        var api = $('#overlay_spe').overlay();
                        api.close(); // Close overlay window
                    }, 5000);
                }
            });
            return false;
        });
    }
    
    // Print button
//    if ($('*').is('#page_controls .print'))
//    {
//        $('#page_controls .print a').click(function () {
//            window.print();
//            return false;
//        });
//    }
        
    // Small search box
    if ($('*').is('#search_small')) {
        // Search box
        $('#search_small .q').focus(function () {
            if ($(this).val() == 'Otsing...' || $(this).val() == 'Search...') $(this).val('');
            else $(this).select();
            $(this).addClass('focus');
        });
        $('#search_small .q').blur(function () {
            if ($(this).val() == '' && getLang() == 'et') $(this).val('Otsing...');
            if ($(this).val() == '' && getLang() == 'en') $(this).val('Search...');
            $(this).removeClass('focus');
        });
            
        // Search button
        $('#search_small input[type="image"]').hover(
            function () { $(this).attr('src', 'img/button_search_small_hover.png'); },
            function () { $(this).attr('src', 'img/button_search_small.png'); }
        );
    }
        
    // Big search box
    if ($('*').is('#search_box_big')) {
        $('#search_box_big .q').focus();
        // Search button
        $('#search_box_big input[class="submit"]').hover(
            function () { $(this).attr('src', 'img/button_search_big_hover_' + getLang() + '.png'); },
            function () { $(this).attr('src', 'img/button_search_big_' + getLang() + '.png'); }
        );
        $('#search_box_big input[class="submit"]').mousedown(function () { $(this).attr('src', 'img/button_search_big_active_' + getLang() + '.png'); });
        $('#search_box_big input[class="submit"]').mouseup(function () { $(this).attr('src', 'img/button_search_big_' + getLang() + '.png'); });
        $('form').submit(function () { if ($('#search_box_big .q').val() == '') return false; })
    }
    
    // Fancybox
    if ($('*').is('a[rel="fancybox"]')) {
        $('a[rel="fancybox"]').fancybox();
    }
});
