$(document).ready(function () {

    var $articleContainer = $('#blog-container'),
        $trigger = $('a#get-more-news'),
        loadingClass = 'loading-more-news',
        val, poststr, isLoading = false;

    $trigger.live('click', function (event) {        
        if (!isLoading) {
            isLoading = true;
            val = parseInt($('#limit').val());
            $trigger.addClass(loadingClass);
            $articleContainer.append('<div id="blog-dummy"></div>');
            $('#limit').val(val += 1);
            $articleContainer.find('#blog-dummy').load('/blog.php?limit=' + ($('#limit').val()) + '&y=' + ($('#y').val()) + '&m=' + ($('#m').val()) + '&tag=' + ($('#tag').val()) + '&id_categoria=' + ($('#id_categoria').val()) + ' .blog-wrapper #blog-container .post', function (response, status, xhr) {
                if (status == "error") {
                    alert("Siamo spiacenti, ma si è verificato il seguente errore: " + xhr.status + " " + xhr.statusText + "; si prega di riprovare in seguito o ricaricare la pagina.");
                }
                $('#blog-dummy').children().unwrap();
                $trigger.removeClass(loadingClass);
                isLoading = false;
                if (1 * $('#num_post').val() < 1 * $('#limit').val()) {
                    $('#get-more-news').hide();
                }
            });
        }
        event.preventDefault();
    });

    $('ul#post-year-archive-list').each(function () {

        $(this).find('ul').hide().end().find('li.current-month').parent().show();
        $(this).children('li').children('a').bind('click', function (event) {
            $(this).parent().find('ul').toggle();
            event.preventDefault();
        });

    });
	
	function validateURL(url) {
  		return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
	}

    function countError(id) {
        id.parent().addClass("error");
        error++;
    }

    if ($("#comment_post").length) {
        $.get("/utils/token.php", function (txt) {
            $("#comment_post").append('<input type="hidden" name="ts" value="' + txt + '" />');
        });
    }

    $('#comment_post_submit').bind('click', function () {
        error = 0;
        $('.error').removeClass('error');
        $('.mandatory').each(function () {
            switch ($(this).attr('id')) {

            case ('user_privacy'):
                if (!$(this).attr('checked')) {
                    countError($(this));
                }
                break;

            case ('user_email'):
                if (!$(this).attr('value').match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/)) {
                    countError($(this));
                }
                break;

            default:
                if (!$(this).val()) {
                    countError($(this));
                }

            }
        });
		if($('#user_web_address').val() && $('#user_web_address').val() != "http://"){
			if (!validateURL($('#user_web_address').attr('value'))) {
				countError($('#user_web_address'));
				error++;
			}
		}
        if (error) {
            return false;
        } else {
            $(this).addClass('disabled-button');
            $(this).attr('disabled","disabled');
			if($('#user_web_address').val() == "http://"){
				$('#user_web_address').val("");	
			}
            $("#comment_post").submit();
        }
    });

    $(window).bind('scroll', function () {
        if (!$("a[rel='_top']").length) {
            if ($('#content').height() >= 1000) {
                $('#footer').append('<a href="#" rel="_top" class="sprite-misc back-to-top png-fix" title="'+titleTop()+'">top</a>');
            }
        }
    });

});
