FlashProxy.prototype._idx = '';
FlashProxy.prototype._conf = new Array();
FlashProxy.prototype._data = new Array();
FlashProxy.prototype._ids = {
	'flash':'qcomic_flash_',
	'view':'qcomic_view_',
	'feedback':'qcomic_feedback'
};
FlashProxy.prototype._flash_obj = null;
FlashProxy.prototype._browser = '';
FlashProxy.prototype._feedback = null;
FlashProxy.prototype._debug_pri = {
	'low':1,
	'normal':2,
	'high':3
};

FlashProxy.prototype.debug = function(msg,pri)
{
	if( location.href.match(/qihoo.net/ig) )
	{
	}
	else
	{
		return;
	}

	switch( pri )
	{
		case this._debug_pri['low']:
			return;
			break;
		case this._debug_pri['normal']:
		default:
			break;
	}

	if( !document.getElementById('qcomic_debug') )
	{
		return;
	}

	if( typeof(msg) == 'string' )
	{
		msg = msg.replace(/</ig,'[');
		msg = msg.replace(/>/ig,']');
		document.getElementById('qcomic_debug').innerHTML+= msg+'<br>';
	}
	else if( typeof(msg) == 'object' )
	{
		var tmp = '';
		for( idx in msg )
		{
			tmp+= idx+'='+msg[idx]+',';
		}
		document.getElementById('qcomic_debug').innerHTML+= tmp+'<br>';
	}
};FlashProxy.prototype.getFlashObj = function(params)
{
	var id = params['id'];
	var obj = null;
	var browser  = this.getBrowser();
	if( browser == 'ie' )
	{
		obj = this.getObj(params);
	}
	else
	{
		obj = window[id];
		if( !obj )
		{
			obj = document.getElementsByName(id)[0];
		}
	}
	this.debug('[getFlashObj]browser='+browser+',id='+id+',obj='+obj);
	return obj;
};FlashProxy.prototype.getBrowser = function()
{
	if (navigator.appName.indexOf('Microsoft') != -1)
	{
		return 'ie';
	}
	else
	{
		return 'ff';
	}
};FlashProxy.prototype.replaceCode = function(code)
{
	code = code.replace(/[+]/g,'%2B');
	code = code.replace(/[ ]/g,'+');
	return code;
};FlashProxy.prototype.qcomic_test = function()
{
	this.debug('[qcomic_test]enter');
	return true;
};FlashProxy.prototype.copyText = function(obj)  
{
	if( this._browser == 'ie' )
	{
		var rng = document.body.createTextRange();
		rng.moveToElementText(obj);
		rng.scrollIntoView();
		rng.select();
		rng.execCommand('Copy');
		rng.collapse(false);
		alert('复制代码成功');
	}
	else
	{
		obj.select();
	}
	return false;
};
FlashProxy.prototype.init = function(params)
{
	this._browser = this.getBrowser();
	this.debug('[init]browser='+this._browser);

	var _conf = new Array();
	_conf = params[0];
	this.setConf(_conf);

	var _data = new Array();
	_data = params[1];
	this.setData(_data);

	var _idx = params[2];
	this._idx = _idx;
	this.debug('[init]_idx='+_idx);
};

FlashProxy.prototype.getViewObj = function(pos)
{
	var view_id = this._ids['view']+this._idx+pos;
	var view_obj = document.getElementById(view_id);
	this.debug('[getViewObj]view_id='+view_id+',view_obj.id='+view_obj.id);
	return view_obj;
};

FlashProxy.prototype.setData = function(params)
{
	this.debug('[setData]enter');
	this.debug(params);
	this.debug(params['data']);
	this._data = params;
};

FlashProxy.prototype.setConf = function(params)
{
	this.debug('[setConf]enter');
	this.debug(params);
	this.debug(params['data']);
	this._conf = params;
};

FlashProxy.prototype.setViewStyle = function(view_obj)
{
	view_obj.className = 'qcomic_tab';
	var style = '<style>';
	style+= '.qcomic_tab{clear:both;overflow:hidden;line-height:22px;}';
	style+= '.qcomic_tab td{margin:0;padding:5px;border:none;font-size:14px;vertical-align:top;}';
	style+= '.qcomic_tab tbody td{margin:0;padding:5px;border:none;font-size:14px;vertical-align:top;}';
	style+= '.qcomic_tab textarea{border:solid #999;border-width:1px;width:400px;height:60px;color:#666;font-size:12px;margin:0;background:#FFC;overflow:hidden;}';
	style+= '</style>';
	return style;
};

