//var isIE = $.browser.msie;
var original_display_mode = 0;  
var anchor_string = window.document.URL;
var firstTime=true;
var msg=window.document.title; 
var delay=300;
var open_=false;
var login_action_record = -1;
var gwk = new Array(["start2.do?"],["index_cj.do?"],["index_pm.do?"],["his.do?"],["info2.do?"],["top_ten.do?"]);
var player_update_switch=1;  
var player_setup_switch=0;   
var leave_bbs_last_url="/bbs/bbs/cityV3.do?city_id=1";
var init_mode_cx=0;

var syncheck = 1; 
var curr_city=-1; 

var test_cx;

var fixed_iframe_height = 0;

function setSyncheck(value){
	syncheck=value;
	if(syncheck>0 && curr_city!=syncheck){
		goto_city_bbs(curr_city);
	}
}

function getSyncheck(){
	return syncheck;
}

function jump_city(city_id,uuid){
	if(syncheck>0){
		//goto_city_bbs(city_id,uuid);
		city_bbs(city_id,uuid);
	}
	curr_city=city_id;
}

function goto_anchor(anchor_name){
   goto_("#"+anchor_name);
   if($.browser.mozilla){
       scrollTo(0,window._content.document);
   } else {
       window.scrollTo(0);
   }
}

function copyToClipBoard(title,id)
{
   if(!$.browser.msie){
      return ;
   }
   var text=window.parent.document.URL+" [ft="+id+"]"+title+"[/ft]";
   window.clipboardData.setData("Text",text);
   alert("复制成功!");
}

function getIFrameDocument(id) {
	var iframe = window.document.getElementById(id);
	if (!iframe) {
		return null;
	}
	var doc = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document;
	return doc;
}

function resetFrameSize() {
	try{
		var oBody = getIFrameDocument("bbsiframe").body;
	       var height_t = oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight);
           var height_now = $("#bbsiframe").height();
           if(height_now>0&&height_t!=0&&height_now!=height_t){
                $("#bbsiframe").height(height_t);
           }
	}catch(e){
	}
}

function flash_window_display() {
	flash_window_set(3);
}

var minModel=false;
function flash_window_set(model){
	/*if((model==3 ||model==1) && original_display_mode==0){
		$("#bbsiframe").height(0);
	    flash.height("100%");
	    change_mode();
	}else if((model==3||model==2) && original_display_mode==1){
	    flash.height(450);
		var oBody = getIFrameDocument("bbsiframe").body;
	    var height_t = oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight);
		$("#bbsiframe").height(height_t);
		change_mode();
	}else if(model==4){
		if(minModel){
			original_display_mode=original_display_mode==1?0:1;
			minModel=false;
			flash_window_set(3);
		}else{
			flash.height(50);
			var oBody = getIFrameDocument("bbsiframe").body;
	   	 	var height_t = oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight);
			$("#bbsiframe").height(height_t);
			minModel=true;
		}
	}*/
}

function change_mode(){
	if(original_display_mode){
		original_display_mode=0;
		if (isIE) {
		    window.document.body.style.overflowY="scroll";
		    window.document.body.style.overflowX="hidden";
		}
	} else {
		original_display_mode=1;
		if (isIE) {
			window.document.body.style.overflow="hidden";
		}
	}
}

function goto_(url){
   window.location.replace(url);
}



function user_login(uuid , action , city){
    var bbsiframe = document.getElementById("bbsiframe");
    $("#bbsiframe").height(10);
    if(action==0){
         login_action_record=0;
         return;
    }
    if(action==1){
    	login_action_record=1;
    	goto_city_bbs_login(city,uuid,0,action);
    	return;
    }
    if(action == 2){
    	login_action_record=2;
    	goto_city_bbs_login(city,uuid,0,action);
    	return;
    }
    if(action ==3){
    	if(action==3){
		var frame_url=window.frames[0].document.URL;
		var fa=frame_url.split("&uuid=");
		var url=fa[0]+"&uuid="+uuid;
		if(fa.length>1 && fa[1].indexOf("&")>0){
			var ifa=fa[1].split("&");
			if(ifa.length>1){
				url=url+"&"+ifa[1];
			}
		}
		$("#bbsiframe").attr("src",url);
		return;
	    }
    }
}

