//
// JavaScript string class extention...
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.isEMAIL = function() {
	var myPatrn = /^[A-Za-z0-9]+([._\-\+]*[A-Za-z0-9]+)*@([A-Za-z0-9]+[-A-Za-z0-9]*[A-Za-z0-9]+\.)+[A-Za-z0-9]+$/;
	return myPatrn.test(this);
}
function glCutString(_text,_len) {
	var nTheLength = 0;
	for(var nIndex = 0; nIndex < _text.length; ++nIndex) {
		if( _text.charCodeAt(nIndex) > 255 )	// Chinese
			nTheLength += 2;
		else
			nTheLength += 1;					// ASIIC
		if( nTheLength >= _len )	
			break;
	}
	var strResult = _text.substr(0, nIndex);
	if( strResult.length < _text.length )
		strResult = strResult + "...";
	return strResult;
}


/** 弹出登录遮盖层**/
function createIframe(){ 
	//mask遮罩层 
	var newMask=document.createElement("div"); 
	newMask.id="mDiv"; 
	newMask.style.position="absolute"; 
	newMask.style.zIndex="1"; 
	_scrollWidth=Math.max(document.body.scrollWidth,document.documentElement.scrollWidth); 
	_scrollHeight=Math.max(document.body.scrollHeight,document.documentElement.scrollHeight); 
	newMask.style.width=_scrollWidth+"px"; 
	newMask.style.height=_scrollHeight+"px"; 
	newMask.style.top="0px"; 
	newMask.style.left="0px"; 
	newMask.style.background="#33393C"; 
	newMask.style.filter="alpha(opacity=40)"; 
	newMask.style.opacity="0.40"; 
	newMask.style.display='none'; 
	var objDiv=document.createElement("DIV"); 
	objDiv.id="div1"; 
	objDiv.name="div1"; 
	objDiv.style.width="300px"; 
	objDiv.style.height="200px"; 
	objDiv.style.left=(_scrollWidth-480)/2+"px"; 
	objDiv.style.top="200px"; 
	objDiv.style.position="absolute"; 
	objDiv.style.zIndex="2"; //加了这个语句让objDiv浮在newMask之上 
	objDiv.style.display="none"; //让objDiv预先隐藏 
	objDiv.innerHTML=' <div id="drag" style="position:absolute;height:20px;width:100%;z-index:10001;top:0; background-color:#000000;cursor:move ;" align="right"> </div>'; 
	//更改了X按钮为触发关闭事件。 
	objDiv.style.border="solid #000000 3px;"; 
	var frm=document.createElement("iframe"); 
	frm.id="ifrm"; 
	frm.name="ifrm"; 
	frm.style.position="absolute"; 
	frm.style.width="100%"; 
	frm.style.height=180; 
	frm.style.top=20; 
	frm.style.display=''; 
	frm.frameborder=0; 
	objDiv.appendChild(frm); 
	document.body.appendChild(newMask); 
	document.body.appendChild(objDiv); 
	var objDrag=document.getElementById("drag"); 
	var drag=false; 
	var dragX=0; 
	var dragY=0; 
	objDrag.attachEvent("onmousedown",startDrag); 
	function startDrag(){ 
		if(event.button==1&&event.srcElement.tagName.toUpperCase()=="DIV"){ 
			objDrag.setCapture(); 
			objDrag.style.background="#adb0b1"; 
			drag=true; 
			dragX=event.clientX; 
			dragY=event.clientY; 
		} 
	}; 
	objDrag.attachEvent("onmousemove",Drag); 
	function Drag(){ 
		if(drag){ 
			var oldwin=objDrag.parentNode; 
			oldwin.style.left=oldwin.offsetLeft+event.clientX-dragX; 
			oldwin.style.top=oldwin.offsetTop+event.clientY-dragY; 
			oldwin.style.left=event.clientX-100; 
			oldwin.style.top=event.clientY-10; 
			dragX=event.clientX; 
			dragY=event.clientY; 
		} 
	}; 
	objDrag.attachEvent("onmouseup",stopDrag); 
	function stopDrag(){ 
		objDrag.style.background="#000000"; 
		objDrag.releaseCapture(); 
		drag=false; 
	}; 
} 
function htmlEditor(){ 
	createIframe()
	var frm=document.getElementById("ifrm"); 
	var objDiv=document.getElementById("div1"); 
	var mDiv=document.getElementById("mDiv"); 
	mDiv.style.display=''; 
	var iframeTextContent=''; 
	
	
	iframeTextContent+='<link href="root/css/other.css" rel="stylesheet" type="text/css" />';
	iframeTextContent+='<div >';
  	iframeTextContent+='<div  align="center">'
   	iframeTextContent+='<div >'
    
    iframeTextContent+='<ul class="Regis_shuxing" style="width:230px;margin-left:20px">'
    iframeTextContent+='<div id="err_code"></div>'
    iframeTextContent+='<li>帐&nbsp;&nbsp;&nbsp;&nbsp;号:&nbsp;'
    iframeTextContent+='<input type="text" name="username" id="username" style="width:150px" />'
    iframeTextContent+="</li>"
    iframeTextContent+="<li>密&nbsp;&nbsp;&nbsp;&nbsp;码:&nbsp;"
    iframeTextContent+='<input type="password" name="password" id="password" style="width:150px"  />'
    iframeTextContent+='</li>'
    iframeTextContent+='</ul>'
    iframeTextContent+='<div  style="margin-top:10px;width:200px;margin-left:10px;"  >'
    iframeTextContent+='<div class="Regis_button"><a href="#" id="btGo"  style="font-size:12px">确 定</a></div>'
    iframeTextContent+='<div class="Regis_button"><a href="#" id="btCannel" style="font-size:12px">取 消</a></div>'
    iframeTextContent+='</div>'
    iframeTextContent+='<div class="Regis_line" style="width:250px; margin-top:0px;margin-bottom:0px"></div>'
    iframeTextContent+='</div>'
  	iframeTextContent+='</div>'
	iframeTextContent+='</div>'
	frm.contentWindow.document.designMode='off'; 
	frm.contentWindow.document.open(); 
	frm.contentWindow.document.write(iframeTextContent); 
	frm.contentWindow.document.close(); 
	objDiv.style.display = ""; //显示浮动的div 
	frm.contentWindow.document.getElementById("username").focus();
	//取消
	var objCannel = frm.contentWindow.document.getElementById("btCannel"); 
	objCannel.attachEvent("onclick",function (){ 
	HideIframe(mDiv,objDiv); 
	});
	//登录
	var objGo=frm.contentWindow.document.getElementById("btGo"); 
	objGo.attachEvent("onclick",function (){ 
	onLogin(frm.contentWindow)
	}); 
} 

