// Press Subnav onload. fade in the nav

var cpTriggers = ['tr_November292007','tr_October82007','tr_June142007','tr_May152007'];
// var cpTriggers = ['tr_June142007','tr_May152007','tr_March152007','tr_December272006','tr_August072006'];
var cpURL = '/press/ajax_content/';
var cpBase = '/press';
var cpPane = 'PressContent';

var pc = {
    click:function(e) {
       e = e ? e : window.event;
       var elem = Event.element(e);
        var pathname = new String(location.pathname);

        if ( pathname == '/press' )
        {
            location.hash=elem.getAttribute('pt');
            var ajax_url = '/press-ajax/'+elem.getAttribute('pt');
            new Ajax.Updater( cpPane, ajax_url, {method:'get', evalScripts:true, queue:'end'});
            $(cpPane).scrollTop=0;
            return false;
        }
        else if( pathname == '/press-archives')
        {
            location.hash=elem.getAttribute('pt');
            var ajax_url = '/press-ajax/'+elem.getAttribute('pt');
            new Ajax.Updater( cpPane, ajax_url, {method:'get', evalScripts:true, queue:'end'});
            $(cpPane).scrollTop=0;
            return false;

        }
        else 
        {
            if( pathname.indexOf('/press') == 0 )
                var url = '/press#'+ elem.getAttribute('pt');
            else if(pathname.indexOf('/press-archives') == 0)
                var url = '/press-archives#'+ elem.getAttribute('pt');


            try {
                location.pathname = url;
            }catch(e) {

            }

            var ajax_url = '/press-ajax/'+elem.getAttribute('pt');
            new Ajax.Updater( cpPane, ajax_url, {method:'get', evalScripts:true, queue:'end'});
            $(cpPane).scrollTop=0;
    
        }

    }

}


function bindContentPaneTriggers()
{

    var cpt = document.getElementsByClassName('article-trigger');

    for(i=0;i<cpt.length;i++)
    {

       var tmp = $(cpt[i]).pathname.replace('/press/','');
       var tmp = tmp.replace('press/','');
       var tmp = tmp.replace('/press-archives/','');
       var tmp = tmp.replace('press-archives/','');
        
       $(cpt[i]).setAttribute('pt',tmp);
       $(cpt[i]).href='javascript:void(0)';
       Event.observe( $(cpt[i]),'click',pc.click.bindAsEventListener() );
       
    }

}

// retrieve pane content via ajax.  The tr_ prefix is needed
// because otherwise the page will scroll to the id of the trigger
// element which in this case is not what we want
// update an element with content.  
// If it isn't visible make it so. 
function paneContentById(id)
{
    id = id.replace('tr_','')
    $(cpPane).style.overflow='auto';
    $(cpPane).morph('opacity: 1.0', {duration:2.0});
    new Ajax.Updater( cpPane, cpURL+id, {method:'get', evalScripts:true, queue:'end'});
    $(cpPane).scrollTop=0;

}

// if someone bookmarks and comes to a hashed page
// we want to do the right thing
function setCurrentTrigger()
{

    if(location.hash)
    {
    for(i=0;i<cpTriggers.length;i++)
    {
        if (location.hash)
        {
        if(location.hash == '#'+cpTriggers[i].replace('tr_',''))
        {
            paneContentById(cpTriggers[i].replace('tr_',''));
            return;
        }
        }
    }

    }

}

function pressOnload()
{
    bindContentPaneTriggers();
    setCurrentTrigger();
}

function doFadeIns() 
{
    f = document.getElementsByClassName('morphClear')
    g = document.getElementsByClassName('morphOpaque')
    for(i=0; i< f.length; i++)
    {
        f[i].style.overflow='auto';
        f[i].morph('opacity: '+mops['morphClear'], {duration:2.0} );
    }

    if( $(cpPane).innerHTML.length > 10 )
    {
        $(cpPane).morph('opacity: 1.0;' ,{duration:2.0});
        $(cpPane).style.overflow='auto';
    }
    else if( location.hash )
    {
        $(cpPane).morph('opacity: 10',{duration:2.0});
        $(cpPane).style.overflow='auto';
    }

}

addLoadEvent(pressOnload);
addLoadEvent(doFadeIns);
