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 : 117,
	heightWithPlaylist :700,
	LOG : function(msg) {
		this.__proto__.LOG.apply(this,["favpart music player: "+ msg]);
	},
	init : function(){
		this.LOG("init");
		var args = window.arguments[0];
		this.__proto__.init.apply(this);
	},
	openLinkInTab : function(){
		this.__proto__.openLinkInTab.apply(this,[true]);
	},
	reportClick : function(link){
		var req = new XMLHttpRequest();  
		req.open('GET', 'http://www.g-fox.cn/livemargins/reportMusicLink.html?value='+encodeURIComponent(link), true);   
		req.send(null);  
	},
	initBrowser: function() {
		this.LOG(['real loaded', this.browser]);
		this.hackAlert();
		this.openLinkInTab();
		if (this.args.widgetArgs == null){
			this.args.widgetArgs = {playlistOpen : true};//set default value for playlistOpen
		}
		if (this.args.widgetArgs.playlistOpen){
			this.switchPlaylist(true);
		}
		if( this.args.widgetArgs && this.args.widgetArgs.songId && this.args.widgetArgs.songId != ""){
			window.clearInterval(this.playMusicInterval);
			this.playMusicInterval = window.setInterval(function(){
					var browser = document.getElementById("player_browser");
					if (browser.contentWindow.wrappedJSObject.firefoxAddSong == null){
						return;
					}
					window.clearInterval(widget.playMusicInterval);
					widget.playMusic(widget.args.widgetArgs.songId);
					widget.args.widgetArgs.songId = "";
			},1000);
		}
	},
	hackAlert: function() {
		var win = this.browser.contentDocument.defaultView.wrappedJSObject;
		var sapAlert = win.sapAlert;
		sapAlert['__txt__'] = sapAlert.txt;
		var me = this;
		sapAlert.txt = function() {
			me.switchPlaylist(true);
			sapAlert.__txt__.apply(sapAlert, arguments);
		};
	},
	switchPlaylist : function(checked) {
		widget.LOG("switchPlaylist");
		var pl = document.getElementById("music-playlist");
		if (checked == undefined) {
			checked = !(pl.getAttribute('checkState') == 1);
		}
		if(checked){
			window.resizeTo(this.width,this.heightWithPlaylist+document.getElementById("chrome-title-box").boxObject.height);
		} else {
			window.resizeTo(this.width,this.height+document.getElementById("chrome-title-box").boxObject.height);
		}
		pl.setAttribute('checked', checked ? 'true' : 'false');
		pl.setAttribute('checkState', checked ? 1 : 0);
		this.args.widgetArgs.playlistOpen = checked ? true : false;
//		this.LOG(checked ? 'show playlist' : 'hide playlist');
	},
	modifyState : function (){
		//this.LOG("tada");
		try {
			if (this.args.widgetArgs == null) this.args.widgetArgs = {};
			this.args.widgetArgs.songId = "";
			this.__proto__.modifyState.apply(this);
		} catch (e){
			this.LOG(e.toString());
		}
	},
	getIcon : function(){
		return "chrome://livemargins/skin/music-icon.png";
	},
	playMusic : function(songId){
		var browser = document.getElementById("player_browser");
		browser.contentWindow.wrappedJSObject.firefoxAddSong(songId,true);
	},
};

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