if ( typeof( amateurpin ) == 'undefined' )
{
	var amateurpin = {};
}
if ( typeof( amateurpin.toolbar ) == 'undefined' )
{
	amateurpin.toolbar = {
		initialized: false,
		language: 'de',
		payment_layer: null,
		stop_layer: null,
		faq_layer: null,
		overlay: null,
		proxy: null,
		openid: '',
		site: '',

		init: function()
		{
			if ( !this.initialized )
			{
				// set up proxy iframe
				this.proxy = document.createElement( 'iframe' );
				this.proxy.src = 'http://www.amateurpin.com/ap_toolbar/xdproxy.php';
				this.proxy.id = 'ap-xdproxy';
				this.proxy.name = 'ap-xdproxy';
				this.proxy.frameBorder = '1';
				this.proxy.scrolling = 'auto';
				this.proxy.width = 30;
				this.proxy.height = 30;
				this.proxy.style.zIndex = -9999;
				this.proxy.style.position = 'absolute';
				this.proxy.style.visibility = 'hidden';
				this.proxy.style.left = '0';
				this.proxy.style.bottom = '-100px';
				this.proxy.style.border = 'none';
				this.proxy.style.backgroundColor = 'transparent';
				document.getElementsByTagName( 'body' )[0].appendChild( this.proxy );
				this.initialized = true;
			}
		},

		ToggleLoginLayer: function()
		{
			var login_layer = jQuery("#ap_login_layer_top");
			if ( login_layer.css( 'display' ) == 'none' )
			{
				this.AddOverlay();
			}
			else
			{
				this.RemoveOverlay();
			}
			login_layer.slideToggle("400" );
		},

		SetCoins: function( coins )
		{
			coins = (+coins);	// force to int
			jQuery('#apt_coins').html( coins );
		},

		SetUnreadMessages: function( msg_unread )
		{
			msg_unread = (+msg_unread);
			jQuery('#apt_msg_unread').html( msg_unread );
		},

		OpenUpgradeLayer: function( )
		{
			this.AddOverlay();
			this.payment_layer = document.createElement( 'iframe' );
			this.payment_layer.src = 'http://www.amateurpin.com/' + this.language + '/payment?openid=' + this.openid + '&source=' + encodeURIComponent( window.location.protocol + '//' + window.location.hostname ) + '&source_site=' + encodeURIComponent( this.site );
			this.payment_layer.width = 856;
			this.payment_layer.height = 621;
			this.payment_layer.scrolling = 'no';
			this.payment_layer.frameBorder = 'no';
			this.payment_layer.transparent = 'yes';
			this.payment_layer.border = '0';
			this.payment_layer.style.height = '621px';
			this.payment_layer.style.width = '856px';
			this.payment_layer.style.position = 'absolute';
			this.payment_layer.style.left = '50%';
			this.payment_layer.style.top = '50%';
			this.payment_layer.style.marginTop = '-310px';
			this.payment_layer.style.marginLeft = '-428px';
			// this.payment_layer.style.background = 'transparent';
			this.payment_layer.style.background = '#fff url(http://www.amateurpin.com/ap_toolbar/images/loader.gif) center center no-repeat';
			this.payment_layer.style.borderStyle = 'none';
			this.payment_layer.style.zIndex = 10000;
			jQuery( 'body' ).append( this.payment_layer );
		},

		CloseUpgradeLayer: function()
		{
			if ( this.payment_layer != null )
			{
				jQuery( this.payment_layer ).remove();
				this.RemoveOverlay();
				this.payment_layer = null;
				window.location.reload();	// reload current page
			}
		},

		OpenStopLayer: function( what )
		{
			if ( typeof( what ) != 'undefined' )
			{
				this.AddOverlay();
				this.stop_layer = document.createElement( 'iframe' );
				this.stop_layer.src = 'http://www.amateurpin.com/' + this.language + '/stop/' + what + 'premium?openid=' + this.openid + '&source=' + encodeURIComponent( window.location.protocol + '//' + window.location.hostname ) + '&source_site=' + encodeURIComponent( this.site );
				this.stop_layer.width = 856;
				this.stop_layer.height = 621;
				this.stop_layer.scrolling = 'no';
				this.stop_layer.frameBorder = 'no';
				this.stop_layer.transparent = 'yes';
				this.stop_layer.border = '0';
				this.stop_layer.style.height = '621px';
				this.stop_layer.style.width = '856px';
				this.stop_layer.style.position = 'absolute';
				this.stop_layer.style.left = '50%';
				this.stop_layer.style.top = '50%';
				this.stop_layer.style.marginTop = '-310px';
				this.stop_layer.style.marginLeft = '-428px';
				// this.payment_layer.style.background = 'transparent';
				this.stop_layer.style.background = '#fff url(http://www.amateurpin.com/ap_toolbar/images/loader.gif) center center no-repeat';
				this.stop_layer.style.borderStyle = 'none';
				this.stop_layer.style.zIndex = 10000;
				jQuery( 'body' ).append( this.stop_layer );
			}
		},

		CloseStopLayer: function()
		{
			if ( this.stop_layer != null )
			{
				jQuery( this.stop_layer ).remove();
				this.RemoveOverlay();
				this.stop_layer = null;
			}
		},

		OpenFaqLayer: function( category )
		{
			if ( typeof( category ) == 'undefined' )
			{
				category = '';
			}
			this.AddOverlay();
			this.faq_layer = document.createElement( 'iframe' );
			this.faq_layer.src = 'http://www.amateurpin.com/' + this.language + '/support/faq?category=' + encodeURIComponent( category ) + '&openid=' + this.openid + '&source_site=' + encodeURIComponent( this.site );
			this.faq_layer.width = 856;
			this.faq_layer.height = 621;
			this.faq_layer.scrolling = 'no';
			this.faq_layer.frameBorder = 'no';
			this.faq_layer.transparent = 'yes';
			this.faq_layer.border = '0';
			this.faq_layer.style.height = '621px';
			this.faq_layer.style.width = '856px';
			this.faq_layer.style.position = 'absolute';
			this.faq_layer.style.left = '50%';
			this.faq_layer.style.top = '50%';
			this.faq_layer.style.marginTop = '-310px';
			this.faq_layer.style.marginLeft = '-428px';
			this.faq_layer.style.background = '#fff url(http://www.amateurpin.com/ap_toolbar/images/loader.gif) center center no-repeat';
			this.faq_layer.style.borderStyle = 'none';
			this.faq_layer.style.zIndex = 10000;
			jQuery( 'body' ).append( this.faq_layer );
		},

		CloseFaqLayer: function()
		{
			if ( this.faq_layer != null )
			{
				jQuery( this.faq_layer ).remove();
				this.RemoveOverlay();
				this.faq_layer = null;
			}
		},

		SwitchToUpgradeLayer: function()
		{
			this.CloseStopLayer();
			this.OpenUpgradeLayer();
		},

		AddOverlay: function()
		{
			if ( this.overlay == null )
			{
				this.overlay = document.createElement( 'div' );
				this.overlay.id = 'ap-overlay';
				this.overlay.style.display = 'block';
				this.overlay.style.position = 'absolute';
				this.overlay.style.margin = '0';
				this.overlay.style.padding = '0';
				this.overlay.style.zIndex = '1000';
				jQuery(this.overlay).css( 'opacity', 0.75 ).css( 'background-color', '#000' );
			}
			this.overlay.style.width = jQuery(document).width() + 'px';
			this.overlay.style.height = jQuery(document).height() + 'px';
			jQuery( 'body' ).prepend( this.overlay );
		},

		RemoveOverlay: function()
		{
			jQuery( this.overlay ).remove();
		},

		ProxyFunction: function( function_string )
		{
			var t = new Date();
			this.proxy.src = 'http://www.amateurpin.com/ap_toolbar/xdproxy.php?f=' + encodeURIComponent( function_string ) + '&t=' + t.getTime();
			this.proxy.width = this.proxy.width == 30 ? 60 : 30;
		}
	}
}

jQuery(document).ready( amateurpin.toolbar.init );
