/*-------------------------------------------------

		1940 Chronicle		Behaviorial Functions
		Date 				June 2010	
		
---------------------------------------------------*/
/*----------------------------------------------------------jQuery---------------------------------------------------------- */
/*--------------------------------------------------------Functions--------------------------------------------------------- */
/* Global Navigation */
var globalNav = function(){
	$('.show-nav-global').click(function(){
		$('.nav-global').slideToggle();
		$(this).toggleClass('open');
		var splitType = $(this).attr('id');
		_gaq.push(['_trackEvent', 'Global Menu', splitType ]);
	});

	$('.nav-global .inner ul').children('li').each(function(i, option){
		$(this).click(function(){
			window.location.href = $(this).children('h3').children('a').attr('href');
			})
		}).css('cursor', 'pointer');
	
	
}


var ie7;
/* HP Character Listing */
var diaryEntries = function(){
	var showCharacters = 3;
	var n = showCharacters -1;
	var characterList = $('.cont-sec .characters');
	
	
	
	var hiddenItems = $(characterList).find('li:gt(' + n + ')');
	
	if( $('.cont-sec .characters').children().length > showCharacters){
	
	$(hiddenItems).hide();	
	$('.cont-sec .show-all').click(function(){
		$(hiddenItems).slideToggle();
		$(this).toggleClass('down');
		var direction;
		if( 'show-all down' == $(this).attr('class') ){
			direction = "Down";
		}else{
			direction = "Up";
		}
		_gaq.push(['_trackEvent', 'View all updates', direction ]);
		if ( true == ie7 ){ $('.fb_iframe_widget').hide().fadeIn(); }
	});
	
	}else{
		$('.show-all').hide();
	}
}



/* Make sure the number of rings is an integer */
var integerRings = function(){
	$(window).load(function(){ // Wait for the document to fully load before running, so that the height of content images can be calculated.
		
		var ringHeight = 54; //Height of each repeating unit
		
		var heightPri = $('#content .cont-pri').height();
		var heightSec = $('#content .cont-sec').height();
		
		if ( heightPri > heightSec ){ // Find which col is longest
			var rings = Math.round(heightPri / ringHeight);
		}else{
			var rings = Math.round(heightSec / ringHeight);
		}
		
		var newHeight = (rings + 1) * ringHeight; // Add a ring, and set new height as multiple of ring height
	
		$('.character #content .cont-pri').height(newHeight);
		 
	});
}



//Special call back function
var k = [], tallyho = "84,65,76,76,89,72,79", konami = "38,38,40,40,37,39,37,39,66,65";;
jQuery(document).keydown(function(e) {
  
  k.push( e.keyCode );
 
  if ( k.toString().indexOf( tallyho ) >= 0 ){
	//jQuery(document).unbind('keydown',arguments.callee);
		flyby();	
		_gaq.push(['_trackEvent', 'Code', 'Tallyho']);
		k = [];
  }else if( k.toString().indexOf( konami ) >= 0 ){
	//jQuery(document).unbind('keydown',arguments.callee);
		flyby();
		_gaq.push(['_trackEvent', 'Code', 'Konami']);
		k = [];  
  }
});




function flyby(){
	var image = document.createElement("img");
	image.src = "/wordpress-content/themes/rafbf/_assets/images/design/plane-shad.png";

	var p = document.createElement("div");
	p.id = "plane";
	
	
	$(p).css('left','-400px');
	$(p).css('display','block');
	
	$('body').append(p);
	
	$(p).animate({
    	left: '+=150%',
		top: '+=200px'
  	}, 5000, function() {
    	$(this).hide();

		
  	});
	
}

//Add expression for external and mailto links
$.expr[':'].external = function(obj){
    return !obj.href.match(/^mailto\:/)
            && (obj.hostname != location.hostname);
	};

function track_outbound_links(){
	// bind click function to all external links
	$('a:external').addClass('external').click(function(){
		var link = $(this).attr('href');
		_gaq.push(['_trackEvent', 'External Link', link]);
		window.location.href = link;
		return false;
	});
}




/*--------------------------------------------------------Doc Ready Calls--------------------------------------------------- */
$(document).ready(function(){
	
	
	
	$('body').addClass('js').removeClass('nojs');
	globalNav();
	diaryEntries();
	integerRings();
	track_outbound_links();
	
	
});
