//=======================================================================
// Social Media Buttons Plugin (RMMV v1.3) v1.6 [Formerly v2.8]
//=======================================================================
// * The  Social Media Buttons plugin is a simple plugin to add  social
//	 media buttons  to the Title Screen.  Clicking on them opens  a new
//	 window with their respective URLs displayed.
//
//   This was inspired by Yanfly's excellent "External Links" plugin. I
//	 You can  also use  the plugin  to display a  web page  in-game  by
//	 calling a Plugin Command and the URL in an event.
//
//   This was inspired by Yanfly's excellent "External Links" plugin. I
//   integrated it into "Adventures of Tryggr", then into "Otherworld",
//   but wrote my own to trim out some of the superfluous code and make
//   it more compatible with "Otherworld"'s menu.
//
//	 So based on that same principle, I decided to add a separate block
//	 on the Title Screen to contain  "Otherworld"'s Twitter account and
//	 official  blogsite,  as well as  RPG  Maker  Times Facebook  page,
//	 rather than putting them in the main menu, to declutter it. I then
//	 adapted it for more general use.
//
//	 The  plugin only  has 4 social media  buttons (Twitter,  Facebook,
//	 Google+ and Website)  displayed on the Title Screen,  but more may
//	 be added over time. You have a choice on whether to open the links
//	 in a separate game window or in your default browser.
//
//       * © 2016-2017, Companion Wulf
//
//========================================================================
// CW_SocialMediaButtons.js
//========================================================================
 
var CWT = CWT || {}, SMButtons = SMButtons || {}, SMB = SMButtons;

