/*-----------------------------------------------------------------------------
Title: Main Javascript
Author: Rubenstein Technology Group (http://www.rubensteintech.com/)
-----------------------------------------------------------------------------*/

function navTo( navToUrl, navToId, updateElement, successCallback, appendURL ) {
    var re = new RegExp( "^/resource" );
    
    if( re.match( navToUrl ) ) { // jump to resource section
	// refresh the page to the URL
	location.href = navToUrl;
    } else { // nav to other sections
	// add our custom suffix
	var url = navToUrl + "?inner=1";

	if( appendURL )
	    url += appendURL;

	// set the correct nav item as active (visible if display XHTML/CSS nav)
	var navArray = new Array('whats-new', 'profile', 'newsletter', 'work', 'resource', 'onechangenow'); 
	for( var i = 0; i < navArray.length; i++ ) {
	    if ( $(navArray[i]) == null ) continue;
	    
	    if( navArray[i] == navToId ) {
		$(navArray[i]).className = 'active';
	    } else {
		$(navArray[i]).className = '';
	    }
	}

// HERE

	// download the content and plug it into the #content div
	if( !updateElement )
	    updateElement = 'content';
	
	var aj = new Ajax.Updater(
	                          { 
				      success: updateElement
				  },
				  url, 
	                          {
				      method: 'get',
				      evalScripts: true,
				      onSuccess : successCallback,
				      onFailure: reportError
				  } );
    }
} // navTo()

function animationCleanup( info )
{
    info.completedAnimations++;

    if( info.completedAnimations == 2 ) {
	
	var buf1 = document.getElementsByClassName( 'buffer' )[0];
	var buf2 = document.getElementsByClassName( 'buffer2' )[0];
	    
	buf2.style.position = 'relative';
	    
	buf1.innerHTML = '';
	buf1.style.position = 'absolute';
	    
	Element.removeClassName( buf1, 'buffer' );
	Element.addClassName( buf1, 'buffer2' );
	
	Element.removeClassName( buf2, 'buffer2' );
	Element.addClassName( buf2, 'buffer' );
    }

    return;
} // animationCleanup

function animateContent( direction )
{
    // Execute animation cleanup, below, after animation finishes
    var animationInfo = { completedAnimations : 0 };
    var afterAnimation = function () {
	animationCleanup( animationInfo );
    }
    
    var buf1 = document.getElementsByClassName( 'buffer' )[0];
    var buf2 = document.getElementsByClassName( 'buffer2' )[0];

    if( direction == 'left' ) {
	buf2.style.position = 'absolute';
	buf2.style.left = '650px';

	new Effect.Move( buf1, { x: -650, duration: 0.5, afterFinish: afterAnimation } );
	new Effect.Move( buf2, { x: -650, duration: 0.5, afterFinish: afterAnimation } );
    }
    else { // (direction == 'right')
	buf2.style.position = 'absolute';
	buf2.style.left = '-650px';
	
	new Effect.Move( buf1, { x: +650, duration: 0.5, afterFinish: afterAnimation } );
	new Effect.Move( buf2, { x: +650, duration: 0.5, afterFinish: afterAnimation } );
    } 
    
    return;
} // animateContent

function slideTo( slideToUrl, slideToId, subsectionId, direction ) {
    
    // Execute the animation, below, after ajax completes
    var aniFunc = direction ? function () { animateContent( direction ) } 
                :             null
                ;
    var buf2    = direction ? document.getElementsByClassName( 'buffer2' )[0]
                :             null
                ;
    var append  = direction ? '&buffer=0'
                :             null
                ;

    // hide the Flash portfolio, if it is present, because animating it causes issues 
    // in selected browsers
    if( aniFunc ) {
	if( document.getElementById ) {
	    var objPortfolio = document.getElementById( 'work-portfolio-' + subsectionId );
	    if( objPortfolio ) {
		objPortfolio.style.display = 'none';
	    }
	}
    }

    return navTo( slideToUrl, slideToId, buf2, aniFunc, append );
} // slideTo()

function reportError( request ) {
    alert( 'Sorry. An unexpected error has occurred. Please try again.' );
} // reportError()

function clearFooterImage() {
    // set background image to NULL
    $('content').style.background = 0;
    $('content').style.backgroundImage = 'none';
    // $('content').style.backgroundPosition = 'inherit';
    // $('content').style.backgroundRepeat = 'no-repeat';

    // set padding to default
    $('content').style.paddingBottom = '64px';
} // clearFooterImage()

function updateFooterImage( src, bottom, left ) {
    // set background image to source, with left offset
    $('content').style.backgroundImage = 'url(' + src + ')';
    $('content').style.backgroundPosition = left + 'px 100%';
    $('content').style.backgroundRepeat = 'no-repeat';

    // set padding to requested amount
    $('content').style.paddingBottom = bottom + 'px';    
} // updateFooterImage()

function handleSearchInput( el, str, bRestore )
{
    if( bRestore ) { // restore string
	if( el.value == "" ) {
	    el.value = str;
	}
    } else { // clear string
	if( el.value == str ) {
	    el.value = "";
	}
    }
} // handleSearchInput
