$(document).ready(function() {
    $('#nav li:not(li li)').hover(function() {
        $('div.flyout', this).show().end().children('a:first').addClass('active');
    },
	function() {
	    $('div.flyout', this).hide().end().children('a:first').removeClass('active');
	});
    $('.tooltip').hover(function(e) {
        $('#tooltip').remove();
        //this.tip = this.title;
        this.tip = $(this).attr("popup");
        //this.alt = '';
        //this.title = '';
        //$(this).attr("popup", "");
        $('body').append('<p id="tooltip">' + this.tip + '</p>');
        var tipHeight = $('#tooltip').height();
        $('#tooltip').css('top', (e.pageY - 15 - tipHeight) + 'px').css('left', (e.pageX + 10) + 'px').fadeIn('fast');
        $('.tooltip').mousemove(function(e) {
            $('#tooltip').css('top', (e.pageY - 15 - tipHeight) + 'px').css('left', (e.pageX + 10) + 'px');
        });
    },
	function() {
	    //this.title = this.tip;
	    $('#tooltip').remove();
	});
    jQuery.fn.fadeToggle = function(speed, easing, callback) {
        return this.animate({ opacity: 'toggle' }, speed, easing, callback);

    };
    $('.promo-search-submit a.promo-search-advanced').live("click", function() {
        $('.advanced').fadeToggle();
        return false;
    });
    $('#promo-search li a').live("click", function() {
        var theFields = $(this).attr('href').split('#')[1];
        var theOtherFields;
        if (theFields === "myself") {
            newForm = $("#promo-search-myself");
            oldForm = $("#promo-search-others");
            theOtherFields = 'others';
        } else {
            newForm = $("#promo-search-others");
            oldForm = $("#promo-search-myself");
            theOtherFields = 'myself';
        }
        $('.advanced').hide();
        $('#promo-search-' + theOtherFields).hide();
        $('#promo-search-' + theFields).show();
        transferForm(oldForm, newForm);
        $('#promo-search li').removeClass('current');
        $(this).parent('li').addClass('current');
        return false;
    });

    $('#promo-search-alt li a').live("click", function() {
        var theFields = $(this).attr('href').split('#')[1];
        var theOtherFields;
        if (theFields === "myself-alt") {
            newForm = $("#promo-search-myself-alt");
            oldForm = $("#promo-search-others-alt");
            theOtherFields = 'others-alt';
        } else {
            newForm = $("#promo-search-others-alt");
            oldForm = $("#promo-search-myself-alt");
            theOtherFields = 'myself-alt';
        }
        $('.advanced').hide();
        $('#promo-search-' + theOtherFields).hide();
        $('#promo-search-' + theFields).show();
        transferForm(oldForm, newForm);
        $('#promo-search-alt li').removeClass('current');
        $(this).parent('li').addClass('current');
        return false;
    });

    if ($('#promo-search input[name=LessThanTwoYears]').is(':checked')) {
        $('.advanced').show().find('input,select').addClass('required');
    }

    $('#promo-search input[name=LessThanTwoYears]').live("click", function() {
        if ($(this).is(':checked')) {
            $('.advanced').show().find('input, select').addClass('required');
            $('p.advanced-instructions').show();
        }
        else {
            $('.advanced').hide().find('input, select').removeClass('required');
            $('p.advanced-instructions').show();
        }
    });

    $('a[rel=new-window]').click(function() {
        window.open($(this).attr('href'));
        return false;
    });
});

function transferForm(oldForm, newForm) {
    jQuery.each(oldForm.find('input:not(input[name=SearchingOnSelf]), select'), function(i, val) {
        var name = $(val).attr('name');
        var value = $(val).val();
        if (name != '') {
            newForm.find('input[name=' + name + '], select[name=' + name + '] ').val(value);
        }
    });
}