$(document).ready(function() {
	
	if( $(".socialloader").length ) {
		
		// Jedes Element anzeigen und Funktionen hinzufuegen
		$('.socialloader').each( function( index ) {
			
			tooltip = "socialplugin" ;
			if ( $(this).hasClass('facebookbutton') ) {
				tooltip = "socialplugin-facebookbutton" ;
				$(this).data( "cookiename", "DatenschutzFacebook" ) ;
			}
			if ( $(this).hasClass('facebooklikebox') ) {
				tooltip = "socialplugin-facebooklikebox" ;
				$(this).data( "cookiename", "DatenschutzFacebook" ) ;
			}
			if ( $(this).hasClass('googleplusbutton') ) {
				tooltip = "socialplugin-googleplusbutton" ;
				$(this).data( "cookiename", "DatenschutzGoogleplus" ) ;
			}
			tooltipfile = tooltip + ".html" ;
			
			// Tooltips einhaengen
			var thisid = this ;
			$.ajax({
				url: "/index.php?aktion=ajax&template=" + tooltipfile ,
				async: false ,
				success: function( html ) {
					
					$( thisid ).CreateBubblePopup({
															selectable		: true ,
															alwaysVisible	: true ,
															position		: 'top' ,
															align			: 'center' ,
															themeMargins	: { total: '7px', difference: '0px' } ,
															distance		: '20px' ,
															innerHtml		: html ,
															width			: '470px' ,
															innerHtmlStyle	: {	color:'#000', 'text-align':'center', 'font-size': '12px' } ,
															themeName		: 'all-orange' ,
															themePath		: '/cms/js/jquery/plugins/jquerybubblepopup/jquerybubblepopup-theme'
														});
				}
			});
			
			// Vorabversion anzeigen
			$(this).css( "display", "inline-block" ) ;
			
			// Socials laden
			$(this).click(function() {
				
				cookiename = $(this).data( "cookiename" ) ;
				$(this).RemoveBubblePopup();
				contentfile = $(this).attr( "rel" ) ;
				var thisid = this ;
				$.ajax({
					url: "/index.php?aktion=ajax&template=" + contentfile ,
					async: false ,
					success: function( html ) {
						$( thisid ).replaceWith( html ) ;
					}
				});
				if ( $(this).hasClass('googleplusbutton') ) {
					gapi.plusone.go();
				}
				
				$.cookie( "argsave-" + cookiename, '1', { expires: 365, path: '/', domain: '.' + window.location.hostname } ) ;
				
				return false ;
			});
			
		});
		
		// Direktes Laden wenn Datenschutz aktzeptiert
		$('.loadinstant').each(function( index ) {
			
			$(this).RemoveBubblePopup();
			contentfile = $(this).attr( "rel" ) ;
			var thisid = this ;
			if ( $(this).hasClass('googleplusbutton') ) { var activategooglebutton = 1 ; }
			$.ajax({
				url: "/index.php?aktion=ajax&template=" + contentfile ,
				async: false ,
				success: function( html ) {
					$( thisid ).replaceWith( html ) ;
				}
			});
			if ( activategooglebutton == 1) {
				gapi.plusone.go();
			}
		});
		
	}
	
});
