var orient = 1;
var stayTop = true;

const consol = opener.consol;
const ifr = document.getElementById('ifr');
const btnRotate = document.getElementById('btnRotate');
const btnStayTop = document.getElementById('btnStayTop');

/*ifr.addEventListener('load', function(ev) {
	ifr.showDevTools(true);
});*/

function ifrBack() {
	ifr.contentWindow.history.back();
}

function ifrHome() {
	ifr.src = opener.workspace.wObj.simulatorUrl;
}

function ifrReload() {
	ifr.contentWindow.location.reload(true);
}

function winMinimize(){
	opener.api.simulatorWin.minimize();
}

function winStayTop()
{
	if(stayTop){
		opener.api.simulatorWin.setAlwaysOnTop(false);
		opener.addClass(btnStayTop, 'rotated');
		stayTop = false;
	}
	else {
		opener.api.simulatorWin.setAlwaysOnTop(true);
		opener.removeClass(btnStayTop, 'rotated');
		stayTop = true;
	}
}

function winClose() {
	window.close();
}

function winRotate(){

	if(orient == 1) {
		var h = 380;
		var w = 690;
		opener.removeClass(btnRotate, 'rotated');
		orient = 2;
	}
	else {
		var w = 380;
		var h = 690;
		opener.addClass(btnRotate, 'rotated');
		orient = 1;
	}
	window.resizeTo(w, h);
	document.body.style.height = h + 'px';
	ifr.style.height = (h - 50) + 'px';
	ifr.style.width = (w - 20) + 'px';
}

function loaded(){

	opener.EventListen('FileSaved', function(){
		if(opener.config.json.autoRefreshSimulator && opener.workspace.wObj.simulatorUrl != "")
			ifrReload(true);
	});

	opener.EventListen('OpenSimulatorDevTools', function(){
		ifr.showDevTools(true);
	});

	opener.EventListen('ProjectClosed', function(){
		window.close();
	});

	ifr.src = opener.workspace.wObj.simulatorUrl;
}

consol.addLine('output', 'Mobile simulator window opened.');
