/*!
 * $Id: ibm-social-media-box.js,v 1.0 2010/01/29 janetka Exp $
 * IBM Social Media Box script
 */

(function($){
	$.fn.extend({ 
 		ibmSocialMediaBox: function(options) {
			var defaults = {
				path_overlay: '#', // this is mandatory, needs to be set by WB
				path_trigger: '#', // this is mandatory, needs to be set by WB
				elem_trigger: 'div.ibm-live-assistance:first' // default value
			},			
			options = $.extend(defaults, options);
			
			// make sure that both paths are set 
			if(options.path_overlay == '#' || options.path_trigger == '#') {
				return false;
			}
			
			// if overlay.css is included, won't do anything, otherwise, it will include it
			if ($("link[href$='/common/v16/css/overlay.css']").length == 0) {
				$("head").append ('<link href="//www.ibm.com/common/v16/css/overlay.css" media="screen,projection" rel="stylesheet" title="www" type="text/css"/>');			
			}
		
			// check if Overlays namespace is defined, otherwise include it
			if (typeof (ibmCommon.Overlays) != 'object' || ibmCommon.Overlays == null) {
				$.getScript("//www.ibm.com/common/js/overlay.js");
			}
			
			// load both overlay code and trigger code
			$.getScript( options.path_overlay );
			$.getScript( options.path_trigger );
			
			// now wait for all the files to be loaded
			ibmCommon.queue.push(function() {
				return typeof(ibmCommon.Overlays) == 'object' && typeof(__ibm_social_media_trigger) != 'undefined' && typeof(__ibm_social_media_overlay) != 'undefined';
			}, function() {
				$('#ibm-content-main').append( __ibm_social_media_overlay );
				$( options.elem_trigger ).after( __ibm_social_media_trigger );
			});
			
			return false;			
    	}
	});	
})(jQuery);
