

var bc_ext_instance = null;

var bc_ext = { 

	onLoad: function() 
	{
		// initialization code
		this.initialized = true;
		this.strings = document.getElementById("bc_ext-strings");
		document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", onContentPopupMenu, false);
		        
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		try
		{
			bc_ext_instance = Components.classes["@bitcomet.com/BitCometExtension;1"].createInstance( Components.interfaces.IBitCometExtension );
			if( !bc_ext_instance )
			{
				window.alert( "Failed BitCometExtension, No Class." );
			}
		}
		catch(e)
		{
		}
	},
	
	__isAboutBlank:function(url_str)
	{
		return url_str == "about:blank" || url_str == "";
	},
	
	__interatorLinks:function(link_array, html_document)
	{
		var links_len = html_document.links.length;
		for (var link_idx = 0; link_idx < links_len;link_idx++)
		{
			var link = html_document.links[link_idx];
			link_array.push(link.href);
			link_array.push(this.extractDescription(link));
		}
	},
	
	__iteratorNode:function(link_array, html_document)
	{
		var ret = false;
		try 
		{
			var iterator = html_document.createTreeWalker(html_document, NodeFilter.SHOW_ELEMENT, null, true);
			if (null != iterator)
			{
				for (var node = iterator.nextNode();null != node;node = iterator.nextNode())
				{
					if (node instanceof Components.interfaces.nsIDOMHTMLAnchorElement)
					{
						link_array.push(node.href);
						link_array.push(this.extractDescription(node));
					}
				}
				ret = true;
			}
		}
		catch(ex)
		{
			ret = false;
		}
		
		return ret;		
	},
	
	__getAllLinksImpl:function(link_array, child_nodes)
	{
		var child_nodes_len = child_nodes.length;
		for (var i = 0;i < child_nodes_len;i++)
		{
			var html_child = child_nodes[i];
			if ((html_child instanceof HTMLIFrameElement) || (html_child instanceof HTMLFrameElement))
			{
				if (!this.__iteratorNode(link_array, html_child.contentDocument))
					this.__interatorLinks(link_array, html_child.contentDocument);
			}
			else if (html_child instanceof HTMLFrameSetElement)
			{
				this.__getAllLinksImpl(link_array, html_child.childNodes);
			}
		}
	},
	
	__getAllLinks:function(link_array, browser)
	{
		var html_doc = browser.contentDocument;
		if (!this.__iteratorNode(link_array, html_doc))
			this.__interatorLinks(link_array, html_doc);
		
		this.__getAllLinksImpl(link_array, html_doc.defaultView.document.body.childNodes);
	},
	
	__hasLinks:function()
	{
		var browser = gBrowser.selectedBrowser;		
		return !this.__isAboutBlank(browser.currentURI.spec);
	},

	showContextMenu: function(event) 
	{
		var bc_menu_download_link_item = document.getElementById("bc_menu_download_link_item");
		
		var has_BitComet = false;		
		if (null != bc_ext_instance)
		{
			var	value =	{value:	0};
			if (bc_ext_instance.GetInfo("version", value))
			{
				has_BitComet = true;
			}
		}
		
		has_BitComet = true;
		
		if (bc_menu_download_link_item)
		{
			if (has_BitComet)
			{
				const isOnLink = gContextMenu.onSaveableLink || (gContextMenu.inDirList && gContextMenu.onLink);
				bc_menu_download_link_item.hidden = !isOnLink;
			}
			else
			{
				bc_menu_download_link_item.hidden = true;
			}
		}
		
		var bc_menu_download_all_item = document.getElementById("bc_menu_download_all_item");
		if (bc_menu_download_all_item)
		{
			if (has_BitComet)
				bc_menu_download_all_item.hidden = !this.__hasLinks();
			else
				bc_menu_download_all_item.hidden = true;
		}
		
		var bc_menu_download_media_item = document.getElementById("bc_menu_download_media_link");
		if (bc_menu_download_media_item)
		{
			bc_menu_download_media_item.hidden = this.__isAboutBlank(gBrowser.selectedBrowser.currentURI.spec);
		}
		
		var mi_seperator = document.getElementById("bc_menu_seperator_1");
		if (mi_seperator)
		{
			mi_seperator.hidden = bc_menu_download_link_item.hidden && bc_menu_download_all_item.hidden && bc_menu_download_media_item.hidden;
		}
	},
	
	onCmdDownloadSingleLink: function(e) 
	{       
		try
		{
			var cur_wnd = document.commandDispatcher.focusedWindow.top;
			var cur_doc = cur_wnd.document;
			var ref_url = cur_doc.URL;
			if (null == ref_url)
				return;
			
			var page_title = cur_doc.title;
			if (null == page_title)
				page_title = '';
			
			var arr_link = new Array();
			var link_url = gContextMenu.link;
			if (null == link_url)
				return;
			arr_link.push(link_url);
			
			var str_coment = this.extractDescription(gContextMenu.target);
			if ( str_coment == '' && gContextMenu.target.parentNode != null )
				str_coment = this.extractDescription(gContextMenu.target.parentNode);
			
			arr_link.push(str_coment);
			
			if(null != bc_ext_instance)
				bc_ext_instance.Download(arr_link.length, arr_link, ref_url, page_title, '');
		}
		catch(e)
		{
			window.alert( "BitCometExtension Failed, " + e.message );
		}
	},

	onCmdDownloadAll: function(e) 
	{
		try
		{	
			var browser = gBrowser.selectedBrowser;
			var ref_url = browser.currentURI.spec;
			if (this.__isAboutBlank(ref_url))
				return;
			var page_title = browser.contentDocument.title;
			var arr_link = new Array();
			this.__getAllLinks(arr_link, browser);
			
			if (0 != arr_link.length && null != bc_ext_instance)
				bc_ext_instance.Download(arr_link.length, arr_link, ref_url, page_title, '');
		}
		catch(e)
		{
			window.alert( "BitCometExtension Failed, " + e.message );
		}
	},
	
	trim : function(t)
	{
		return t.replace(/^[ \t_]+|[ \t_]+$/gi, "").replace(/(_){2,}/g, "_");
	},
	
	fmt : function(s)
	{
		return s.replace(/(\n){1,}/gi, " ").replace(/(\s){2,}/gi, " ");
	},

	extractDescription : function(child)
	{
		var rv = "";
		
		if ( rv == "" && child.nodeValue && child.nodeValue != "")
		{
			rv = this.fmt( child.nodeValue );
		}

		if ( rv == "" && child.hasAttribute )
		{
			if (child.hasAttribute('title'))
			{
				rv = this.fmt( child.getAttribute('title') );
			}
			else if (child.hasAttribute('alt'))
			{
				rv = this.fmt( child.getAttribute('alt') );
			}
		}

		if ( rv == "" && child.nodeType == 1)
		{
			for (var i = 0, len = child.childNodes.length; i < len && rv == ''; ++i)
			{
				rv = this.extractDescription( child.childNodes[i] );
			}
		}
		return this.trim(rv);
	}
  
	/*getVersion: function()
	{
		try
		{
			if (null == bc_ext_instance)
				return;
	
			var	value =	{value:	0};
	
			bc_ext_instance.GetInfo("version", value);
			window.alert( value.value );
			return parseFloat( value.value );
	
		}
		catch (e)
		{
			window.alert( "BitCometExtension Failed, " + e.message );
		}
	},*/

}; //var bc_ext


function onContentPopupMenu(event)
{
	bc_ext.showContextMenu(event);
}

window.addEventListener("load", function(e) { bc_ext.onLoad(e); }, false);

