$(function()
{    
    // Small search box
    if ($('*').is('#search_box_small')) {
        // Search box
        $('#q').focus(function () {
            if ($(this).val() == 'Otsing...' || $(this).val() == 'Search...') $(this).val('');
            $(this).addClass('focus');
        });
        $('#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_box_small input[type="image"]').hover(
            function () { $(this).attr('src', 'img/button_search_small_hover.png'); },
            function () { $(this).attr('src', 'img/button_search_small.png'); }
        );
        $('#search_box_small input[type="image"]').mousedown(function () { $(this).attr('src', 'img/button_search_small_active.png'); });
        $('#search_box_small input[type="image"]').mouseup(function () { $(this).attr('src', 'img/button_search_small_hover.png'); });
    }
    
    // Big search box
    if ($('*').is('#search_box_big')) {
        $('#q').focus();
        $('#search_box_big input[type="image"]').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[type="image"]').mousedown(function () { $(this).attr('src', 'img/button_search_big_active_' + getLang() + '.png'); });
        $('#search_box_big input[type="image"]').mouseup(function () { $(this).attr('src', 'img/button_search_big_hover_' + getLang() + '.png'); });
        $('form').submit(function () { if ($('#search_box_big #q').val() == '') return false; })
    }
	
	// Musicplayer
    if ($('*').is('#button_musicplayer')) {
        $('#button_musicplayer').click(function() {
            window.open('musicplayer.php', 'musicplayer', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=255,height=300,left=' + (screen.width - 285) + ',top=' + (screen.height - 420) + '');
            return false;
        });
    }	
	
	// News block
	if ($('*').is('#news')) {
		$('#news').accordion({
			header: '.news_header',
			active: false,
			alwaysOpen: false,
			animated: 'slide',
			autoHeight: true,
			event: 'mouseover'
		});
		
		$('.news_header').hover(
			function () { $(this).css('background', 'url(img/news_heading_bg_over.png) left bottom repeat-x #E9EBF5'); },
			function () { $(this).css('background', 'url(img/news_heading_bg.png) left bottom repeat-x #f5f5f5'); }
		);
	}
	
	// Page numbers
	if ($('*').is('#page_numbers')) {
		$('#page_numbers input').focus(function () {
			$(this).addClass('focus');
		});
		$('#page_numbers input').blur(function () {
			$(this).removeClass('focus');
		});
	}
	
	// Font size
	function font_size()
	{
		if ($('*').is('div[class="font_size"]')) {
			var prev_font_size = '';
			
			$('.a1 a').click(function () {
				$('#container_content').css('font-size', '0.7em'); // 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
				$('.a' + prev_font_size).wrapInner('<a rel="nofollow" href="font-size/' + prev_font_size + '"></a>'); // Make previous font size clickable
				font_size();				
				return false;
			});
			$('.a2 a').click(function () {
				$('#container_content').css('font-size', '0.8235em');
				(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());
				$('.a' + prev_font_size).wrapInner('<a href="font-size/' + prev_font_size + '"></a>');
				font_size();
				return false;
			});
			$('.a3 a').click(function () {
				$('#container_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());
				$('.a' + prev_font_size).wrapInner('<a href="font-size/' + prev_font_size + '"></a>');
				font_size();
				return false;
			});
			$('.a4 a').click(function () {
				$('#container_content').css('font-size', '1.2em');
				(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());
				$('.a' + prev_font_size).wrapInner('<a href="font-size/' + prev_font_size + '"></a>');
				font_size();
				return false;
			});
			$('.a5 a').click(function () {
				$('#container_content').css('font-size', '1.4em');
				(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());
				$('.a' + prev_font_size).wrapInner('<a href="font-size/' + prev_font_size + '"></a>');
				font_size();
				return false;
			});
		}
//alert("olin siin");
	}
	
	font_size();
	
	// Print button
	if ($('*').is('div[class="print"]')) {
		$('div[class="print"] a').click(function () {
			window.print();
			return false;
		});
	}
	
	// Email-it button
    if ($('#email_it').html().length > 6) {
        // Set up overlay
        var position = $('#email_it').position(); // Get start pos
        $('#overlay_suggest_article').overlay({
            start: {
                top: position.top,
                left: position.left
            },
            onLoad: function () {
                $('#overlay_suggest_article #name').focus()
            },
            close: '.close a',
            closeOnClick: false
        });
        
        $('#email_it a').click(function () {
            var api = $('#overlay_suggest_article').overlay(); // get access to the overlay API
            api.load(); // call it's open() method
            return false;
        });
        
        // Prevent default event firing on close btn
        $('#overlay_suggest_article .close a').click(function () { return false; });
                
        // Submit btn
        $('#overlay_suggest_article input[type="image"]').hover(
            function () { $(this).attr('src', 'img/button_ea_submit_hover_' + getLang() + '.png'); },
            function () { $(this).attr('src', 'img/button_ea_submit_' + getLang() + '.png'); }
        );
        $('#overlay_suggest_article input[type="image"]').mousedown(function () { $(this).attr('src', 'img/button_ea_submit_active_' + getLang() + '.png'); });
        $('#overlay_suggest_article input[type="image"]').mouseup(function () { $(this).attr('src', 'img/button_ea_submit_hover_' + getLang() + '.png'); });
        
        $('#overlay_suggest_article input[type="image"]').click(function () {
            $(this).attr('disabled', 'disabled').attr('src', 'img/button_ea_submit_disabled_' + getLang() + '.png');
            var name = $('#overlay_suggest_article #name').val();
            var email = $('#overlay_suggest_article #email').val();
            var recipient_name = $('#overlay_suggest_article #recipient_name').val();
            var recipient_email = $('#overlay_suggest_article #recipient_email').val();
            var comment = $('#overlay_suggest_article textarea').val();
            var suggest_url = $('#overlay_suggest_article 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_suggest_article .message').addClass('bg_error');
                    $('#overlay_suggest_article .message').html(message);
                    $('#overlay_suggest_article input[class="submit"]').removeAttr('disabled').attr('src', 'img/button_ea_submit_' + getLang() + '.png');
                    setTimeout(function() {
                        $('#overlay_suggest_article .message').removeClass('bg_error');
                        $('#overlay_suggest_article .message').html('&nbsp;');
                    }, 5000);
                }
                else if (status == 'ok') {
                    $('#overlay_suggest_article .message').addClass('bg_ok');
                    $('#overlay_suggest_article .message').html(message);
                    setTimeout(function() {
                        $('#overlay_suggest_article .message').removeClass('bg_ok');
                        $('#overlay_suggest_article .message').html('&nbsp;');
                        $('#overlay_suggest_article #name').val('');
                        $('#overlay_suggest_article #email').val('');
                        $('#overlay_suggest_article #recipient_name').val('');
                        $('#overlay_suggest_article #recipient_email').val('');
                        $('#overlay_suggest_article textarea').val('');
                        $('#overlay_suggest_article input[class="submit"]').removeAttr('disabled').attr('src', 'img/button_ea_submit_' + getLang() + '.png');
                    }, 3000);
                    
                    setTimeout(function () {
                        var api = $('#overlay_suggest_article').overlay();
                        api.close(); // Close overlay window
                    }, 5000);
                }
            });
            return false;
        });
    }
    
    // Fancybox
    if ($('*').is('a[rel="fancybox"]')) {
        $('a[rel="fancybox"]').fancybox();
    }
    
    // Hide generated img after #overlay_suggest_article
    // jquery_accordion.js causes this weird bug, exists only in Safari & Chrome
    $('img:last').hide();
});