function HideIframe(mDiv,objDiv){ 
	mDiv.style.display='none'; 
	objDiv.style.display = "none"; //隐藏浮动的div 
} 

/** 弹出聊天窗口**/
function newswin(url){
    window.open(url,"chat"," height=490 width=608,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,left=0,top=0,fullscreen=no,resizable=no");
     return false;
}
/**获取网页当url实现收藏功能**/
function SouChangeUrl(_type){
	var clipBoardContent=""; 
	clipBoardContent=this.location.href; 
	window.external.AddFavorite(clipBoardContent, _type)
}
/** 搜索处理函数 **/
function SearchVlaue(_this){
	_this.value = '';
}
function Search(_type,_key){
	if(_type=='s'){
		var key = $F('ssKey');
	}else{
		if (_type=='tag'){
			var key = _key;
		}else{
			var key = $F('sKey');	
		}
	}
	if(key.length < 1){
		return;
	}
	
	var strURL ='index.php?controller=search&action=index&key=' + encodeURI(key) + '&type=' + _type ;
	window.location = strURL;
	return false;
}
/** 搜索函数结束 **/
var g_root = null;				// 全局对象

function onLogin(_div) {
	g_root = new GMCommon();
	g_root.doLogin(_div);
}
function unlogin(){
	g_root = new GMCommon();
	g_root.unLogin();
}
function onVodPage(login,class_id,page){
	g_root = new GMCommon();
	g_root.doVodPage(login,class_id,page);
}

