jQuery(document).ready(function () {
    
    if(top != window) {
        jQuery('body').addClass('embedded');
    }
    
    // jQuery('#footer_image').scrollFollow({
    //     speed: 750,
    //     relativeTo: 'bottom',
    //     offset: -350
    // });
    
    var col_sub_height = $('#column_sub').height();
    var col_main_height = $('#column_main').height();
    var image_height = $('#footer_image').height();
    
    if(col_main_height > (col_sub_height + image_height)){
        $('#column_sub').height(col_main_height - image_height);
    }
    
    //add hover class for drop-down menus
    jQuery('li').hover(
          function () {
            jQuery(this).addClass('hover');
          }, 
          function () {
            jQuery(this).removeClass('hover');
          }
        );
        
    jQuery('.calendar_monthly table tr').each(function(){
        jQuery(this).children('td').filter(':eq(6)').addClass('last');
    });
    
    jQuery('#main_nav li:first-child').addClass('first');
    jQuery('#main_nav li:last-child').addClass('last');
    jQuery('#main_nav li:only-child').addClass('only');
    
    jQuery('.calendar_week li, .calendar_monthly td').each(function(){
        var self = jQuery(this);
        jQuery(this).find('span.featured').each(function(){
            if( jQuery(this).html() == 'y' ){
                self.addClass('featured');
            }
        });
    });
    
    Cufon.replace('#column_main h1, h2.zapfino', { fontFamily: 'Zapfino' });
    
    jQuery('div.printMailShare li.print a').click(function() {
        window.print();
        return false;
    });
    
    
    jQuery('div.printMailShare li.mail a').click(function () {
        container = jQuery(this).parent().parent().next();
        
        //if main container is open
        if (container.is(':visible')) {
            //if other container div is open, close everything and re-open with the email div visible
            if (container.find('div.share_content').is(':visible')) {
                //hide share and outer div
                container.removeClass('share_box');
                container.hide();
                container.find('div.share_content').hide();
                //show email and outer div
                container.addClass('email_box');
                container.find('div.email_content').show();
                container.show();
            } else {
                container.removeClass('email_box');
                container.hide();
                container.find('div.email_content').hide();
            }
        } else {
            container.addClass('email_box');
            container.find('div.email_content').show();
            container.show();
        }
        return(false);
    });
    
    jQuery('div.printMailShare li.share a').click(function () {
        container = jQuery(this).parent().parent().next();
        
        if (container.is(':visible')) {
            //if other container div is open, close everything and re-open with the share div visible
            if (container.find('div.email_content').is(':visible')) {
                //hide email and outer div
                container.removeClass('email_box');
                container.hide();
                container.find('div.email_content').hide();
                //show share and outer div
                container.addClass('share_box');
                container.find('div.share_content').show();
                container.show();
            } else {
                container.removeClass('share_box');
                container.hide();
                container.find('div.share_content').hide();
            }
        } else {
            container.addClass('share_box');
            container.find('div.share_content').show();
            container.show();
        }
        return(false);
    });
    
    jQuery('.emailShare_box a.close').click(function() {
        whichDiv = jQuery(this).attr('rel');
        jQuery(this).parent().hide().closest('.emailShare_box').removeClass(whichDiv + '_box').hide();//hide containing div and outer div
        return(false);
    });
    
    // //add span to title and enable tooltip
    // jQuery('#column_side_buttons a').each(function() {
    //    x = jQuery(this).children().html();
    //    jQuery(this).children().html('');
    //    jQuery(this).attr('title' , x);
    // });
    // 
    // if(typeof jQuery('#column_side_buttons *').tooltip == 'function') {
    //     jQuery('#column_side_buttons *').tooltip({
    //         showURL: false,
    //         track: true,
    //         top: 5
    //     });
    // }
    
    //jQuery(".blog_teaser, .articles_teaser, .tips_teaser, .media_teaser, .links_teaser, .news_teaser").hide();
    jQuery(".blog_teaser:first, .articles_teaser:first, .tips_teaser:first, .media_teaser:first, .links_teaser:first, .news_teaser:first").show();

    jQuery(".blog_title, .articles_title, .tips_title, .media_title, .links_title, .news_title").click(function() {
        var the_id = jQuery(this).attr('id');
        var id_num = the_id.substring(the_id.lastIndexOf('_')+1);
        var channel_name = the_id.substring(0, the_id.indexOf('_'));

        toggleContent(channel_name, id_num);
        
        return false;
    });

    var toggleContent = function(channelName, idNum) {
            jQuery('.'+channelName+'_teaser:visible').slideUp(function(){
                jQuery('#'+channelName+'_teaser_'+idNum).slideDown();
            });
    };
        
    // Fix button blur on IE
    jQuery('a.button').click(function() {
        this.blur();
        return false;
    });
    

    jQuery('a[rel="_blank"], a.paypal_link').popUp();
    
    jQuery('#year_archive li.show ul').show();
    jQuery('#year_archive li.year span').click(function() {
        if (jQuery(this).parent().find('ul').is(':visible')) {
            jQuery(this).parent().find('ul').hide('slow');
        } else {            
            jQuery('#year_archive li.year ul').hide('slow');
            jQuery(this).parent().find('ul').show('slow');
        }
    });
    
    
    jQuery('.calendar_week li').autoHeight();
    
    /* this removes the submit button from inside a form tag, places it after
    the white box on the page and 'attaches' it to the bottom of the white box,
    then binds a submit action to the form it came from, so it continues to work */
    $('input.attached_button', '#column_main form').each(function(i){
        var button = $(this);
        var form = $(this).parents('form');
        var value = button.attr('value');
        var type = button.attr('type');
        var name = button.attr('name');
        var cssClass = button.attr('rel');
        button.remove();
        
        var new_button = $('<button type="'+ type +'" class="'+ cssClass +'" name="'+ name +'">'+ value +'</button>');
        
        if(i == 0)
        {
            new_button.insertAfter('.box_white_full').wrap('<div class="attached_button_container"></div>').click(function(){
                form.submit();
            });
        }
        else
        {
            new_button.appendTo('.attached_button_container').click(function(){
                form.submit();
            });
        }
    });
    
    $('button.button_send, button.subscribe').click(function(){
        $(this).parents('form').submit();
    });
    
    
    $('.login_form .terms_agree').show();
    $('.login_form input[name="username"], .login_form input[name="password"]').val('').attr('disabled', true);
    
    $('.login_form input#agree').click(function(){
        if($(this).is(':checked')){
            $('.login_form input[name="username"], .login_form input[name="password"]').attr('disabled', false);
            $('.terms_of_service').slideUp();
        } else {
            $('.login_form input[name="username"], .login_form input[name="password"]').attr('disabled', true);
            $('.terms_of_service').slideDown();
        }
    });
    
    $('.login_form input[name="username"], .login_form input[name="password"]').click(function(){
        if(!$('.login_form input#agree').is(':checked')){
            alert('You need to agree to the Terms of Service before logging in.');
        } 
    });
    
    // dumb hack to move the header above both columns on the guest services page.
    $('.col_big.two_column h3').remove().clone().prependTo('.col_big');
    
	// $.jqwidont.auto(false); // disable it
    $('.col_small ul li a').widont();
    
    $('#content h1').each(function(){
        var text = $(this).text();
        var pattern = /\((.*?)\)/;
        var matches = text.match(pattern);
        if(matches){
            $(this).text(text.replace(matches[0], ''));
            $(this).after('<p class="h1_sub_title">'+ matches[1] +'</p>');
            Cufon.replace('#content p.h1_sub_title', { fontFamily: 'Zapfino' });
        }
    });
    
    /* This is to remove all instances of {image_N} from the search results page */
    $('body.site_search li').each(function(){
        var regex = /({image_\d+})/gim; 
        var input = $(this).html(); 
        var new_text = input.replace(regex, '');
        $(this).html(new_text);
    });
    
}); // jQuery(document).ready()

