(function() {
	if( /template\.asp/.test(document.URL) ) {
		return false;
	}
	
	var sScriptsDir		= '/efincludes/js/social/';
	var sStylesDir		= '/efincludes/css/social/';
	var oContainer		= null;
	var sContainerId	= 'SocialLikeItButtonsContainer';
	var LikeItButton	= {};
	
	function override(defaultData, newData) {
		var resData = {};
		
		for(var p in defaultData) {
			if(typeof newData[p] !== 'undefined')
				resData[p] = newData[p];
			else
				resData[p] = defaultData[p];
		}
		
		return resData;
	}
		
	function insertAfter(newNode, refNode) {
		refNode.parentNode.insertBefore(newNode, refNode.nextSibling);
	}
	
	function addContainer() {
		if(!document.getElementById(sContainerId)) {
			document.write('<div id="'+ sContainerId +'"></div>');
		}
		
		return document.getElementById(sContainerId);
	}
	
	function appendCode(content, toObj) {
		var oButtonContainer = document.createElement('DIV');
		oButtonContainer.className = 'socialButtonContainer';
		
		if(typeof content == 'object') {
			oButtonContainer.appendChild(content);
		} else {
			oButtonContainer.innerHTML = content.toString();
		}
		
		if(toObj) {
			toObj.appendChild( oButtonContainer );
		} else {
			oContainer = oContainer || addContainer();		
			oContainer.appendChild( oButtonContainer );
		}
		
		return oButtonContainer;
	}
		
	function createScriptElement(attrs, innerHtml) {
		var oScript = document.createElement('SCRIPT');
		
		oScript.setAttribute('type', 'text/javascript');
		
		if(typeof attrs == 'string') {
			oScript.setAttribute('src', attrs);
			//return oScript;
		}
		
		if(innerHtml) {
			//oScript.innerHTML = innerHtml;
			oScript.text = innerHtml;
		}
		
		/*for(var a in attrs) {
			oScript.setAttribute(a, attrs[a]);
		}*/
		
		return oScript;
	}
		
	function createStyleElement(attrs, innerHtml) {
		var oScript = document.createElement('LINK');
		
		oScript.setAttribute('type', 'text/css');
		oScript.setAttribute('rel', 'stylesheet');
		
		if(typeof attrs == 'string') {
			oScript.setAttribute('href', attrs);
			return oScript;
		}
		
		for(var a in attrs) {
			oScript.setAttribute(a, attrs[a]);
		}
		
		if(typeof innerHtml !== 'undefined') {
			oScript.innerHTML = innerHtml;
		}
		
		return oScript;
	}
		
	function addEvent(elm, evType, fn, useCapture) {
	    if (elm.addEventListener) {
	        elm.addEventListener(evType, fn, useCapture);
	        return true;
	    }
	    else if (elm.attachEvent) {
	        var r = elm.attachEvent('on' + evType, fn);
	        return r;
	    }
	    else {
	        elm['on' + evType] = fn;
	    }
	}
		
	function addStyles() {
		var oHead = document.getElementsByTagName('HEAD')[0];

		oHead.appendChild(createStyleElement(sStylesDir + 'social-srvs-1.0.css'));
	}

	function addSocialLikeItButtons(oButtonList, sEnabledWidgets) {
		addContainer();
		addStyles();
		
		var widgetList = sEnabledWidgets.split(',');
		var len = widgetList.length;
		var widgetName = '';
		
		for(var i = 0; i < len; i++) {
			widgetName = widgetList[i];
			
			if(typeof LikeItButton[widgetName] == 'function') {
				LikeItButton[widgetName](oButtonList[widgetName]);
			}
		}
		
		/*
		for(sSocNetName in oButtonList) {
			if(typeof LikeItButton[sSocNetName] == 'function' && sEnabledWidgets.indexOf(sSocNetName) != -1)
				LikeItButton[sSocNetName](oButtonList[sSocNetName]);
		}*/
	}
	
	function getInternetExplorerVersion() {
	// Returns the version of Internet Explorer or a -1
	// (indicating the use of another browser).
		var rv = -1; // Return value assumes failure.
		
		if (navigator.appName == 'Microsoft Internet Explorer') {
			var ua = navigator.userAgent;
			var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
			
			if (re.exec(ua) != null)
				rv = parseFloat( RegExp.$1 );
		}
		
		return rv;
	}
	
	
	/**
	 * href - the URL to like. The XFBML version defaults to the current page.
	 	send - specifies whether to include a Send button with the Like button. This only works with the XFBML version. 
		layout - there are three options. 
			standard - displays social text to the right of the button and friends' profile photos below. Minimum width: 225 pixels. Default width: 450 pixels. Height: 35 pixels (without photos) or 80 pixels (with photos). 
			button_count - displays the total number of likes to the right of the button. Minimum width: 90 pixels. Default width: 90 pixels. Height: 20 pixels. 
			box_count - displays the total number of likes above the button. Minimum width: 55 pixels. Default width: 55 pixels. Height: 65 pixels.
		show_faces - specifies whether to display profile photos below the button (standard layout only) 
		width - the width of the Like button. 
		action - the verb to display on the button. Options: 'like', 'recommend' 
		font - the font to display in the button. Options: 'arial', 'lucida grande', 'segoe ui', 'tahoma', 'trebuchet ms', 'verdana' 
		colorscheme - the color scheme for the like button. Options: 'light', 'dark' 
		ref - a label for tracking referrals; must be less than 50 characters and can contain alphanumeric characters and some punctuation (currently +/=-.:_). The ref attribute causes two parameters to be added to the referrer URL when a user clicks a link from a stream story about a Like action: 
		fb_ref - the ref parameter 
		fb_source - the stream type ('home', 'profile', 'search', 'other') in which the click occurred and the story type ('oneline' or 'multiline'), concatenated with an underscore.
	 */
	
	LikeItButton.Facebook = function(oData) {
		var oSettings = {
			locale		: 'en_US',
			href		: location.href,
			send		: 'false',
			layout		: 'button_count',
			show_faces	: 'false',
			action		: 'like',
			colorscheme	: 'light',
			width		: '150',
			height		: '22',
			font		: ''
		};
		
		var oQueryData = override( oSettings, oData );
		var sQuery = '';
		var aQueryParts = [];
		
		for(var sP in oQueryData) {
			aQueryParts.push( [sP, oQueryData[sP]].join('=') );
		}
		
		sQuery = encodeURI( aQueryParts.join( '&' ) );
		
		var oIFrame = document.createElement('IFRAME');
		oIFrame.src = 'http://www.facebook.com/plugins/like.php?' + sQuery;
		oIFrame.scrolling = 'no';
		oIFrame.frameborder = '0';
		oIFrame.style.border = 'none';
		oIFrame.style.overflow = 'hidden';
		oIFrame.style.width = parseInt(oQueryData.width) + 'px';
		oIFrame.style.height = parseInt(oQueryData.height) +'px';
		oIFrame.allowTransparency = true;
		
		appendCode(oIFrame);
		
		/*addEvent(window, 'load', function() {
			var oDoc = oIFrame.contentWindow.document;
			//connect_widget_interactive_area
			var aTables = oDoc.getElementsByTagName('TABLE');
			var len = '';
			
			for(var i = 0; i < len; i++) {
				el = aTables[i];
				
				if(el.className && /\bconnect_widget_interactive_area\b/.test(el.className)) {
					oIFrame.style.width = el.offsetWidth + 5 + 'px';
					break;
				}
			}
		});*/
		
		/*
		appendCode([
			'<iframe',
			'src="http://www.facebook.com/plugins/like.php?' + sQuery,
			'scrolling="no" frameborder="0"',
			'style="border:none; overflow:hidden;',
			'width:'+ parseInt(oQueryData.width) +'px; height:'+ parseInt(oQueryData.height) +'px;"',
			'allowTransparency="true"></iframe>'
		].join(' '));
		*/
	}
	
	LikeItButton.Odnoklassniki = function(oData) {
		var oSettings = {
			width	: '100'
		};
		
		var oNewData = override( oSettings, oData );
		
		var oHead = document.getElementsByTagName('HEAD')[0];

		oHead.appendChild(createStyleElement('http://stg.odnoklassniki.ru/share/odkl_share.css'));
		oHead.appendChild(createScriptElement('http://stg.odnoklassniki.ru/share/odkl_share.js'));
		
		var oBlock = appendCode('\
			<div style="float: left;" id="odklLikeItButton">\
				<a class="odkl-klass-oc" href="'+ location.href +'" onclick="ODKL.Share(this);return false;" ><span>0</span></a>\
			</div>\
		');
		
		oBlock.style.width = oNewData.width + 'px';
		
		addEvent(window, 'load', function() {
			var sc = createScriptElement(sScriptsDir + 'odklinit.js');
			var oODKLContainer = document.getElementById('odklLikeItButton');
			
			insertAfter(sc, oODKLContainer);
		});
	}
	
	/**
	 * width - block width.
	 * apiId - application ID.
	 * type  - button design. Possible values:
	 	 full		- button with text counter,
	 	 button		- button with compact counter,
	 	 mini		- compact button,
	 	 vertical	- compact button, counter above.
	 */
	
	
	LikeItButton.VKontakte = function(oData) {
		var oHead = document.getElementsByTagName('HEAD')[0];
		var oSettings = {
			width		: '160',
			apiId		: '2450773',
			type		: 'button',
			onlyWidgets	: true
		};
		
		
		function VKOverride( oSettings, oData ) {
			var oNewData = override( oSettings, oData );
			
			window.VKWidgetsLikeSettings = window.VKWidgetsLikeSettings || {};
			window.VKInitSettings = window || {};
			
			window.VKWidgetsLikeSettings.options = {
				type	: oNewData.type,
				width	: oNewData.width
			};
			
			window.VKInitSettings.options = {
				apiId		: oNewData.apiId,
				onlyWidgets	: oNewData.onlyWidgets
			};

			
			return oNewData;
		}
		
		var oSettings = VKOverride( oSettings, oData );

		oHead.appendChild(createScriptElement('http://userapi.com/js/api/openapi.js?34'));
		
		var oBlock = appendCode('<div id="vk_like"></div>');
		oBlock.style.width = oSettings.width + 'px';
		
		
		addEvent(window, 'load', function() {
			var sc1 = createScriptElement(sScriptsDir + 'vkinit.js');
			var oVKContainer = document.getElementById('vk_like');
			
			//pending
			
			insertAfter(sc1, oVKContainer);
			//EfSocialVKInit();
		});
	}
	
	
	/*
	 * count - add counter;
	 * lang - language (en, uk, ru);
	 * width - block width;
	 * size - block size default|small|medium|tall
	 * 
	 * http://www.google.com/webmasters/+1/button/
	 */
	LikeItButton.GPlusOne = function(oData) {
		
		var ieVersion = getInternetExplorerVersion();
		
		if(ieVersion > 0) {
			ieVersion = parseInt(ieVersion);
			
			if(ieVersion < 9) {
				return false;
			}
		}
		
		var oSettings = {
			size	: 'medium',		// tall|medium|small|<default>
			lang	: 'ru',			// uk, ru, <default - en>
			count	: 'true',		// true|false
			width	: '100'
		};
		var oNewData	= override( oSettings, oData );
		var oHead		= document.getElementsByTagName('HEAD')[0];
		var oGplusone	= document.createElement('G:PLUSONE');
		
		if(oNewData.size != 'default') {
			oGplusone.setAttribute('size', oNewData.size);
		}
		
		oGplusone.setAttribute('count', oNewData.count);
		//oGplusone.setAttribute('annotation', 'inline');
		//oGplusone.setAttribute('href', location.href);
		
		var oBlock = appendCode(oGplusone);
		oBlock.style.width = oNewData.width + 'px';		
		
		document.body.appendChild(createScriptElement('http://apis.google.com/js/plusone.js', '{lang: "'+ oNewData.lang +'"}'));
		
		/*Fix for IDN*/
		addEvent(window, 'load', function() {
			var iframes = oBlock.getElementsByTagName('IFRAME');
			
			if(iframes[0]) {
				iframes[0].style.top = '0px';
				iframes[0].style.left = '0px';
			}
			
			//iframes[0].style.position = 'relative';
		});
	};
	
	window.addSocialLikeItButtons = addSocialLikeItButtons;
})();