function refresh_page(){
    var url = window.location.href;
    goto_(url);
}

function changeScene(id) {
	window.location.hash = id;
}

function tha_setHash(){
	document.title = 'fotologue';
	document.currentHref =location.href;
}

function flash_tell_goto_thread(thread_id,uuid,city_id){
//	setCookie('uuid',uuid,-1, '/', '.this6.com');
	window.open("http://m.this6.com/"+thread_id+"_1.html");
}

function move_clear(){
   move_real_count = 0 ;
   move_stop_count = 0 ;
}

function dynamic_ifream_height(){
	resetFrameSize();
    setTimeout("dynamic_ifream_height();",2000);
}

function set_leave_bbs_last_url(url){
    this.leave_bbs_last_url=url; 
}

function get_leave_bbs_last_url(){
    return this.leave_bbs_last_url;
}

function return_bbs(){
    $("#bbsiframe").attr("src",get_leave_bbs_last_url());
}

function goto_agreement_file(){
    $("#bbsiframe").attr("src","/bbs/agreement.html");
}

function goto_work_manager(system_id){
   if(system_id<1){
      return;
   }
   var fream = $("#bbsiframe");
   var src_ = fream.attr("src");
   if(src_.indexOf("/bbs/workmanager/")<0){
   		set_leave_bbs_last_url(src_);
   }
   var date = new Date();
   $("#bbsiframe").attr("src","/bbs/workmanager/index.do?system_id="+system_id+"&time="+date.getTime());
}

function goto_city_bbs(city,uuid){
    city_bbs(city,uuid);
    
    if(original_display_mode==1){
      flash_window_display();
    }
}


function city_bbs(city,uuid) {
	var date = new Date();
	if(uuid==null){
		$("#bbsiframe").attr("src","/bbs/bbs/cityV3.do?city_id="+city+"&time="+date.getTime());
	}else{
		$("#bbsiframe").attr("src","/bbs/bbs/cityV3.do?city_id="+city+"&uuid="+uuid+"&time="+date.getTime());
	}
}

function go_curr_city_bbs(){
	var city=get_curr_url("city")
	if(city){
		city_bbs(city);
	}	
}

function goto_city_bbs_login(city,uuid,jump,action){
	var thread = get_curr_url("thread");
	var cityhash = get_curr_url("city");
	var user = ger_curr_url("user");
	if(thread>0){
	}else if (cityhash>0){
	}else if(user>=0){
	}else{
		if(jump==1){
		    curr_city=city;
	        if(syncheck>0){
		       city_bbs(city,uuid);
	        }
		}
	}
	if(original_display_mode==1){
      flash_window_display();
    }
}

function getKeyNumber(key){
	if (key=="scene"){
   	  return "s";
   } else if (key=="thread"){
      return "t";
   } else if (key=="city"){
   	 return "c";
   } else if (key=="user"){
   	 return "u"
   } else {
   	 return -1;
   }
}

function changeUrl(key,value){
   var number = getKeyNumber(key);
   if(number==-1){
   	  return;
   }
   if(value==null){
   	  value = "";
   }
  
   var url=window.location.hash;
   if(!url){
      window.location.hash = (number=="s"?"":number)+value;
      return ;
   }
   
   var has2 = "not";
   if(url.indexOf("t")>-1){
   	   has2="t";
   } else if (url.indexOf("c")>-1){
       has2="c";
   }else if (url.indexOf("u")>-1){
       has2="u";
   }
   
   if(has2=="not"){
   	  if(number=="s"){
   	  	window.location.hash=value;
   	  }else{
   	  	if(value!=""){
   	  		window.location.hash=url+number+value;
   	  	}
   	  }
   	  return ;
   }
   
   if(number=="s"){
   	  var re=value;
   	  window.location.hash=re+has2+url.split(has2)[1];
   	  return;
   }
   
   if(has2==number){
   	  var re=url.split(has2)[0];
   	  if(value!=""){
   	  		re=re+number+value;
   	  }
   	  window.location.hash=re;
   	  return;
   }
   
   if(value==""){
   	  window.location.hash=url;
   	  return ;
   }

   var re=url.split(has2)[0];
   window.location.hash=re+number+value;
}

