/**
 * @author Administrator
 */

var TIME;
var NEWTIME = 8;
var WEBPHTEST = true;
$(document).ready(function(){
    $("#XyExp").tabs();
    out_exp_info();
    $("#ph_yes").click(function(){
        WEBPHTEST = true;
        ph_yes();
    });
    $("#ph_yes2").click(function(){
        WEBPHTEST = false;
        ph_yes();
    });
    $("#ph_clear").click(function(){
        $(".textarea_xy1").val("XG");
    });
	$(".textarea_xy1").keyup(function(){
		onpchange();
	}).mouseup(function(){
		onpchange();
	});
	$(".txt_center input").click(function(){
		$("#success_exp").PopupDiv("close");
	});
	$("#feild_ms :button").click(function(){
		clearTimeout(TIME);
        $("#feild_ms").PopupDiv("close");
	});
	//请求最新动态的数据
	getRssXml(10);
	nav1_xSty("首页");
	index_xml();
});
function onpchange(){
    var textval = $(".textarea_xy1").val();
	var i1=	textval.substring(0, 1);
	var i2=textval.substring(1, 2)
    if (textval.substring(0, 2) !== "XG") {
		alert("短信修改心语必须以'XG'开头!");
		if(i1=="X"||i1=="G"){
			$(".textarea_xy1").val("XG"+textval.substr(1));
		}else{
		$(".textarea_xy1").val("XG" + textval);
		}        
    }    
}
//体验成功的弹出层
function out_exp_info(){
    $("#success_exp").PopupDiv({
        title: "心语体验",
        modal: true,
        minWidth: 214,
        autoOpen: false,
        close_fn_before: function(){
            stopTaste();
        }
    });
}

function ph_yes(){
    var usrid = "";
    var msisdn = "";
    var content = "";
    var id = "";
    //取体验号码
    if ($(".textarea_xy1").val().length > 70 ) {
        if (WEBPHTEST) {
            alert("您现在的输入的心语内容长度为：" + $(".textarea_xy1").val().length + "个字。心语内容长度不能大于70个字。请修改内容！");
        }
        else {
            alert("您现在的输入的心语内容长度为：" + $(".textarea_xy2").val().length + "个字。心语内容长度不能大于70个字。请修改内容！");
        }
    }
//    else 
//        if (!WEBPHTEST && $(".textarea_xy2").val().length < 1) {
//            alert("请输入心语内容！");
//        }
        else 
            if (WEBPHTEST && $(".textarea_xy1").val().substr(2) === "") {
                alert("请输入心语内容！");
            }
            else {
                $.ajax({
                    type: "POST",
                    url: "user_try.action",
                    dataType: "json",
                    cache: false,
                    async: false,
                    //ifModified: true,
                    success: function(json){
                        if (json.id == null) {
                            feild_ms();
                        }
                        else {
                            // alert("取号码完成" + json.msisdn);
                            usrid = json.usrid;
                            msisdn = json.msisdn;
                            content = json.content;
                            id = json.id;
                            saveContent(usrid, msisdn, id);
                        }
                    }
                });
            }
}

function saveContent(usrid, msisdn, id){
    //alert("锁号码计时开始" + usrid + "/" + msisdn + "/" + id);
    $.ajax({
        type: "POST",
        url: "Try_timeStart.action",
        dataType: "text",
        cache: false,
        async: false,
        //ifModified: true,
        success: function(json){
            //alert(json);
            if (json == '"0"') {
                // alert("锁定失败！");
                feild_ms();
            }
            else {
                //  alert("锁定成功！");
                edxy(usrid, msisdn, id);
            }
        }
    });
}

function edxy(usrid, msisdn, id){
    // alert("修改心语内容");
    var content = $(".textarea_xy1").val().substr(2);//内容
    if (!WEBPHTEST) {
        content = $(".textarea_xy2").val();
    }
    $.ajax({
        type: "POST",
        url: "saveTry_content.action",
        dataType: "text",
        data: {
            content: content
        },
        cache: false,
        async: false,
        //ifModified: true,
        success: function(json){
            if (json == '"1"') {
                // alert("修改心语成功！");
                //$("#success_ms").show(); 
                success_ms(msisdn);
            }
            else {
                $("#success_ms").show();
                //alert("修改心语失败！");
                feild_ms();
            }
        }
    })
}

