window.fbAsyncInit = function() {
	FB.Event.subscribe('edge.create', function(targetUrl) {
		_gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
	});
	FB.Event.subscribe('edge.remove', function(targetUrl) {
		_gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
	});
};

$(function() {
	// Single-post view pages are distinguishable by their .html extension
	if (window.location.pathname.search("\.html") != -1) {
		$.getScript('http://aviawest.disqus.com/embed.js');
	}
	
	createTweetButtons();
	
	// Remove the (0) from all comments
	$("a[href$=#comments]").each(function() {
		$(this).html('Comments');
	});
});

function createTweetButtons() {
	// blog index or category page, (multiple listings)
	$(".entry-header").each(function() {
		var href = $(this).children("a").attr('href');
		var title = $(this).children("a").html();
				
		var tweetButton = $('<a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a>');
		var facebookButton = $('<div class="fb-like" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false" data-font="arial"></div>');
		var gPlusButton = $('<div class="g-plusone" data-size="tall" data-annotation="none"></div>');
		
		if (typeof(href) != 'undefined' && href != null) {
			tweetButton.attr('data-url', href);
			facebookButton.attr('data-href', href);
			gPlusButton.attr('data-href', href);
		}
		
		if (typeof(title) != 'undefined' && title != null) {
			tweetButton.attr('data-text', title);
		}
		
		// select which account to tweet from
		var domain = document.domain.toLowerCase();
		if (domain.search('aviawest.com') != -1) {
			tweetButton.attr('data-via','AviawestResorts');
		} else if (domain.search('parksidevictoria.com') != -1 || domain.search('parksidelifestyle.com') != -1) {
			tweetButton.attr('data-via','parkside_vic');
		} else if (domain.search('watersedgeresort.ca') != -1) {
			tweetButton.attr('data-via','waters_edge');
		} else if (domain.search('pacific-shores.com' )!= -1) {
			tweetButton.attr('data-via','pacific_shores');
		}
		
		tweetButton = $('<li class="blog-twitter">').append(tweetButton);
		facebookButton = $('<li class="blog-facebook">').append(facebookButton);
		gPlusButton = $('<li class="blog-gplus last">').append(gPlusButton);
		
		var container = $('<ul class="social-network-buttons">');
		$(container).append(facebookButton).append(tweetButton).append(gPlusButton).insertAfter($(this));
	});	
	
	$.getScript('http://platform.twitter.com/widgets.js', function() { 	
		twttr.events.bind('tweet', function(event) {
			if (event) {
				var targetUrl;
				if (event.target && event.target.nodeName == 'IFRAME') {
					targetUrl = extractParamFromUri(event.target.src, 'url');
				}
				_gaq.push(['_trackSocial', 'twitter', 'tweet', targetUrl]);
			}
		});
	});
	
	// Facebook
	(function(d, s, id) {
		var js, fjs = d.getElementsByTagName(s)[0];
		if (d.getElementById(id)) {return;}
		js = d.createElement(s); js.id = id;
		js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
		fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));
		
	// Google+
	(function() {
		var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
		po.src = 'https://apis.google.com/js/plusone.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
	})();
}

function extractParamFromUri(uri, paramName) {
	if (!uri) {
		return;
	}
	var uri = uri.split('#')[0];
	var parts = uri.split('?');
	if (parts.length == 1) {
		return;
	}
	var query = decodeURI(parts[1]);

	// Find url param.
	paramName += '=';
	var params = query.split('&');
	for (var i = 0, param; param = params[i]; ++i) {
		if (param.indexOf(paramName) === 0) {
			return unescape(param.split('=')[1]);
		}
	}
}
