$(function() {
    //$("#Content .TextContent .news p").hide();
    //$("#Content .TextContent .news p:first").show();
    //$("#Content .TextContent .news p:first:last").parent(".news").find("a").hide();
});


$(function() {
    /*$('input[type=radio]').click(function() {
        $(this).siblings().removeAttr('checked');
    });*/


    $('#Content div.finalTask').hide();
    $('#Content ul#fieldNavigator li').filter(':first').addClass('active');
    $('#Content ul#fieldNavigator li').filter(':last').addClass('last');
    $('.nextStep').filter(':last').parent().prev().find('.nextStep').addClass('last');
    $('#Content div.fieldGroup').filter(':first').show();
    $('#Content div.fieldGroup').filter(':last').find('.nextStep').hide();

    $('#Content ul#fieldNavigator li').click(function () {
		var rell = $(this).find('a').attr('rel');
		var nrTo = parseInt(rell.replace('fieldGroup', ''));
		var current = parseInt($('.fieldGroup').filter(':visible').attr('id').replace('fieldGroup', ''));
		
        if (nrTo > current && !verifySubmit($('#' + rell).find('p'))){
			alert('Ti chiediamo di inserire tutti i dati prima di andare avanti');
            return false;
		}
		if(nrTo < current || (nrTo==current+1 && verifySubmit($('#' + rell).find('p')))){
			if ($(this).hasClass('last'))
				$('#Content div.finalTask').show();
			$('.fieldGroup').hide();
			$(this).siblings().removeClass('active');
			$(this).addClass('active');
			$('#' + rell).fadeIn();
			return false;
		}
    });
    $('.nextStep a').click(function() {
        $('html, body').animate({scrollTop:360}, 300);        
        if (!verifySubmit($(this).parent())){
			alert('Ti chiediamo di inserire tutti i dati prima di andare avanti');
            return false;
		}

        $('.fieldGroup').hide();
        $('#Content ul#fieldNavigator li.active').next().addClass('active');
        $('#Content ul#fieldNavigator li.active:last').prev().removeClass('active');

        $('#' + $(this).attr('rel')).fadeIn();
        if ($(this).parent().hasClass('last') || $('#' + $(this).attr('rel')).next('div').length==0){
            $('#Content div.finalTask').show();
        }
        return false;
    });

    $("#Content .news a.link").click(function () {
        if (!$(this).hasClass('active')) {
            $("#Content .news").hide();
            $(this).parent().show();
            $(this).parent('.news').css('width', '617px');
            $(this).prev('.text').css('height', 'auto').slideDown('slow');
            $(this).addClass('active');
            $(this).text('Chiudi');
        } else {
            $(this).removeClass('active');
            $(this).text('Leggi...');
            $(this).prev('.text').animate({'height':'40px'}, 'slow');
            $(this).parent('.news').css('width', '310px');
            $("#Content .news").show();
        }
        return false;
    });

    $(".Box").find('.Content').hide();

    $(".Box").each(function() {
        if (readCookie($(this).attr('id')))
            if (readCookie($(this).attr('id')) == 'active') {
                $(this).addClass('active');
            }
    });

    $(".Box.active .Content").show();
    $(".Box.active .Title").find('img.plus').attr('src', 'images/minus-sign.png');
    $(".Box").find('.Title').click(function() {
        if ($(this).parent().hasClass('active')) {
            $(this).parent().removeClass('active');
            $(this).parent().find('.Content').slideUp();
            $(this).find('img.plus').attr('src', 'images/plus-sign.png');
            eraseCookie($(this).parent().attr('id'));
        } else {
            $(this).parent().addClass('active');
            $(this).parent().find('.Content').slideDown();
            $(this).find('img.plus').attr('src', 'images/minus-sign.png');
            createCookie($(this).parent().attr('id'), 'active', 7);
        }
    });
});

function createCookie(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}
