//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
$(document).ready(function() {
	//	Menu DHTML
	$("#mainnav").menumaker({
		menu_speed:	50,
		constrain_to:	"center-x",
		offset_mode:	"left"
	});

	//	Toggle
	$('.toggler').click(function() {
		$(this).next('.toggle-content').slideToggle();
	});
	
	$('#share-toggler').click(function() {
		$(this).next('.toggle-content').show();
	});

	//	Share widget toggling
	$('#share-toggler').toggle(function() {
		$('#share ul').show();
		$('#share-toggle-image').attr('src','/site/images/share_minus.png');
	},function(){
		$('#share ul').hide();
		$('#share-toggle-image').attr('src','/site/images/share_plus.png');
	});
	
	//	Lightbox popup
	$('.litebox-popup').click(function(){
		var id = $(this).attr('title');
		id = '#lightbox-' + id;
		$(id).wrap('<div id="lightbox-container">');
		$(id).fadeIn();
		$(id + ' .close').click(function(){
			$(id).fadeOut().unwrap();
		});
	});
		
	//	Init searchbar
	$('#header-search input[name="findtext"]').liveSearch({url: '/search/live.php?findtext='});
	
	//	Make this text a link
	$('#globalnav-10277 a').click(function(event){
		event.preventDefault();
	});
	
	$('.sidebar-widget input.textfield').focus(function(e){
		$(this).siblings('label').hide();
	});

	$('.sidebar-widget input.textfield').blur(function(e){
		var content = $(this).val();
		if (!content) {
			$(this).siblings('label').show();
		};
	});

	//	PDF & external links open new tab
	$("a[href$='pdf'],a[href^='http']").click(function(){
		$(this).attr("target","_blank");
	});

	//	Global nav divider class
	$("#globalnav li:nth-child(n+7) a").addClass("with-divider");

	//	Download tracking
	var fileTypes = ['doc','xls','pdf','mp3'];
 
	$('a').each(function() {
		var a = $(this);
		var href = a.attr('href');
		if(href)
		{		
			var hrefArray = href.split('.');
			if( hrefArray )
			{
				var extension = hrefArray[hrefArray.length - 1];
				if ( extension && $.inArray(extension,fileTypes) != -1) {
					a.click(function() {
						_gaq.push(['_trackPageview','/documents/' + href]);
					//	Old syntax
					//	pageTracker._trackPageview('/documents/' + href);
					});
				}
			}
		}
	});
});

//===========================================================================================
//	GA Youtube Event Tracking
//===========================================================================================
var currentid 	= '';
var reportedid	= '';

function onYouTubePlayerReady(playerId) {
	currentid = playerId;
	ytplayer = document.getElementById(playerId);
	ytplayer.addEventListener("onStateChange", "OnPlayerStateChange");
}

function OnPlayerStateChange(newState) {
	if (currentid != reportedid && newState == 1)
	{
		_gaq.push(['_trackEvent', 'Videos', 'Play', currentid]);
		reportedid = currentid;
	};
}

//===========================================================================================
//	Email popup
//===========================================================================================
function openEmailPopup( id )
{
	sizeX = 500;
	sizeY = 400;
	
	//	Center the window
	leftpos	= (screen.width)  ? (screen.width-sizeX)/2 : 100;
	toppos	= (screen.height) ? (screen.height-sizeY)/2 : 100;

	//	Define the window size
	widthVar  = 'width=' + sizeX + ',';
	heightVar = 'height=' + sizeY + ',';
	
	//	Open the window
	contenturl				= '/blog/email.php/id/' + id;
	target					= 'email' + id;
	winobject				= window.open(contenturl,target,"menubar=0,statusbar=0,scrollbars=1,toolbar=0,location=0," + widthVar + heightVar + "left=" + leftpos + ",top=" + toppos + ",resizable=1" );
	winobject.focus();
}