function get_curr_url(key){
  var url=window.location.hash;
  if(key==null){
  	var str = url;
  	return str;
  }
  var number = getKeyNumber(key);
  if(number==-1){
  	return ;
  }
  if(url!=null && url.length > 0){
      var value = null;
      if(number=="s"){
      	 if(url.indexOf("t")>-1){
      	 	value = url.split("t")[0];
      	 } else if (url.indexOf("c")>-1){
      	 	value = url.split("c")[0];
      	 } else if (url.indexOf("u")>-1){
      	 	value = url.split("u")[0];
      	 } else {
      	 	value = url;
      	 }
      }else{
      	 if(url.indexOf(number)>-1){
      	 	value=url.split(number)[1];
      	 }else{
      	 	value="";
      	 }
      }
      if(value.indexOf('#')==0){
      	 return value.substr(1,value.length-1);
      }
      return value;
   }else{
      return null;
   }
}

function deleteMark(key){
   changeUrl(key,null);
}

function refresh_index(){
	var url = window.document.URL.split("#")[0];
	window.location.replace(url);
}

function coop_type(){
	var url = window.document.URL.split("#")[0];
	if(url.indexOf("jdz.this6.com")>=0) {
		return 3;
	}
	return 0;
}

var coop_this6 = new Array(["http://www.this6.com/bbs/newpage/register.do"],["http://www.this6.com"],["http://www.this6.com"],["http://m.this6.com"],["http://www.this6.com/bbs/pay/index.html"],["http://www.this6.com/bbs/c/user_info.do"],["http://www.this6.com/bbs/c/user_info.do"]);
// type 1 注册 2 登录 3 退出 4 论坛 5 充值 6 个人资料 7 防沉迷
function coop_register(coop_type,type,server_id,uuid){
	if(type==3) { // logout
		removeCookie('username', '/', '.this6.com');
		removeCookie('uuid', '/', '.this6.com');
		removeCookie('save_password', '/', '.this6.com');
		removeCookie('auto_login', '/', '.this6.com');
	}

	var url;
	if(coop_type==14) {
		url = "/bbs/coop/sc_jump.do?type="+type+"&uuid="+uuid;
	} else {
		url = coop_this6[type-1];
	}

	if (type==4||type==5||type==6||type==7) {
		window.open(url);
	} else {
		window.location.replace(url);
	}
}

function removeCookie(cookieName, path, domain) {
	document.cookie = escape(cookieName) + '=' + '; expires=-1' + '; path=' + (path ? path : '/') + (domain ? '; domain=' + domain : '');
}

function setCookie(cookieName, cookieValue, micseconds, path, domain, secure) { 
     var expires = new Date(); 
     expires.setTime(expires.getTime() + micseconds); 
     document.cookie = escape(cookieName) + '=' + escape(cookieValue) + (expires ? '; expires=' + expires.toGMTString() : '') + '; path=' +(path ?  path : '/') + (domain ? '; domain=' + domain : '') + (secure ? '; secure' : ''); 
}

function getCookieValue(key){
	var cookie_start = document.cookie.indexOf(key+"="); 
    var cookie_end = document.cookie.indexOf(";", cookie_start);
    var value = cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length))); 
	return value==''?value : value.split("=")[1];
}

var video_file_path  ;

function video_post(video_name){
	var array_s = new String(video_name);
	var array = array_s.split(",");
	var content=" ";
	for(var i=0 ; i<array.length ; i++){
		if(array[i].length>0){
			content=content+"[ktv]"+array[i]+"[/ktv]";
		}
	}
	try{
		var url = window.frames[0].creat_thread(content);
		$("#bbsiframe").attr("src", "/bbs/bbs/"+url);
		if(original_display_mode==1){
            flash_window_display();
        }
	}catch(exce){
		return ;
	}
}

function video_download(video_name){
	var html_content="<a href='"+video_name+"' target=''><div id='video_download_' name='video_download_'></div></a>"
	$("#video").html(html_content);
	$("#video_download_").click();
}

