/*
 * Font replacements and jquery initialization
 */

//Minion font replacement
Cufon.set('fontFamily','MinionPro');
Cufon('h1',{hover:true});
Cufon('#businessunit');
Cufon('.slug-welcome h1');
Cufon('.hometab-content h2');

//Myriad font replacement
Cufon.set('fontFamily','MyriadPro');
Cufon('.hometab-content h2 span');
//Cufon('#utilitynav',{hover:true});
//Cufon('#contactinfo span',{hover:true});
Cufon('.navitem',{hover:true});
Cufon('#subsidiary h2');
Cufon('#home-subsidiary h2');
Cufon('.entry-content h2');
Cufon('.entry-content h3');
Cufon('.aside h3');
Cufon('h2.entry-title',{hover:true});
Cufon('legend');


$(document).ready(function() {

    // javascript for navigation
    $('#navigation > li ul li:first-child').addClass('first');
    $('#navigation > li ul li:last-child').addClass('last');
    $('#navigation > li')
            .hover( function() { $(this).addClass('hover').find('div').show(); }, function() { $(this).removeClass('hover').find('div').hide(); })
            //.each(function() { if($(this).position().left > 678) { $(this).addClass('r1'); } });


     // homepage tabs
    $("#tabs").tabs();
    $("#tabs").tabs("rotate",10000);
    $("#tabs").tabs({ fx: { opacity: 'toggle' } });
    $("#tabs").show();

    // manually stop tab rotation onclick
    $("a.hometab").click(function() {
        $("#tabs").tabs( "rotate" ,0,false);
    });
    
    
    // FAQ pages
    $("a[name^='faq-']").each(function() {
            $(this).click(function() {
                    if( $("#" + this.name).is(':hidden') ) {
                            $("#" + this.name).slideToggle('slow');
                            $("#" + this.name).fadeIn("slow");
                    } else {
                            $("#" + this.name).slideToggle('slow');
                            $("#" + this.name).fadeOut("slow");
                    }
                    return false;
            });
    });

    // Defintion Changes //
    $("dl#faq dd").each(function(i) {
            $(this).addClass('faq-answer');
            $(this).attr('id','answer-'+i);
    });

    $("dl#faq dt").each(function(i) {
            $(this).addClass('faq-question');
            $(this).attr('id', 'question-'+i);
            $(this).click(function(){
                $("#answer-"+i).slideToggle('slow', function() {
                    $('#content').height('auto');
                    checkheight();
                });
            });
    });

    if (document.getElementById("map_canvas"))
    {
        initializeMaps();
        window.setTimeout(function() { checkheight(true); }, 1000);
    }
    // check to see if sidebars are too tall
    checkheight(true);
});

// executes when complete page is fully loaded, including all frames, objects and images
$(window).load(function()
{
     // validation for the contact form
     if ($('#thoughts').length > 0)
     {
        $("#thoughts").validator();

        $("#thoughts").bind("onFail", function(e, errors)  {
                // we are only doing stuff when the form is submitted
                if (e.originalEvent.type == 'submit') {

                        // loop through Error objects and add the border color
                        $.each(errors, function()  {
                                var input = this.input;
                                input.css({border: '2px solid red'}).focus(function()  {
                                        input.css({border: '1px solid #ccc'});
                                });
                        });
                }
        });
     }
});

function checkheight(setauto)
{
    if (setauto)
    {
        $('#content').height('auto');
    }
    var leftheight = $('#primary').height();
    var centerheight = $('#content').height();
    var rightheight = $('#secondary').height();

    // find which one is tallest;
    if (leftheight > centerheight)
    {
        $('#content').height(leftheight+60)
        centerheight = $('#content').height();
    }
    if (rightheight > centerheight)
    {
        $('#content').height(rightheight+40)
    }
}
