/**
 * JavaScript to load social media widget JavaScript in the background.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS\module\social
 * @copyright Copyright (c) 2013 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

$fl(document).ready(function() {

	// Look through style sheets for CSS rules
	var hasSocialCSS = false;
	r = new RegExp('\\.cfSocial.*\\b');
	var sheets = document.styleSheets;
	for(var i=0;i<sheets.length && !hasSocialCSS;i++) {
		try {
			var rules = sheets[i].rules ? sheets[i].rules : sheets[i].cssRules;
			if(rules) {
				for(var n=0;rules && n<rules.length && !hasSocialCSS;n++) {
					var s = rules[n].selectorText ? rules[n].selectorText : rules[n].cssText;
					if(r.test(s))
						hasSocialCSS = true;
				}
			}
		} catch(e) {}
	}

	// If no social CSS found then load our default CSS
	if(!hasSocialCSS) {
		var link = document.createElement("link");
		link.rel = "stylesheet";
		link.href = "system/modules/social/social.css";
		document.getElementsByTagName("head")[0].appendChild(link);
	}

	// Load the social JavaScript
	var script,
		first = document.getElementsByTagName('script')[0],
		withFBComments = $fl('.cfSocialFBComments').length;

	// Load Facebook script if required
	if($fl('.cfSocialFBLike').length || withFBComments) {
		// If with comments then subscribe to events
		if(withFBComments) {
			window.fbAsyncInit = function() {
				FB.Event.subscribe('comment.create', function(response) {
					$fl.post(document.location, {
						'op' : 'fbCommentAdd',
						'href' : response.href
					});
				});
				FB.Event.subscribe('comment.remove', function(response) {
					$fl.post(document.location, {
						'op' : 'fbCommentRemove',
						'href' : response.href
					});
				});
			};
		}

		// Add fb-root div
		var root = document.createElement('div');
		root.id = "fb-root";
		document.getElementsByTagName('body')[0].appendChild(root);

		// Load JS SDK
		script = document.createElement('script');
		script.async = 'true';
		script.type = 'text/javascript';
		script.src = '//connect.facebook.net/en_US/all.js#xfbml=1';
		first.parentNode.insertBefore(script, first);
	}

	// Load Pinterest
	if($fl('.cfSocialPinterest').length) {
		var pinterestImgSrc = false;

		// Look for an image with class pinit-image
		$fl('img.pinit-image').each(function() {
			pinterestImgSrc = this.src;
			return false;
		});

		// No specific image so try for 1st on the page
		if(pinterestImgSrc == false) {
			$fl('img').each(function() {
				pinterestImgSrc = this.src;
				return false;
			});
		}

		// Find the pin buttons without media
		if(pinterestImgSrc != false) {
			$fl('.pin-it-button').each(function() {
				rt = new RegExp('&media=');
				if(!rt.test(this.href)) {
					this.href += '&media=' + encodeURI(pinterestImgSrc);
					return false;
				}
			});
		}

		// Load pinterest script
		script = document.createElement('script');
		script.async = 'true';
		script.type = 'text/javascript';
		script.src = '//assets.pinterest.com/js/pinit.js';
		first.parentNode.insertBefore(script, first);
	}

	// Load Google Plus
	if($fl('.cfSocialGPlus').length) {
		script = document.createElement('script');
		script.async = 'true';
		script.type = 'text/javascript';
		script.src = 'https://apis.google.com/js/plusone.js';
		first.parentNode.insertBefore(script, first);
	}

	// Load Twitter
	if($fl('.cfSocialTweet').length) {
		script = document.createElement('script');
		script.async = 'true';
		script.type = 'text/javascript';
		script.src = '//platform.twitter.com/widgets.js';
		first.parentNode.insertBefore(script, first);
	}

	// Load LinkedIn
	if($fl('.cfSocialLinkedIn').length) {
		script = document.createElement('script');
		script.async = 'true';
		script.type = 'text/javascript';
		script.src = '//platform.linkedin.com/in.js';
		first.parentNode.insertBefore(script, first);
	}

	// Load LinkedIn
	if($fl('.cfSocialBuffer').length) {
		script = document.createElement('script');
		script.async = 'true';
		script.type = 'text/javascript';
		script.src = '//static.bufferapp.com/js/button.js';
		first.parentNode.insertBefore(script, first);
	}
});
