var win,menuType,type,dataURL=[],tabid,taburl,tabtitle,counter,ratio,scrollBar,centerW=0,centerH=0,tabids={};
chrome.extension.onRequest.addListener(function(a,c){if(!c.tab)menuType=a.action;switch(a.action){case "visible":if(menuType=="selected"){type="visible";centerW=a.centerW;centerH=a.centerH}captureVisible();break;case "selected":captureSelected();break;case "entire":captureEntire();break;case "insert_script":if(menuType=="selected"){chrome.tabs.executeScript(tabid,{file:"javascripts/dragresize.js"});chrome.tabs.insertCSS(tabid,{file:"stylesheets/selected.css"})}chrome.tabs.executeScript(tabid,{file:"javascripts/content_script.js"},
function(){sendRequest("tab",tabid,{action:"init_"+menuType+"_capture"})});break;case "script_running":sendRequest("tab",tabid,{action:"init_"+type+"_capture"});break;case "check_shortcuts":updateShortcutsRequest(c.tab.id);break;case "update_shortcuts":chrome.tabs.getAllInWindow(null,function(d){for(var e=0,i=d.length;e<i;e++){var f=d[e],g=f.url;!g.match(/https?:\/\/*\/*/gi)||g.match(/https:\/\/chrome.google.com\/extensions/i)||updateShortcutsRequest(f.id)}});break;case "scroll_next_done":saveAndScroll();
break;case "entire_capture_done":counter=a.counter;ratio=a.ratio;scrollBar=a.scrollBar;type="entire";if(menuType=="selected"){centerW=a.centerW;centerH=a.centerH}newTab();console.log(dataURL.length);break;case "ready":var b=document.getElementById("test_image"),h=function(){sendRequest("tab",tabid,{menuType:menuType,type:type,data:dataURL,taburl:taburl,tabtitle:tabtitle,counter:counter,ratio:ratio,scrollBar:scrollBar,centerW:centerW,centerH:centerH,w:b.width,h:b.height});dataURL=[];b.src="";b.removeEventListener("onload",
h,false);b=null};b.onload=h;b.src=dataURL[0];break;case "exit":chrome.tabs.getSelected(null,function(d){chrome.tabs.remove(d.id)});break}});chrome.tabs.onRemoved.addListener(function(a){tabids[a]&&chrome.tabs.update(tabids[a],{selected:true})});function captureVisible(){type="visible";getSelectedTab(function(){chrome.tabs.captureVisibleTab(null,function(a){dataURL.push(a);newTab()})})}function captureSelected(){type="selected";getSelectedTab(checkContentScript)}
function captureEntire(){type="entire";getSelectedTab(checkContentScript)}function getSelectedTab(a){chrome.tabs.getSelected(null,function(c){tabid=c.id;taburl=c.url;tabtitle=c.title;a!=null&&a()})}function checkContentScript(){chrome.tabs.executeScript(tabid,{file:"javascripts/isload.js"})}function saveAndScroll(){pushDataURL();sendRequest("tab",tabid,{action:"scroll_next"})}function pushDataURL(){chrome.tabs.captureVisibleTab(null,function(a){dataURL.push(a)})}
function updateShortcutsRequest(a){sendRequest("tab",a,{action:"update_shortcuts",msObj:localStorage.msObj})}function newTab(){if(dataURL){menuType=="selected"&&sendRequest("tab",tabid,{action:"destroy_selected"});chrome.tabs.create({url:"edit.html"},function(a){console.log(tabid+"+"+a.id);tabids[a.id]=tabid;tabid=a.id})}else alert("Screen Capture Fail!!")}function sendRequest(a,c,b){switch(a){case "tab":chrome.tabs.sendRequest(c,b);break;case "popup":chrome.extension.sendRequest(b);break}};
