/// <reference path="jquery-vsdoc.js" />

$(document).ready(function() {
    navMain();
    navBenefits();
    adjustFontPrint();
    formValidation();
//    charts();
//    dev();
});

function navMain() {
    var speed = 200;
    $('#header .navigation ul').css('opacity', 0).hide();
    //$('#header .navigation #ni3 ul a').each(function() { $(this).width($(this).closest('#ni3').width()); });
    
    $('#header .navigation > li > a').hover(function() {
        $('#header .navigation ul').stop().animate({opacity:0}, speed, '', function() { $(this).hide(); });
        $(this).next().stop().show().animate({opacity:1}, speed);        
    }, function() { });
    
    $('#header .navigation ul').hover(function() { }, function() {
        if ($(this).find('.current').length == 0) {
            $(this).stop().animate({opacity:0}, speed);
            $('#header .navigation .current').parents('ul').stop().show().pause(speed * 2.5).animate({opacity:1}, speed * 2.5);
        }
    });
    
    $('#header .navigation').hover(function() { }, function() {
        $(this).find('ul').stop().animate({opacity:0}, speed);
        $('#header .navigation .current').parents('ul').stop().show().pause(speed * 2.5).animate({opacity:1}, speed * 2.5);
    });
    
    navShowCurrent();
}

function navShowCurrent() {
    $('#header .navigation .current').parents('ul').show().css({ opacity: 1 }).end().closest('ul').prev().addClass('current');
}

function navBenefits() {
    var speed = 200;
    $('#benefits li').clickpanel();
    if (jQuery.browser.msie) return;
    $('#benefits li').hover(function() {
        $('#benefits li').not(this).stop().fadeTo(speed, 0.6);
    }, function() {
        $('#benefits li').not(this).stop().fadeTo(speed, 1);
    });
}

function adjustFontPrint() {
    var host = "http://" + window.location.host;
    $('a[href^="/"]').each(function() {
        $(this).attr('href', host + $(this).attr('href'));
    });
    
    if ($('#fontprint').length == 0) return;
    
    $('#fontprint li a').removeClass();
    var size = readCookie('mircera-font');
    if (size) {
        $('#fontprint li a[title=' + size + ']').addClass('current');
        $('#content').addClass(size);
    }
    else $('#fontprint li a:eq(0)').addClass('current');

    $('#fontprint li a').click(function() {
        console.log('click');
        $('#fontprint li a').removeClass();
        $('#content').removeClass().addClass($(this).addClass('current').attr('title'));
        createCookie('mircera-font', $(this).attr('title'), 7);
    });

    $('#fontprint .print').click(function() {
        window.print();
        return false;
    });
}

function formValidation() {
    $('fieldset span').addClass('invalid');
    $('.formbutton').click(function() {
        var valid = true;
        $('fieldset .invalid').prev('input, textarea, select').removeClass('error').end().each(function() {
            if ($(this).css('visibility') == 'visible') {
                $(this).prevAll('input, textarea, select').addClass('error')
                valid = false;
            };
        });
        if (!valid) $('.required').addClass('requiredfocus').text('Please enter required information and ensure it is correct.');
    });
}

function charts() {
    var speed = 200;
    $('.chart p').css({opacity:0});
    $('.chart img, .chart a').hover(function() {
        $(this).parent().find('p').stop().animate({ opacity: 1 }, speed);
    }, function() {
        $(this).parent().find('p').stop().animate({ opacity: 0 }, speed);
    });
}

function dev() {
    $('a[href=#]').css({'background-color':'#fdd017'});
}
