
var autoPlayTimeOutId;
//SlideRight
var currentPhIdx = 1;
var isSetPre = false;
function ReturnToMainHome() {
    window.external.ReturnToMainHome();
}
function ReturnToMainHomeByBrowsing() {
    StopAutoPlay();
    window.external.ReturnToMainHomeByBrowsing();
}
function StudyModel() {
    if (window.external.ModelSelectedStudy()) {
        $('#divModelSlt').hide();
        $('#divLearn').show();
    }
}
function Hide1() {
    $('#divMemoryInfo').hide();
}
function Hide2() {
    $('#divEachUnit').hide();
}
function Show2() {
    $('#divEachUnit').show();
}
function Hide3() {
    $('#divStContent4').hide();
}
function Show3() {
    $('#divStContent4').show();
}
function DocReady() {
    $('.divleft').hover(
			function () {
			    $(this).css("background-image", "url(img/folder_BG.png)");
			},
			function () {
			    $(this).css("background-image", "");
			}
		);
	$('.divleft').click(
        function () {
            var flag = window.external.SelectedVocabulary($(this).attr("id"));
            if (flag) {
                $('#divModelSlt').show();
                $('#divHome').hide();
            }
        }
    );   
}
function UnitReady() {
    $('.divUnit').click(
        function () {
            window.external.SelectedWordUnit($(this).attr("unitId"));
        }
    );
}
function cardFlip() {

    var f = window.external.GSFlipTime(600);
    if (!f) {
        return;
    }
    $('.flashCard').quickFlipper();
    $('.headWord').html("");    
    var arr = $('.definition');
    $(arr[1]).html("");
    $(arr[2]).html("");
    var arr1 = $('.headWord2');   
    $(arr1[1]).html("");
    $(arr1[2]).html("");
       
    $('.flipBtn').hide();
    $('.judgeMe').show();
    window.external.ConvertToExp();
}
function RightAndWrong() {
    $('.rightBTN').attr("href", "javascript:leftSlider(true);");
    $('.wrongBTN').attr("href", "javascript:leftSlider(false);");
}
function leftSlider(flag) {
    var fl = window.external.GSFlipTime(500);
    if (!fl) {
        return;
    }
    var i = $('#divFlashCard1').offset().left;
    var j = $('#divFlashCard2').offset().left;
    if (i <= 30 && i >= 0) {        
        $('#divFlashCard2').css({ left: "0px" });
        z = $('#divFlashCard1').css("z-index");
        z--;
        $('#divFlashCard2').css({ "z-index": z });
        ResponseClick(flag, true);
        $('#divFlashCard1').animate({ left: "-390px" }, 500);
        z = z + 2;
        $('#divFlashCard1').css({ "z-index": z });
    }
    if (j <= 30 && j >= 0) {        
        $('#divFlashCard1').css({ left: "0px" });
        z = $('#divFlashCard2').css("z-index");
        z--;
        $('#divFlashCard1').css({ "z-index": z });
        ResponseClick(flag, false);
        $('#divFlashCard2').animate({ left: "-390px" }, 500);
        z = z + 2;
        $('#divFlashCard2').css({ "z-index": z });
    }
    $('.flipBtn').show();
    $('.judgeMe').hide();

}
function ResponseClick(isTrue, cardSlt) {
    if (isTrue) {
        window.external.KnowClick(cardSlt);
    }
    else {
        window.external.UnKnowClick(cardSlt);
    }
}
function initialFlip() {
    $('#divFlashCard1').quickFlip();
}

