/**
 * 
 */

/**
 * 变量
 */
template_directory = '/content/themes/2010';
var domain = 'http://www.365m.cn';
var gameUrl = "http://zywy.365m.cn/client/index.html?s=";
var serversPage = domain + "/account/service/servers.php";
var loginPage = domain + "/account/service/jsmodal.php";

/**
 * 加载完后运行
 * @param {Object} u
 */
$(function(){
    showpics('header_flash_img'); //页头Flash
    ScrollImgLeft(); //跑马灯
    $('.conc ul:even').removeClass('list').addClass('list2'); //列表页用到的
    getdp('1'); //初始化侧边栏的玩家PK
    getdp('2'); //初始化侧边栏的玩家PK
    
    /**
     * 按下回车时登陆（按下登陆按钮）
     */
    $('#username,#password').keydown(function(event){
    	if(event.keyCode==13 && $('#username').val() && $('#password').val()) $('#login_submit').click();
    });
});



/**
 * 页头Flash
 */

function showpics(u) {
	// 图片新闻
	var focus_width=350
	var focus_height=220
	var text_height=0
	var swf_height = focus_height+text_height

	var pics = '';
	var links = '';
	var texts = '';
	
	$('#'+u+' li').each(function(i){
		if(pics != '')
		{
			pics = "|" + pics;
			links = "|" + links;
			texts = "|" + texts;
		}

		pics = escape($(this).children('a').children('img').attr('src')) + pics;
		links = escape($(this).children('a').attr('href')) + links;
		texts = escape($(this).children('a').attr('title')) + texts;
	 });
	 $('#'+u).after('<embed src="'+template_directory+'/images/pixviewer.swf" wmode="opaque" FlashVars="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'" menu="false" bgcolor="#000000" quality="high" width="'+ focus_width +'" height="'+ swf_height +'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"/>');

}

/**
 * 放到桌面
 */
function toDesktop(sUrl,sName){
  try
  {
    var WshShell = new ActiveXObject("WScript.Shell"); 
    var oUrlLink = WshShell.CreateShortcut(WshShell.SpecialFolders("Desktop") + "\\" + sName + ".url"); 
    oUrlLink.TargetPath = sUrl; 
    oUrlLink.Save();
  }
  catch(e) {
	  document.getElementById("downloadURL").src="/data/庄园物语.url";
    //alert("不是IE或当前IE安全级别不允许操作！");   
  }

} 

/**
 * 头部走马灯
 */
function ScrollImgLeft(){
    
    var speed=20;
    var scroll_begin = document.getElementById("scroll_begin");
    var scroll_end = document.getElementById("scroll_end");
    var scroll_div = document.getElementById("scroll_div");
    if (scroll_div.offsetWidth > scroll_begin.offsetWidth) { //如果长度不够，直接退出
        return;
    }
    scroll_end.innerHTML=scroll_begin.innerHTML;
    function Marquee(){
        if(scroll_end.offsetWidth-scroll_div.scrollLeft<=0) {
            scroll_div.scrollLeft-=scroll_begin.offsetWidth;
        } else {
            scroll_div.scrollLeft++;
            
        }
        
    }
    var MyMar=setInterval(Marquee,speed);
    scroll_div.onmouseover=function() {clearInterval(MyMar)};
    scroll_div.onmouseout=function() {MyMar=setInterval(Marquee,speed)};
}





/**
 * 玩家PK投票
 * @param {Object} id
 */
function voteP(id){
    $.ajax({
        type: "GET",
        url: template_directory+"/vote.php",
        data: "playerId="+id,
        success: function(data){
            if(data.indexOf("|") != -1) {
                var data_arr = data.split('|');
                $('#player'+data_arr[1]).html(data_arr[0]);
            }
            if(data =="") {
                alert("提示信息：你今天已经投过票了，感谢参与");
            }
        }
    });
}

/**
 * 玩家PK加载
 * @param {Object} id
 */
function getdp(id){
    $.ajax({
        type: "GET",
        url: template_directory+"/tvote.php",
        data: "playerId="+id,
        success: function(data){
            if(data.indexOf("|") != -1) {
                var data_arr = data.split('|');
                $('#player'+data_arr[1]).html(data_arr[0]);
            }
        }
    });
}

/**
 * 百度贴吧搜索
 * @return
 */
function baiduSearch() {
    var o = $('#baiduText').val();
    o = $.trim(o);
    if(!o) {$('#baiduText').focus(); return;}
    
    window.open('http://tieba.baidu.com/f?ct=352321536&tn=baiduPostSearch&rn=10&pn=0&lm=65536&cm=0&kw=&rs2=1&sc=&un=&rs1=&rs5=&sn=&rs6=&word='+o+'&tb=on&ie=utf-8');
}

/**
 * 百度知道提问
 * @return
 */
function baiduQuestion() {
    var o = $('#baiduText').val();
    o = $.trim(o);
    if(!o) {$('#baiduText').focus(); return;}
    
    window.open('http://zhidao.baidu.com/q?word='+o+'&ct=17&pn=0&tn=ikask&rn=10&cm=1&qf=3&lm=394496');
}
/**
 * 检查并设置登陆框
 */
function setLoginbox() {
	$.getJSON('/account/checklogin.php', function(json){
		if (json && json.statusID == 100) {
			$('#log_account').html(json.username);
			$('.log').hide();$('.log2').show();
		} else {
			$('.log').show();$('.log2').hide();
		}
	});
}
/**
 * 退出
 */
function logout(path, isHome) {
    var current = window.location.href;
    $.ajax({
        type: "GET",
        data: '',
        url: path+"logout.php",
        async : false,
        success: function(response){
            var num = parseInt(response, 10);
            if(num == 100) {
                /*if(!isHome) {
                    window.location.href = current;
                }else{
                    statusPanel(); 
                } */
               $('.log').show();$('.log2').hide();
            }
        }
    });
}
/**
 * 登陆
 */
active = true;
function login(path, serverId) {
    gpath = path;
    var username = $('#username').val();
    var password = $('#password').val();
    username = $.trim(username);
    password = $.trim(password);

    if(!username)
    {
        alert("请输入邮箱或庄园号...");
        //$('#username').focus();
        //$('#username').css('background', 'url('+path+'images/invalid_line.gif) repeat-x scroll center bottom #FFFFFF');
        return false;
    }
    $('#username').css('background', '');
    if(!password)
    {
        alert("请输入密码...");
        //$('#password').focus();
        //$('#password').css('background', 'url('+path+'images/invalid_line.gif) repeat-x scroll center bottom #FFFFFF');
        return false;
    }
    $('#password').css('background', '');
    if(!active){ 
       alert("正在进行中，请稍候..."); 
       return false;
    }
    
    active = false; 
    $.ajax({
        type: "POST",
        url: path+"login.php",
        async : false,
        data: "username="+username+"&password="+password+"&serverId="+serverId,
        success: function(response){
            active = true; // activate it again !
            if(response.indexOf('|') != -1) {
                /*var data = response.split('|');
                var num = parseInt(data[0], 10);
                    var params = '';
                if(num == 100 && data.length == 3){
                    var s = parseInt(data[1], 10);
                    if(s == 0){
                        window.location.href = serversPage;
                    }else{
                        params += s +"&stype="+data[2];
                        window.location.href = gameUrl + params;
                    }
                    
                }*/
            	$('#password').val('');
                $('#log_account').html(username);
                $('.log').hide();
                $('.log2').show();
                window.location.href = serversPage;
                //window.location.reload();
                //setTimeout(function(){window.location.href = serversPage;}, 1000);
            }else {
                if(response.indexOf('验证失败') != -1) {
                    //maintain('登录名或密码不正确');
                    alert("登录名或密码不正确,请重新输入");
                }else {
                    errorMsg(response);
                }
            }
        }
    });
     return true;
}
/*
 * 
 *can't delete
 **/
status = true;
function play(path) {
    gpath = path;
    if(!status) return;
    $.ajax({
        type: "GET",
        url: path + "play.php",
        success: function(response){
            if(response.indexOf('|') != -1) {
                var data = response.split('|');
                var params = '';
                var num = parseInt(data[0], 10);
                if (num == 100 && data.length == 3) {
                    window.location.href = serversPage;
                }
            }else{
                errorMsg(response);
            }
        }
    });
}


/*
 * 进入选择的服务器
 * enter selected server 
 **/
function playIt(path, serverId) {
    gpath = path;
    //if(!status) return;
    $.ajax({
        type: "GET",
        data: "serverId="+serverId,
        url: path+"play.php",
        success: function(response){
            
            if(response.indexOf('|') != -1) {
                var data = response.split('|');
                var num = parseInt(data[0], 10);
                if(num == 100 && data.length == 3){
                    window.location.href = gameUrl + data[1] + "&stype="+ data[2];
                }else if(num == 103 && data.length == 3) {
                    window.location.href = loginPage;
                }
            }else{
                errorMsg(response);
            }
        }
    });
}

