(function(){
	function init(){
		var footer_bar = $('#footer-bar.inst-229');
		var dlg = footer_bar.closest('.uk-modal-dialog');
		var meta;
		var google_counter = 0;
		var facebook_counter = 0;
		var folder = '';

		if (typeof(Graphite.App.folder)!='undefined'){
			folder = Graphite.App.folder + '/';
		}

		get_meta(function(out){
			meta = out;
			send_data_to_google();
			send_data_to_facebook();
		})

		function shorten_url(meta,callback){
			return $.ajax({
			    type: "GET",
			    url: folder+'process/shorten_url.php',
			    dataType:'text',
			    data:{
			    	url:meta.url
			    }
			}).success(function(response){
				meta.shorturl = response;
				callback(meta);
			})
		}

		function get_meta(callback){
			var out = {};
			if (dlg.length>0){
				out.url = dlg.data('url');
				out.shorturl = dlg.data('short-url');
				var meta = dlg.find('.metadata');
				if (meta.length>0){
					out.title = meta.data('title');
					out.description = meta.data('description');
					out.image = meta.data('image');
				}
				callback(out);
			} else {
				var head = $('head');
				out.url = head.find('link[rel=canonical]').attr('href');
				out.title = head.find('title').text();
				out.description = head.find('meta[name=Description]').attr('content');
				out.image = head.find('meta[property="og:image"]').attr('content');				
				shorten_url(out,function(out){
					callback(out);
				})
			}
		}

		function send_data_to_google(){
			if ((typeof(ga)!='undefined') && (dlg.length>0)){
				ga('send', {
				  hitType: 'pageview',
				  title: meta.title,
				  page: meta.url.replace(Graphite.App.base_url,'')
				});
			}
		}

		function send_data_to_facebook(){
			if ((typeof(fbq)!='undefined') && (dlg.length>0)){
				var ts = new Date().getTime();
				var url = 'https://www.facebook.com/tr/?id='+fbq.pixel_id+'&ev=PageView&dl='+meta.url+'&rl=https%3A%2F%2Fwww.facebook.com%2F&if=false&ts='+ts+'&v=2.7.21&ec=0&o=30&it='+(ts+642);
				footer_bar.after('<img style="position: absolute;left:0;top:0" src="'+url+'">');
			}			
		}

		function bindings(){
			var social_icons = footer_bar.find('.social-icons');

			social_icons.on('click','a.icon-facebook',function(evt){
				evt.preventDefault();
				var facebook_app_id = footer_bar.data('facebook-app-id');
				var href = 'https://www.facebook.com/sharer/sharer.php?app_id='+facebook_app_id+'&sdk=joey&u='+encodeURIComponent(meta.shorturl)+'&display=popup&ref=plugin&src=share_button';
				var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
				var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
		    	var w = 555;
		    	var h = 540;
		    	var left = ((width / 2) - (w / 2));
		    	var top = ((height / 2) - (h / 2));	

		    	if (width>580){
		    		w = 555;
		    		h = 460;
		    		left = ((width / 2) - (w / 2));
		    		top = ((height / 2) - (h / 2));			    		
					window.open(href,'_blank','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=555,height=460,top='+top+',left='+left);
				} else {
					window.open(href,'_blank');
				}
			})		

			social_icons.on('click','a.icon-twitter',function(evt){
				evt.preventDefault();
				var href = 'https://twitter.com/intent/tweet?url='+encodeURIComponent(meta.shorturl)+'&text='+encodeURIComponent(meta.title);
				var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
				var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
		    	var w = 555;
		    	var h = 460;
		    	var left = ((width / 2) - (w / 2));
		    	var top = ((height / 2) - (h / 2));	
			    if (width>540){
					window.open(href,'_blank','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=555,height=460,top='+top+',left='+left);
				} else {
					window.open(href,'_blank');
				}
			})		

			social_icons.on('click','a.icon-line',function(evt){
				evt.preventDefault();
				var href = 'https://timeline.line.me/social-plugin/share?url='+encodeURIComponent(meta.shorturl)+'&text='+encodeURIComponent(meta.title);
				var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
				var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
			   	var w = 555;
			   	var h = 505;
			   	var left = ((width / 2) - (w / 2));
			   	var top = ((height / 2) - (h / 2));	
			   	if (width>540){
					window.open(href,'_blank','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=555,height=505,top='+top+',left='+left);
				} else {
					window.open(href,'_blank');
				}
			})		

			social_icons.on('click','a.icon-email',function(evt){
				evt.preventDefault();
				var href = 'https://mail.google.com/mail/u/0/?view=cm&fs=1&to&su='+encodeURIComponent(meta.title)+'&body='+encodeURIComponent(meta.shorturl)+'&ui=2&tf=1';
				var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
				var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
			   	var w = 555;
			   	var h = 505;
			   	var left = ((width / 2) - (w / 2));
			   	var top = ((height / 2) - (h / 2));	
			   	if (width>540){
					window.open(href,'_blank','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=555,height=505,top='+top+',left='+left);
				} else {
					window.open(href,'_blank');
				}
			})		

			social_icons.on('click','a.icon-pocket',function(evt){
				evt.preventDefault();
				var href = 'https://getpocket.com/edit?url='+encodeURIComponent(meta.shorturl)+'&title='+encodeURIComponent(meta.title);
				var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
				var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
			   	var w = 555;
			   	var h = 505;
			   	var left = ((width / 2) - (w / 2));
			   	var top = ((height / 2) - (h / 2));	
			   	if (width>540){
					window.open(href,'_blank','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=555,height=505,top='+top+',left='+left);
				} else {
					window.open(href,'_blank');
				}
			})
		}

		if (dlg.length>0){
			dlg.find('.fb-comments').attr('data-href',dlg.data('url'));
		}

		bindings();

		<!-- send_data_to_google(); -->
		<!-- send_data_to_facebook(); -->
	}

	init();
})()	