//================================================
/*

Rotate that Video Player
The ability to rotate a video clip by 90, 180 or 270 degrees.
Copyright (C) 2022 Stefan vd
www.stefanvd.net

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


To view a copy of this license, visit http://creativecommons.org/licenses/GPL/2.0/

*/
//================================================


function $(id) { return document.getElementById(id); }
var currentRatio = 0; var ratio = 0; var job = null;
var allzoom; var allzoomvalue;

var currenttopRatio = 0; var topratio = 0;
function topmove(topratio){
currenttopRatio = topratio / 100;
chrome.tabs.query({windowId: chrome.windows.WINDOW_ID_CURRENT, active: true}, function(tabs) {
toptab(tabs[0],topratio/100);
});
}

function toptab(tab,topratio){
document.getElementById("topnumber").value=Math.round(topratio*100);
document.getElementById("toprange").value=Math.round(topratio*100);
chrome.tabs.query({active:true}, function(tabs) {job = tabs[0].url;
var webjob = job.match(/^[\w-]+:\/*\[?([\w\.:-]+)\]?(?::\d+)?/)[1];
chrome.extension.sendMessage({name: 'setTop', value: topratio}); // settings topposition
});
try{chrome.tabs.executeScript(tab.id,{code:"document.getElementById('movie_player').style.top = '" + topratio*100 + "px'"});
}
catch(e){}
}
//--

function zoom(ratio){
currentRatio = ratio / 100;
chrome.tabs.query({windowId: chrome.windows.WINDOW_ID_CURRENT, active: true}, function(tabs) {
zoomtab(tabs[0],ratio/100);
});
}

function zoomtab(tab,ratio){
document.getElementById("number").value=Math.round(ratio*100);
document.getElementById("range").value=Math.round(ratio*100);
chrome.tabs.query({active:true}, function(tabs) {job = tabs[0].url;
var webjob = job.match(/^[\w-]+:\/*\[?([\w\.:-]+)\]?(?::\d+)?/)[1];
chrome.extension.sendMessage({name: 'setZoom', value: ratio, urlwebsite: webjob});
});
try{chrome.tabs.executeScript(tab.id,{code:"document.getElementById('movie_player').style.webkitTransform = 'rotate(" + ratio*100 + "deg)'"});
}
catch(e){}
// save for all zoom feature
window.localStorage['allzoomvalue'] = ratio;
}

function zoomview(direction){zoom(nextratio(currentRatio*100,direction));}

function nextratio(ratio,direction){
switch(ratio){
	case 0: return (direction==-1)?270:90;
	case 90: return (direction==-1)?0:180;
	case 180: return (direction==-1)?90:270;
	case 270: return (direction==-1)?180:360;
	case 360: return (direction==-1)?270:0;
	default: return 180; //more than 400% or less 50% go back to 100%
	}
}

var tempcurrentpopupzoom = "";
function handle(delta) {
	tempcurrentpopupzoom = document.getElementById("number").value;
    if(delta < 0){ tempcurrentpopupzoom -= Number(1);
		if(tempcurrentpopupzoom != 0 && tempcurrentpopupzoom >= 1){ document.getElementById("number").value = tempcurrentpopupzoom; zoom(tempcurrentpopupzoom); }
	}
	else{
		if(tempcurrentpopupzoom != 0 && tempcurrentpopupzoom < 400){ tempcurrentpopupzoom = Number(tempcurrentpopupzoom) + Number(1);document.getElementById("number").value = tempcurrentpopupzoom; zoom(tempcurrentpopupzoom); }
	}
	tempcurrentpopupzoom = ""; // reset
}

function wheel(event){
    var delta = 0;
    if (!event){ event = window.event; }
    if (event.wheelDelta) { delta = event.wheelDelta/120; }
    if (delta){ handle(delta); } // do the UP and DOWN job
    // prevent the mouse default actions using scroll
    if (event.preventDefault){ event.preventDefault(); }
	event.returnValue = false;
}

document.addEventListener('DOMContentLoaded', function () {
// default settings
chrome.tabs.query({active:true}, function(tabs) {job = tabs[0].url;
var webjob = job.match(/^[\w-]+:\/*\[?([\w\.:-]+)\]?(?::\d+)?/)[1];
if(localStorage[webjob]){ratio = localStorage[webjob];} else { ratio = 0; }
document.getElementById("number").value = Math.round(ratio*100);
document.getElementById("range").value = Math.round(ratio*100);

if(localStorage["topposition"]){topratio = localStorage["topposition"];} else { topratio = 0; }
document.getElementById("topnumber").value = Math.round(topratio*100);
document.getElementById("toprange").value = Math.round(topratio*100);
});

function displayinput(newValue) {document.getElementById("number").value=newValue;}
function showValue(newValue){document.getElementById("range").innerHTML=newValue;document.getElementById("number").value=newValue;zoom(newValue);}
$("range").addEventListener('input', function() {showValue(this.value);});
$("range").addEventListener('change', function() {showValue(this.value);});
$("number").addEventListener('change', function() {showValue(this.value);});
$("hund").addEventListener('click', function() {zoom(0);displayinput(0);});
$("minus").addEventListener('click', function() {zoomview(-1);});
$("plus").addEventListener('click', function() {zoomview(+1);});

function topshowValue(newValue){document.getElementById("toprange").innerHTML=newValue;document.getElementById("topnumber").value=newValue;topmove(newValue);}
$("toprange").addEventListener('input', function() {topshowValue(this.value);});
$("toprange").addEventListener('change', function() {topshowValue(this.value);});
$("topnumber").addEventListener('change', function() {topshowValue(this.value);});

// mouse scroll
document.addEventListener("mousewheel", wheel, { passive: false });

chrome.extension.sendMessage({comando:'zrequest'}, function(response){
allzoom = response.allzoom;if(!allzoom)allzoom = 'false'; // default allzoom false
allzoomvalue = response.allzoomvalue;
document.getElementById("number").value = Math.round(allzoomvalue*100);
document.getElementById("range").value = Math.round(allzoomvalue*100);
});

});