Components.utils['import']('resource://livemargins/utils.jsm');
var widget = {
	__proto__ : widgetbase,	//make this object 'extends' widgetbase
	isMovingContent : false,
	isMovingWindow : false,
	isResizing : false,
	moveStartPos : {x : 0,y : 0},
	defaultView : null,
	titlebarMinWidth : 160,
	target : null,
	targetInterval : null,
	LOG : function(msg) {
		this.__proto__.LOG.apply(this,["favpart video player: "+ msg]);
	},
	
	logger: utils.logger('LM4.FlashExtractor'),
	
	init : function(){
		this.LOG("init");
		var args = window.arguments[0];	
		this.__proto__.init.apply(this);
		widget.showFav(args.id,args.widgetArgs.docX,args.widgetArgs.docY,args.screenX,args.screenY,args.widgetArgs.width,args.widgetArgs.height,args.widgetArgs.browserWidth,args.widgetArgs.browserHeight,args.widgetArgs.location,args.widgetArgs.tagName,args.widgetArgs.elementIndex,args.widgetArgs.elementWidth,args.widgetArgs.elementHeight);
		this.args.startWithFirefox = 1;
//		this.showControl(true);
		if (args.widgetArgs.autoReload == true){
			this.setAutoReload(true);
		}
		window.addEventListener("resize",this,false);
		this.LOG("init done");
	},
    openLinkInTab : function(){
        this.__proto__.openLinkInTab.apply(this,[true]);
    },	
	showFav : function(id,docX,docY,screenX,screenY,width,height,browserWidth,browserHeight,location,tagName,elementIndex,elementWidth,elementHeight){
		this.LOG("showFav");
		var contentBrowser = document.getElementById("player_browser");
		contentBrowser.setAttribute("src",location);
		var contentDoc = contentBrowser.contentDocument;
		contentBrowser.addEventListener('DOMContentLoaded',function(ev){
				if (! ev.originalTarget instanceof HTMLDocument ) {
					return;
				}
				widget.LOG(ev.originalTarget.URL);
				window.setTimeout(function(){
// guess that the document is center aligned
					var newBrowserWidth = contentBrowser.contentWindow.scrollMaxX + contentBrowser.boxObject.width;
					var newDocX = docX - (browserWidth - newBrowserWidth)/2;
					contentBrowser.contentWindow.scrollTo(newDocX,docY);
					widget.LOG("new browserWidth: "+newBrowserWidth);
					widget.LOG("docXs: "+ newDocX +" " + docX);
					widget.LOG("browserWidth changed: "+(browserWidth - newBrowserWidth)/2);
							var doc = document.getElementById("player_browser").contentDocument;
							var target = doc.getElementsByTagName(tagName)[elementIndex];
							if (target != null && target.clientWidth == elementWidth && target.clientHeight == elementHeight && target.style.display != "none"){
								widget.target = target;
								widget.adjustTarget();
							} else {
// the element is inserted afterwards, I can only guess from the position
								if (widget.targetInterval) window.clearInterval(widget.targetInterval);
								var count = 0;
								widget.targetInterval = window.setInterval(function(){
										
										target = doc.getElementsByTagName(tagName)[elementIndex];
										if (typeof(target) == "undefined" 
														|| target.clientWidth != elementWidth 
														|| target.clientHeight != elementHeight){
// the elementIndex nolonger works...
											target = doc.elementFromPoint(width/2,height/2);
										}
										widget.LOG("inside interval:" + target);
										if ((target &&target.clientWidth == elementWidth) || count > 20){
											widget.target = target;
											window.clearInterval(widget.targetInterval);
											widget.adjustTarget();				
										}
										count++;
									},1000);
							}
					},0);
			}, false);
		this.LOG("showFav done");
	//	alert(window.content.document.documentElement.browserWidth);
	//	alert(window.content.document.documentElement.browserHeight);
		
	//	alert("done");
		//alert(body.innerHTML);
	},
	adjustTarget : function(){
		var contentBrowser = document.getElementById("player_browser");
		var coords = utils.getRectTRBLWH(widget.target,contentBrowser.contentWindow);
		widget.LOG("scrollTo: "+ coords.left+","+ coords.top);contentBrowser.contentWindow.scrollBy(coords.left,coords.top);
		
		widget.target.style.position = "fixed";
		widget.target.style.top = "0";
		widget.target.style.left = "0";
		widget.target.style.zIndex = "99999";
	},
	returnURL : function(ev){
		var browser = document.getElementById("player_browser");
		var frame = browser.contentDocument.getElementById("contentFrame");
		const wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                         .getService(Components.interfaces.nsIWindowMediator);
		var mainWin = wm.getMostRecentWindow( "navigator:browser" );
		if (mainWin) {
			mainWin.focus();
			var gBrowser = mainWin.getBrowser();
			gBrowser.selectedTab = gBrowser.addTab(browser.getAttribute("src"));
		} else {
			window.open(browser.getAttribute("src"));
		}
	},
	
	toggleStartWithFirefox : function(){
		var startWithFirefox = document.getElementById("startWithFirefox").getAttribute('checkState');
		if(startWithFirefox != '1'){
			this.args.startWithFirefox = 1;
		} else {
			this.args.startWithFirefox = 0;
		}
	},
// nothing out of normal in modifyState.

	handleEvent: function(event) {
		switch(event.type) {
			case "resize":
				if (widget.target){
					//this.LOG("resize");
					var browser = document.getElementById("player_browser");
					var doc = browser.contentDocument;
					widget.target.style.position = "fixed";
					widget.target.style.zIndex = "99999";
					widget.target.style.left = "0";
					widget.target.style.top = "0";
					widget.target.style.width = browser.boxObject.width+"px";
					widget.target.style.height = browser.boxObject.height+"px";
					widget.target.style.backgroundColor = "black";
					if (widget.target.getAttribute("wmode") == "transparent"){
						widget.target.setAttribute("wmode","opaque");
					}
					var bgDiv1 = doc.getElementById("favpart-background-div");
					var bgDiv2 = doc.getElementById("favpart-background-div-2");

					if (bgDiv1 == null){
						widget.LOG("bgDiv is null")
						bgDiv1 = doc.createElement("div");
						bgDiv1.setAttribute("id", "favpart-background-div");
						bgDiv1.setAttribute("class", "backgroundClass");
						bgDiv1.style.position = "fixed";
						bgDiv1.style.zIndex = "99998";
						bgDiv1.style.top = "0";
						bgDiv1.style.left = browser.boxObject.width + 1;
						bgDiv1.style.backgroundColor = "black";
						doc.body.appendChild(bgDiv1);
						bgDiv2 = doc.createElement("div");
						bgDiv2.setAttribute("id", "favpart-background-div-2");
						bgDiv2.setAttribute("class", "backgroundClass");
						bgDiv2.style.position = "fixed";
						bgDiv2.style.zIndex = "99998";
						bgDiv2.style.top = browser.boxObject.height + 1;
						bgDiv2.style.left = 0;
						bgDiv2.style.backgroundColor = "black";
						doc.body.appendChild(bgDiv2);
					}
					bgDiv1.style.top = "0";
					bgDiv1.style.left = browser.boxObject.width +"px";
					bgDiv1.style.width = browser.boxObject.width + "px";
					bgDiv1.style.height = browser.boxObject.height * 2 + "px";
					bgDiv2.style.top = browser.boxObject.height +"px";
					bgDiv2.style.left = 0;
					bgDiv2.style.width = browser.boxObject.width * 2 + "px";
					bgDiv2.style.height = browser.boxObject.height + "px";
				}
				break;
			default:
				this.__proto__.handleEvent.apply(this,[event]);
				break;
		}
	}
};
    
window.addEventListener("load", function() {
	widget.init();
}, false);
