var socialServices = {
	facebookWindow: null,
	twitterWindow: null,
	myspaceWindow: null,
	windowCloseTimeout: null,
	socialLogin: false,
	appUrl: null,
	sessId: null,
	shoppingCartForm: null,
	selectedService: null,
	selectedProduct: null,
	initialized: false,
	canClick: true,
	logoutAction: false,
	init: function(config) {
		facebookIsActive = config['facebook']; 
		twitterIsActive = config['twitter'];
		myspaceIsActive = config['myspace'];
		if (config['logout_action'])
			socialServices.logoutAction = config['logout_action'];
		socialServices.appUrl = config['application_url'];
		socialServices.sessId = config['session_id'];
		if (facebookIsActive) {
			socialServices.facebook.init()
		}
		if (twitterIsActive) {
			socialServices.twitter.init()
		}
		if (myspaceIsActive) {
			socialServices.myspace.init()
		}
		socialServices.initialized = true;
	},
	facebook: {
		init: function() {
			$('#facebook-service').bind('click', function(){
				socialServices.facebook.clickFunction();
				return false;
			});
		},
		clickFunction: function() {
			if (!socialServices.canClick)
			{
				return false;
			}
			socialServices.canClick = false;
			$('#error_message').hide();
			$('#service_unavailalble_message').hide();
			socialServices.setService('facebook');
			socialServices.startLoading();
			var _url = socialServices.appUrl+'facebook/?sess='+socialServices.sessId;
			if (socialServices.logoutAction)
			{
				_url += '&action=logout';
			}
			socialServices.facebookWindow = window.open(_url, 'facebookwin', 'width=800,height=400,status=0,directories=0,location=0');
			socialServices.facebookWindow.focus();
			socialServices.checkingWindowIsClosed();
			$('#applying_of_discount_message').hide();
			$('#adding_to_shopping_cart_message').hide();
		},
		image : 'facebook.png'
	},
	twitter: {
		init: function() {
			$('#twitter-service').bind('click', function(){
				socialServices.twitter.clickFunction();
				return false;
			});
		},
		clickFunction: function() {
			if (!socialServices.canClick)
			{
				return false;
			}
			socialServices.canClick = false;
			$('#error_message').hide();
			$('#service_unavailalble_message').hide();
			socialServices.setService('twitter');
			socialServices.startLoading();
			socialServices.twitterWindow = window.open(socialServices.appUrl+'twitter/?sess='+socialServices.sessId, 'twitterwin', 'width=800,height=400,status=0,directories=0,location=0');
			socialServices.twitterWindow.focus();
			socialServices.checkingWindowIsClosed();
			$('#applying_of_discount_message').hide();
			$('#adding_to_shopping_cart_message').hide();			
		},
		image : 'twitter.png'
	},
	myspace: {
		init: function() {
			$('#myspace-service').bind('click', function(){
				socialServices.myspace.clickFunction();
				return false;
			});
		},
		clickFunction: function() {
			if (!socialServices.canClick)
			{
				return false;
			}
			socialServices.canClick = false;
			$('#error_message').hide();
			$('#service_unavailalble_message').hide();
			socialServices.setService('myspace');
			socialServices.startLoading();
			socialServices.myspaceWindow = window.open(socialServices.appUrl+'myspace/?sess='+socialServices.sessId, 'myspacewin', 'width=800,height=400,status=0,directories=0,location=0');
			socialServices.myspaceWindow.focus();
			socialServices.checkingWindowIsClosed();
			$('#applying_of_discount_message').hide();
			$('#adding_to_shopping_cart_message').hide();			
		},
		image : 'myspace.png'
	},
	setService: function(serviceType) {
		if (socialServices.windowCloseTimeout != null)
		{
			clearTimeout(socialServices.windowCloseTimeout);
			socialServices.windowCloseTimeout = null;
		}
		if (serviceType == 'facebook')
		{
			socialServices.selectedService = 'facebook';
			if (socialServices.twitterWindow != null && socialServices.twitterWindow.closed == false)
			{
				socialServices.twitterWindow.close();
			}
			if (socialServices.myspaceWindow != null && socialServices.myspaceWindow.closed == false)
			{
				socialServices.myspaceWindow.close();
			}
		}
		if (serviceType == 'twitter')
		{
			socialServices.selectedService = 'twitter';
			if (socialServices.facebookWindow != null && socialServices.facebookWindow.closed == false)
			{
				socialServices.facebookWindow.close();
			}
			if (socialServices.myspaceWindow != null && socialServices.myspaceWindow.closed == false)
			{
				socialServices.myspaceWindow.close();
			}
		}
		if (serviceType == 'myspace')
		{
			socialServices.selectedService = 'myspace';
			if (socialServices.twitterWindow != null && socialServices.twitterWindow.closed == false)
			{
				socialServices.twitterWindow.close();
			}
			if (socialServices.facebookWindow != null && socialServices.facebookWindow.closed == false)
			{
				socialServices.facebookWindow.close();
			}
		}
	},
	checkingWindowIsClosed: function() {
		if ((socialServices.facebookWindow != null && socialServices.facebookWindow.closed == false) || 
			(socialServices.twitterWindow != null && socialServices.twitterWindow.closed == false) ||
			(socialServices.myspaceWindow != null && socialServices.myspaceWindow.closed == false))
		{
			socialServices.windowCloseTimeout = setTimeout(socialServices.checkingWindowIsClosed, 100)
		} 
		else
		{
			clearTimeout(socialServices.windowCloseTimeout);
			socialServices.windowCloseTimeout = null;
			socialServices.servicesCallback();
		}
	},
	startLoading: function() {
		$('#social-loader').show()
	},
	stopLoading: function() {
		$('#social-loader').hide()
	},
	servicesCallback: function() {
		socialServices.checkIsLoggedIn()
	},
	checkIsLoggedIn: function() {
		$.ajax({type: "GET",
			url: window.location.href.replace(/#/gi, ''),
			data: "check_is_logged_in&service="+socialServices.selectedService,
			context: $('#services-container'),
			dataType: 'json',
			success: function(response) {
				if (response.is_logged == 'true')
        		{
        			socialServices.applyDiscount()
        		}
				else
				{
					if (typeof(response.logoutBeforeLogin) != "undefined")
					{
						socialServices.logoutAction = true;
					}
					else
					{
						promotionServices.logoutAction = false;
					}
					$('#error_message').html(response.message);
					$('#error_message').show();
					socialServices.stopLoading();
					socialServices.canClick = true;
				}
			},
			error: function() {
				$('#share_message').hide();
				$('#services-container').hide();
				$('#service_unavailalble_message').show();
				socialServices.stopLoading();
				socialServices.canClick = true;
			}
		});
	},
	applyDiscount: function() {
		$('#share_message').hide();
		$('#services-container').hide();
		$('#applying_of_discount_message').show();
		$.ajax({type: "GET",
			url: window.location.href.replace(/#/gi, ''),
			data: "apply_discount&service="+socialServices.selectedService+"&products_id="+socialServices.selectedProduct,
			context: $('#services-container'),
			dataType: 'json',
			success: function(response) {
        		if (response.applied == 'true')
        		{
        			$('#applying_of_discount_message').hide();
        			$('#adding_to_shopping_cart_message').show();
        			socialServices.shoppingCartForm.submit();
        		}
        		else
        		{
        			$('#applying_of_discount_message').hide();
        			$('#error_message').html(response.message);
					$('#error_message').show();
					socialServices.stopLoading();
        		}
        		socialServices.canClick = true;
			},
			error: function() {
				$('#applying_of_discount_message').hide();
				$('#service_unavailalble_message').show();
				socialServices.stopLoading();
				socialServices.canClick = true;
			}
		});
	},
	reinit: function() {
		if (socialServices.initialized)
		{
			$('#share_message').show();
			$('#services-container').show();
			$('#applying_of_discount_message').hide();
			$('#adding_to_shopping_cart_message').hide();
			$('#error_message').hide();
			$('#service_unavailalble_message').hide();
		}
	}
}
