/* base */
String.prototype.Length = function(){var orString = new String(this);  var len = 0; if (orString=="") {return len;}for(var vii=0;vii<orString.length;vii++){len++; } return len;}
if (!Function.prototype.apply){
	Function.prototype.apply = function(object, parameters){
		var parameterStrings = [];
		if (!object)	object = window;
		if (!parameters)	parameters = [];
		for (var i=0; i<parameters.length; i++)
			parameterStrings[i] = "parameters[" + i + "]";
		object.__apply__ = this;
		var result = eval("object.__apply__(" + parameterStrings.join(", ") + ")");
		object.__apply__ = null;

		return result;
	}
}
var Class = {
	create: function(){
		return function(){
			this.initialize.apply(this, arguments);
		}
	}
}

var Delegate = {
	create: function (obj, func){
		var f = function()	{
			var target = arguments.callee.target;
			var func = arguments.callee.func;
			return func.apply(target, arguments);
		}
		f.target = obj;
		f.func = func;
		return f;
	}
};

var isUndef = function(a){ return typeof a == "undefined";};

var Thunder = {
	_type:-1,
	getInstance: function(){
		//优先使用web迅雷,web迅雷未安装则使用迅雷5,迅雷5也未安装或初始化出错调迷你迅雷
		var opt = [ Thunder.Thunder5, Thunder.WebThunder, Thunder.MiniThunder ];
		for (var i=0; i<opt.length; i++){
			try{
				return opt[i].getInstance();
			}catch(e){
				continue;
			}
		}
		return null;
	},
	
	download: function(cid, url, refer, name, stat){
		this.getInstance().download(cid, url, refer, name, stat);
	},
	
	batchDownload: function(data, stat){
		this.getInstance().batchDownload(data, stat);
	}
}

Thunder.WebThunder = Class.create();
Thunder.WebThunder.getInstance = function(){
	//alert(this._thunder);
	if (isUndef(this._thunder))
		this._thunder = new Thunder.WebThunder();
	return this._thunder;
}
Thunder.WebThunder.prototype = {	
	initialize: function(){
		this.__thunder = new ActiveXObject("ThunderServer.webThunder.1");
		if(this.__thunder!=null)
			Thunder._type=1;
	},
	
	download: function(cid, url, refer, name, stat){
		this.__thunder.CallAddTask(url, name, refer, 1, cid, stat);
		return 0;
	},
	
	batchDownload: function(res, _stat){
		 var s = this.__thunder.BeginBatchTask();
		 for (var i = 0; i < res.length; ++i)
		 {
			var o = res[i];
			this.__thunder.AddTaskToBatch(s, o.url, o.name, o.refer, o.cid, _stat);
		 }
		 this.__thunder.EndBatchTask(s);
	}
}

Thunder.Thunder5 = Class.create();
Thunder.Thunder5.getInstance = function(){
	if (isUndef(this._thunder))
		this._thunder = new Thunder.Thunder5();
	return this._thunder;
}
Thunder.Thunder5.prototype = {
	initialize: function(){
		try{
			this.__thunder = new ActiveXObject("ThunderAgent.Agent.1");
		}catch(e){
			this.__thunder = new ActiveXObject("ThunderAgent.Agent");
		}
		if(this.__thunder!=null)
			Thunder._type=0;
	},
	
	download: function(cid, url, refer, name, stat){
		try{
			this.addTask(cid, url, refer, name, stat);
			this.commitTasks();
		}catch(e){
			//alert(e.message);
		}
	},
	
	batchDownload: function(data, stat){
		try{
			for(i=0; i<data.length; i++)
				this.addTask(data[i].cid, data[i].url, data[i].refer, data[i].name, stat);
			this.commitTasks();
		}catch(e){
			//alert(e.message);
		}
//		delete this.__thunder;
	},
	
	addTask: function(cid, url, refer, name, stat){
		var _addTask = [
			Delegate.create(this, function(){this.__thunder.AddTask5(url, name, "", name, refer, -1, 0, -1, "", cid, stat, 1 ,"", -1);}),
			Delegate.create(this, function(){this.__thunder.AddTask4(url, "", "", name, refer, -1, 0, -1, "", cid, stat);}),
			Delegate.create(this, function(){this.__thunder.AddTask3(url, "", "", name, refer, -1, 0, -1, "", cid);}),
			Delegate.create(this, function(){this.__thunder.AddTask2(url, "", "", name, refer, -1, 0, -1, "");}),
			Delegate.create(this, function(){this.__thunder.AddTask(url, "", "", name, refer, -1, 0, -1);})
		];
		for (var i=0; i<_addTask.length; i++){
			try{
				_addTask[i]();
				return;
			}catch(e){	
			}
		}
		throw "不支持此方法，请安装最新的迅雷客户端";
	},
	
	commitTasks: function(){
		var _commitTasks = [
			Delegate.create(this, function(){this.__thunder.CommitTasks2(1);}),
			Delegate.create(this, function(){this.__thunder.CommitTasks();})
		];
		for (var i=0; i<_commitTasks.length; i++){
			try{
				_commitTasks[i]();
				return;
			}catch(e){
			}
		}
		throw "不支持此方法，请安装最新的迅雷客户端";
	}
}


Thunder.MiniThunder = Class.create();
Thunder.MiniThunder.getInstance = function(){
	if (isUndef(this._thunder))
		this._thunder = new Thunder.MiniThunder();
	return this._thunder;
}
Thunder.MiniThunder.prototype = {
	initialize: function(){
		try{
			this.__thunder = new ActiveXObject("ToolbarThunder.DownloadAgent.1");
		}catch(e){
		}
		if(this.__thunder!=null)
			Thunder._type=2;
	},
	
	download: function(cid, url, refer, name, stat){
		try{
			this.__thunder.addTask(url, refer, name, cid,  stat);
		}catch(e){
			//alert(e.message);
		}
	}
}