//号码成功时的处理
function success_ms(msisdn){
    $("#msisdn_jing1").text(msisdn);
    $("#exp_phNum1").text(msisdn);
    $("#success_exp").PopupDiv("open");
    NEWTIME = 60 * 1;
    gotime_b();
}

//号码占用处理
function feild_ms(){
    if (confirm("体验号码被占用中，系统将在 30秒后自动再次提交您的心语！")) {
        $("#feild_ms").PopupDiv({
            title: "等待号码中",
            modal: true,
            PopupDivId: "feild_ms_div",
            minWidth: 214,
            minHeight:80,
            close_fn_before: function(){
                clearTimeout(TIME);
            }
        });
        NEWTIME = 30;
        gotime_a();
    }
}

//延时
function gotime_a(){
    if (NEWTIME > 0) {
        $("#feild_ms_t").eq(0).text(NEWTIME);
        TIME = setTimeout("gotime_a()", 1000);
        NEWTIME = NEWTIME - 1;
    }
    else {
        clearTimeout(TIME);
        $("#feild_ms").PopupDiv("close");
        ph_yes();
    }
}

function gotime_b(){
    if (NEWTIME > 0) {
        if (NEWTIME > 60) {
            $("#time_jingsc").text(Math.floor(NEWTIME / 60) + "分" + NEWTIME % 60 + "秒");
        }
        else {
            $("#time_jingsc").text(NEWTIME + "秒");
        }
        TIME = setTimeout("gotime_b()", 1000);
        NEWTIME = NEWTIME - 1;
    }
    else {
        clearTimeout(TIME);
        $("#success_exp").PopupDiv("close");
        //$("#getck").text("点此次获取手机验证码");        
    }
}

function stopTaste(){
    //  alert("解锁");
    $.ajax({
        type: "POST",
        url: "stopTaste.action",
        dataType: "text",
        cache: false,
        async: false,
        //ifModified: true,
        success: function(json){
            //alert(json);
            if (json == '"0"') {
                //alert("解锁失败！");
                //feild_ms();
            }
            else {
                //alert("解锁成功！");
                clearTimeout(TIME);
            }
        }
    });
}
//场景弹出
function out_flash(fname,ftitle,wid,heit){
	var shtml="<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='"+wid+"' height='"+heit+"'>"+
     "<param name='movie' value='images/"+fname+"' />"+
     "<param name='quality' value='high' />"+
     "<embed src='images/"+fname+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+wid+"' height='"+heit+"' ></embed>"+
	 "<param name='wmode' value='transparent' /></object>";
	$("#flash_out").html(shtml);
    if($(".myTi_l").text()!==ftitle){
 	   $(".myTi_l").html("").text(ftitle);
    }
	$("#flash_out").PopupDiv({
            PopupDivId: "flash_out1",//因为个演示页面有多个弹出层，所以我给这个加个指定的id号
            title: ftitle,//自定义的标题名称    
            close_fn_later:function(){
        	$("#flash_out").PopupDiv("remove");
        		}
        });
}
//修改XML配置文件页面变化的方法
function index_xml(){
//页面左侧flash地址XML
	var getXmlData=getXmlOb($("#head").data("confXml"));
//	var getXmlData=$("#head").data("confXml");
	var flashName=$(getXmlData).find("person").find("flashUrl").find("index_flashUrl").text();  
	var flashStr="<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='230' height='200'><param name='movie' value='"+flashName+"' /><param name='quality' value='high' /><embed src='"+flashName+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='230' height='200' ></embed><param name='wmode' value='transparent' /></object>"
	$(".index_div").html(flashStr);
}
//我要引用
function iwantit(obj){
	//alert($(obj).parents(".xinyu_xiu").find(".xiu_xiu").text());
	var con=$(obj).parents(".xinyu_xiu").find(".xiu_xiu").text();
	//var con=Str2Unicode($(obj).parents(".xinyu_xiu").find(".xiu_xiu").text());
	//var con=$(obj).parents(".xinyu_xiu").find(".xiu_xiu").text().toString();
	if($("#id").val()){
		addCookie("con",con,0);
		document.location = $("#basePath").val() + "myXY/library.jsp?con=true";
	}else{
		alert("请先登录！");
	}
	
}