function deriveRoot()
{
  var root = window.location.protocol+'//'+window.location.hostname;
  
  if ( window.location.port != 80 )
  {
    root += ':'+window.location.port;
  }
  
  root += '/';
  
  if ( ( pos = window.location.pathname.search('public_html') ) != -1 )
  {
    root += window.location.pathname.substring(1,pos+12);
  }
  
  if ( window.location.pathname.search('demo') != -1 )
  {
    root += 'demo/';
  }
  
  return root;
}

// Casestudy Slideshow
function slideSwitch()
{
  var $active = $('#aboutusSlideshow div.active,#casestudySlideshow div.active,#newsSlideshow div.active');
  
  if ( $active.length == 0 ) $active = $('#aboutusSlideshow div:last,#casestudySlideshow div:last,#newsSlideshow div:last');

  var $next =  $active.next().length ? $active.next() : $('#aboutusSlideshow div:first,#casestudySlideshow div:first,#newsSlideshow div:first');

  $active.addClass('last-active');

  $next.css( {opacity: 0.0} )
       .addClass('active')
       .animate( {opacity:1.0},1000,function()
       {
         $active.removeClass('active last-active');
       } );
}

$(window).load( function()
{
  //Cufon.replace('.cufon');
  
  if ( $('div#slideshow').length )
  {
    $('#slider').nivoSlider({effect:'fade'}); // Slideshow
  }
  
  if (
       ( $('div#aboutusSlideshow').length && $('div#aboutusSlideshow div.slide').length > 1 ) ||
       ( $('div#casestudySlideshow').length && $('div#casestudySlideshow div.slide').length > 1 ) ||
       ( $('div#newsSlideshow').length && $('div#newsSlideshow div.slide').length > 1 )
     )
  {
    $(function() { setInterval( "slideSwitch()",5000 ); });
  }
  
  if ( $('#scroller').length ) // Portfolio Scroller
  {
    scroller();
  }
  
  $('div#map a.branchMarker').live( 'mouseover',function()
  {
    var id = $(this).attr( 'id' );
    
    id = id.substring(7);
    
    $('div#markerDetails-'+id).show();
  } )
  .live( 'mouseout',function()
  {
    var id = $(this).attr( 'id' );
    
    id = id.substring(7);
    
    $('div#markerDetails-'+id).hide();
  } );
});

function inputAutoClear()
{
  $('#newsletterEmail').addClass( 'autoclear' );
  
  $('input.autoclear').each( function()
  {
    $(this).attr('default',$(this).val());
    
    $(this).focus( function()
    {
      if ( $(this).val() == $(this).attr( 'default' ) )
      {
        $(this).val('');
      }
    });
    
    $(this).blur( function()
    {
      if ( $(this).val() == '' )
      {
        $(this).val( $(this).attr( 'default' ) );
      }
    } );
  } );
}

function branchSelector( root )
{
  $('select#region').live('change',
  function()
  {
    var region = $(this).val();
    
    if ( region != '' )
    {
      $.post( root+'ajax.php',
      {
        'ajax'    :  'true',
        'action'  :  'branchSelector',
        'region'  :  region
      },
      function( data )
      {
        $('div#branchSelector').html( data );
      } );
    }
    else
    {
      $('div#branchSelector').html( '&nbsp;' );
    }
  } );
}

function activateInlineValidation( root )
{
  $('form.validate input[type=text],form.validate input[type=password],form.validate select:not(select[id="region"]),form.validate textarea').live('blur',
  function()
  {
    var id     = $(this).attr('id');
    var field  = $(this).attr('name');
    
    $.post( root+'ajax.php',
    {
      'ajax'     :  'true',
      'action'   :  'inlineValidation',
      'section'  :  $('input[name=section]').val(),
      'class'    :  $('input[name=class]').val(),
      'field'    :  field,
      'value'    :  $(this).attr('value')
    },
    function( data )
    {
      if ( data=='false' )
      {
        $('#'+id).parent().addClass('warning');
      }
      else
      {
        $('#'+id).parent().removeClass('warning').find('span[class=warning]').remove();
      }
    } );
  } );
}

