var ExInWidget = function() {
	var jQ;

/**
 * Check if jquery is present and if not get and initialize it
 */
	if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
		var scriptTag = document.createElement('script');
		scriptTag.setAttribute("type", "text/javascript");
		scriptTag.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
	    scriptTag.onload = scriptLoadHandler;

	    // IE
	    scriptTag.onreadystatechange = function () {
	    	if (this.readyState == 'complete' || this.readyState == 'loaded') {
	        	scriptLoadHandler();
	    	}
		};
		(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(scriptTag);
	} else {
		jQ = window.jQuery;
		init();
	}

/**
 * jQuery handler
 */
	function scriptLoadHandler() {
		jQ = $.noConflict();
		init();
	};

/**
 * Widget initialization
 */
	function init() {
		options = ExInOptions;
		jQ(document).ready(function($) {
	        var cssLink = $("<link>", {
	            rel: "stylesheet", 
	            type: "text/css", 
	            href: options.baseUrl + "css/widgets.css"});
	        cssLink.appendTo('head');


		var tooltip = options.tooltip ? '/tooltip:'+options.tootltip : '';

	        // Get the widget
	        var url = options.baseUrl + 'widgets/profile/' + options.user + '/' + options.type + '/widget' + tooltip + '.json?callback=?';
	        $.getJSON(url, function(data, textStatus, jqXHR) {
	        	$('#ExInWidget').html(data.content);
	        	$('#ExInWidget').attr('class', options.type);
	        });

	        //Tooltip
		if (options.tooltip != 'undefined') {
				if (!$.fn.qtip) {
						$.getScript(options.baseUrl + 'js/jquery.qtip.js', function() {
					});
				}
				setupToolTip($);
		}
	        // Link for button widgets
		if (options.type.indexOf('button') != -1) {
			// $('#ExInWidget').css('cursor', 'pointer');
			$('#ExInWidget').click(function() {
				var redirect = options.baseUrl + options.slug;
				_gaq.push(['b._setAccount', 'UA-21995837-1'],['b._setDomainName', '.expertinsight.com'] ['b._trackPageview'], ['b._setCustomVar', 1, 'hostname', window.location, 3], ['b._trackEvent', 'Widget', 'click', options.slug]);
				window.location = redirect;
			});
		}
		});
		
		if(_gaq != null){
			_gaq.push(['b._setAccount', 'UA-21995837-1'],['b._setDomainName', '.expertinsight.com'] ['b._trackPageview'], ['b._setCustomVar', 1, 'hostname', window.location, 3], ['b._trackEvent', 'Widget', 'view', options.slug]);
		}
		else{
			(function() {		
				var ga = document.createElement('script'); 
				ga.type = 'text/javascript'; ga.async = true;		
				ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';		
				var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);		
			})();	
			_gaq = _gaq || [];
			
			_gaq.push(['_setAccount', 'UA-21995837-1'],['_setDomainName', '.expertinsight.com'] ['_trackPageview'], ['_setCustomVar', 1, 'hostname', window.location, 3], ['_trackEvent', 'Widget', 'view', options.slug]);
		}
	};

	function setupToolTip($) {
		$('#ExInWidget').qtip({
			content: {
				text: function() {
					return $('#ExInWidget .tooltip-container').html();
				}
			},
			when: {
				target: $('#ExInWidget, #ExInWidget .tooltip-container')
			},
			position: {
				my: 'left center',
				at: 'right center',
				viewport: $(window)
			},
			style: {
				classes: 'ui-tooltip-light ui-tooltip-shadow ui-tooltip-rounded'
			},
			hide: {
				delay: 500,
				fixed: true
			}
		});
	}

	return { init:init };
}();