var GMCommon = Class.create();
GMCommon.prototype = {
	initialize: function() {
		this.m_AjaxType = "";
		this.m_AjaxObject = null;
		this.m_UrlBase = "index.php?controller=Default";
		this.m_Div = "";
		this.doInitLogin();
	},
	AbortAjax: function() {
		if( this.m_AjaxObject != null ) {
			var theTrans = this.m_AjaxObject.transport;
			theTrans.abort(); theTrans = null;
		}
	},
	SendAjaxRequest: function(_url) {
		var theComplete = this.DoAjaxResponse.bindAsEventListener(this);
		var theCmd = ((arguments[1] != null) ? arguments[1] : 'GET');
		var theBody = ((arguments[2] != null) ? arguments[2] : null);
		var theOptions = {
      		method:     theCmd,
      		postBody:	theBody,
			onComplete: theComplete
		};
		delete this.m_AjaxObject;
		this.m_AjaxObject = new Ajax.Request(_url,theOptions);
	},
	DoAjaxResponse: function(_response) {
		if( _response.responseText.length <= 0 || _response.readyState != 4 ) {
			// alert('-- Ajax none response-text --');
			return;
		}
		switch(this.m_AjaxType) {
			case 'Login':			this.doAjaxLogin(_response);	       break;
			case 'unLogin':			this.doAjaxunLogin(_response);	       break;
			case 'VodPage':         this.doAjaxVodPage(_response);         break;
			case 'GetDateVoid':     this.doAjaxGetDateVoid(_response);     break;
			default:  				alert('-- unknown error --');          break;
		}
	},
	doInitLogin: function() {
		if( typeof(userName) != 'object' || typeof(passWord) != 'object' )
			return;
		userName.onkeydown = this.doLoginCheck.bindAsEventListener(this);
		passWord.onkeydown = this.doLoginCheck.bindAsEventListener(this);
	},
	doLoginCheck: function() {
		if( event.keyCode != 13 )
			return;
		this.doLogin();
	},
	doLogin: function(_div) {
		if (_div){
			userName = _div.document.getElementById("username").value.trim();
			passWord = _div.document.getElementById("password").value.trim();
		}else{
			userName = username.value.trim();
			passWord = password.value.trim();
		}
		
		
		if( userName.length <= 0 && passWord.length <= 0 ) {
			alert('用户名不能空');
			return;
		}
		if( userName.length <= 0 || passWord.length <= 0 ) {
			alert('帐号或密码不能为空，请重新输入！');
			username.focus();
			return;
		}
		var theUrl = "index.php?controller=Login&action=Login&username=" + userName + "&password=" + passWord;
		this.m_AjaxType = "Login";
		this.m_Div = _div;
		this.SendAjaxRequest(theUrl);
	},
	doAjaxLogin: function(_response) {
		var myJson = _response.responseText.evalJSON(true);
		if( myJson.err_code != 0 ) {
			if (this.m_Div){
				this.m_Div.document.getElementById("err_code").style.display = '';
				this.m_Div.document.getElementById("err_code").innerHTML= "<font color=red>"+myJson.err_text+"</font>";
				this.m_Div.document.getElementById("username").value="";
				this.m_Div.document.getElementById("password").value="";
				this.m_Div.document.getElementById("username").focus();
			}else{
				errorname.innerHTML= "<font color=red>"+myJson.err_text+"</font>";
				username.value="";
				password.value="";
				username.focus();

			}
			return;
		}
		if (this.m_Div){
			window.location.href='index.php';
		}else{
			uInfo.style.display='none';
			login.innerHTML = myJson.entity;
		}
	},
	doVodPage: function(login,class_id,page){
		var theUrl = "index.php?controller=board&action=ClassName&type="+login+"&class_id="+class_id+"&page="+page;
		this.m_AjaxType = "VodPage";
		this.SendAjaxRequest(theUrl);
	},
	doAjaxVodPage: function(_response) {
		content.innerHTML = _response.responseText;
	},
	unLogin: function() {
		var theUrl = "index.php?controller=Login&action=Logoff";
		this.m_AjaxType = "unLogin";
		this.SendAjaxRequest(theUrl);
	},
	doAjaxunLogin: function(_response) {
		if (document.getElementById("uLivInfo")){
			window.location.href="index.php?controller=LiveCast"
		}else{
			var content = '<DIV class=gui id=uInfo >'
				content += '<STRONG class=first><A 	href="#" onClick="htmlEditor()">登录</A></STRONG>'
				content += '<STRONG><A title=注册 	href="index.php?controller=Register&action=index">注册</A></STRONG> '
				content += '</DIV>'
			$('LogInfo').innerHTML = content
				$('login').innerHTML = _response.responseText;//全功能和点播功能网页的登录
		}
		if (document.getElementById("playDemand")){
			playDemand.innerHTML = "<img src='root/images/noplay.gif'>"
		}
	}
}