function initialRecom() {
    $('.recommand').click(function () {
        $('.snsShare').show();
    });
    $('.closeShare').click(function () {
        $('.snsShare').hide();
    });
}
function ReBindAutoAndPause() {
    autoAndpauseEvent();
}
function autoAndpauseEvent() {
    $('#autoBTN').removeClass().unbind();
    $('#pauseBTN').removeClass().unbind();

    clearTimeout(autoPlayTimeOutId);
    $('#autoBTN').addClass("divBTNBase divautoBTN");
    $('#autoBTN').mouseover(function () {
        $(this).removeClass("divautoBTN").addClass("divautoBTN1");
    }).mouseout(function () {
        $(this).removeClass("divautoBTN1").addClass("divautoBTN");
    }).mousedown(function () {
        $(this).removeClass("divautoBTN1").addClass("divautoBTN2")
    }).mouseup(function () {
        $(this).removeClass("divautoBTN2").addClass("divautoBTN");
    }).click(function () {
        AutoPlay();
        $(this).hide();
        $("#pauseBTN").show();
    });
    
    $('#pauseBTN').addClass("divBTNBase divpauseBTN");
    $('#pauseBTN').mouseover(function () {
        $(this).removeClass("divpauseBTN").addClass("divpauseBTN1");
    }).mouseout(function () {
        $(this).removeClass("divpauseBTN1").addClass("divpauseBTN");
    }).mousedown(function () {
        $(this).removeClass("divpauseBTN1").addClass("divpauseBTN2")
    }).mouseup(function () {
        $(this).removeClass("divpauseBTN2").addClass("divpauseBTN");
    }).click(function () {
        StopAutoPlay();
        $(this).hide();
        $("#autoBTN").show();
    });
    $("#pauseBTN").hide();
}
function preBTNEventNotValid() {    
    $('#preBTN').unbind();
    $('#preBTN').removeClass().addClass("divBTNBase divpreBTN3");
}
function preBTNEvent() {
    $('#preBTN').removeClass("divpreBTN3").addClass("divpreBTN");
    $('#preBTN').mouseover(function () {
        $(this).removeClass("divpreBTN").addClass("divpreBTN1");
    }).mouseout(function () {
        $(this).removeClass("divpreBTN1").addClass("divpreBTN");
    }).mousedown(function () {
        $(this).removeClass("divpreBTN1").addClass("divpreBTN2");
    }).mouseup(function () {
        $(this).removeClass("divpreBTN2").addClass("divpreBTN");
    });
}
function nextBTNEvent() {
    $('#nextBTN').addClass("divBTNBase divnextBTN");
    $('#nextBTN').mouseover(function () {
        $(this).removeClass("divnextBTN").addClass("divnextBTN1");
    }).mouseout(function () {
        $(this).removeClass("divnextBTN1").addClass("divnextBTN");
    }).mousedown(function () {
        $(this).removeClass("divnextBTN1").addClass("divnextBTN2")
    }).mouseup(function () {
        $(this).removeClass("divnextBTN2").addClass("divnextBTN");
    });
}

var SiteMap = {
    amsn: 'http://profile.live.com/badge?url={0}',
    asina: 'http://v.t.sina.com.cn/share/share.php?title={1}...{0}',
    atsohu: 'http://t.sohu.com/third/post.jsp?url={0}&title={1}&content=utf-8',
    adouban: 'http://www.douban.com/recommend/?url={0}&title={1}',
    a163: 'http://t.163.com/article/user/checkLogin.do?info={1}+{0}',
    atianya: 'http://share.tianya.cn/openapp/restpage/activity/appendDiv.jsp?ccTitle={1}&ccUrl={0}',
    arenren: 'http://share.xiaonei.com/share/buttonshare.do?link={0}',
    akaixin: 'http://www.kaixin001.com/~repaste/repaste.php?rtitle={1}&rurl={0}'
};
var cnts = {
    url: "http://dict.bing.msn.cn/",
    ctent: '我正在使用必应背单词，记忆曲线模型记单词，很有效哦！'
};

