Components.utils["import"]("resource://livemargins/intent.jsm");
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,
	HISTORY_LENGTH : 10,
	LOG : function(msg) {
		this.__proto__.LOG.apply(this,["favpart weather widget: "+ msg]);
	},
	init : function(){
		this.LOG("init");
		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);
	},
	
	initBrowser: function() {
		var args = window.arguments[0];
		if (args.widgetArgs != null && args.widgetArgs.data != null) {
			this.checkWeather(args.widgetArgs.data);
		} else if (args.widgetArgs != null && args.widgetArgs.history != null && args.widgetArgs.history.length != 0){
			this.checkWeather(args.widgetArgs.history[args.widgetArgs.history.length - 1].data);
		} else {
			var self = this;
			this.getCurrentCity(function(city){
				self.checkWeather(city);
			});
		}
		
		this.registerActivityHandler();
	},
	
	// register web acitivity handler.
	registerActivityHandler: function() {
		var self = this;
		function _handle(data) {
			self.checkWeather(data.text);
		}
		AppIntent.registerHandler("app_weather", _handle);
		window.addEventListener("unload", function() {
			AppIntent.unregisterHandler("app_weather", _handle);
		}, false);
	},
	getCurrentCity : function(callback){
		var jsm = {};
		Components.utils.import("resource://livemargins/apps/search/searchService.js", jsm);
		jsm.SearchService.asyncGetGeoPosition(function(geoposition){
			if (geoposition !== null && geoposition.city !== null && geoposition.city !== '') {
				callback(geoposition.city);
			} else {
				callback(null);
			}
		});
	},
	getIcon : function(){
		return "chrome://livemargins/skin/weather-icon.png";
	},
	checkWeather : function(data){
		widget.LOG("checkWeather");
		var browser = document.getElementById("player_browser");
		var request_uri = "resource://livemargins/apps/weather/weather.html";
		var link = data
				? [request_uri, encodeURIComponent(data)].join('?')
				: [request_uri, 'error'].join('#');
		if (browser.hasAttribute('src')) {
			browser.setAttribute("src",link);
		} else {
			browser.setAttribute("asrc",link);
		}
		if (windowTools.minimized == true){
			windowTools.minimized = false;
		}
		if (!data) {
			return;
		}
		var textbox = document.getElementById("favpart_textbox_search");
		textbox.value = data;
		this.addHistory(data);
	},
	addHistory : function(data){
		this.LOG("addHistory");
		if (this.args.widgetArgs == null){
			this.args.widgetArgs = {history:[]};
		}
		if (this.args.widgetArgs.history == null){
			this.args.widgetArgs.history = new Array();
		}
		var time = (new Date()).valueOf();
		var visited = false;
		for (var i = 0; i < this.args.widgetArgs.history.length; i++){
			if (this.args.widgetArgs.history[i].data.toLowerCase() == data.toLowerCase()){
				this.args.widgetArgs.history[i].time = time;
				visited = true;
				break;
			}
		}
		if (visited){
			this.args.widgetArgs.history.sort(function cmp(e1,e2){return e1.time - e2.time;});
		} else {
			this.args.widgetArgs.history.push({data:data,time:time});
		}
		while(this.args.widgetArgs.history.length > this.HISTORY_LENGTH){
			this.args.widgetArgs.history.shift();
		}
		this.rebuildHistoryMenu();
	},
	rebuildHistoryMenu : function(){
		this.LOG("rebuildHistoryMenu");
		if (this.args.widgetArgs == null) return;
		var hisMenu = document.getElementById("history-menupopup");
		while (hisMenu.hasChildNodes()){
			hisMenu.removeChild(hisMenu.childNodes[0]);
		}
		if (this.args.widgetArgs.history.length >0) {
			var strbundle = document.getElementById("livemarginsStrings");
			var clearStr = strbundle.getString("weather.clearHistory");
			var menuItem = document.createElement("menuitem");
			menuItem.setAttribute("label",clearStr);
			menuItem.setAttribute("oncommand","widget.clearHistory();");
			hisMenu.appendChild(menuItem);
			var menuSep = document.createElement("menuseparator");
			hisMenu.appendChild(menuSep);
		}
		for (var i = 0; i < this.args.widgetArgs.history.length; i++){
			var menuItem = document.createElement("menuitem");
			menuItem.setAttribute("label",this.args.widgetArgs.history[i].data);
			menuItem.setAttribute("oncommand","widget.checkWeather('"+this.args.widgetArgs.history[i].data+"');");
			hisMenu.appendChild(menuItem);
		}
	},
	clearHistory : function(){
		if (this.args.widgetArgs == null){
			this.args.widgetArgs = {history:[]};
		}
		this.args.widgetArgs.history = new Array();
		this.rebuildHistoryMenu();
	},
	onTextEntered : function(){
		widget.LOG("onTextEntered");
		var textbox = document.getElementById("favpart_textbox_search");
		var textvalue = textbox.value;
		widget.checkWeather(textvalue);
	},
};

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