/**
 * 	@file saks.social.js
 * 
 * 	@description 
 * 	Loads required JavaScript documents from their respective CDN into the current page to 
 * 	properly initialize social content.
 */

(function ($) {
	
	if (!$) return;
	
	// code ran when FB SDK is initialized
	window.fbAsyncInit = function() {
  		FB.init({
	      	appId      : '112579470003', // Saks App ID
	      	status     : true, // check login status
	      	cookie     : true, // enable cookies to allow the server to access the session
	      	xfbml      : true   // parse XFBML
	    });
  	};
	
	/**
  	 * 	Loads a single script into the document head.
  	 * 	@param params {Object} object with script attributes.
  	 * 	@method loadScript(params)
  	 *  @returns void
  	 */
  	
	function loadScript (params) {
  		var script = document.createElement('script');
  		script.type = "text/javascript"; script.asynch = params.asynch;
  		script.src = params.src;
  		document.getElementsByTagName('head')[0].appendChild(script);			  		
  	}
	
	/**
	 * 	Loads an array of scripts into the document head.
	 * 	@param params {Array} array of object with each script attribute
	 * 	@method loadScript(params)
	 * 	@returns void 
	 */
  	
  	function loadScripts (params) {
  		for (var i=0; i<params.length; i++) loadScript(params[i]);
  	}
  	
  	
  	// Load social SDKs Asynchronously
  	
  	loadScripts([
   		{ src: '//connect.facebook.net/en_US/all.js', asynch: true },			  		
   		{ src: 'https://apis.google.com/js/plusone.js', asynch: true }
   		/*{ src: '//platform.twitter.com/widgets.js', asynch: true }*/
   	]);
	
  	$(function () {
		
		var context = $('#share-product'); 
		
		$('a.twitter-custom-button', context).click(function(event){
			
			event.preventDefault();
			
			var width = 550, height = 420,
				left = (screen.width / 2) - (width / 2), 
				top = (screen.height / 2) - (height / 2);
			
			window.open(this.href, '', 'width=' + width + ', height=' + height + ', top=' + top + ', left=' + left);
		});
		
	});
  	
})(jQuery);