FlashProxy.prototype.displayThread = function()
{
	/* conf */
	flag 		= this._conf['flag'];
	maxnum 		= this._conf['maxnum'];
	colnum 		= this._conf['colnum'];
	width 		= this._conf['width'];
	height 		= this._conf['height'];
	pos 		= (this._conf['pos']==1) ? '_before' : '_after';
	nav_width	= this._conf['nav_width'];
	nav_height	= this._conf['nav_height'];
	percent		= this._conf['percent'];
	if( percent == 1 )
	{
		width = nav_width;
	}

	/* data */
	sid 	= this._data['sid'];
	sname 	= this._data['sname'];
	surl 	= this._data['surl'];
	gid 	= this._data['gid'];
	code 	= this._data['code'];
	_data 	= this._data['data'];
	len 	= (_data.length>maxnum) ? maxnum : _data.length;

	/* view object */
	view_obj = this.getViewObj(pos);

	/* output html */
	html = '<table id="thread_view_'+this._idx+'" cellpadding="0" cellspacing="0" border="0" width="100%"></table>';
	html+= this.setViewStyle(view_obj);
	this.debug('[displayThread]'+html,this._debug_pri['low']);

	view_obj.innerHTML = '';
	if( this._browser == 'ff' )
	{
		view_obj.innerHTML+= html;
	}
	else
	{
		this.qcomic_display_flash({
			'view_obj':view_obj,
			'html':html
		});
	}
};

FlashProxy.prototype.displayThreadOneByOne = function(params)
{
	this.debug('[displayThreadOneByOne]enter');
	if( typeof(params['data']) == 'undefined' )
	{
		this.debug('[displayThreadOneByOne]param error!');
		return;
	}
	else
	{
		this.debug('[displayThreadOneByOne]params[data].length='+params['data'].length);
		for( var i=0;i<params['data'].length;i++ )
		{
			this.debug(params['data'][i]);
		}
	}

	var thread_view = 'thread_view_'+params['idx'];
	var thread_view_obj = document.getElementById(thread_view);
	var _row = thread_view_obj.insertRow(-1);
	this.debug('[displayThreadOneByOne]thread_view='+thread_view+',thread_view_obj='+thread_view_obj+',_row='+_row);

	for( var i=0;i<params['data'].length;i++ )
	{
		active	= params['data'][i]['active'];
		burl 	= params['data'][i]['burl'];
		burl_w 	= params['data'][i]['burl_w'];
		burl_h 	= params['data'][i]['burl_h'];
		rurl 	= params['data'][i]['rurl'];
		rurl_w 	= params['data'][i]['rurl_w'];
		rurl_h 	= params['data'][i]['rurl_h'];
		qurl	= params['data'][i]['qurl'];
		qurl_w 	= params['data'][i]['qurl_w'];
		qurl_h 	= params['data'][i]['qurl_h'];
		desc 	= params['data'][i]['desc'];

		_url = burl;
		if( active == 1 )
		{
			_url = qurl;
		}

		_width 	= burl_w;
		_height	= burl_h;
		if( _width=='' || _height=='' )
		{
			_resize = 2;
			_width = width;
			_height = height;
		}
		else
		{
			_resize = 1;
			this.debug('[displayThreadOneByOne]before,_width='+_width+',_height='+_height);
			if( _width > width )
			{
				_height = Math.ceil(_height/(_width/width));
				_width = width;
			}
			else if( _height > height )
			{
				_width = Math.ceil(_width/(_height/height));
				_height = height;
			}
			this.debug('[displayThreadOneByOne]after,_width='+_width+',_height='+_height);
		}

		if( _width!='' && _height!='' )
		{
			flash_vars = 'sid='+(sid);
			flash_vars+= '&sname='+(sname);
			flash_vars+= '&surl='+(surl);
			flash_vars+= '&desc='+(desc);
			flash_vars+= '&url='+(_url);
			flash_vars+= '&rurl='+(rurl);
			flash_vars+= '&rurl_w='+rurl_w;
			flash_vars+= '&rurl_h='+rurl_h;
			flash_vars+= '&rurl_size=';
			flash_vars+= '&resize='+(_resize);
			flash_vars+= '&exif=';
			this.debug('[displayThreadOneByOne]flash_vars='+flash_vars,this._debug_pri['low']);

			var html = '';
			if( active == 1 )
			{
				_url = _url.replace(/yp.qihoo.com/,'gp.qihoo.com');
				html+= '<img src="'+_url+'" width="'+_width+'" height="'+_height+'" />';
			}
			else
			{
				html+= '<embed src="http://p.qihoo.com/flash/single_img.swf" quality="high" bgcolor="#" width="'+_width+'" height="'+_height+'" type="application/x-shockwave-flash" allowScriptAccess="always" swLiveConnect="true" flashvars="'+flash_vars+'" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
			}
			if( desc )
			{
				html+= '<p style="margin:4px 0;">'+desc+'</p>';
			}

			var _col = _row.insertCell(-1);
			_col.innerHTML = html;
		}
	}
};

