
// BitCometAgent Usage:
// ---------------------
// bitcomet_get_version
// bitcomet_download
// bitcomet_download_url

// BitComet	Link Encode:
// ---------------------
// bitcomet_link_encode_bt
// bitcomet_link_encode_http

var	bitcomet_agent;

function bitcomet_navigator_is_ie_impl()
{
	return ( navigator.userAgent.indexOf('MSIE') !=	-1 );
}

function bitcomet_agent_create_ie_impl()
{
	if(	bitcomet_agent )
		return true;
			
	if (window.ActiveXObject)
	{
		try
		{
			bitcomet_agent = new ActiveXObject('BitCometAgent.BcAgent.1');
			if(	bitcomet_agent )
				return true;			
		}
		catch(ex)
		{
		}
	}
	return false;
}

function bitcomet_agent_create_ns_impl(plugname)
{
	if(	bitcomet_agent )
		return true;

	if (navigator.plugins && navigator.plugins.length >	0) 
	{
		var	pluginsArrayLength = navigator.plugins.length;
		for	(pluginsArrayCounter=0;	pluginsArrayCounter	< pluginsArrayLength; pluginsArrayCounter++	) 
		{
			if(	navigator.plugins[pluginsArrayCounter].name	== 'BitCometAgent' )
			{
				try
				{
					document.writeln('<embed id="bitcomet_agent" type="application/bitcomet-agent" width=0 height=0	hidden>');
					bitcomet_agent = document.getElementById("bitcomet_agent");
					
					if(	!bitcomet_agent	)
						return true;
				} 
				catch(e)
				{
				}			
			}
		}
	}
	
	return false;
}

function bitcomet_agent_create_auto()
{
	if(	bitcomet_navigator_is_ie_impl()	)
	{
		bitcomet_agent_create_ie_impl();
	}
	else
	{
		bitcomet_agent_create_ns_impl();
	}
}

function bitcomet_get_version()
{
	bitcomet_agent_create_auto();

	if(	bitcomet_navigator_is_ie_impl()	)
	{
		return parseFloat( bitcomet_agent.QueryAppInfo("version") );
	}
	else
	{
		var	value =	{value:	0};
		bitcomet_agent.GetInfo("version", value);
		return parseFloat( value.value );
	}	
}

function bitcomet_download(download_array, refer_url, page_title, page_html	)
{
	bitcomet_agent_create_auto();
	
	if(	bitcomet_navigator_is_ie_impl()	)
	{
		if(	bitcomet_get_version() > 0.98 )
		{
			return bitcomet_agent.Download(	download_array,	refer_url, page_title, page_html );
		}
		else
		{
			var	params_flash = new Array();
			return bitcomet_agent.AddLinkList( page_html, refer_url, page_title, download_array, params_flash );
		}
	}
	else
	{
		return bitcomet_agent.Download(	download_array.length, download_array, refer_url, page_title, page_html	);
	}	
}

function bitcomet_download_url(url,	refer_url)
{
	if(	!refer_url )
		refer_url =	document.location.href;
	
	var	download_array = new Array();
	download_array.push( url );
	download_array.push( ""	);
	return bitcomet_download( download_array, refer_url, "", ""	);
}

function bitcomet_link_encode_bt(torrent_url, filename)
{
	if ( !filename )
		filename = " ";
	
	var	utils =	new	bitcomet_base64_encode();
	var	bitcomet_link =	"bc://bt/";
	bitcomet_link += utils.EncodeB64("AA/" + encodeURIComponent(filename) +	"/0/0000000000000000000000000000000000000000/?torrent="	+ encodeURIComponent(torrent_url) +	"ZZ");
	return bitcomet_link;
}

function bitcomet_link_encode_http(http_url, filename)
{
	if ( !filename )
		filename = " ";
	
	var	utils =	new	bitcomet_base64_encode();
	var	bitcomet_link =	"bc://http/";
	bitcomet_link += utils.EncodeB64("AA/" + filename +	"/?url=" + encodeURIComponent(http_url)	+ "ZZ");
	return bitcomet_link;
}

function bitcomet_base64_encode()
{
	this.keyStr	= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
}

bitcomet_base64_encode.create=function(){};

bitcomet_base64_encode.prototype.EncodeB64=function(rawStr)
{
	var	escapedStr = encodeURIComponent(rawStr);
	var	count =	0;
	var	ints = new Array();
	var	b64Str = "";
	for(var	i=0,size=escapedStr.length;i<size;i++)
	{
		var	ch = escapedStr.charAt(i);
		
		if(ch=='%')
		{
			ints.push(parseInt(escapedStr.charAt(i+1),16)*16+parseInt(escapedStr.charAt(i+2),16));
			i += 2;
		}
		else
		{
			ints.push(ch.charCodeAt());
		}
		
		if(count++ == 2)
		{
			b64Str += this.GetB64Str(ints);
			count =	0;
			ints.length	= 0;
		}
	}

	b64Str += this.GetB64Str(ints);
	return b64Str;
};

bitcomet_base64_encode.prototype.GetB64Str=function(ints)
{
	if(ints.length==0) 
	return "";

	var	rawLen = ints.length;
	while(ints.length<3)
	{
		ints.push(0);
	}

	var	ch1	= this.keyStr.charAt(ints[0] >>	2);
	var	ch2	= this.keyStr.charAt(((ints[0] & 3)	<< 4) |	(ints[1] >>	4));
	var	ch3	= this.keyStr.charAt(((ints[1] & 15) <<	2) | (ints[2] >> 6));
	var	ch4	= this.keyStr.charAt(ints[2] & 63);
	
	if(rawLen==1)
	{
		ch3='=';
		ch4='=';
	}
	
	if(rawLen==2)
	{
		ch4='=';
	}
	
	return ch1+ch2+ch3+ch4;
};

bitcomet_agent_create_auto();