// image rollover
function rollover( imageobj )
{
	var mystring = imageobj.src;
	if( mystring.indexOf( ".gif" ) >= 1)	{ imageobj.src = mystring.replace( /\.gif/gi, '_on.gif' ); }
	else					                { imageobj.src = mystring.replace( /\.jpg/gi, '_on.jpg' ); }
}


// image rollout
function rollout( imageobj )
{
	var mystring = imageobj.src;
	if( mystring.indexOf( ".gif" ) >= 1 )	{ imageobj.src = mystring.replace( /\_on\.gif/gi, '.gif' ); }
	else					                { imageobj.src = mystring.replace( /\_on\.jpg/gi, '.jpg' ); }
}

$(document).ready(function() {
    init_panel_navigation();    

    $('a.change_panel').click(function(){
        rotate_active_panel( parseInt( $(this).text() ) - 1 );
    });
});


// initialize the panel navigation items
function init_panel_navigation()
{
    var panel_nav = $('#panel_navigation');
    
    // show the panel navigation
    if( $('div.available_panel').size() >= 1 ) { panel_nav.show(); }
    
    // create the 1 / 2 / 3 navigation
    for( i=1; i <= $('div.available_panel').size(); i++ )
    {        
        // if this isn't the fist item in the list, add a separator
        if( i != 1 ) { panel_nav.append( "&nbsp;/&nbsp;" ); }
        
        // append the <a>1</a> link for the panel
        panel_nav.append( "<a href='javascript:void(0);' class='change_panel'>" + i + "</a>" );
    }
    
    // append the container for the panel title
    panel_nav.append( "&nbsp;&nbsp;&nbsp;<span id='active_panel_title'></span>" );
    $('#active_panel_title');
    
    // append the container for the panel link
    panel_nav.append( "&nbsp;&nbsp;&nbsp;<span id='active_panel_link'></span>" );
    $('#active_panel_link');
 
    // hide all panels
    $( 'div.available_panel' ).hide(); 
    
    // set the first panel as active
    set_active_panel( 0 );
}


// set a particular panel to active
function set_active_panel( id )
{
    // get the current panel and show it
    var this_panel = $( 'div.available_panel:eq(' + id + ')' );
    this_panel.fadeIn(1200);
     
    // get the first h2 in the active panel and set the active
    // title to the text inside <h2> .... </h2> 
    $('#active_panel_title').text( this_panel.find('h2:first').text() );
    var this_link_text = this_panel.find('h6:first').text();
        this_link_text = this_link_text ? this_link_text : 'view link';
    
    // if there is a link
    url = this_panel.find('a:first').attr('href') 
    if( url ) { $('#active_panel_link').html( "- <a target='_blank' href='" + url + "'>" + this_link_text + "</a>" ); }
    else      { $('#active_panel_link').html( "" ); }
}


// rotate between panels
function rotate_active_panel( id )
{
    // hide all panels
    $( 'div.available_panel:visible' ).fadeOut( 500, function () {
        set_active_panel( id );
    });
}