Components.utils.import("resource://livemargins/intent.jsm");
Components.utils.import("resource://livemargins/apps/gallery/grabpicservice.js");

var widget = {
	__proto__ : widgetbase,	//make this object 'extends' widgetbase
	isMovingContent : false,
	isMovingWindow : false,
	isResizing : false,
	moveStartPos : {x : 0,y : 0},
	defaultView : null,
	reloader : null,
	reloadInterval : 20000,
	titlebarMinWidth : 160,
	width : 300,
	height :300,
	heightWithPlaylist :700,
	LOG : function(msg) {
		this.__proto__.LOG.apply(this,["favpart search player: "+ msg]);
	},
	init : function(){
		this.LOG("init");
		//in case this is opened manually
		
		var args = window.arguments[0];
		if (args.widgetArgs == null){
			args.widgetArgs = {docked:false};
		}
		if (args.widgetArgs.docked == null){
			args.widgetArgs.docked = false;
		}
		this.__proto__.init.apply(this);
		this.args.startWithFirefox = 0;
		if( args.widgetArgs && args.widgetArgs.picId){
			this.doGrabPic(args.widgetArgs.picId);
		}
		var contentBrowser = document.getElementById("player_browser");
		
		this.registerActivityHandler();
		this.LOG("init done");
	},
	
	// register web activity handler.
	registerActivityHandler: function() {
	    var self = this;
	    function _handle(data) {
	        let picId = GrabPicturesService.addPicture(data.imgsrc, data.referrer, GrabPicturesService.DEFAULT_GROUP_ID);
	        self.doGrabPic(picId);
	    }
	    AppIntent.registerHandler("app_gallery", _handle);
	    window.addEventListener("unload", function() {
	        AppIntent.unregisterHandler("app_gallery", _handle);
	    }, false);
	},
	
	modifyState : function (){
		//this.LOG("tada");
		try {
			var browser = document.getElementById("player_browser");
			if (this.args.widgetArgs == null) this.args.widgetArgs = {};
			this.args.widgetArgs.width = browser.boxObject.width;
			this.args.widgetArgs.height = browser.boxObject.height;
			this.args.widgetArgs.picId = "";
			this.__proto__.modifyState.apply(this);
		} catch (e){
			this.LOG(e.toString());
		}
	},
	getIcon : function(){
		return "chrome://livemargins/skin/search-icon.png";

	},
	doGrabPic : function(picId){
		if (picId == null || picId == ""){
			return;
		}
		var browser = document.getElementById("player_browser");
		var request_uri = "resource://livemargins/apps/gallery/grabpic.html?s=";
		var link = request_uri + encodeURIComponent(picId);
		browser.setAttribute("src",link);
		if (windowTools.minimized == true){
			windowTools.minimized = false;
		}
		window.focus();
	// handle dock
		dockable.hiding = false;
		dockable.showAndHideDockable();
	},
	closePic : function(force){
		if (force){
			window.close();
		} else {
			//no close
		}
	},
};


//basic handling
window.addEventListener("load",function() {
	widget.init();
},false);
