///////////////////////////////
/// stranka popup.js        ///
///////////////////////////////


// globalni promenne 
var i=0, komentare;


// otevre lokalni html stranku
function openurl() {
  chrome.tabs.create({url:'options.html'},function(tab){
					activeTab = tab.id;
					activeWin = tab.windowId;
					activeTabLoading = true;
				});
}

// spusti script ze souboru v background page
function script() {
    chrome.tabs.executeScript({
      file: 'myscript.js'
    });
}

// komunikace s chrome storage (get i set)
function storage() {
  chrome.storage.sync.get("data", function(items) {
    i = items.data;
  });
    i++;  
  chrome.storage.sync.set({'data': i}, function() {
  });
    i = i.toString();
    badge(i);
}


// badge ikony
function badge(bnum) {
  chrome.browserAction.setBadgeText({
    text: bnum
  });
}

// vlozi komentar
function insert() {
  // badge ikony
  komentare = document.getElementById('komentare').value;
  komentare = parseInt(komentare)+1;
  document.getElementById("komentare").value = komentare;
  chrome.storage.sync.set({'data': komentare}, function() {
  });
  i = komentare.toString();
  badge(i);

  // lokalni promenne
  var left = document.getElementById('vlevo').value;
  var top = document.getElementById('shora').value;
  var size = document.getElementById('velikost').value;
  var color = document.getElementById('barva').value;

  // prevod na integer, celkove nastaveni promenne top
  top = parseInt(top)+60;
  // kvuli defaultim hodnotam - kdyz kliknu na ikonu rozsireni
  if (top==200 && komentare>0) {top = 150 + komentare*60;}
  document.getElementById("shora").value = top;

  // prikazy pro vlozeni komentare do webpage
  var insert = "<div class='draggable' id='divkoment' style='position:absolute;left:+"+ left +"px;top:+"+ top +"px;z-index:1000;'><textarea id='textkoment' rows='2' cols='18' style='color:#"+ color +";font-size:"+ size +"px;' placeholder='Okomentujte web :)'></textarea></div>"; 
  code = '$("head").after($("'+ insert +'"));';
  chrome.tabs.executeScript({ code: code });
}

// odstrani komentar
function remove() {
  // kvuli badge a top
  komentare = document.getElementById('komentare').value;
  komentare = parseInt(komentare)-1;
  var top = document.getElementById('shora').value;
  //prevod na integer
  top = parseInt(top);  
  if (komentare < 0) {komentare = 0; }
  else {
    document.getElementById("komentare").value = komentare;
    chrome.storage.sync.set({'data': komentare}, function() {
    });
    document.getElementById("shora").value = top-60;
  }
  i = komentare.toString();  
  badge(i);

  // prikazy pro odstraneni komentaru
  code = '$( "#divkoment" ).remove();';
  chrome.tabs.executeScript({ code: code });
}

// funkce po kliknuti na ikonu badge
function load(){
  // nacteni badge ze storage + nastaveni badge a top
  chrome.storage.sync.get("data", function(items) {
    i = items.data;
    document.getElementById("komentare").value = i;
    i = i.toString();
    badge(i);
    document.getElementById("shora").value = 150+i*60;
  });
}

// presun komentaru
function move() {
  code2 = '$(document).ready( function() { var $draggable = $(".draggable").draggabilly(); });';
  chrome.tabs.executeScript({ code: code2 });
}


// akce po kliknuti na ikonu rozsireni
window.addEventListener('load', load);

// akce po kliknuti na tlacitka - obrazky
document.getElementById('butt-new').addEventListener('click', insert);
document.getElementById('butt-move').addEventListener('click', move);
document.getElementById('butt-delete').addEventListener('click', remove);

// export do formatu *.PNG
document.getElementById("butt-png").addEventListener("click", function(){
var screenshot = {
	content : document.createElement("canvas"),
	data : '',

	init : function() {
		this.initEvents();
	},	
	saveScreenshot : function() {
		var image = new Image();
		image.onload = function() {
			var canvas = screenshot.content;
			canvas.width = image.width;
			canvas.height = image.height;
			var context = canvas.getContext("2d");
			context.drawImage(image, 0, 0);

			// ulozeni obrazku
			var link = document.createElement('a');
			link.download = "webpage.png";
			link.href = screenshot.content.toDataURL();
			link.click();
			screenshot.data = '';
		};
		image.src = screenshot.data; 
	},	
	initEvents : function() {
			chrome.tabs.captureVisibleTab(null, {
				format : "png",
				quality : 100
			}, function(data) {
				screenshot.data = data;
				
				// alert zprava do webpage
				chrome.tabs.query({
					active : true,
					currentWindow : true
				}, function(tabs) {
					chrome.tabs.sendMessage(tabs[0].id, {ready : "ready"}, function(response) {
						if (response.download === "download") {
							screenshot.saveScreenshot();
						}
						else {
							screenshot.data = '';
						}
					});
				}); 

			});
	}
};
screenshot.init();
});
