var _alertTitle = '趋势网&middot;移动互联网社区MTrend';
var _mturl = 'http://www.mtrend.net.cn';

// Ajax类
function classAjax(){
	var _z = false; //xmlHTTP
	try {
		_z = new XMLHttpRequest();
	}catch (trymicrosoft){
		try {
			_z = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (othermicrosoft){
			try {
				_z = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (failed){
				_z = false;
			}
		}
	}
	this.setRequest = function(url,para,fun){
		_z.open("POST",url,true); 
		_z.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		_z.onreadystatechange = function(){
			if (_z.readyState==4){
				if (_z.status==200){
					fun(_z.responseText);
				}
			}
		}
		_z.send(encodeURI(para));
	}
}

function inArray(value, arr){
	var i;
	for(i=0; i<arr.length; i++){
		if(value == arr[i]){
			return true;
		}
	}
	return false;
}
//判断浏览器类型
function getOs(){
    var OsObject = "";
   if(navigator.userAgent.indexOf("MSIE")>0){
        return "MSIE";
   }
   if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
        return "Firefox";
   }
   if(isSafari=navigator.userAgent.indexOf("Safari")>0){
        return "Safari";
   } 
   if(isCamino=navigator.userAgent.indexOf("Camino")>0){
        return "Camino";
   }
   if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){
        return "Gecko";
   } 
}

//popup
function ShowAlert(title, content){
	var pop=new Popup({ contentType:4, isReloadOnClose:false, width:340, height:90});
	pop.setContent("title", title);
	pop.setContent("alertCon", content);
	pop.build();
	pop.show();
}
function ShowHtmlString(title, strHtml){
	var pop=new Popup({ contentType:2,isReloadOnClose:false,width:340,height:300});
	pop.setContent("contentHtml",strHtml);
	pop.setContent("title",title);
	pop.build();
	pop.show();
}

//元素ID，最大宽度，最大高度 
function picHW(objID,_width,_height){
	var _obj = document.getElementById(objID);
	//获取图片原始大小 
	var imgheight = _obj.height;
	var imgwidth = _obj.width; 
	var hwval = imgwidth/imgheight;

	var x1 = 0;
	var x2 = 0;
	var x = 0;
	if(_width <= imgwidth){
		x1 = _width/imgwidth;
	}
	if(_height <= imgheight){
		x2 = _height/imgheight;
	}
	if(x1 == 0 && x2 == 0){//如果图片宽高都小于预设值，不做处理
		return;
	}
	if(x1 == 0 && x2 != 0){//高度大于预设高度，宽带小于或等于预设宽度，按高度缩放比例
		var _h = _height;
		var _w = Math.ceil(_h * hwval);
	}
	if(x1 != 0 && x2 == 0){//宽度大于预设宽度，高度小于或等于预设高度，按宽度度缩放比例
		var _w = _width;
		var _h = Math.ceil(_w/hwval);
	}
	if(x1 != 0 && x2 != 0){//按缩缩放比例最大的
		x = getMin(x1, x2);
		var _w = Math.ceil(imgwidth * x);
		var _h = Math.ceil(imgheight *x);
	}
	
	_obj.height = _h;
	_obj.width = _w;
}

function getMin(a, b){
	if(a < b) return a;
	return b;
}

function copyToClipBoard(content){
	var clipBoardContent = ""; 
	clipBoardContent += content;
	window.clipboardData.setData('Text', clipBoardContent); 
	showAlert("复制成功，请粘贴到你的QQ/MSN上推荐给你的好友");
}


//登录弹框
function login_popup(){
	 var strHtml ='<div class="lgbox" style="font-size: 12px;color: #404040;width: 340px;height: 200px;margin-top: 50px;margin-left: 80px;"> <div class="lg_bd" style="background-color: #F7F7F7;border: 1px solid #ccc;padding: 10px;"> <div class="lg_msg" style="margin: 8px 0;text-align: center;width: auto;color:red;" id="pop_msg">您还没有登录，登录之后才可进行此操作</div> <form id="lg_tkform" action="'+_mturl+'/register/login.php?refer='+document.location.href+'" method="post"  onSubmit="return chkPopLogin();"> <div class="lg_field" style="margin: 20px 0 0 10px;"> <label>用户名:</label> <input class="lg_text" type="text" name="uname" id="pop_uname" size="32" tabindex="1" style="border:1px solid #A7A6AA;height: 18px;padding: 2px 2px 1px;width: 160px;"/> </div> <div class="lg_field" style="margin: 20px 0 0 10px;"> <label>密&nbsp;&nbsp;码:</label> <input class="lg_text" type="password" name="password" id="pop_pwd" size="32" tabindex="2" style="border:1px solid #A7A6AA;height: 18px;padding: 2px 2px 1px;width: 160px;"/> </div> <div class="lg_btn" style="margin: 20px 0 0 65px;"> <span class="lg_submit"> <button type="submit" tabindex="3">登&nbsp;&nbsp;&nbsp;录</button> </span> <span class="lg_register"> <button type="button" onclick="window.location.href=\''+_mturl+'/register\'" tabindex="3" >注&nbsp;&nbsp;&nbsp;册</button> </span> </div> </form> </div> </div>';
	 
	var pop=new Popup({ contentType:2,isReloadOnClose:false,width:500,height:300});
	pop.setContent("contentHtml", strHtml);
	pop.setContent("title", _alertTitle);
	pop.build();
	pop.show();
}

function chkPopLogin(){
	var popmsg = document.getElementById('pop_msg');
	var uname = document.getElementById('pop_uname').value;
	var upwd = document.getElementById('pop_pwd').value;
	if(uname == '' || upwd == ''){
		popmsg.innerText = '用户名或密码不能为空';
		return false;
	}
	return true;
}

function toMyBlog(){
	//检查用户是否登录
	var ajax = new classAjax();
	ajax.setRequest(_mturl+'/blog/ajaxCall.php', 'ac=tomyblog&refer='+document.location.href, toMyBlog2);
}
function toMyBlog2(data){
	var json = eval('['+data+']');
	var flag = json[0]['data'];
	var url = json[0]['url'];
	
	if(flag == 0){ //未登录
		login_popup();
		return ;
	}
	
	if(flag == 1 || flag == 2){ //未开通或已经开通
		window.location.href = url;
		return ;
	}
}

function I(a, b){
	if (!a) return;
	a.innerHTML = (b) ? b : '';
}