function SendShareMsg(el, wsite, isEncode) {
    u = SiteMap[wsite];   
    en = encodeURIComponent;
    el.href = isEncode ? fmt(u, en(cnts.url), en(cnts.ctent)) : fmt(u, cnts.url, cnts.ctent);

}
function fmt(src, ag1, ag2) {
    return src.replace("{0}", ag1).replace("{1}", ag2);
}
function iShare() {
    $('#amsn').click(function () { SendShareMsg(this, this.id, '1'); $('.snsShare').hide(); }).attr("target", "_blank");
    $('#asina').click(function () { SendShareMsg(this, this.id,'1'); $('.snsShare').hide(); }).attr("target", "_blank");
    $('#atsohu').click(function () { SendShareMsg(this, this.id, '1'); $('.snsShare').hide(); }).attr("target", "_blank");
    $('#adouban').click(function () { SendShareMsg(this, this.id, '1'); $('.snsShare').hide(); }).attr("target", "_blank");
    $('#a163').click(function () { SendShareMsg(this, this.id, '1'); $('.snsShare').hide(); }).attr("target", "_blank");
    $('#atianya').click(function () { SendShareMsg(this, this.id, '1'); $('.snsShare').hide(); }).attr("target", "_blank");
    $('#arenren').click(function () { SendShareMsg(this, this.id); $('.snsShare').hide(); }).attr("target", "_blank");
    $('#akaixin').click(function () { SendShareMsg(this, this.id, '1'); $('.snsShare').hide(); }).attr("target", "_blank");
}


function SetRightSlider() {
    $("#nextBTN").click(
        function () {

            vid = currentPhIdx + "card";
            currentPhIdx++;
            vid2 = currentPhIdx + "card";
            $('#' + vid ).animate({ left: "-360px" }, 600);
            $('#' + vid2).animate({ left: "0px" }, 600);

            if (!isSetPre) {
                preBTNEvent();
                isSetPre = true;
            }            
        });
}

function UnitCntView() {
    isSetPre = false;    
    $('.scroll-pane').jScrollPane(
		{
            clickOnTrack:false,
		    animateScroll: true,
            horizontalDragMinWidth: 20
        });
        var api = $('.scroll-pane').data('jsp'); 
              
        $('#preBTN').unbind('click');
        
		$('#nextBTN').unbind('click');
		$('#nextBTN').bind('click', function () {

		    SetNextButton(api);
		    
		});
}

function setPreBTNNotValid() {
    isSetPre = false;
    preBTNEventNotValid();
}
function setPreBTNValid() {
    var api = $('.scroll-pane').data('jsp');
    preBTNEvent();
    $('#preBTN').unbind('click');
    $('#preBTN').bind('click', function () {
        SetPreviousButton(api);
    });
    isSetPre = true;
}
function destroyJSP() {
    var api = $('.scroll-pane').data('jsp');
    api.destroy();
    window.external.SetEndUnitCnt();
}
function ScrollToJSP(oIdx) {
    var api = $('.scroll-pane').data('jsp');
    var idx = parseInt(oIdx);
    idx = (idx - 1) * 360;
    api.scrollToX(idx);
    if (idx != 0) {
        setPreBTNValid();
    }    
}
function unitLinkClick(obj, idx) {
    if (!$(obj).hasClass("current")) {
        $('.unitPav .current').removeClass();
        $(obj).addClass("current");
        window.external.GetUnitWordsByLink(idx);
    }
}
function pageLinkClick(obj, idx) {
    if (!$(obj).hasClass("current")) {
        window.external.GetUnitPages(idx);
    }
}
function setScrollWidth(obj) {
    
    $('#scroll').css({ width: obj });
    
    var api = $('.scroll-pane').data('jsp');    
    api.reinitialise();    
    ScrollToZero();
    
}
function ScrollToZero() {
    $('.jspPane').css("left", "0px");
    $('.jspDrag').css("left", "0px");
}
function setScrollWidth1(obj) {
    $('#scroll').css({ width: obj });
    var api = $('.scroll-pane').data('jsp');
    api.reinitialise();
    api.scrollToX(0);
}


