function globalInit() { 
	gallery();
	about();
	bios();
	setupTracking();
	arrowHighlight();
	newWindow();
	fearless();
}

//Trick target="_blank" behavior
function newWindow() {
	$('a[rel*=external]').live('click', function() {
		window.open(this.href);
		return false;
	});
}

//Load the Fearless Q+A JTV video
function fearless() {
		var vidWidth = $('.category-fearless').width();
		var vidHeight = Math.round((vidWidth/16)*9+46); //JTV player controls are 46px tall

	$('#fearlessQA').replaceWith('<object type="application/x-shockwave-flash"'
			+ 'width="' + vidWidth + '"'
			+ 'height="' + vidHeight + '"'
			+ 'id="clip_embed_player_flash" data="http://www.justin.tv/widgets/clip_embed_player.swf" bgcolor="#000000">'
			+	'<param name="movie" value="http://www.justin.tv/widgets/clip_embed_player.swf" />'
			+	'<param name="allowScriptAccess" value="always" />'
			+	'<param name="allowNetworking" value="all" />'
			+	'<param name="allowFullScreen" value="always" />'
			+	'<param name="flashvars" value="auto_play=true&title=@green_garage visits FearLessQA. @ryanferrero + @bagni join @bogusky #FQA&start_time=1263495616000&end_time=1263499226000&channel=fearlessqa&tip_id=2482527" />'
			+	'<param name="wmode" value="transparent" />'

			+	'<a href="http://get.adobe.com/flashplayer/" rel="external"><img src="/wp-content/themes/gg-theme1.0/img/noFlash.gif" /></a>'
			+ '</object>'
	);
}

//Change opacity of next/back buttons
function arrowHighlight() {
	$('.photo').hover(function(){
		$(this).find('a#garageback').addClass('on');
		$(this).find('a#garagenext').addClass('on');
	},
	function() {
		$(this).find('a#garageback').removeClass('on');
		$(this).find('a#garagenext').removeClass('on');
	});
}

//Peek inside the garage - image gallery
function gallery() {
	var currentImage = 1;
	var imageLimit = 5;
	
	//Thumbnails
	$('#garagethumbs li').hover(function() {
		$(this).find('h4').addClass('hover');
			},
			function() {
		$(this).find('h4').removeClass('hover');
	});
	
	$('#garagethumbs li').bind('click', function(){
		var newImage = $(this).find('img').attr('id');
		var myImageNumber = $(this).find('h4').html();

		currentImage = myImageNumber;
		$('#garagegallery .photo img').attr('src', newImage);

		$('h4').removeClass('hover');
		$('h4').removeClass('on');
		$(this).find('h4').addClass('on');
	});

	//Previous image button
	$('#garageback').bind('click', function(){

		if(currentImage == 1) {
			currentImage = imageLimit;
		} else {
			currentImage--;
		}
		//This path will need to change when pushed to production
		var newImage = 'http://dev.greengarage.com/wp-content/themes/gg-theme2.0/img/contentImgs/garageCarousel/' + currentImage + '.full.jpg';
		$('#garagegallery .photo img').attr('src', newImage);

		$('h4').removeClass('on');
		$('#garagethumbs li.' + currentImage).find('h4').addClass('on');
		
		return false;
	});
	
	//Next image button
	$('#garagenext').bind('click', function(){
		
		if(currentImage == imageLimit) {
			currentImage = 1;
		} else {
			currentImage++;
		}
		//This path will need to change when pushed to production
		var newImage = 'http://dev.greengarage.com/wp-content/themes/gg-theme2.0/img/contentImgs/garageCarousel/' + currentImage + '.full.jpg';
		$('#garagegallery .photo img').attr('src', newImage);

		$('h4').removeClass('on');
		$('#garagethumbs li.' + currentImage).find('h4').addClass('on');
		return false;
	});
}

//About gallery
function about() {
	$('#aboutthumbs li').click(function(){
		var aboutName = $(this).find('img').attr('id');
		
		$('.about_inner').css({ 'display' : 'none' });
		$('#' + aboutName).css({ 'display' : 'block' });
		
		$('#aboutthumbs li').removeClass('on');
		$(this).addClass('on');
	return false;
	});
	
	$('#aboutthumbs li').hover(function(){
			$(this).addClass('hover');
		},
		function() {
		$(this).removeClass('hover');
	});
}

//Bios gallery
function bios() {
	$('#biothumbs li').click(function(){
		var bioName = $(this).find('img').attr('title');
		
		$('.bio_inner').css({ 'display' : 'none' });
		$('#' + bioName).css({ 'display' : 'block' });
				
		$('#biothumbs li').removeClass('on');
		$(this).addClass('on');
	return false;	
	});
	
	$('#biothumbs li').hover(function(){
			$(this).addClass('hover');
		},
		function() {
		$(this).removeClass('hover');
	});
}

