$(document).ready(function() {

  var $plainCufon = $('.cufon, h1.page_title, .page_content .box h3'),
      $cufonWithHover = $('#fatfooter :header, .aboutcolumn h4');

  	Cufon.replace('.slidebox h3', {fontWeight: 'bold'});
		Cufon.replace($plainCufon, {fontFamily: 'TradeGothic2'});
  	Cufon.replace($cufonWithHover, {fontFamily: 'TradeGothic2', hover: true});
  	Cufon.replace('#sidebar h2', { fontWeight: 'bold', hover: true});


  // Content Switcher
  var $switcherContent = $('.switcher-content');
  var $switcherNav = $('.switcher');
  $('a', $switcherNav).click(function(){
    $('li', $switcherNav).removeClass('selected').removeClass('current');
    $(this).parent().addClass('selected');
    $('div', $switcherContent).removeClass('current');
    $(this.hash, $switcherContent).addClass('current');
    return false;
  });

  // Homepage sliding promo blocks: services
  var running = true,
  $slider = $('.slider'),
  $slidenav = $slider.before('<ul class="slidenav"/>').prev();
  $pauseindicator = $slidenav.before('<div class="pause"/>').prev();
  $panels = $('.slidebox', $slider);

  // Set up Navigation
  $panels.each(function(i){
    my_id = 'slide-' + i;
    this.id = my_id;
    $slidenav.append('<li><a href="#' + my_id + '">&nbsp;</a></li>');
  });

  $panels.css({
      'float' : 'left',
      'position' : 'relative' // IE fix to ensure overflow is hidden
    });

  // calculate a new width for the container (so it holds all panels)
  // $slider.css('width', $panels[0].offsetWidth * $panels.length);


  // Find all navigation items
  $navitems = $('a', $slidenav);

  $slider
  .wrap('<div class="slidewrapper" />')
  .parent()
  .css({overflow: 'hidden', position: 'relative'})
  .serialScroll({
    // target: '.slider',
    items: $panels, // Selector to the items ( relative to the matched elements, '#sections' in this case )
    prev: 'a#prev',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
    next: 'a#next',// Selector to the 'next' button (absolute too)
    axis: 'xy',// The default is 'y' scroll on both ways
    navigation: $navitems,
    force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
    //stop:false,// Each click will stop any previous animations of the target. (false by default)
    //lock:true, // Ignore events if already animating (true by default)
    interval:5000, // It's the number of milliseconds to automatically go to the next
    constant:false, // constant speed; 'true' slows down the wrap-back last element effect
    duration:700,
    onBefore: function(eventObject, target){
      if (running == false){
        return false;
      }
      $navitems.removeClass('current');
      $slidenav.find('a[href$=' + target.id + ']').addClass('current');
    }
  }).hover(function(){
    $pauseindicator.addClass('active');
    running = false;
  },function(){
    $pauseindicator.removeClass('active');
    running = true;
  });

  $slidenav.find('a:first').addClass('current');

	// Secondary Navigation
  $secondarynav = $('#secondary-nav');
  // current state
  $('a[href$=' + location.pathname + ']').addClass('current');



	// Comments form
	var $commentform = $('#comments-form'),
			$ajaxmessage = $('#ajax-comment').html('<img src="/_images/ajax-loader.gif" border="0" class="loading"/>');

			$ajaxmessage.find('img.loading').hide();
			$commentform.parent().wrap('<div style="min-height:355px"/>');

	successful = false;
	if ($commentform.length > 0) {
		  $commentform.ajaxForm({
			target: $ajaxmessage,
			beforeSubmit: function(data, $form, opt){
				$ajaxmessage.html('<img src="/_images/ajax-loader.gif" border="0" class="loading"/>');
				$ajaxmessage.find('img.loading').show();
				$commentform.parent().fadeOut(500);
			},
			success: function(){
				if (successful != true ){
					$commentform.parent().fadeIn(400);
				}
			}
		});

	}; //end of if

  // About Entry boxes
  $('.page_content .box').hover(function(){
    $(this).addClass('hover');
  },function(){
    $(this).removeClass('hover');
  }).click(function(){
    var linkTo = $(this).find('a[href]:first-child').attr('href');
    location.href += linkTo;
  });

  $('#violator div.box').click(function(){
    var linkTo = $(this).parent().find('a:first').attr('href');
    location.href = linkTo;
  }).css('cursor','pointer');

});
