function slide1(){
    if (ajax.currentUser == undefined) {
        openLoginDialog(null, function(){
            window.location.href = "/gps/";
        });
        return false;
    } else {
        window.location.href = "/gps/";
    }
}

function slide2(){
    if (ajax.currentUser == undefined) {
        openLoginDialog(null, function(){
            window.location.href = "/study/";
        });
        return false;
    } else {
        window.location.href = "/study/";
    }
}


function slide3(){
    //if(ajax.currentUser==undefined){
    //	openLoginDialog(null, function(){
    //		window.location.href="/aboutUs/studyContent.html";
    //	});
    //	return false;
    //}else{
    window.location.href = "/aboutUs/studyContent.html";
    //}
}

function slide4(){
    //window.location.href="/aboutUs/studyContent.html";
}

$(function(){
    var data = null;
    var books = {
        elementary: {
            length: 0
        },
        intermediate: {
            length: 0
        },
        advanced: {
            length: 0
        }
    };
    
    var index = {
        __construct: function(){
            getCurrentUser(function(){
                if (ajax.currentUser == null) {
                    $(".level1 .panel").removeClass("hidden");
                    $(".level1 .studyPage").removeClass("hidden");
                    $(".level1 .loggedinPage").addClass("hidden");
                } else {
                    $(".level1 .panel").addClass("hidden");
                    $(".level1 .studyPage").addClass("hidden");
                    $(".level1 .loggedinPage").removeClass("hidden");
                    index.loadLevel1();
                }
                index.loadLevel1();
                // index.loadLevel2();
                index.loadLevel3();
                loadCommentModule();
				loadVipModule();
            });
            
            
            //firefox check
            if (!$.browser.mozilla) {
                if ($.cookie("showBrowserWarning") == null) {
                    var html = '';
                    html += '<div class="warningBrowserText">To get the best experience, you\'re recommended to use Firefox 3.0 or above.</div>';
                    html += '<div class="closeIcon"></div>';
                    html += '<input class="donotShow" type="button" value="Don\'t show any more" />';
                    $("<div />").prependTo("body").attr("id", "warningBrowser").addClass("warningBrowser").html(html);
                    
                    $("#warningBrowser .donotShow").unbind("click").click(function(){
                        $.cookie("showBrowserWarning", "false", {
                            expires: 90
                        });
                        $("#warningBrowser").remove();
                    });
                    
                    $("#warningBrowser .closeIcon").unbind("click").click(function(){
                        $("#warningBrowser").remove();
                    });
                }
                
            }
        },
        
        loadLevel1: function(){
            //slideShow
            //            window.onload = function(){
            //                $('.introduce').slideShow({
            //                    interval: false
            //                });
            //            }
            
            if (util.isOnline()) {
                $(".level1 .slides .slide img").attr("src", "http://www.ladderchinese.com/mt/mt-static/themes/aid200906/pics/11.jpg");
                $(".level1 .slides .slide").unbind("click").click(function(){
                    window.location.href = "/regist.html";
                });
            } else {
                $(".level1 .slides .slide img").attr("src", "http://www.ladderchinese.com/mt/mt-static/themes/aid200906/pics/12.jpg");
                $(".level1 .slides .slide").unbind("click").click(function(){
                    window.location.href = "/study/";
                });
            }
            
            //bind events
            //personal
            $(".level1 .personal").unbind("mouseover").mouseover(function(){
                $(this).addClass("personalOver");
            }).unbind("mouseout").mouseout(function(){
                $(this).removeClass("personalOver");
            }).unbind("click").click(function(){
                if (ajax.currentUser == undefined) {
                    openLoginDialog(null, function(){
                        gotoMyUrl();
                    });
                    return false;
                }
                gotoMyUrl();
            });
            //school
            $(".level1 .school").unbind("mouseover").mouseover(function(){
                $(this).addClass("schoolOver");
            }).unbind("mouseout").mouseout(function(){
                $(this).removeClass("schoolOver");
            }).unbind("click").click(function(){
                $("#schoolDialog").dialog({
                    bgiframe: true,
                    width: 440,
                    height: 287,
                    modal: true,
                    closeOnEscape: false,
                    resizable: false
                });
                $("#schoolDialog").dialog('open');
            });
            $("#schoolDialog").unbind("click").click(function(){
                $("#schoolDialog").dialog('close');
            });
            //company
            $(".level1 .company").unbind("mouseover").mouseover(function(){
                $(this).addClass("companyOver");
            }).unbind("mouseout").mouseout(function(){
                $(this).removeClass("companyOver");
            }).unbind("click").click(function(){
                $("#companyDialog").dialog({
                    bgiframe: true,
                    width: 440,
                    height: 287,
                    modal: true,
                    closeOnEscape: false,
                    resizable: false
                });
                $("#companyDialog").dialog('open');
            });
            $("#companyDialog").unbind("click").click(function(){
                $("#companyDialog").dialog('close');
            });
            //studyPage
            /*$(".level1 .studyPage").unbind("mouseover").mouseover(function(){
             $(this).addClass("studyPageOver");
             }).unbind("mouseout").mouseout(function(){
             $(this).removeClass("studyPageOver");
             }).unbind("click").click(function(){
             if(ajax.currentUser==undefined){
             openLoginDialog(null, function(){
             gotoStudyUrl();
             });
             return false;
             }
             gotoStudyUrl();
             });*/
            $("#loginMainForm").unbind("submit").submit(function(){
                var username = $("#loginMainForm input[name='j_username']").attr("value");
                var password = $("#loginMainForm input[name='j_password']").attr("value");
                var rememberme = $("#loginMainForm input[name='_spring_security_remember_me']").attr("value");
                if (username == "") {
                    alert("Email can't be empty");
                    $("#loginMainForm input[name='j_username']").focus().select();
                    return false;
                }
                if (password == "") {
                    alert("Password can't be empty");
                    $("#loginMainForm input[name='j_password']").focus().select();
                    return false;
                }
                
                ajax.login(username, password, rememberme, function(){
                    if (ajax.currentUser == null) {
                        loginDialogError();
                    } else {
                        location.reload();
                    }
                });
                
                return false;
            });
        },
        
        loadLevel2: function(){
            //get hot-books
            index.getHotBooks(function(){
                //show book list
                /*
                 //Elementary
                 for(var i=1;i<=books.elementary.length;i++){
                 var sHTML='';
                 sHTML+='<a href="/course/#bookName='+books.elementary[i].name+'&bookLevel='+books.elementary[i].level+'">';
                 sHTML+=books.elementary[i].name+' '+books.elementary[i].level;
                 sHTML+='</a>';
                 $(".level2 .elementary .bookName").eq(i-1).html(sHTML);
                 if(i==6){break;}
                 }
                 //Intermediate
                 for(var i=1;i<=books.intermediate.length;i++){
                 var sHTML='';
                 sHTML+='<a href="/course/#bookName='+books.intermediate[i].name+'&bookLevel='+books.intermediate[i].level+'">';
                 sHTML+=books.intermediate[i].name+" "+books.intermediate[i].level;
                 sHTML+='</a>';
                 $(".level2 .intermediate .bookName").eq(i-1).html(sHTML);
                 if(i==6){break;}
                 }
                 //Advanced
                 for(var i=1;i<=books.advanced.length;i++){
                 var sHTML='';
                 sHTML+='<a href="/course/#bookName='+books.advanced[i].name+'&bookLevel='+books.advanced[i].level+'">';
                 sHTML=books.advanced[i].name+" "+books.advanced[i].level;
                 sHTML+='</a>';
                 $(".level2 .advanced .bookName").eq(i-1).html(sHTML);
                 if(i==6){break;}
                 }*/
                var sHTML = '';
                for (var i = 1; i <= data.length; i++) {
                    if (i == 11) {
                        break;
                    }
                    //sHTML+='<div class="bookName">';
                    //sHTML+='<a href="/course/#bookName='+data[i].name+'&bookLevel='+data[i].level+'">';
                    //sHTML+=data[i].name+' '+data[i].level;
                    //sHTML+='</a>';
                    //sHTML+='</div>';
                    sHTML += '<img src="' + imageUrlPrefix + data[i].image + '" width="80" height="80" alt="" onclick="window.location.href=\'/course/#bookName=' + data[i].name + '&bookLevel=' + data[i].level + '\'" />';
                }
                $(".level2 .hotBooks").html(sHTML);
            });
        },
        
        loadLevel3: function(){
            $(".level3 .p").unbind("hover").hover(function(){
                //$(this).addClass("pHover");
            }, function(){
                //$(this).removeClass("pHover");
            });
            $(".level3 .p .gotoMyCredit").unbind("click").click(function(){
                if (ajax.currentUser == undefined) {
                    openLoginDialog(null, function(){
                        window.location.href = "/my/credit/";
                    }, "quick");
                    return false;
                }
                window.location.href = "/my/credit/";
            });
        },
        
        /*sortData:function(){
         for(var i=1;i<=data.length;i++){
         if(data[i].level=="R1" || data[i].level=="R2" || data[i].level=="R3" || data[i].level=="R4"){
         books.elementary.length++;
         var index=books.elementary.length;
         books.elementary[index]=data[i];
         }else if(data[i].level=="R5" || data[i].level=="R6" || data[i].level=="R7" || data[i].level=="R8"){
         books.intermediate.length++;
         var index=books.intermediate.length;
         books.intermediate[index]=data[i];
         }else if(data[i].level=="R9" || data[i].level=="R10" || data[i].level=="R11" || data[i].level=="R12"){
         books.advanced.length++;
         var index=books.advanced.length;
         books.advanced[index]=data[i];
         }
         }
         },*/
        getHotBooks: function(fn){
            $("#hotBooks").unbind("submit").submit(function(){
                $(this).ajaxSubmit({
                    cache: false,
                    url: wsUrl + "/hot-books/",
                    type: "GET",
                    dataType: "json",
                    success: function(rs){
                        data = rs.books;
                        if (fn != undefined) {
                            fn();
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                        ajaxError(XMLHttpRequest, textStatus, errorThrown);
                    }
                });
                return false;
            });
            $("#hotBooks").submit();
        }
    }
    //load data
    index.__construct();
});

//TODO
var mBooks = [];

function loadBooks(){
    $(this).ajaxSubmit({
        cache: false,
        url: wsUrl + "/books/",
        type: "GET",
        dataType: "json",
        success: function(rs){
            mBooks = rs.books;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
        }
    });
}

function loadCommentModule(){

    /*
     * ajax.currentUser == null 没登录
     *
     * ajax.currentUser undefined 没加载完
     */
    var image = '';
    var isHasEditor = false;
    var app = 'ladderchiese';//
    var tag = 'all';
    var author = '';
    
    if (ajax.currentUser == null || ajax.currentUser.avatar == null || ajax.currentUser.avatar == '') {
        image = '/mt/mt-static/themes/aid200906/img/head.jpg';
        isHasEditor = false;
    } else {
        image = "/avatar/" + ajax.currentUser.avatar;
    }
    
    if (ajax.currentUser != null && ajax.currentUser.username != null) {
        author = ajax.currentUser.username;
        isHasEditor = true;
    }
    
    if (typeof placeComment == "function") {
        placeComment({
            "container-id": "comment-place-holder",
            "app": app,
            "tags": tag,
            "has-editor": isHasEditor,
            "show-comments": true,
            "author": author,
            "author-link": "#",
            "avatar": image,
            "referenced-link": "#",
            "date-format": "MM/dd/yyyy 'at' hh:mm",
            "page-size": 5,
            "word-limit-min": 3,
            "word-limit-max": 140
        });
    } else if (typeof placeComment == "undefined") {
        setTimeout("loadCommentModule();", 100);
    } else {
    }
    //$('.commentsTextLabel').text('comments');
    return;
}

function loadVipModule() {
	if (typeof placeVipLink == "function") {
	    placeVipLink('vipLink');
	} else if (typeof placeVipLink == "undefined") {
	    setTimeout("loadVipModule();", 100);
	} else {
	    // ignored
	}
}

