jQuery( document ).ready( function () {

  // if working locally...
  if (jQuery( 'base' ).exists() && jQuery( 'base' ).attr( 'href' ).match( '127.0.0.1' )) {

    var sPath = jQuery( 'base' ).attr( 'href' ).replace( /\/application\/.+\//, '' );

    // prepend absolute links with (abbreviated) local path
    jQuery( 'a[href^=/]' ).each( function () { jQuery( this ).attr( 'href', sPath + jQuery( this ).attr( 'href' ) ); } );

    // prepend form actions with (abbreviated) local path
    jQuery( 'form' ).each( function () {
      if (jQuery( this ).attr( 'action' ).match( /^\// )) { jQuery( this ).attr( 'action', sPath + jQuery( this ).attr( 'action' ) ); }
    } );

/*  // in progress...

    // prepend absolute links with (abbreviated) local path
    jQuery( 'a[href^=/]' ).each( function () { jQuery( this ).attr( 'href' ).replace( /^\/(.+)/, sPath + '$1' ); } );

    // prepend form actions with (abbreviated) local path
    jQuery( 'form' ).each( function () { jQuery( this ).attr( 'action' ).replace( /^\/(.+)/, sPath + '$1' ); } );

    // prepend meta refresh with (abbreviated) local path
    jQuery( 'meta[http-equiv=refresh]' ).attr( 'content' ).replace( /\//, sPath );
*/
  }

  // remove focus border
  jQuery( 'a' ).focus( function () { this.blur(); } );

  // remove focus border
  jQuery( 'a.external' ).click( function () {
    window.open( jQuery( this ).attr( 'href' ) );
    return false;
  } );

  // assign name (to avoid: id="txtFoo" name="txtFoo")
  jQuery( 'form :input[id]' ).each( function () {
    if (jQuery( this ).attr( 'name' ) == '' || jQuery( this ).attr( 'name' ) == null) {
      jQuery( this ).attr( 'name', jQuery( this ).attr( 'id' ) );
    }
  } );

  // add image reflections
  jQuery( 'div#divFooter dl img' ).reflect( { height: 0.7, opacity: 0.2 } );

  // remove textarea resize (for safari)
  jQuery( 'textarea' ).css( 'resize', 'none' );

/*
  // initialize header link hover
  jQuery( 'div#divHeader dl#dlMenu dt[class!=current][class!=separator]' ).hover(
    function () { jQuery( this ).animate( { top: '-4px' }, 250 ); },
    function () { jQuery( this ).animate( { top: '0'    }, 250 ); }
  );

  // initialize current animation
  jQuery( 'div#divHeader dl#dlMenu dt[class=current]' ).everyTime( 1800, function() {
    jQuery( this ).animate( { top: '0' }, 900, 'swing', function () { jQuery( this ).animate( { top: '-8px' }, 900, 'swing' ) } );
  }, 0 );

  // initialize left menu span event
  jQuery( 'div#divLeft ul[class!=blog] ul li' ).click( function () {
    jQuery( this ).find( 'span' ).text( jQuery( this ).find( 'span' ).text() == '+' ? '' : '+' );
  } );

  // initialize left menu child event
  jQuery( 'div#divLeft ul[class!=blog] ul li' ).click( function () {
    jQuery( this ).find( 'ul' ).slideToggle( 'normal' );
  } );
*/

  // if blog navigation exists...
  if (jQuery( 'div.navigation' ).exists()) {

    // if empty navigation bar... hide
    jQuery( 'div.navigation' ).each( function () {
      if (jQuery( this ).children( 'div.nav-previous' ).html() == '' && jQuery( this ).children( 'div.nav-next' ).html() == '') { jQuery( this ).hide(); }
    } );
  }

  // if slideshow exists...
  if (jQuery( 'div#divSlideShow' ).exists()) {

    // show slide
    jQuery( 'div#divSlideShow *' ).show();

    // initialize slideshow
    jQuery( 'div#divSlideShow' ).cycle( {

      pause: 1,
      speed: 2000,
      timeout: 5000,
      pager: '#dlSlideShow',
      pagerAnchorBuilder: function ( nIndex ) { return '<dt><a href="#">' + (nIndex + 1) + '</a></dt>'; },
      before: function( currSlideElement, nextSlideElement ) {
        jQuery( 'div#divHeader table div.info p' ).fadeOut( 'slow', function () { jQuery( this ).fadeIn( 'slow' ).text( nextSlideElement.title ); } );
      }

    } );
  }

} );


/*
 * Custom jQuery methods
 */
jQuery.fn.exists  = function() { return jQuery( this ).length > 0; }


function displayMediaPlayer( sURL ) {

  var oCONSTANT = { BASE        : jQuery( 'base' ).attr( 'href' ) + 'asset/media/swf/',
                    SWF         : 'mediaPlayer',
                    BGCOLOR     : '#FF0000',
                    WIDTH       : 687,
                    HEIGHT      : 421,
                    IMAGE_DELAY : sURL.match( '_homepage' ) ? 10 : 6 };

  var sParam = '<param name="allowScriptAccess" value="sameDomain" />'
             + '<param name="allowFullScreen" value="false" />'
             + '<param name="bgcolor" value="' + oCONSTANT.BGCOLOR + '" />'
             + '<param name="base" value="'    + oCONSTANT.BASE    + '" />'
             + '<param name="menu" value="false" />'
             + '<param name="quality" value="high" />'
             + '<param name="wmode" value="transparent" />'
             + '<param name="FlashVars" value="url=' + jQuery( 'base' ).attr( 'href' ) + sURL.replace( /^\//, '' ) + '&image_delay=' + oCONSTANT.IMAGE_DELAY + '" />';

  var sHTML  = '<div>'
             +   '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + oCONSTANT.WIDTH + '" height="' + oCONSTANT.HEIGHT + '">'
             +     '<param name="movie" value="' + oCONSTANT.BASE + oCONSTANT.SWF + '.swf?nocache=' + new Date().getTime() + '" />'
             +     sParam
             + '<!--[if !IE]>-->'
             +     '<object type="application/x-shockwave-flash" data="' + oCONSTANT.BASE + oCONSTANT.SWF + '.swf?nocache=' + new Date().getTime() + '" width="' + oCONSTANT.WIDTH + '" height="' + oCONSTANT.HEIGHT + '">'
             +        sParam
             + '<!--<![endif]-->'
             +       '<a href="http://www.adobe.com/go/getflashplayer" class="external"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a>'
             + '<!--[if !IE]>-->'
             +     '</object>'
             + '<!--<![endif]-->'
             +   '</object>'
             + '</div>';

  document.write( sHTML );
}

function displayVideo( oData ) {

  var aEntry = oData.feed.entry || [];

  if (aEntry.length) {
    var oVideo = aEntry.shift();
    swfobject.embedSWF( oVideo.media$group.media$content[0].url + '&rel=1&border=0&fs=1&autoplay=0', 'objYouTubePlayer', '190', '140', '9.0.0', false, false, { allowfullscreen : 'true' } );
  }
}

function jsonFlickrFeed( oFeed ) {

  for (var i in oFeed.items) {
    if (i < 9) {
      document.write( '<a href="' + oFeed.items[i].link + '" class="external flickr"><img src="' + oFeed.items[i].media.m + '" alt="' + oFeed.items[i].title + '" /></a>' );
    }
  }
}