FlashProxy.prototype.displaySlide = function()
{
	/* conf */
	width	= this._conf['fwidth'] ? this._conf['fwidth'] : 700;
	height 	= this._conf['fheight'] ? this._conf['fheight'] : 556;

	/* data */
	sid 	= this._data['sid'];
	code 	= this._data['code'];
	code	= this.replaceCode(code);
	pos 	= (this._conf['pos']==1) ? '_before' : '_after';

	/* get view object */
	view_obj = this.getViewObj(pos);

	/* assign flashVars */
	flash_vars = 'sid='+sid;
	flash_vars+= '&code='+code;
	flash_vars+= '&auto=1';
	flash_vars+= '&ver=dz_2';
	this.debug('[displaySlide]flash_vars='+flash_vars);

	/* assign html */
	flashTagIns.setNew('http://p.qihoo.com/flash/pic_config_v2.swf',width,height);
	flashTagIns.setVersion('7,0,19,0');
	flashTagIns.allowScriptAccess = 'always';
	flashTagIns.allowFullScreen = 'true';
	flashTagIns.swLiveConnect = 'true';
	flashTagIns.wmode = null;
	flashTagIns.flashVars = flash_vars;
	html = '';
	html+= flashTagIns.toString();
	html+= this.setViewStyle(view_obj);
	this.debug('[displaySlide]'+html);

	/* display html */
	view_obj.innerHTML = '';
	if( this._browser == 'ff' )
	{
		view_obj.innerHTML+= html;
	}
	else
	{
		this.qcomic_display_flash({
			'view_obj':view_obj,
			'html':html
		});
	}
};

FlashProxy.prototype.displayUBB = function()
{
	/* data */
	sid 	= this._data['sid'];
	surl	= this._data['surl'];
	code 	= this._data['code'];
	code	= this.replaceCode(code);
	proxy	= this._data['proxy'];
	pos 	= (this._conf['pos']==1) ? '_before' : '_after';

	/* get view object */
	view_obj = this.getViewObj(pos);

	url_raw = 'http://p.qihoo.com/flash/pic_link.swf?sid='+sid+'&code='+code+'&local=1&auto=1';
	url = surl+'/'+proxy+'?action=proxy&sid='+sid+'&code='+code+'&url='+escape(url_raw);
	width = 430;
	height = 320;
	var site = {
		'blog.163.com':{
			'name':'网易博客',
			'code':'<EMBED src="'+url+'" width="'+width+'" height="'+height+'" allowScriptAccess="true" allowFullScreen="true" type="application/x-shockwave-flash"></EMBED>'
		},
		'blog.sina.com':{
			'name':'新浪博客',
			'code':'<EMBED src="'+url+'" width="'+width+'" height="'+height+'" allowScriptAccess="true" allowFullScreen="true" type="application/x-shockwave-flash"></EMBED>'
		},
		'blog.sohu.com':{
			'name':'搜狐博客',
			'code':'<EMBED src="'+url_raw+'" width="'+width+'" height="'+height+'" allowScriptAccess="true" allowFullScreen="true" type="application/x-shockwave-flash"></EMBED>'
		},
		'qzone.qq.com':{
			'name':'QQ空间',
			'code':'[flash,'+width+','+height+']'+url+'[/flash]'
		},
		'blog.tianya.cn':{
			'name':'天涯博客',
			'code':'<EMBED src="'+url+'" width="'+width+'" height="'+height+'" allowScriptAccess="true" allowFullScreen="true" type="application/x-shockwave-flash"></EMBED>'
		},
		'blog.hexun.com':{
			'name':'和讯博客',
			'code':'<EMBED src="'+url+'" width="'+width+'" height="'+height+'" allowScriptAccess="true" allowFullScreen="true" type="application/x-shockwave-flash"></EMBED>'
		}
	};

	/* output html */
	html = '<div style="color:#ff0000;">把下面的代码复制到你的博客或个人空间，就可以把这组幻灯嵌到你的博客上。</div>';
	html+= '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
	for( var site_url in site )
	{
		html+= '<tr>';
			html+= '<td width="150">'+site[site_url]['name']+'<br>'+site_url+'</td>';
			html+= '<td><textarea onclick="qcomic_proxy.copyText(this);">'+site[site_url]['code']+'</textarea></td>';
		html+= '</tr>';
	}
	html+= '</table>';
	html+= this.setViewStyle(view_obj);
	this.debug('[displayUBB]'+html);
	view_obj.innerHTML = html;
};

