

var DlgSaveAs = {
	
	init: function()
	{
		document.documentElement.setAttribute('ondialogaccept', 'if(DlgSaveAs.dialogAccepted()) { ' + document.documentElement.getAttribute('ondialogaccept') +'}');
//		this.preferences_init();
        this.bc_radio_save_as_init();
        this.forceNormal(false);
		//alert(document.title);
	},
	
//	preferences_init: function()
//	{
//        try
//        {
//            this.prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
//            if (this.prefService)
//            {
//                this.prefrence = this.prefService.getBranch("");
//            }
//        }
//        catch(e)
//        {
//            this.prefService = null;
//            this.prefrence = null;
//        }
//        
//        var fname = dialog.mLauncher.suggestedFileName;
//        var ext = fname.split('.');
//        if(ext.length > 1)
//        {
//            if (this.is_support_file(ext[ext.length-1]))
//            {
//                this.download();
//            }
//        }
//	},
//	
//	set_support_file: function(ext)
//	{
//	    if (!this.is_support_file(ext) && this.prefrence )
//	    {
//	        var extensions = this.prefrence.getCharPref("BitComet.extensions");
//	        if(extensions)
//	        {
//	            extensions += ",";
//	            extensions += ext;
//	            this.prefrence.setCharPref("BitComet.extensions", extensions);
//	        }
//	        else
//	            this.prefrence.setCharPref("BitComet.extensions", ext);
//	    }
//	},
//	
//	get_support_file: function()
//	{
//	    if(this.prefrence)
//	    {
//	        var val = this.prefrence.getCharPref("BitComet.extensions");
//	        return extensions = val ? val.split(',') : [];
//	    }
//	    return [];
//	},
//	
//	is_support_file: function(ext)
//	{
//	    var ext = ext.toLowerCase();
//	    var extensions = this.get_support_file();
//	    for (var i = extensions.length - 1; i >= 0; --i)
//	    {
//            if (extensions[i].toLowerCase() == ext)
//            {
//                return true;
//            }
//        }
//        return false;
//	},
//	
//	preferences: function()
//	{
//	    try
//	    {
//	        var radio_selected = document.getElementById("mode").selectedItem;
//	        var rememberChoice = document.getElementById("rememberChoice");
//	        var bc_radio_save_as = document.getElementById("bc_radio_save_as");
//	        if (rememberChoice && bc_radio_save_as == radio_selected)
//	        {
//	            if (rememberChoice.checked)
//	            {
//	                var fname = dialog.mLauncher.suggestedFileName;
//                    var ext = fname.split('.');
//                    if(ext.length > 1)
//                    {
//                        this.set_support_file(ext[ext.length-1]);
//                    }
//	            }
//	        }
//	    }
//	    catch(e)
//	    {
//	    }
//	},
	
    forceNormal: function(secondChance) 
    {
        var basicBox = document.getElementById('basicBox');
        var normalBox = document.getElementById('normalBox');
        var self = this;
        if((normalBox && basicBox))
        {
            if(normalBox.collapsed && basicBox.collapsed && !secondChance)
            {
                window.setTimeout(function() { self.forceNormal(true); }, 10);
                return;
            }
            if(normalBox.collapsed)
            {
                basicBox.collapsed = true;
                normalBox.collapsed = false;
                document.getElementById('open').parentNode.collapsed = true;
                var nodes = normalBox.getElementsByTagName('separator');
                for(var j = nodes.length - 1; j >= 0; --j)
                {
                    nodes[j].collapsed = true;
                }
            }
        }
        window.setTimeout(self.sizeToContent, 0);
    },
  
    sizeToContent: function()
    {
        try
        {
            window.sizeToContent();
        }
        catch(e)
        { // bug 367184  
        }
        
        window.resizeTo(window.outerWidth, document.getElementById("container").boxObject.height + 80);
    },
  
    bc_radio_save_as_init: function()
    {
        var bc_ext_instance = null;
		bc_ext_instance = Components.classes["@bitcomet.com/BitCometExtension;1"].createInstance( Components.interfaces.IBitCometExtension );
		if( !bc_ext_instance )
		{
			window.alert( "Failed BitCometExtension, No Class." );
		}
		
        var has_BitComet = false;		
		if (null != bc_ext_instance)
		{
			
			var	value =	{value:	0};
			if (bc_ext_instance.GetInfo("version", value))
			{
				has_BitComet = true;
			}
		}
		
        var bc_radio_save_as = document.getElementById("bc_radio_save_as");
        if (bc_radio_save_as)
        {
            if (!has_BitComet) 
            {
               bc_radio_save_as.hidden = true;      
            }
        }
    },
    
	dialogAccepted: function()
	{   
		var radio_selected = document.getElementById("mode").selectedItem;
		var bc_radio_save_as = document.getElementById("bc_radio_save_as");
		
		if (radio_selected != bc_radio_save_as)
			return true;
			
//		this.preferences();
		this.download();
		return false;
	},
	
	download: function()
	{
	    try
		{
			var link_url = dialog.mLauncher.source.spec;
			if (null == link_url)
				return;
			
			var ref_url = '';
			try
			{
				ref_url = dialog.mContext.QueryInterface(Components.interfaces.nsIWebNavigation).currentURI.spec;
			}
			catch(ex)
			{
				ref_url = link_url;
			}
			
			var page_title = '';
			
			var arr_link = new Array();
			arr_link.push(link_url);
			
			var str_coment = '';
			arr_link.push(str_coment);
			
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			var bc_ext_instance = Components.classes["@bitcomet.com/BitCometExtension;1"].createInstance( Components.interfaces.IBitCometExtension );
			if( !bc_ext_instance )
			{
				window.alert( "Failed BitCometExtension, No Class." );
				return;
			}	
			bc_ext_instance.Download(arr_link.length, arr_link, ref_url, page_title, '');
			
			document.documentElement.removeAttribute('ondialogaccept');
			document.documentElement.cancelDialog();
		}
		catch(e)
		{
			window.alert( "BitCometExtension Failed, " + e.message );
		}
	},
}

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