/**
 * @version		$Id: socialconnect.js 447 2011-07-01 13:08:03Z lefteris.kavadas $
 * @package		SocialConnect
 * @author		JoomlaWorks http://www.joomlaworks.gr
 * @copyright	Copyright (c) 2006 - 2011 JoomlaWorks Ltd. All rights reserved.
 * @license		Commercial - This code cannot be redistributed without permission from JoomlaWorks Ltd.
 */

window.addEvent('domready', function(){
	// Sign out event
	$$('.socialConnectSignOutButton').addEvent('click', function(){
		if(socialConnectLoginType=='twitter'){
			if(typeof(twttr)!='undefined' && typeof(twttr.anywhere)!='undefined'){
				twttr.anywhere.signOut();
			}
		}
		else if(socialConnectLoginType=='facebook' && typeof(FB)!='undefined'){
			FB.logout();
		}
		else {
			$$('.socialConnectSignOutForm')[0].submit();
		}
	});
	// Twitter events
	if(typeof(twttr)!='undefined' && typeof(twttr.anywhere)!='undefined'){
		twttr.anywhere(function (T) {
			T.bind('authComplete', function (e, user) {
				window.location = socialConnectVerifyURL;
			});
			T.bind('signOut', function (e) {
				window.location = socialConnectSignOutURL;
			});
	    	$$('.socialConnectTwitterButton').addEvent('click', function(e){
	    		e = new Event(e).stop();
	    		T.signIn();
	    	});
	  	});
	}
	// Facebook Events
	if(typeof(FB)!='undefined'){
		FB.init({appId: socialConnectFacebookApplicationID, status: false, cookie: true, oauth: true});
		$$('.socialConnectFacebookButton').addEvent('click', function(e){
			e = new Event(e).stop();
			FB.getLoginStatus(function(response) {
				  if (response.status === 'connected') {
					  window.location = socialConnectVerifyURL;
				  } else {
					  FB.login(function(response) {
						  if(response.authResponse) {
							  window.location = socialConnectVerifyURL;
						  }
					  }, {scope:'email'});
				  }
			});
		});
	}
});

