$(function(){
	
	// IE Hack to fix TRIM LAME!
	if(typeof String.prototype.trim !== 'function') {
	  String.prototype.trim = function() {
	    return this.replace(/^\s+|\s+$/g, ''); 
	  }
	}
	
	
// load fancybox
$('.fancybox').fancybox({
	'padding': 0,
	'overlayColor': '#000',
	'overlayOpacity': 0.85,
	'titleShow': false
});
    

// start slideshow with cycle

$('#carousel').cycle({
    fx: 'fade',
    timeout:5000,
    pager:"#pagernav",
    slideExpr:'.slide',
    delay:-1000
});

// advance slideshow on click
$('#carousel .slide').click(function(){
    $('#carousel').cycle('next');
});

  
// make sure that we have not added main navigation
$('#carousel a').addClass('selflink');

// setup accordian navigaton for savanna main content and publisher main content.
$("a.accordian").after("<span class='fancy'>&nbsp; }</span>")
	.hover(function(){ 
	    $(this).next().addClass('hover');
	    }, function(){
	       $(this).next().removeClass('hover');
	    }
	);
$("a.accordian").not('.expanded').parent().next().css("display","none");
$("a.accordian").click(function(){
	    $('a.accordian').not($(this)).parent().next().slideUp('fast'); // hide all others
	   $(this).parent().next().slideToggle("fast");
	   return false;
});
	   
	   
// savanna features
$('#sfeatures .feature-presentation').not('#sfeatures .feature-presentation:first').hide();
$('#sfeatures ul.features li>a').click( function(e){
    e.preventDefault();
    $('#sfeatures .feature-presentation').hide();
    $(this).next('.feature-presentation').slideDown('slow');
    
});

//publisher features
$('#features .feature-presentation').not('#features .feature-presentation:first').hide();
   $('#features ul.features li>a').click( function(e){
       e.preventDefault();
       $('#features .feature-presentation').hide();
       $(this).next('.feature-presentation').slideDown('slow');
       $(this).siblings().removeClass('selected');
       $(this).addClass('selected');
   });

// fancy form
$('#whitepaper').slidinglabels({
	topPosition  : '6px',  // how far down you want each label to start
	leftPosition : '4px',  // how far left you want each label to start
	axis         : 'y',    // can take 'x' or 'y' for slide direction
	speed        : 'fast'  // can take 'fast', 'slow', or a numeric value
});

// form submission
$('#savannawhitepaper #btn').click(function(e){
//before we proceed we need to add some validation
	$("#savannawhitepaper").validate();
// if validated move on to code below, else return error message  
	if($('#savannawhitepaper').valid()) {

	var params = $('#savannawhitepaper').serialize();
	$.post($(this).attr('action'),params,function(){
	$('#savannawhitepaper').html("<div id='message'></div>");
	$('#message').html("<h3>Thank you!</h3>")
	.append("<p>We want to thank you for your comments, and will have a represntative reply to your comments shortly.</p>")
	.hide().fadeIn('slow');
	});
	} else {
	// FORM DIDN'T PASS VALIATION
	return false;
	}
// Remove the thing & replace w/ Downloading verbage
  $('#savannawhitepaper').hide();
  
  var newElement = '<div id="downloadingMessage">Downloading</div>';
  $('#whitepaper .content').append(newElement);
  
  $('#downloadingMessage').attr('style', 'top: 62px; font-size: 24px; color: #333; background: #ccc; display: inline-block; padding: 24px; -moz-border-radius: 10px; /* Firefox */   -webkit-border-radius: 10px; /* Safari, Chrome */   border-radius: 10px; /* CSS3 */')
  
 	e.preventDefault();
 	var $el = $('#savannawhitepaper');  // store the form data
   	
   	
	$.post( 'contact-form.php',{ arg0 : $el.serialize() },
       function(data) { 
    	   if(!$el.find('.session').length){
				$el.prepend('<input type="hidden" name="session" class="session" value="'+data+'" />');
				$el.trigger('submit');
				
		   } else {  // something failed
		   	
		   	//$(this).find('#btn').removeAttr('disabled');    
		   }
		   
       }
   ); 
   
});

// load news and events page
	$('#events').load('events.html #upcoming .event',{limit:5},
			 function (){
			//	 on callback load up our front page
			var title = $('#upcoming .event a:first').text();
			if(title) {
				title.trim();
				$('#welcome .announcements .latestEvent').text(title);
			}
	});
	//$('#welcome .announcements a.latestEvent').attr('text', $('events.html #upcoming .event a:first.text').attr('text'));
	//$('#welcome .announcements a.latestEvent').html($('#upcoming .event a:first').attr('text').trim());
	//console.log($('#welcome .announcements a.latestEvent').html());
	
	
/* load the blog entries 
 $('#blog .content').load('blog/index.html .main-content',function(){
     $('.main-content').find('h1:first').remove();
     $('#blog .fancybox').fancybox({
         'padding':0,
         'overlayColor':'#000',
         'overlayOpacity':0.85,
         'titleShow':false
     });
 });
 
*/
 // interactive architecture graphic;
 
 $('#interactive-graphic .graphic-bg').hide();
 $('#architecture a.archgraph').hover(function(){
     var myname = $(this).attr('href');
     if(!$('#interactive-graphic').has('.selected').length){
         $(myname).show();
     }
     },function(){
         
     var myname = $(this).attr('href');
     if(!$('#interactive-graphic').has('.selected').length){
          $(myname).stop().hide();
      }
     }
 ).click(function(e){
     e.preventDefault();
     
     var myname = $(this).attr('href');
      $(myname).show().siblings().not('#all').hide();
         
      $(myname).toggleClass('selected').siblings().removeClass('selected');
     
 });
 
 
     
   
});

