/*
 * Font replacements and jquery initialization
 */

//Minion font replacement
Cufon.set('fontFamily','MinionPro');
Cufon('h1',{hover:false});
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');


jQuery(document).ready(function() {

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


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

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

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

    jQuery("dl#faq dt").each(function(i) {
            jQuery(this).addClass('faq-question');
            jQuery(this).attr('id', 'question-'+i);
            jQuery(this).click(function(){
                jQuery("#answer-"+i).slideToggle('slow', function() {
                    jQuery('#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
jQuery(window).load(function()
{
     // validation for the contact form
     if (jQuery('#thoughts').length > 0)
     {
        jQuery("#thoughts").validator();

        jQuery("#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
                        jQuery.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)
    {
        jQuery('#content').height('auto');
    }
    var leftheight = jQuery('#primary').height();
    var centerheight = jQuery('#content').height();
    var rightheight = jQuery('#secondary').height();

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