/*
This file sets the window toolbar & site & chat views in the wrapper.

WARNING: Do not edit this file! 
To disable the core UI, set "DisableCoreUi" to true in your skin's params.ini
Read the README for more info.
*/

node_remote = require("electron").remote;

$("#MinimizeButton").click(function() {
	node_remote.getCurrentWindow().minimize(); 
});

$("#MaximizeButton").click(function() {
	if (!node_remote.getCurrentWindow().isMaximized()) {
		node_remote.getCurrentWindow().maximize();
	} else {
		node_remote.getCurrentWindow().unmaximize();
	}	 
});

$("#CloseButton").click(function() {
	node_remote.getCurrentWindow().close();
});

$("#ChatViewToggleButton").click(function() {
	var _sModeToSet;
	
	if ($(this).hasClass("SharedMode")) {
		_sModeToSet = "SiteMode";
	}
	else if ($(this).hasClass("SiteMode")) {
		_sModeToSet = "ChatMode";
	}
	else {
		_sModeToSet = "SharedMode";
	}
	
	vSetViewMode(_sModeToSet);
});

function vSetViewMode(p_sModeToSet) {
	var _sModes="SharedMode SiteMode ChatMode";
	
	$("#ChatView,#SiteView,#ChatViewToggleButton").removeClass(_sModes).addClass(p_sModeToSet);

	g_oChatView.executeJavaScript('$("html").removeClass("'+_sModes+'").addClass("'+p_sModeToSet+'")');
	g_oSiteView.executeJavaScript('$("html").removeClass("'+_sModes+'").addClass("'+p_sModeToSet+'")');
}

$("#ViewsWrapper").append('<webview id="SiteView" class="SharedMode" src="'+node_remote.getGlobal("sharedObj")._sMainViewLandingPageUrl+'" id="SiteView"></webview>'
	+'<webview id="ChatView" class="SharedMode" src="'+node_remote.getGlobal("sharedObj")._sChatViewLandingPageUrl+'" id="SiteView"></webview>');

var g_oChatView = document.querySelector("#ChatView");
var g_oSiteView = document.querySelector("#SiteView");
var {shell} = require("electron");

g_oChatView.addEventListener("dom-ready",(e) => {
	if (!node_remote.getGlobal("sharedObj").g_oSkinParams["DisableCoreUi"]) {
		g_oChatView.executeJavaScript('$("head").append(\'<script src="asset://'+node_remote.getGlobal("sharedObj").c_sCoreUiChatLayoutFileRelPath+'">\')');
		g_oChatView.executeJavaScript('$("head").append(\'<link rel="stylesheet" type="text/css" href= "asset://'+node_remote.getGlobal("sharedObj").c_sCoreUiChatStyleFileRelPath+'">\')');
	}
		
	g_oChatView.executeJavaScript('$("head").append(\'<script src="asset://'+node_remote.getGlobal("sharedObj").g_sSkinChatLayoutFileRelPath+'">\')');
	g_oChatView.executeJavaScript('$("head").append(\'<link rel="stylesheet" type="text/css" href= "asset://'+node_remote.getGlobal("sharedObj").g_sSkinChatStyleFileRelPath+'">\')');
	
	if (node_remote.getGlobal("sharedObj").g_oConfig["ShowChatViewDevTools"]) {
		g_oChatView.openDevTools();
	}
});
g_oSiteView.addEventListener("dom-ready",(e) => {
	if (!node_remote.getGlobal("sharedObj").g_oSkinParams["DisableCoreUi"]) {
		g_oSiteView.executeJavaScript('$("head").append(\'<script src="asset://'+node_remote.getGlobal("sharedObj").c_sCoreUiSiteLayoutFileRelPath+'">\')');
		g_oSiteView.executeJavaScript('$("head").append(\'<link rel="stylesheet" type="text/css" href= "asset://'+node_remote.getGlobal("sharedObj").c_sCoreUiSiteStyleFileRelPath+'">\')');
	}
	
	g_oSiteView.executeJavaScript('$("head").append(\'<script src="asset://'+node_remote.getGlobal("sharedObj").g_sSkinSiteLayoutFileRelPath+'">\')');
	g_oSiteView.executeJavaScript('$("head").append(\'<link rel="stylesheet" type="text/css" href= "asset://'+node_remote.getGlobal("sharedObj").g_sSkinSiteStyleFileRelPath+'">\')');
	
	if (node_remote.getGlobal("sharedObj").g_oConfig["ShowSiteViewDevTools"]) {
		g_oSiteView.openDevTools();
	}
});

g_oChatView.addEventListener("new-window",(e) => {
	e.preventDefault();
	if (e.url.charAt(0) === "#"
		|| e.url.charAt(0) === "?"
		|| e.url.charAt(0) === "/"
		|| e.url.substr(0,10) === "javascript"
		|| e.url.substr(0,21) === "http://gamebanana.com"
		|| e.url.substr(0,25) === "http://dev.gamebanana.com") {
	
		if ($("#ChatViewToggleButton").hasClass("ChatMode")) {
			vSetViewMode("SharedMode");
		}
		
		g_oSiteView.loadURL(e.url);
	}
	else {
		shell.openExternal(e.url);
	}
});
g_oSiteView.addEventListener("new-window",(e) => {
	e.preventDefault();
	if (e.url.charAt(0) === "#"
		|| e.url.charAt(0) === "?"
		|| e.url.charAt(0) === "/"
		|| e.url.substr(0,10) === "javascript"
		|| e.url.substr(0,21) === "http://gamebanana.com"
		|| e.url.substr(0,25) === "http://dev.gamebanana.com") {
		g_oSiteView.loadURL(e.url);
	}
	else {
		shell.openExternal(e.url);
	}
});