/*:
 @plugindesc Add social media buttons to the Title Screen.
 @author Companion Wulf

 @param Show Twitter
 @desc Toggles Twitter button ON/OFF.
 @default On

 @param Twitter Action
 @desc The action/behaviour type of the Twitter button (Screen Name, User ID or Follow).
 @default Screen Name
 
 @param ----------------------------------------------------------

 @param Twitter Account
 @desc Your Twitter Username or User ID.
 @default CompanionWulf

 @param Show Facebook
 @desc Toggles Facebook button ON/OFF.
 @default On

 @param Facebook Page Name
 @desc The name of your project's Facebook page.
 @default RPGMakerTimes
 
 @param ----------------------------------------------------------

 @param Show Google+
 @desc Toggles Google Plus button ON/OFF.
 @default On

 @param Google+ Account
 @desc Your Google+ name or ID.
 @default +RpgmakertimesInfo
 
 @param ----------------------------------------------------------

 @param Show YouTube
 @desc Toggles YouTube button ON/OFF.
 @default On

 @param YouTube Action
 @desc The action/behaviour type of the YouTube button (User, Channel, Video or Playlist).
 @default User

 @param YouTube Account
 @desc Your YouTube Username, Channel ID, Video ID or Playlist ID.
 @default Ferewulf
 
 @param ----------------------------------------------------------
 
 @param Show Pinterest
 @desc Toggles Pinterest button ON/OFF.
 @default On
 
 @param Pinterest Username
 @desc Your Pinterest name.
 @default CompanionWulf
 
 @param Pinterest Board Name
 @desc Optional board name to promote.
 @default
 
 @param ----------------------------------------------------------

 @param Show Website
 @desc Toggles Website button ON/OFF.
 @default On

 @param Website URL
 @desc The URL to show your website.
 @default http://blog.rpgmakertimes.info
 
 @param ----------------------------------------------------------

 @param Display Method
 @desc Choose to display in a Window or Browser
 @default Window

 @param Icons
 @desc The list of icons to use for the social media buttons.
 @default Twitter, Facebook, Google+, YouTube, Pinterest, Website

@help
 The Social Media Buttons plugin has five options: Twitter, Facebook,
 Google+, YouTube and Website. The icons show in the bottom right corner
 of the Title Screen. Clicking on them will open the corresponding website
 in either a window or a browser.

 A video tutorial is also available here: https://youtu.be/4wEEafk3gRE

 You can also call a website from within the game (see Plugin Commands
 below).

 NOTE: If playing in a browser, the URL might not open if you have a popup
 blocker, so make sure you allow it. In Windows versions, a new window
 will open to display the website.

 ==============================================================
  * TWITTER *
 ==============================================================
 These are the settings for sharing Twitter accounts via your games.

 --------------------------------------------------------------
  Show Twitter
 --------------------------------------------------------------
 This setting turns the Twitter button ON or OFF.

 --------------------------------------------------------------
  Twitter Action
 --------------------------------------------------------------
 Twitter Action is the behaviour type of the Twitter button. You can
 use one of three options: Screen Name, User ID or Follow.

 Screen Name - The user name of the Twitter account you'd like your
 	players to visit. 

 User ID - The unique identifier of the Twitter account.

 	To find your User ID, log into your Twitter account and go into
 	"Settings", then under "Your Twitter Data", your User ID is right
 	under your Username. (You will have to reenter your password to
 	access this area.)

 	Alternatively, go to https://tweeterid.com/ and enter the Twitter
 	"handle" to find the User ID number (or vice versa).

 Follow - Enables players to follow your Twitter account directly
 	from within RPG Maker MV, whether in a window or browser. As long
 	as the "Twitter Action Settings" is set correctly, this will
 	automatically use the Screen Name or User ID.

 --------------------------------------------------------------
  Twitter Account
 --------------------------------------------------------------
 This sets the Username or User ID for the Twitter account you wish to
 use. It's used in conjunction with the "Twitter Action" setting.

 For example, if you set that as "Screen Name", mine is "CompanionWulf",
 which will then lead to the my "RPG Maker Times" account there.

 Bear in mind, however, that Usernames change often so it's probably
 better to use the User ID, as it's unique to each account whatever the
 Username may be.

 ==============================================================
  * FACEBOOK *
 ==============================================================
 These are the settings for sharing a Facebook page from within RMMV.

 --------------------------------------------------------------
  Show Facebook
 --------------------------------------------------------------
 Show (ON) or hide (OFF) the Facebook button on-screen.

 --------------------------------------------------------------
  Facebook Page Name
 --------------------------------------------------------------
 This is the page name reference to display your Facebook page.

 For example, mine is "RPGMakerTimes" there, so clicking on the
 button will go to my "RPG Maker Times & Projects" page.

 ==============================================================
  * GOOGLE+ *
 ==============================================================
 These options are to share a Google+ profile from within RMMV.

 --------------------------------------------------------------
  Show Google+
 --------------------------------------------------------------
 Show (ON) or hide (OFF) the Google+ button on the Title Screen.

 --------------------------------------------------------------
  Google+ Account
 --------------------------------------------------------------
 Google+ has two options (depending on how you've set it up).

 In most cases you can use your page or profile name, preceded
 by the + sign. For instance, mine is "RpgmakertimesInfo" there,
 so I'd put

 	+RpgmakertimesInfo

 in this option. NOTE: You *must* include the + sign, otherwise you'll
 receive a Page Not Found Error.

 You can also set your 21-digit User ID number here. If you click on
 your Profile and you'll see your ID number. Or go to your Page
 Manager and look for the 21-digit number in its web address.

 In most cases, however, your page or profile name is already set up
 with the + sign identifier (because of Google+'s updates).

 ==============================================================
  * YOUTUBE *
 ==============================================================
 These options are to display sharing something from YouTube.

 --------------------------------------------------------------
  Show YouTube
 --------------------------------------------------------------
 Show (ON) or hide (OFF) the YouTube button on the Title Screen.

 --------------------------------------------------------------
  YouTube Action
 --------------------------------------------------------------
 This is the behaviour type for the YouTube button. The options
 you can have are: User, Channel, Video or Playlist.

 --------------------------------------------------------------
  YouTube Account
 --------------------------------------------------------------
 This provides the information for the "YouTube Action". You don't
 need the full URL, just the identifying information.

 Username - The username you use for your main profile.

 	Mine is "Ferewulf". When you type

 		https://www.youtube.com/ferewulf

 	into the address bar, my YouTube profile loads, displaying
 	all of the videos I've created.

 Channel ID - The ID reference of your video channel.

 	When you click on "My Channel" at YouTube, the channel page
 	with its videos will load. This is the part of the URL that
 	contains upper and lower case letters and numbers.

 Video ID - The ID for the video you want to play in RPG Maker MV.

 	This is part of the URL with upper/lower case letters and
 	numbers. You can either obtain it with "Share" or from
 	the main URL (after "watch?v=").

 Playlist ID - The ID of the playlist you'd like to feature.

 	When you put videos into playlists, each one has a rather
 	long and unique identifier comprising upper/lower case letters and
 	numbers. It's after the "playlist?list=" part but minus any other
 	parameters.	
	
 ==============================================================
  * PINTEREST *
 ==============================================================
 These are the settings for sharing Pinterest account via your games.

 --------------------------------------------------------------
  Show Pinterest
 --------------------------------------------------------------
 The setting for turning the Pinterest button ON or OFF.
 
 --------------------------------------------------------------
  Pinterest Username
 --------------------------------------------------------------
 This is the username you want to use. This will take visitors to
 your default profile page containing all your pins and boards.
 
 --------------------------------------------------------------
  Pinterest Board Name
 --------------------------------------------------------------
 If you want to share a specific board, then put the name of that
 board from the last part of its URL. Make sure you also put your
 username in the Pinterest Username parameter for it to work properly.
 
 Leave this blank if you just want to display your normal profile.
 
 Note: Visitors will most likely need to log in with Pinterest if
 "Window" is selected as the Display Method.
 
 ==============================================================
  * WEBSITE *
 ==============================================================
 These options set the website address you'd like visitors to go
 to when they click the button.

 --------------------------------------------------------------
  Show Website
 --------------------------------------------------------------
 Turns the Website icon ON or OFF.

 --------------------------------------------------------------
  Website URL
 --------------------------------------------------------------
 This is for setting your full website address. When the button
 is clicked, it'll open in a window or browser.

 ==============================================================
  * DISPLAY METHOD *
 ==============================================================
 This selects how to display the "Website URL". You can choose to
 display your link in a game Window or in your default Browser.

 ==============================================================
  * PLUGIN COMMANDS *
 ==============================================================
 You can also call a website from within the game using the following
 Plugin Command:

 showurl url

 where "url" is the FULL web address to open. This will open in a new
 window or browser tab (depending on the "Display Method" setting above).

 ==============================================================
  * ICONS *
 ==============================================================
 These are the icons you want to use for your social media buttons,
 separated by commas (in order): Twitter, Facebook, GooglePlus, YouTube,
 Pinterest and Website.

 Recommended size is 32x32.

 Save these in the Pictures folder. If you use your own icons, be sure
 to change the settings to the filenames you used.

  

 ==============================================================
  * TERMS & CONDITIONS OF USE *
 ==============================================================
 This plugin is free to use under CC BY-NC 4.0, but please refer to
 the RPG Maker Times blogsite for other details, including for
 commercial use.
 
 Credit "Companion Wulf" or "RPG Maker Times" if using this plugin
 in your projects.
 
	For all Terms of Use, visit: http://wp.me/P2Vm8L-1z4
*/