FlashProxy.prototype.displayFeedback = function()
{
	/* data */
	sid 	= this._data['sid'];
	code 	= this._data['code'];
	code	= this.replaceCode(code);
	pos 	= (this._conf['pos']==1) ? '_before' : '_after';

	/* get view object */
	view_obj = this.getViewObj(pos);

	/* output html */
	html = '';
	html+= '关于组图功能觉得哪里不爽，可以在下面反馈给我们哦！<br>';
	html+= '<textarea id="'+this._ids['feedback']+'" style="width:400px;height:200px;"></textarea>';
	html+= '<input type="button" value="提交反馈" onClick="qcomic_proxy.submitFeedback();" />';
	html+= this.setViewStyle(view_obj);
	this.debug('[displayFeedback]'+html);
	view_obj.innerHTML = html;
};

FlashProxy.prototype.submitFeedback = function()
{
	this.getFlashObj(this._ids['flash']+this._idx);
	this._feedback = {
		'desc':document.getElementById(this._ids['feedback']).value
	};
	this.debug('[submitFeedback]desc='+this._feedback['desc']);
	this.debug('[submitFeedback]typeof='+typeof(this._flash_obj.fb_name()));
};

FlashProxy.prototype.getFeedback = function()
{
	var cnt = {
		'desc' : this._feedback['desc']
	};
	return cnt;
};

FlashProxy.prototype.qcomic_display_slide = function(params)
{
	try
	{
		this.debug('[qcomic_display_slide]enter');
		this.init(params);
		this.displaySlide();
	}
	catch(e)
	{
		this.debug('[qcomic_display_slide]catch error is '+e.message);
	}
};

FlashProxy.prototype.qcomic_display_thread = function(params)
{
	try
	{
		this.debug('[qcomic_display_thread]enter');
		this.init(params);
		this.displayThread();
	}
	catch(e)
	{
		this.debug('[qcomic_display_thread]catch error is '+e.message);
	}
};

FlashProxy.prototype.qcomic_display_ubb = function(params)
{
	try
	{
		this.debug('[qcomic_display_ubb]enter');
		this.init(params);
		this.displayUBB();
	}
	catch(e)
	{
		this.debug('[qcomic_display_ubb]catch error is '+e.message);
	}
};

FlashProxy.prototype.qcomic_display_feedback = function(params)
{
	try
	{
		this.debug('[qcomic_display_feedback]enter');
		this.init(params);
		this.displayFeedback();
	}
	catch(e)
	{
		this.debug('[qcomic_display_feedback]catch error is '+e.message);
	}
};

FlashProxy.prototype.qcomic_openpage = function(params)
{
	this.debug('[qcomic_openpage]enter');

	var window_hdl = new Array();
	for( var i=0;i<params['tinfo'].length;i++ )
	{
		this.debug(params['tinfo'][i]);
		window_hdl[i] = window.open(params['tinfo'][i]['turl'],'_blank');
	}

	window_hdl[window_hdl.length-1].focus();
};