jQuery.fn.popUp = function(options)
{
    var settings = {
        name: 'newWindow',
        width: 500,
        height: 600,
        left: false,
        top: false,
        scrollbars: 'yes',
        resizeable: 'no',
        statusbar: 'no',
        menubar: 'no',
        toolbar: 'no',
        href: false
    }

    if(options) {
        jQuery.extend(settings, options);
    };

    this.each(function(){
        jQuery(this).click(function(){
            var rel = jQuery(this).attr('rel');
            var href = settings.href ? settings.href : jQuery(this).attr('href');
            if(!rel || rel == '_blank'){
                window.open(href, settings.name);
            } else {
                var rel_split = rel.split('|');
                var width = rel_split[0] ? rel_split[0] : settings.width;
                var height = rel_split[1] ? rel_split[1] : settings.height;
                var scrollbars = rel_split[2] ? rel_split[2] : settings.scrollbars;
                var leftPos = settings.left ? settings.left : (screen.width-width)/2;
                var topPos = settings.right ? settings.right : (screen.height-height)/2;
                
                var config = 'width='+ width +',height='+ height +', \
                            left='+ leftPos +',top='+ topPos +', \
                            scrollbars='+ scrollbars +', \
                            resizable='+ settings.resizeable +', \
                            statusbar='+ settings.statusbar +', \
                            menubar='+ settings.menubar +', \
                            toolbar='+ settings.toolbar;
                    
                window.open(href,settings.name, config);
            }
            return false;
        });
    });
}



jQuery.fn.autoHeight = function(options) 
{
    var settings = {
    	minHeight   : false,
        limitHeight  : false,
        ignore	: ''
    }
	
    if(options) {
        jQuery.extend(settings, options);
    };

    var maxHeight = 0;

	this.not(settings.ignore).each(function(){
        if ($(this).height() > maxHeight){
        	if(settings.limitHeight && maxHeight >= settings.limitHeight) {
        		maxWidth = settings.limitWidth;
        	} 
        	else if(settings.minHeight && maxHeight <= settings.limitHeight)
        	{
        		maxHeight = settings.minHeight;
        	} 
        	else 
        	{
        		maxHeight = $(this).height();
        	}
        }
	});	 
	
	this.not(settings.ignore).height(maxHeight);
}

function navHover(which) {
    if (document.getElementById(which.id)) {
        cur = document.getElementById(which.id).className;
        
        if (cur == 'buttonLarge') {
            document.getElementById(which.id).className = 'buttonLargeHover'
        } else if (cur == 'buttonSmall') {
            document.getElementById(which.id).className = 'buttonSmallHover'
        }
    }
}

function navReset(which) {
    if (document.getElementById(which.id)) {
        cur = document.getElementById(which.id).className;
        
        if (cur == 'buttonLargeHover') {
            document.getElementById(which.id).className = 'buttonLarge'
        } else if (cur == 'buttonSmallHover') {
            document.getElementById(which.id).className = 'buttonSmall'
        }
    }
}

function navJump(where) {
    window.location = where;
} 


jQuery.fn.equalCols = function(){
	//Array Sorter
	var sortNumber = function(a,b){return b - a;};
	var heights = [];
	//Push each height into an array
	$(this).each(function(){
		heights.push($(this).height());
	});
	heights.sort(sortNumber);
	var maxHeight = heights[0];
	return this.each(function(){
		//Set each column to the max height
		$(this).css({'height': maxHeight});
	});
};


