/*
 * 刷新验证码
 */
jQuery.fn.fresh_authcode=function(options){
	var default_options={
		authcode:'#authcode'	
	}; 
	
	options=jQuery.extend(default_options);
	
	jQuery(this).click(function(){
		jQuery(options.authcode).attr('src', '/img/authcode_default.gif' );
		
		var url = "/authcode?t=" + (new Date()).getTime();
		
		jQuery(options.authcode).attr('src', url);
		
		return false;
	});
};

/**
 * 取全局变量 global 中的配置
 */
function common_config(main, sub){
	return eval('global.'+main+'.'+sub);
}

/**
 * 日志 ，仅firefox时输出到 console
 */
function common_log(){
	if( window.console && $.browser.mozilla && common_config('site','is_debug')){
		console.log.apply('console.log', arguments);
		//console.log(arguments);
	}
}

/**
 * 非debug模式下，关闭 window.onerror
 */
if(!common_config('site','is_debug')){
	window.onerror = function(){ return true;};
}

/**
 *  字符串 string转xml对象
 *  尽量使用浏览器原生方法
 */
function string_to_xml(xml){
	if (window.ActiveXObject) {
		//for IE
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.loadXML(xml);
		return xmlDoc;
	} else if (document.implementation && document.implementation.createDocument) {
		//for Mozila
		var parser = new DOMParser();
		var xmlDoc = parser.parseFromString(xml,"text/xml");
		return xmlDoc;
	}
	return false;
}

/**
 *  xml 对象 转 string
 *  尽量使用浏览器原生方法
 */
function xml_to_string(xml_object){
	if(xml_object){
	}else{
		return '';
	};	
	if (window.ActiveXObject) {
		// for IE
		return xml_object.xml;
	} else if( XMLSerializer ){
		//for Mozila
		return (new XMLSerializer()).serializeToString(xml_object);
	}
	return '';
}

/**
 * 重新加载js
 * 
 */
function load_js(id, url, charset, callback){
    var obj = document.getElementById(id);
    if(obj){
        obj.parentNode.removeChild(obj);
    }
    
    var newscript=document.createElement("script");
    newscript.type="text/javascript";
    newscript.src=url;
    newscript.id=id;
    
    try{
        newscript.charset=charset;
    }catch (e){};
    
    var head=document.getElementsByTagName('head')[0];
    
    head.appendChild(newscript);
    
	if (!/*@cc_on!@*/0) { //if not IE  
		//Firefox2、 Firefox3、Safari3.1+、Opera9.6+ support js.onload  
		newscript.onload = callback;
	} else {  
		//IE6、 IE7 support js.onreadystatechange  
		newscript.onreadystatechange = callback;
	}  
}

/**
 *  从cookie中判断用户有没有登录
 *  如果登录显示登录状态和注销
 *  如果没有登录，显示登录和注册
 */ 
function common_is_login(){	
	return ($.cookie('s[username]') != null);
}

/**
 * StringBuilder 为优化IE6等古老浏览器的string +操作
 * 
 */
var StringBuilder = function(){
    this._buffer    = [];
    this._arg1        = "";
    this._arg2        = "";
    if ( arguments.length > 0 ) this._arg1 = String( arguments[0] );
    if ( arguments.length > 1 ) this._arg2 = String( arguments[1] );
};
StringBuilder.prototype.append = function( str ){
    this._buffer[this._buffer.length] = String( str );
};
StringBuilder.prototype.toString = function(){
    return ( this._arg2 ? this._arg1 : "" ) + this._buffer.join( this._arg2 + this._arg1 ) + this._arg2;
};
StringBuilder.prototype.clear = function(){
    this._buffer = [];
};
StringBuilder.prototype.add = StringBuilder.prototype.append;

/**
 *  class=tabs 点击事件 全藏后仅显示被选中的
 * 
 */
function tabs_turn(){	
	$('ul.tabs').each(function(){	
		//div编号
		$(this).parent().children('div').each(
			function(i){
				$(this).attr("tabs_order",i);
			}
		);
		//li 点击事件 全藏后仅显示被选中的
		$(this).children('li').each(function(j){
			$(this).click(function(){
				$(this).parent().children('li').removeClass('current');
				$(this).addClass('current');				
				$(this).parent().parent().children('div').hide();
				$(this).parent().parent().children('div:eq('+ j + ')').fadeIn();				
				//IE 下 取消链接虚线
				$(this).children('a').blur();
				
				return false;
			});
		});
		//默认第一个被点击
		$(this).children('li:eq(0)').click();
	});
}

/*
 * 股票行情卡中的的配置文件  在boxy 弹出的iframe中
 * 被init_all中的 init_stock_boxy 调用
 */
function common_show_stock_chart(stock_code){
	url="/stock/stock_market/"+ stock_code;
	var boxy = new Boxy('<iframe src="'+url+'" width="690" height="450" scrolling="no"></iframe>', {
		width:680,
		title:'收益宝 股票行情 ' + stock_code,
		closeable:true,
		modal:false
	});	
	boxy.show();
}

/**
 * 提交表单，但通常response响应时间太久，所以有时候会先做 fadeOut
 * init_all 中通过 setInterval( "init_re_show_container();	" , 6 * 1000); 确保不会有 “退回”键 bug
 * 
 */
function commn_hide_before_submit(){
	//股票行情卡关闭	
	//Boxys 关闭
	//底部导航条关闭	
	//$('#toolbar_wrap').hide();
	//如果有弹出层的 locking ，关闭
	$('#locking').hide();
	$('#container').fadeOut( 2*1000);
}

/**
 * 比较两个日期之间的相差天数，参数形式为 YYYY-MM-DD
 */
function commn_datediff(sDate1,sDate2){
	var aDate,oDate1,oDate2,iDays ;
	aDate =sDate1.split('-'); 
	oDate1 = new Date(aDate[0],aDate[1]-1,aDate[2]) ;
	
	aDate = sDate2.split('-'); 
	oDate2 = new Date(aDate[0],aDate[1]-1,aDate[2]) ;
	
	//把相差的毫秒数转换为天数
	iDays = parseInt((oDate1 -oDate2)/1000/60/60/24);
	//Math.abs
	return iDays ;
};

/**
 * 页面最大化 chrome有沙盒，仅给IE6使用
 * 首页、登陆页、注册页等显示调用
 */
function common_max_window(){
	var w = window;
	var view_port = common_viewport_size();
	try{
		var avail_width = screen.availWidth;
		if(view_port.width > 0 && (view_port.width < (screen.availWidth - 100) ) ){
			w.moveTo(-2,-2); w.resizeTo( avail_width , screen.availHeight); 
		}
	}catch (e){};
}

/**
 * 页面实际宽高
 */
function common_viewport_size()
{
	var _width = 0, _height = 0;
	if(typeof(window.innerWidth ) == 'number' ){
		//Non-IE
		_width = window.innerWidth;
		_height = window.innerHeight;
	}else if(  document.documentElement &&  ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){ 
		//IE 6
		_width = document.documentElement.clientWidth;
		_height = document.documentElement.clientHeight;
	}else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
		//IE 4
		_width = document.body.clientWidth;
		_height = document.body.clientHeight;
	}
	return {width:_width, height:_height};
}

function get_avatar_path(person_id){
	var avatar = 'http://file.shouyibao.com/file/';

	var y = person_id%100;
	var s = parseInt(person_id/100);

	if(y<10){
		avatar += "0";
	}

	avatar += y+'/';

	y = parseInt(s%100);

	if(y<10){
		avatar += "0";
	}

	avatar += y+'/';

	avatar += person_id+'/';

	return avatar;
}
