$(document).ready(function() {

    /* set ie6 variable */
    $.browser.msie6 =
    $.browser.msie
    && /MSIE 6\.0/i.test(window.navigator.userAgent)
    && !/MSIE 7\.0/i.test(window.navigator.userAgent);

    imageItem = new Array();
    imageItem[0] = new Image();
    imageItem[1] = new Image();
    imageItem[2] = new Image();
    imageItem[3] = new Image();
    imageItem[4] = new Image();
    imageItem[5] = new Image();
    imageItem[6] = new Image();
    imageItem[7] = new Image();

    imageItem[0].src = "/images/layout/tabs/google-on.png";
    imageItem[1].src = "/images/layout/tabs/google.png";
    imageItem[2].src = "/images/layout/tabs/yahoo-on.png";
    imageItem[3].src = "/images/layout/tabs/yahoo.png";
    imageItem[4].src = "/images/layout/tabs/google-on-solid.gif";
    imageItem[5].src = "/images/layout/tabs/google-solid.gif";
    imageItem[6].src = "/images/layout/tabs/yahoo-on-solid.gif";
    imageItem[7].src = "/images/layout/tabs/yahoo-solid.gif";


    /* Change search fields */
    $('#searchbox a.google').click(function(){
        $('#yahooSearchWrap').hide();
        $('#googleSearchWrap').show();
        return false;
    });

    $('#searchbox a.yahoo').click(function(){
        $('#googleSearchWrap').hide();
        $('#yahooSearchWrap').show();
        return false;
    });

    /* slide notice in on page load */
    $('.notice').hide();
    if($.browser.msie){
        $('.notice').show();
    } else {
        $('.notice').slideDown("medium");
    }

    /* close notice */
    $('.notice a.close').click(function(){
        $('.notice').slideUp("fast");
    });

    /* causes - moreinfo links */
    $('* .moreinfo').hide();
    $('a.moreinfoLink').click(function(){
        $('* .moreinfo').hide();
        if($.browser.msie6){
            $(this).parent().siblings('.moreinfo').show();
           } else {
            $(this).parent().siblings('.moreinfo').fadeIn('slow');
        }
        return false;
    });

    /* tooltips */
    function tooltip(){
        /* CONFIG */
            xOffset = 33;
            yOffset = -45;
            // these 2 variable determine popup's distance from the cursor
            // you might want to adjust to get the right result
        /* END CONFIG */
        $("a.tooltip").hover(function(e){
            this.t = this.title;
            this.title = "";
            $("body").append("<p id='tooltip'>"+ this.t +"</p>");
            $("#tooltip")
                .css("top",(e.pageY - xOffset) + "px")
                .css("left",(e.pageX + yOffset) + "px")
                .fadeIn("fast");
        },
        function(){
            this.title = this.t;
            $("#tooltip").remove();
        });
        $("a.tooltip").mousemove(function(e){
            $("#tooltip")
                .css("top",(e.pageY - xOffset) + "px")
                .css("left",(e.pageX + yOffset) + "px");
        });
    };

    tooltip();


    /***************************************************/
    /* FORM HILIGHTING & INSTRUCTIONS                  */
    /***************************************************/

    // hide all instructions
    $('.defaultForm li > .instructions').hide();

    function hilite(elem){
        // remove any previously selected hilite's and instructions if any
        $('.defaultForm li.selected .instructions').hide();
        $('.defaultForm li.selected').removeClass("selected");

      // hilite and show instructions if any
      elem.parent('li').addClass("selected");
      elem.parent('label').parent('li').addClass("selected");
      elem.siblings('.instructions').show();
      elem.parent().siblings('.instructions').show();
    }


    // on focus tests for various types of form elements
    var inputFocused = false;

    // input fields
    $('.defaultForm * > input').focus(function () {
        // check to see if input field is not a button
        if( !$(this).attr("src") || $(this).attr("value") != "submit"){
            hilite($(this));
        }
        inputFocused = true;
    });

    // radio and checkboxes only get detected with onclick (or tab) for chrome/safari
    if(!inputFocused){
        $('.defaultForm * > input').click(function () {
            // check to see if input field is not a button
            if( !$(this).attr("src") || $(this).attr("value") != "submit"){
                hilite($(this));
            }
        });
    }

    // text areas
    $('.defaultForm * > textarea').focus(function () {
      hilite($(this));
    });

    // select boxes
    $('.defaultForm * > select').focus(function () {
      hilite($(this));
    });

    $(function(){
        $('a.new-window').click(function(){
            window.open(this.href, "terms_window", "width=960,height=680,status=0,menubar=0,toolbar=0,location=0,directories=0,scrollbars=1");
            return false;
        });
    });

});