
/* IE Hack HTML5 */
document.createElement('header');
document.createElement('footer');
document.createElement('section');
document.createElement('aside');
document.createElement('nav');
document.createElement('article');

/* Cufon */
Cufon.replace('#post .wrapper h1', { fontFamily: 'Gotham Book' });
Cufon.replace('nav a', { hover: true, fontFamily: 'Gotham Book' });
Cufon.replace(('#post #btnNextPost'), { hover: true, fontFamily: 'Gotham Book' });
Cufon.replace(('#post #btnPrevPost'), { hover: true, fontFamily: 'Gotham Book' });
Cufon.replace('.caption', { fontFamily: 'Gotham Book' });

var expandable = false;

$(function() {

	/* deeplinking */
	$.address.change(function(event)
	{
		var section = event.value.replace('/', '');
		
		if(section == 'home')
		{
			$('html, body').animate({scrollTop: $('header').offset().top},'medium');
		}
			else
		{
			if(section != 'contact')
			{	
				expandSection(section, expandable, "#"+section+" .button");
			}
			$('html, body').animate({scrollTop: $("#"+section).offset().top},'medium');
		}
	});
	
	/* all */ 
	$('header #title').stop().animate({'opacity' : ".7"}, (200));
	$('header #title').mouseenter(function(){$(this).stop().animate({'opacity' : "1"}, (200));});
	$('header #title').mouseleave(function(){$(this).stop().animate({'opacity' : ".7"}, (200));});
	$('nav #menu a').mouseenter(function(){$(this).stop().animate({'marginRight' : "5px"}, (200));});
	$('nav #menu a').mouseleave(function(){$(this).stop().animate({'marginRight' : "0px"}, (200));});
	
	function scrollToTop()
	{
		$.address.value('home');
		$.address.update();
	}
	
	function expandSection(id, toggle, button)
	{
		var content = ('#'+id+' .content').toString();
		if(toggle)
		{ 
			if(!$(content).is(':visible'))
			{
				$(content).slideToggle(500);
				if($(button))
				{
					$(button).removeClass('off').addClass('on');
				}
			}
		}
			else
		{
			$(content).slideToggle(500);
			
			if($(button))
			{
				if($(button).hasClass('off'))
				{
					$(button).removeClass('off').addClass('on');
				}
					else
				{
					$(button).removeClass('on').addClass('off');
				}
			}
		}
	}
	
	$('nav #logo').click(function(e) { e.preventDefault(); scrollToTop(); });
	
	// Fade In/Out
	if (!$.browser.msie) {
		$('body').fadeIn(400);
		
		$('#done article a,nav #menu a#buttonBack,#btnNextPost a,#btnPrevPost a').click(function() {
			var a = this;
			$('body').fadeOut(200,function() {
				window.location = a.href;
			});
			return false;
		});
	}
	
	$('nav').scrollFollow({speed: 1000, easing:'easeInOutQuad', offset:0});
	
	// Open links with the class external in a new window
	$('a.external').click( function() {
		window.open(this.href);
		return false;
	});
	
	// Rotating "Hero" Images
	// Documentation: http://tobia.github.com/CrossSlide/
	var header = $('header #images');
	var header_title = $('header #image_bar_text');
	var header_set = false;
	
	/* Structure
		{
			src: url+'/path/to/image.jpg', 	// The relative path to the image
			alt: 'Alternate text',			// The text to appear if the image doesn't show up
			href: '/page/to/link/to',		// Optional page to link to
			pageid: '100'					// WordPress page ID. This is required to for a page to have a static header
		},
	*/
	var images = [

		
		{
		src: url+'/img/header/mrpizza.jpg',
			alt: 'Korean-based Mr. Pizza engages in a little revisionist history exposing pizza\'s true origins.',
			href: 'http://www.youtube.com/watch?v=KiLA6Bk_ivs',
			pageid: '107'
		},
		{
			src: url+'/img/header/afl.jpg',
			alt: 'AFL-CIO taps into shared values that unite all Americans, union or not.',
			href: 'http://www.workconnectsusall.org/',
			pageid: '113'
		},
	{
			src: url+'/img/header/rob.jpg',
			alt: 'Rob Shepardson on how the online-piracy bills were killed by a generation\'s expectations.',
			href: 'http://www.thedailybeast.com/articles/2012/01/21/the-online-piracy-bills-were-killed-by-a-generation-s-expectations.html',
			pageid: '109'
		},
		{
			src: url+'/img/header/tree.jpg',
			alt: 'Facebookers plant a virtual tree, Chevrolet plants a real one with the Chevrolet Tree Project.',
			href: 'https://apps.facebook.com/chevrolettree/',
			pageid: '441'
		},
		{
			src: url+'/img/header/cl.jpg',
			alt: 'SS+K helps relaunch Crystal Light.',
			href: 'http://www.kraftbrands.com/crystallight/Pages/default.aspx#/home',
			pageid: '105'
		},

		{
			src: url+'/img/header/mioenergy.jpg',
			alt: 'Watch "Thunder Dave" fail to become the spokesperson for MiO Energy.',
			href: 'http://www.youtube.com/watch?v=09R27IQDiwU',
			pageid: '109'
		},
	{
			src: url+'/img/header/kevin.jpg',
			alt: 'Digital Strategist Kevin Skobac dishes on the future of TV + Facebook.',
			href: 'http://www.adage.com/article/digitalnext/nbc-abc-cbs-fox-facebook/231703/',
			pageid: '113'
		},
		{
			src: url+'/img/header/kaku.jpg',
			alt: 'Innovative web series touts Chevrolet\'s commitment to reduce 8M metric tons² of CO2.',
			href: 'http://www.chevrolet.com/environmental-projects/carbon-reduction/',
			pageid: '109'
		},
		{
			src: url+'/img/header/brotherhood.jpg',
			alt: 'Chevy\'s Table of Brotherhood celebrates MLK Jr.\'s National Memorial unveiling.',
			href: 'http://www.chevrolet.com/experience/chevrolet-mlk/',
			pageid: '109'
		},

		{
			src: url+'/img/header/mark.jpg',
			alt: 'Mark Kaminsky on how SS+K honors the legacy of David McCall.',
			href: 'http://www.adage.com/article/goodworks/ss-k-remember-david-mccall/231629/',
			pageid: '101'
		},

	];
	
	// Check for static header
	$.each(images,function(index,value) {
		if (!header_set && $('body').hasClass('postid-'+value['pageid'])) {
			header.html('<a href="'+images[index]['href']+'"><img src="'+images[index]['src']+'" alt="'+images[index]['alt']+'" /></a>');
			header_set = true;
		}
	});
	
	// Default rotating header
	if (!header_set) {
		header.crossSlide({ sleep: 5, fade: 1 },images, function(idx, img, idxOut, imgOut) {
			if (idxOut == undefined) {
					$('span#prev_caption').text(img.alt).fadeIn(1000);
					Cufon.replace('.caption', { fontFamily: 'Gotham Book' });
			} else {
				// Manual caption swap
				if(idx % 2 == 0) {
					$('span#prev_caption').fadeOut(1000);
					$('span#next_caption').hide().text(img.alt).fadeIn(1000);
					Cufon.replace('.caption', { fontFamily: 'Gotham Book' });
				} else {
					$('span#next_caption').fadeOut(1000);
					$('span#prev_caption').hide().text(img.alt).fadeIn(1000);
					Cufon.replace('.caption', { fontFamily: 'Gotham Book' });
				}
			}
		});
	}
});