/**
 ═════════════════════════════════════
  SOCIAL MEDIA BUTTONS - MAIN CORE
 ═════════════════════════════════════
**/

(function($, undefined) {

	$.SocialMediaButtons = {
		Version: 	1.6,
		Build: 		13.10,
		Copyright: 	'© 2016-2017, Companion Wulf',
		MVBuild: 	1.3,
		Error: 		['Social Media Button Plugin Error', 'You have RPG Maker MV version '+Utils.RPGMAKER_VERSION+'.<br><br>The Social Media Button plugin is compatible with RMMV version 1.3+. You appear to have an older version, so errors may occur. Please update your copy of RMMV (see the Help files for details).']
	};

	/**
	 ═════════════════════════════════════
	  FUNCTION: TO BOOLEAN
	 ═════════════════════════════════════
	**/
	$.toBoolean = function(string) {
		switch(string.toLowerCase()) {
			case 'true': case 'yes': case 'enable': case 'on': case 'show': return true; break;
			case 'false': case 'no': case 'disable': case 'off': case 'hide': return false; break;
			default: return false; break;
		}
	};


	/**
	 ═════════════════════════════════════
	  FUNCTION: TO ARRAY
	 ═════════════════════════════════════
	**/
	$.toArray = function(string) { return string.split(/\s*,\s*/).filter(function(idx) { return idx; }); };


	/**
	 ═════════════════════════════════════
	  FUNCTION: OPEN URL
	 ═════════════════════════════════════
	**/
	$.openUrl = function(uri, method) {
		method = method || 'Window';
		switch(method.toUpperCase()) {
			case 'WINDOW': var urlWin = window.open(uri); break;
			case 'BROWSER': var gui = require('nw.gui'); gui.Shell.openExternal(uri); break;
			default: var urlWin = window.open(uri); break;
		}
	};


	/**
	 ═════════════════════════════════════
	  FUNCTION: PRINT ERROR
	 ═════════════════════════════════════
	**/
	$.printError = function(e1, e2) { Graphics.printError(e1, e2); };


	/**
	 ═════════════════════════════════════
	  STRING FUNCTION: IS USER ID?
	 ═════════════════════════════════════
	**/
	$.isUserId = function(txt) { if (txt.match(/^[0-9]+$/)) { return true } else { return false}; };
	
	$.bWidth = 0;


	/**
	 ═════════════════════════════════════
	  PLUGIN MANAGER
	 ═════════════════════════════════════
	**/
	(function($) {
		SMB.Params = $.parameters('CW_SocialMediaButtons');
		SMB.twitterToggle = SMButtons.toBoolean(SMB.Params['Show Twitter'] || 'ON');
		SMB.twitterIntent = String(SMB.Params['Twitter Intent'] || 'Screen Name');
		SMB.twitterIntentSettings = String(SMB.Params['Twitter Account'] || 'Twitter');
		SMB.facebookToggle = SMButtons.toBoolean(SMB.Params['Show Facebook'] || 'ON');
		SMB.facebookUrl = String(SMB.Params['Facebook Page Name'] || 'https://facebook.com');
		SMB.googlePlusToggle = SMButtons.toBoolean(SMB.Params['Show Google+'] || 'On');
		SMB.googlePlusUrl = String(SMB.Params['Google+ Account'] || 'https://plus.google.com');
		SMB.youtubeToggle = SMButtons.toBoolean(SMB.Params['Show YouTube'] || 'ON');
		SMB.youtubeAction = String(SMB.Params['YouTube Action'] || 'User');
		SMB.youtubeUrl = String(SMB.Params['YouTube Account'] || 'https://youtube.com');
		SMB.pinterestToggle = SMButtons.toBoolean(SMB.Params['Show Pinterest'] || 'ON');
		SMB.pinterestUsername = String(SMB.Params['Pinterest Username']);
		SMB.pinterestBoardname = String(SMB.Params['Pinterest Board Name']);
		SMB.websiteToggle = SMButtons.toBoolean(SMB.Params['Show Website'] || 'ON');
		SMB.websiteUrl = String(SMB.Params['Website URL'] || 'http://blog.rpgmakertimes.inf');
		SMB.displayMethod = String(SMB.Params['Display Method' || 'Window']);
		SMB.showIcons = SMButtons.toArray(SMB.Params['Icons'] || '');
	})(PluginManager);


	/**
	 ═════════════════════════════════════
	  SCENE BOOT
	 ═════════════════════════════════════
	**/
	(function($) {
		SMB.CW_alias__Scene_Boot_start = $.prototype.start;
		$.prototype.start = function() {
			(Utils.RPGMAKER_VERSION < SMB.SocialMediaButtons.MVBuild.toString()) ? SMButtons.printError(SMB.SocialMediaButtons.Error[0], SMB.SocialMediaButtons.Error[1]) : SMB.CW_alias__Scene_Boot_start.call(this);
		};
	})(Scene_Boot);


	/**
	 ═════════════════════════════════════
	  GAME INTERPRETER
	 ═════════════════════════════════════
	**/
	(function($) {
		SMB.CW_alias_Game_Interpreter_pluginCommand = $.prototype.pluginCommand;
		$.prototype.pluginCommand = function(command, args) {
			SMB.CW_alias_Game_Interpreter_pluginCommand.call(this, command, args);
			if (command.toLowerCase() === 'showurl') SMB.openUrl(args[0]);
		};
	})(Game_Interpreter);


	/**
	 ═════════════════════════════════════
	  SCENE TITLE
	 ═════════════════════════════════════
	**/
	(function($) {
		SMB.CW_alias__Scene_Title_create = $.prototype.create;
		$.prototype.create = function() {
			SMB.CW_alias__Scene_Title_create.call(this);
			this.createSocialMediaButtons();
		};

		$.prototype.createSocialMediaButtons = function() {
			this._twitterButton = new Sprite_Button(), this._twitterButton.bitmap = ImageManager.loadPicture(SMB.showIcons[0]);
			this._facebookButton = new Sprite_Button(), this._facebookButton.bitmap = ImageManager.loadPicture(SMB.showIcons[1]);
			this._googleButton = new Sprite_Button(), this._googleButton.bitmap = ImageManager.loadPicture(SMB.showIcons[2]);
			this._youtubeButton = new Sprite_Button(), this._youtubeButton.bitmap = ImageManager.loadPicture(SMB.showIcons[3]);
			this._pinterestButton = new Sprite_Button(), this._pinterestButton.bitmap = ImageManager.loadPicture(SMB.showIcons[4]);
			this._websiteButton = new Sprite_Button(), this._websiteButton.bitmap = ImageManager.loadPicture(SMB.showIcons[5]);

			this.addChild(this._twitterButton);
			this.addChild(this._facebookButton);
			this.addChild(this._googleButton);
			this.addChild(this._youtubeButton);
			this.addChild(this._pinterestButton);
			this.addChild(this._websiteButton);

			this._twitterButton.setClickHandler(this.visitTwitter.bind(this));
			this._facebookButton.setClickHandler(this.visitFacebook.bind(this));
			this._googleButton.setClickHandler(this.visitGooglePlus.bind(this));
			this._youtubeButton.setClickHandler(this.visitYouTube.bind(this));
			this._pinterestButton.setClickHandler(this.visitPinterest.bind(this));
			this._websiteButton.setClickHandler(this.visitWebsite.bind(this));

			this.positionBlock((Graphics.width - SMB.bWidth) / 1.5, 570);
		};

		// * Twitter *
		$.prototype.visitTwitter = function() {
			switch (SMB.twitterIntent.toLowerCase()) {
				case 'screen name': var text = 'user?screen_name='; break;
				case 'user id': var text = 'user?user_id='; break;
				case 'follow': SMB.isUserId(SMB.twitterIntentSettings) ? txt = 'user_id=' : txt = 'screen_name='; text = 'follow?'+txt; break;
				default: var text = 'user?screen_name='; break;
			}
			SMB.openUrl('https://twitter.com/intent/' + text + SMB.twitterIntentSettings, SMB.displayMethod);
		};

		// * Facebook *
		$.prototype.visitFacebook = function() {
			SMB.openUrl('https://facebook.com/' + SMB.facebookUrl, SMB.displayMethod);
		};

		// * Google+ *
		$.prototype.visitGooglePlus = function() {
			SMB.openUrl('https://plus.google.com/' + SMB.googlePlusUrl, SMB.displayMethod);
		};

		// * YouTube *
		$.prototype.visitYouTube = function() {
			var _uri = SMB.youtubeUrl;
			switch (SMB.youtubeAction.toLowerCase()) {
				case 'user': var text = 'user/'; break;
				case 'channel': var text = 'channel/'; break;
				case 'video': var text = 'watch?v='; break;
				case 'playlist': var text = 'playlist?list='; break;
			}
			SMB.openUrl('https://www.youtube.com/' + text + _uri, SMB.displayMethod);
		};
		
		// * Pinterest *
		$.prototype.visitPinterest = function() {
			var _uri = 'http://pinterest.com/' + SMB.pinterestUsername + '/';
			if (SMB.pinterestBoardname !== '') _uri += SMB.pinterestBoardname + '/';
			SMB.openUrl(_uri, SMB.displayMethod);
		};

		$.prototype.visitWebsite = function() {
			SMB.openUrl(SMB.websiteUrl, SMB.displayMethod);
		}

		$.prototype.positionBlock = function(x, y) {
			if (SMB.twitterToggle) this._twitterButton.x = x, SMB.bWidth += 34, this._twitterButton.y = y;
			if (SMB.facebookToggle) this._facebookButton.x = x += 34, SMB.bWidth += 34, this._facebookButton.y = y;
			if (SMB.googlePlusToggle) this._googleButton.x = x += 34, SMB.bWidth += 34, this._googleButton.y = y;
			if (SMB.youtubeToggle) this._youtubeButton.x = x += 34, SMB.bWidth += 34, this._youtubeButton.y = y;
			if (SMB.pinterestToggle) this._pinterestButton.x = x += 34, SMB.bWidth += 34, this._pinterestButton.y = y;
			if (SMB.websiteToggle) this._websiteButton.x = x += 34, SMB.bWidth += 34, this._websiteButton.y = y;
			SMB.bWidth = 0;
		};
	})(Scene_Title);

})(SMButtons || {});