function primeClickOnceButtons()
{
  $('button.clickOnce').live( 'click',function()
  {
    // Our PHP logic listens for the submit button variable, so we must dupe it before disabling the button...
    
    // First, rid existing variable from a previous submission...
    if ( $('input:hidden[value="submit"]').length )
    {
      $('input:hidden[value="submit"]').remove();
    }
    
    $('button.clickOnce').before( '<input type="hidden" name="'+$('button.clickOnce').attr('name')+'" value="submit" />' );
    
    $('button.clickOnce').addClass( 'disabled' );
    $('button.clickOnce').attr( 'disabled','disabled' );
    
    $('button.clickOnce').parent().parent('form').submit();
    
    return false;
  } );
}

function referrerOther()
{
  if ( $('input#referrerOther').length )
  {
    if ( $('select#referralID').val() != 'other' )
    {
      $('input#referrerOther').hide();
    }
    
    $('select#referralID').live('change',
    function()
    {
      var selection = $(this).val();
      
      if ( selection == 'other' )
      {
        $('input#referrerOther').show();
      }
      else
      {
        $('input#referrerOther').hide();
      }
    } );
  }
}

$(document).ready( function()
{
  var root = deriveRoot();
  
  $('ul#nav').superfish({});
  
  // Capture External Links
  $('a[rel*=external]').click( function()
  {
    window.open( $(this).attr('href') );
    return false;
  } );
  
  $('a[rel*=lightbox]').lightBox(); // Enable Lightboxes...
  
  if ( $('div.subscriptionAlert').length ) // Newsletter Subscription Message
  {
    $('div.subscriptionAlert').animate({opacity:1.0},3000); // Delay Fade By 3 Seconds...
    $('div.subscriptionAlert').fadeOut('slow',function(){$(this).remove();});
  }
  
  if ( $('body#home').length ) // ie. On Homepage
  {
    if ( $('div#carousel li').length > 4 )
    {
      $('div#recent div#carousel')
        .after( '<a id="carouselNext" title="View Next Projects">View Next Projects</a>' )
        .after( '<a id="carouselPrev" title="View Previous Projects">View Previous Projects</a>' );
      
      $('div#carousel').jCarouselLite( // Recent Projects
      {
        btnNext:  'a#carouselNext',
        btnPrev:  'a#carouselPrev',
        visible:  4,
        scroll:   4,
        circular: true,
        speed:    800
      });
    }
  }
  
  if ( $('body#faqs').length ) // ie. On FAQs page
  {
    $('dl.accordion').accordion(
    {
      header:      'dt',
      collapsible: 'true',
      active:      'false'
    } );
  }
  
  $('div#map')
    .append( '<span id="shadowLeft"><!-- cosmetic --></span>' )
    .append( '<span id="shadowRight"><!-- cosmetic --></span>' );
  
  if ( $('#primary div.article ul.clients').length && $.browser.msie && $.browser.version.substr(0,1)<='7' )
  {
    $('#primary div.article ul.clients img').each( function() // Achieve central alignment via absolute positioning in IE<=7
    {
      var y = Math.ceil( ( 98  - $(this).height() ) / 2 );
      var x = Math.ceil( ( 208 - $(this).width()  ) / 2 );
      
      $(this).css( { 'position':'absolute','top':y,'left':x } );
    } );
  }
  
  if ( $('#primary div.article ul.associates').length && $.browser.msie && $.browser.version.substr(0,1)<='7' )
  {
    $('#primary div.article ul.associates img').each( function() // Achieve central alignment via absolute positioning in IE<=7
    {
      var y = Math.ceil( ( 98  - $(this).height() ) / 2 );
      var x = Math.ceil( ( 152 - $(this).width()  ) / 2 );
      
      $(this).css( { 'position':'absolute','top':y,'left':x } );
    } );
  }
  
  inputAutoClear();
  
  $('form#quickJump').show(); // Show QuickJump for JS enabled visitors...
  
  $('form#quickJump select').live( 'change',function()
  {
    if ( $(this).val() != '' )
    {
      window.location = $(this).val();
    }
  } );
  
  if ( $('body#contact').length ) // ie. On Contact Page
  {
    branchSelector( root );
    activateInlineValidation( root );
    primeClickOnceButtons();
    referrerOther();
  }
  
  if ( $('body#vacancy').length ) // ie. On Vacancy Page
  {
    activateInlineValidation( root );
    primeClickOnceButtons();
  }
  
} );
