$(document).ready(function() {

var newslettermsg = 'Newsletter Sign Up: ENTER EMAIL',
    emailform = $('#newsletter #email'),
    archive_tree = $('.blog_archives > ul');

archive_tree.treeview({
	animated: 200,
	collapsed: false,
	unique: true
});

if (!archive_tree.hasClass('short'))
    $('.blog_archives > ul ul ul').hide();

$('#nav > div > ul > li')
    .mouseover(function() {
        $(this).addClass('hover');
    }).mouseout(function() {
        $(this).removeClass('hover');
    });
$('#nav > div li li')
    .mouseover(function() {
        $(this).addClass('hover');
    }).mouseout(function() {
        $(this).removeClass('hover');
    });

emailform
    .val(newslettermsg)
    .focus(function() {
        if (emailform.val() == newslettermsg)
            emailform.val('');
    }).blur(function() {
        if (emailform.val() == '')
            emailform.val(newslettermsg);
    });

/**
 * Filenames is used for the option text. The two character state initials
 * are only for list links and are not used in the dropdown. The list 
 * items which this form derives from must be in this format:
 *      <li><a href='/file/states/New_York.pdf'>NY</a></li>
 */
$('#states').append("<form name='state_select'><div><select name='state'></select></div></form>");
$("form[name='state_select'] select[name='state']")
    .append(
        $('#states a')
            .map(function() {
                var link = $(this).attr('href'),
                    name = link.substring(13, link.lastIndexOf('.')).replace(/_/g, ' ');
                return '<option value="'+link+'">'+name+'</option>';
            })
            .get()
            .join())
    .change(function() { 
        window.location.pathname = $(this).val(); 
    });
$('#states ul').remove();

$('#content > div.maps > div.page').append("<p><a class='map' href='http://map.measureofamerica.org/maps.aspx'>Launch the interactive maps</a></p><p><a class='map' href='http://map.measureofamerica.org/maps.aspx'><img src='/wp-content/themes/moa/img/map-lg.jpg' /></a></p>");

$('#content > div.maps a.map').click(function() {
    var ah = screen.availHeight - 60,
        aw = screen.availWidth - 10,
        x, y;
    if (document.all) {
        x = window.screenTop;
        y = window.screenLeft;
    } else if (document.layers) {  
        x = window.screenX;
        y = window.screenY;
    }
    var MyWin = window.open($(this).attr('href'),'','width='+aw+',height='+ah+',top=0,screenY='+y+',left=0,screenX='+x+',scrollbars=0,toolbar=0,status=1,resizable=0,location=0');
    return false;
});

/**
 * Home page transitions using cycle plugin
 */

$('#pub-cycle').cycle({
    fx:      'scrollHorz',
    timeout:  0,
    prev:    '#prevPub',
    next:    '#nextPub'
});

$('#endorse-cycle').cycle({
    fx: 'fade',
    random: 1,
    timeout: 8000
});

$('#tools-cycle').cycle({
    fx:     'fade', 
    timeout: 0, 
    speed: 500,
    pager:  '#tools-nav', 
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#tools-nav li:eq(' + idx + ') a'; 
    } 
});

});