//Tracking
function setupTracking(){
	var loc=location.href;
	loc=loc.replace(/http:\/\/([^\/]+)\//, '');
	loc=loc.replace(/#/, '');
	loc='/' + loc;
	try {
		pageTracker._trackPageview(loc);
	} catch (e) {}
	//alert(loc);
	/*
	try {
		pageTracker._trackEvent('', '', '');
	} catch (e) {}
	*/
	
	$("li.contact a").click(function(){
		try {
			pageTracker._trackEvent('RightNav', loc, 'RightNav - ' + loc + ' - drivegood@greengarage.com');
		} catch (e) {}
	});
	
	$("ul.categories li a").click(function(){
		var mailto=$(this).attr("href");
		mailto=mailto.replace(/mailto:/, '');
		try {
			pageTracker._trackEvent('Contact Info', "Contact Info - " + loc, 'Contact Info - ' + mailto);
		} catch (e) {}
	});
	
	$("#header a").click(function(){
		try {
			pageTracker._trackEvent(loc, 'Header', loc + ' - Header - Back to Home');
		} catch (e) {}
	});
	
	$("div#main_content a.back").click(function(){
		try {
			pageTracker._trackEvent(loc, 'Body', loc + ' - Back to Home');
		} catch (e) {}
	});
	
	// Comment submission and pagination tracking in plugin
	
	$("#footer a.contactus").click(function(){
		try {
			pageTracker._trackEvent('Footer', loc, 'Footer - ' + loc + ' - Contact Us');
		} catch (e) {}
	});
	
	$("#sidebar a.show-more-tweets").click(function(){
		try {
			pageTracker._trackEvent('RightNav', loc, 'RightNav - ' + loc + ' - More Tweets');
		} catch (e) {}
	});
	
	$("#garagethumbs img.thumbimage").click(function(){
		try {
			pageTracker._trackEvent(loc, "Body", loc + ' - Thumbnails - GarageThumbs - Click');
		} catch (e) {}
	});
	
	$("#biothumbs li a").click(function(){
		var thumbclicked=$(this).html();
		try {
			pageTracker._trackEvent(loc, "Body", loc + ' - Thumbnails - BioThumbs - Click - ' + thumbclicked);
		} catch (e) {}
	});
	
	$("#biothumbs li img").click(function(){
		var parent=$(this).parent();
		var thumbclicked=$('a', parent).html();
		try {
			pageTracker._trackEvent(loc, "Body", loc + ' - Thumbnails - BioThumbs - Click - ' + thumbclicked);
		} catch (e) {}
	});
	
	$("ul#aboutthumbs li a").click(function(){
		var thumbclicked=$(this).html();
		try {
			pageTracker._trackEvent(loc, "Body", loc + ' - Thumbnails - OurStory - Click - ' + thumbclicked);
		} catch (e) {}
	});
	
	$("ul#aboutthumbs li img").click(function(){
		var parent=$(this).parent();
		var thumbclicked=$('a', parent).html();
		try {
			pageTracker._trackEvent(loc, "Body", loc + ' - Thumbnails - OurStory - Click - ' + thumbclicked);
		} catch (e) {}
	});
	
	$("#sidebar div.addthis_toolbox li a").click(function(){
		var type=$(this).parent().attr("class");
		try {
			pageTracker._trackEvent('RightNav', loc, 'RightNav - ' + loc + ' - ' + type);
		} catch (e) {}
	});
	
	$("p.postmetadata a.share").click(function(){
		try {
			pageTracker._trackEvent('Body', loc, 'Body - ' + loc + ' - ShareThis');
		} catch (e) {}
	});
	
	$("a.comments").click(function(){
		var linkto=$(this).attr("href");
		try {
			pageTracker._trackEvent(loc, 'Body', 'CommentButton - Click - ' + linkto);
		} catch (e) {}
	});

	$("a#garageback").click(function(){
		try {
			pageTracker._trackEvent(loc, 'Body', loc + ' - Carousel - left');
		} catch (e) {}
	});
	
	$("a#garagenext").click(function(){
		try {
			pageTracker._trackEvent(loc, 'Body', loc + ' - Carousel - right');
		} catch (e) {}
	});
	
	$("div#main_content a.valet").click(function(){
		var mailto=$(this).attr("href");
		mailto=mailto.replace(/mailto:/, '');
		try {
			pageTracker._trackEvent('At Your Service', "Body - " + loc, 'Valet Pickup Click - ' + mailto);
		} catch (e) {}
	});
	
	$("div#main_content a.mobile").click(function(){
		var mailto=$(this).attr("href");
		mailto=mailto.replace(/mailto:/, '');
		try {
			pageTracker._trackEvent('At Your Service', "Body - " + loc, 'Mobile Tuning Click - ' + mailto);
		} catch (e) {}
	});
	
	$("ul.social_media li.twitter a, ul.social_media li.facebook a").click(function(){
		var linkto=$(this).attr("href");
		var type=$(this).parent().attr("class");
		try {
			pageTracker._trackEvent('Contact Info', "Contact Info - " + loc, 'Follow/Fan Click - ' + linkto + ' - ' + type);
		} catch (e) {}
	});
}

jQuery(document).ready(function($) {
	globalInit();
});