function SetNextButton(api) {
    xy = api.getContentPositionX();
    yy = xy % 360;
    if (yy != 0) {
        xy = xy - yy + 360;
    }
    if (xy == 0) {
        var w = api.getContentPane().css("width");
        if (w != "360px") {
            api.scrollToX(360);            
        }
    }
    else {
        api.scrollToX(xy + 360);        
    }

    xy = api.getContentPositionX();
    yy = xy % 360;
    if (yy != 0) {
        xy = xy - yy + 360;
    }
    var i = xy / 360;
    
    if (!isSetPre) {
        preBTNEvent();
        $('#preBTN').bind('click', function () {
            SetPreviousButton(api);
        });
        isSetPre = true;
    }
    
    var j = window.external.SetPhIdxInUnitByButton(true);    
    if (j != i + 2) {
        j--;
        api.scrollToX(j * 360);
    }
    return false;
}
function SetPreviousButton(api) {
    xy = api.getContentPositionX();
    yy = xy % 360;
    if (yy != 0) {
        xy = xy - yy + 360;
    }
    api.scrollToX(xy - 360);
    x = api.getContentPositionX();
    if (x <= 360) {
        isSetPre = false;
        preBTNEventNotValid();
    }
    xy = api.getContentPositionX();
    yy = xy % 360;
    if (yy != 0) {
        xy = xy - yy + 360;
    }
    var i = xy / 360;
    var j = window.external.SetPhIdxInUnitByButton(false);
    if (j != i) {
        j--;
        api.scrollToX(j * 360);
    }
    //clear timer
    StopAutoPlay();
    return false;
}
function AutoPlay() {
    clearTimeout(autoPlayTimeOutId);
    autoPlayTimeOutId = setTimeout(function () {
        window.external.SetAutoPlayStatus(true);
        SetAutoPlayToActive();
    }, "300");
}
function StopAutoPlay() {
    if (autoPlayTimeOutId != undefined && autoPlayTimeOutId != null) {
        clearTimeout(autoPlayTimeOutId);
        window.external.SetAutoPlayStatus(false);
        $("#autoBTN").show();
        $("#pauseBTN").hide();
    }
}

function SetAutoPlayToActive() {
    $('#nextBTN').click();
    clearTimeout(autoPlayTimeOutId);
    autoPlayTimeOutId = setTimeout(SetAutoPlayToActive, "3000");
}
function MOver() {
    if (autoPlayTimeOutId != undefined && autoPlayTimeOutId != null) {
        clearTimeout(autoPlayTimeOutId);
        window.external.SetAutoPlayStatus(false);
    }; 
}
function MOut() {    
    var tempx = event.clientX - document.body.scrollLeft;
    var tempy = event.clientY - document.body.scrollTop;
    if (tempx < 26 || tempx > 360 || tempy < 41 || tempy > 239) {
        autoPlayTimeOutId = setTimeout(function () {
            if ($('#pauseBTN').is(":visible") == true) {
                window.external.SetAutoPlayStatus(true);
                SetAutoPlayToActive();
            }
        }, "400");
    }
}


function PlayWord(headword) {
    var hash = window.external.GetHeadwordHash(headword);
    var url = "http://dict.bing.com.cn/ah/" + hash + ".mp3";    
    //var audioPlayer = document.getElementById("_aplayer");
    //audioPlayer.Settings.PlayCount = 1;
    //audioPlayer.URL = url;
    var audioPlayer = document.getElementById("_aplayer");
    try {          
        audioPlayer.SetVariable("hash", url);
        audioPlayer.GotoFrame(2); // Stop potentially any playing sounds
        audioPlayer.GotoFrame(1);
    } catch (e) {
        audioPlayer.Settings.PlayCount = 1; // Call Settings.PlayCount especially to draw an exception if this is not present
        //this._audioPlayer.URL = audioUrl;
        audioPlayer.URL = url;
    }
}