function test(){
	var yy = new Number;
	var time = new Date();
	time.getSeconds()
	time.setTime(time.getTime()+3600000);
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function get_uuid() {
	var uuid = getCookieValue('uuid');
	if(!uuid) {
		uuid = readCookie('uuid');
	}
	return uuid;
}

function index_page() {
	window.location.replace(coop_this6[2]); // the same as logout
}

function save_password(username,save_password,auto_login) {
	if(username&&save_password) {
		var ms = 100*24*60*60*1000;
		setCookie('username',username,ms, '/', '.this6.com');
		setCookie('save_password',save_password,ms, '/', '.this6.com');
		if(auto_login) {
			setCookie('auto_login','1',ms, '/', '.this6.com');
		}
	}
}

function get_save_password() {
	return new Array(getCookieValue('username'),getCookieValue('save_password'));
}

/* invoke flash */
function flash_user_info(user_id,fromOutPage){
	flash.focus();
	window.document.this6index.display_user_info(user_id);
	if(fromOutPage==null){
		beDoo();
	}
}
function user_info(user_id){
	flash_user_info(user_id);
}
function go_scene(type,scene,fromOutPage) {
	flash.focus();
	window.document.this6index.go_scene(type,scene);
	if(fromOutPage==null){
		setTimeout("beDoo()",500);
	}
}
function go_user_local(user_id){
	flash.focus();
	window.document.this6index.go_user_local(user_id);
	setTimeout("beDoo()",500);
}
function user_edit(text){
	flash.focus();
	window.document.this6index.user_edit(text);
	setTimeout("beDoo()",500);
}
function flash_open_rank_paihang(type) {
	flash.focus();
	window.document.this6index.open_rank_paihang(type);
}

function flash_open_fight_paihang(type) {
	flash.focus();
	window.document.this6index.open_fight_paihang(type);
}
function display_vote(vote_id) {
	flash.focus();
	window.document.this6index.display_vote(vote_id);
}

function go_user_zone(id,uuid){
	return;
}

var save_game_before_url;
function workshop_game(id, uuid, npc_id){
	open_workshop(id,npc_id);
	/*
	var curr_url=window.frames[0].location.href;
	if (!npc_id) {
		npc_id = 0;
	}
	if(curr_url.indexOf("/bbs/workshop.do?")==-1){
		save_game_before_url=curr_url;
	}
	$("#bbsiframe").attr("src", "/bbs/bbs/workshop.do?id=" + id + "&npc_id=" + npc_id);
	if(original_display_mode==1){
            flash_window_display();
    }*/
}

function exite_workshop_game(){
	if(original_display_mode==0){
         flash_window_display();
    }
    $("#bbsiframe").attr("src", save_game_before_url);
}

function flash_add_friend(user_id,nickname){
	flash.focus();
	window.document.this6index.add_friend(user_id,nickname);
}

function jump_scene(scene_id , scene_name){
	changeUrl("scene",scene_id);
	title_text=scene_name;
	window.document.title="☀ "+scene_name+" ☀ 第6大陆 ☀";
}

var do_count=0;
var do_lock=false;
function beDoo(count){
	if((count!=1)&&(do_lock)){
		return;
	}
	if(open_||(do_count==20)){
		do_lock=false;
		do_count=0;
		return;
	}
	do_lock=true;
	var currFalg=window.document.title;
	if((do_count%2)==0){
		window.document.title="　　　"+currFalg;
	}else if((do_count%2)==1){
		window.document.title=currFalg.substring(3,currFalg.length);
	}
	do_count++;
	setTimeout("beDoo(1)",180);
}

function receive_message(action , nickname ){
   if(action == 0){
     	msg = "☜ 收到 ["+nickname+"] 的消息！ ";
        if(!open_){
        	open_=true;
            setTimeout("scrollTitle()",200);
        }
   }else{
   		open_=false;
   		setTimeout("scrollTitle()",200);
   		window.document.title="☀ "+title_text+" ☀ 第6大陆 ☀";
   }
}

function scrollTitle() {
  if(!open_){
     return;
  }
  window.document.title=msg; 
  msg=msg.substring(1,msg.length)+msg.substring(0,1,1); 
  setTimeout("scrollTitle()",delay); 
} 


function addfavorite()
{
   if (document.all)
   {
      window.external.AddFavorite('http://www.this6.com','☀ 创造世界 享受生活 ☀ 第6大陆 ☀'); 
   }
   else if (window.sidebar)
   {
      window.sidebar.addPanel('☀ 创造世界 享受生活 ☀ 第6大陆 ☀', 'http://www.this6.com',  "");
   }
} 

var flash;
var title_text="创造世界 享受生活";
function bbs_index_function(){
	if(isIE){
	      flash = $("object[@name='this6index']");
	}else{
	      flash = $("embed[@name='this6index']");
	}
	
	flash.focus(function() { 
	  window.document.title="☀ "+title_text+" ☀ 第6大陆 ☀";
	});

    /*var thread = get_curr_url("thread"); 
	var scene = get_curr_url("scene");
	var user = get_curr_url("user");
	var city = get_curr_url("city");
	if(thread>0 && !scene>0){
        $("#bbsiframe").attr("src", "/bbs/bbs/thread.do?id="+thread);
	}else if(user>=2000000 && !scene>0){
		$.post("/bbs/getUserHomeSceneId.do", { user_id: user },
            function(data){
              if(data>0){
              	  flash_go_scene(2,data);
                  $("#bbsiframe").attr("src", "/bbs/bbs/user.do?id="+user);
              }
            } 
        ); 
	}*/
	//dynamic_ifream_height();

	$("#bbsiframe").load(function() {
		try{
			resetFrameSize();
		    if($.browser.msie){
                window.frames[0].last_load_img_flash();
			}  
		}catch(exception){
		    return ;
		}finally{
		    return ;
		}
	});
}

function open_user_figure(id){
	if(id>=2000000){
		window.open("/bbs/figure.do?id="+id, '_blank', 'height=310, width=410, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
	}
}

function open_url(url) {
	window.open(url);
}
function open_bbs(uuid, server_id) {
	if(uuid) {
		//setCookie('uuid',uuid,-1, '/', '.this6.com');
	}
	open_url('http://m.this6.com');
}
function open_workshop(id, npc_id) {
	if (!npc_id) {		npc_id = 0;	}
	open_url("/bbs/bbs/workshop.do?id=" + id + "&npc_id=" + npc_id);
}

function cancelDefault(event)
{
	if(event.preventDefault){
		event.preventDefault();
	}else{
		event.returnValue=false;
	}
}

function not_protect(hash) {
	window.location.replace('/bbs/newpage/not_protect.do?hash='+hash);
}

function cancelBubble(e)
{   	
    if(e.stopPropagation) e.stopPropagation(); 
    else e.cancelBubble=true;
}

function user3Dmodel(uuid){
	//window.open("/modeldiy/user3Dmodel.html?uuid="+uuid, '_blank', 'height=800, width=800, top=100%, left=100%, toolbar=no, menubar=no, scrollbars=yes, location=no, status=no');
}

function OpenNewWin(url)
{
    window.open(url,"_blank");
}

function go_top(){
	flash.focus();
}

function wordcount(tagid,minval,maxval){
    var count=window.document.getElementById(tagid).value.length;
    var show="当前字符数为"+count;
    if(minval!=null && minval>count){
    	show=show+" 您至少还需要输入"+(minval-count)+"个字符";
    }else if(maxval!=null){
    	if(maxval>=count){
    		show=show+" 您还可以输入"+(maxval-count)+"个字符";
    	}else{
    		show=show+" 最多只能输入"+(maxval)+"个字符";
    	}
    }
    alert(show);
}

function wordclear(tagid){
	var count=window.document.getElementById(tagid).value="";
}

function linkPost(url,inputs){
	var f = document.createElement("form");
	if(inputs){
		var kvs=inputs.split(";");
		var count=kvs.length;
		for(var i=0;i<count;i++){
			var kv=kvs[i].split("=");
			if(kv.length==2){
				var i = document.createElement("input");
				i.type="hidden";
				f.appendChild(i);
				i.name = kv[0];
				i.value = kv[1];
			}
		}
	}
	f.action = url;
	f.method = "POST";
	f.style.display = 'none';
	document.body.appendChild(f);
	f.submit();
}

function user_info(uuid,type,server_id) {
	//setCookie('uuid',uuid,-1, '/', '.this6.com');
	if(server_id==200){
			window.open('/bbs/c/user_info.do');
	}else{
			window.open('http://www.this6.com/bbs/c/user_info.do');
	}
}
function coop_anti_indulge(uuid,coop_type,server_id) {
	if(coop_type==0||coop_type==8||coop_type==10) {		
		if(server_id==200){
			window.open('/bbs/c/user_info.do');
		}else{
			window.open('http://www.this6.com/bbs/c/user_info.do');
		}
	}
}
