/**
 * Created by IntelliJ IDEA.
 * User: Gao Yuan
 * Date: 2012-5-30
 * Time: 15:58:58
 * To change this template use File | Settings | File Templates.
 */
$(function($) {
    // 邮箱登陆
    var cur_url = document.location.href;
    var MailConfig = [
        {val:0},
        {action:"https://reg.163.com/logins.jsp",params:{url:"http://account.youdao.com/login?back_url="+cur_url+"&success=1",username:"#{u}",password:"#{p}",savelogin:"true"}},
        {action:"https://reg.163.com/logins.jsp",params:{savelogin:"true",domain:"126.com",username:"#{u}@126.com",password:"#{p}",url:"http://account.youdao.com/login?back_url="+cur_url+"&success=1"}},
        {action:"https://reg.163.com/logins.jsp",params:{savelogin:"true",domain:"yeah.net",username:"#{u}@yeah.net",password:"#{p}",url:"http://account.youdao.com/login?back_url="+cur_url+"&success=1"}},
        {action:"https://edit.bjs.yahoo.com/config/login",params:{login:"#{u}@yahoo.com.cn",passwd:"#{p}",domainss:"yahoo",".intl":"cn",".src":"ym"}},
        {action:"https://edit.bjs.yahoo.com/config/login",params:{login:"#{u}@yahoo.cn",passwd:"#{p}",domainss:"yahoocn",".intl":"cn",".done":"http://mail.cn.yahoo.com/inset.html"}},
        {action:"http://passport.sohu.com/login.jsp",params:{loginid:"#{u}@sohu.com",passwd:"#{p}",fl:"1",vr:"1|1",appid:"1000",ru:"http://login.mail.sohu.com/servlet/LoginServlet",ct:"1173080990",sg:"5082635c77272088ae7241ccdf7cf062"}},
        {action:"http://mail.sina.com.cn/cgi-bin/login.cgi",params:{u:"#{u}",psw:"#{p}"}},
        {action:"http://passport.renren.com/PLogin.do",params:{email:"#{u}",password:"#{p}",origURL:"http://www.renren.com/Home.do",domain:"renren.com"}},
        {action:"https://passport.baidu.com/?login",params:{u:"http://passport.baidu.com/center",username:"#{u}",password:"#{p}"}},
        {action:"http://passport.tianya.cn:80/login",params:{vwriter:"#{u}",vpassword:"#{p}"}},
        {action:"http://passport.51.com/login.5p",params:{passport_51_user:"#{u}",passport_51_password:"#{p}",gourl:"http%3A%2F%2Fmy.51.com%2Fwebim%2Findex.php"}},
        {val:0},
        {action:"http://gmail.google.com",type:"link"},
        {action:"http://mail.qq.com",type:"link"},
        {action:"http://www.hotmail.com",type:"link"},
        {action:"http://mail.139.com",type:"link"},
        {action:"http://www.kaixin001.com",type:"link"},
        {action:"http://qzone.qq.com/index.html",type:"link"},
        {action:"http://web2.qq.com",type:"link"},
        {action:"https://www.alipay.com/user/login.htm",type:"link"}
    ];

    var selectIndex = ($('#mailtype').get(0) && $('#mailtype').get(0).selectedIndex); // selector index
    var mailInfo = MailConfig[selectIndex]; // current mail infomation
    // type = link
    $('#mailtype').change(function(e) {
        selectIndex = $('#mailtype').get(0).selectedIndex;
        mailInfo = MailConfig[selectIndex];
        if (mailInfo.type == "link") {
            window.open(mailInfo.action);
            resetEmailForm();
        }

    });

    $('#emailLogin').click(function(e) {
        // predication
        var username = $.trim($('#nav_username').val());
        var password = $.trim($('#nav_password').val());
        if (mailInfo.val == 0) {
            alert("请选择邮箱！");
            e.preventDefault();
            return;
        }
        if (username == "") {
            alert("用户名不能为空!");
            e.preventDefault();
            return;
        }
        if (password == "") {
            alert("密码不能为空!");
            e.preventDefault();
            return;
        } else if (password.length < 6) {
            alert("请把密码长度不能小于6!");
            e.preventDefault();
            return;
        }
        var loginInfo = {u:username, p:password};
        // change form
        var params = mailInfo.params;
        if (params) {
            // change form
            $('#loginForm').remove();
            var form = $("<form id='loginForm' name='loginForm'></form>");
            form.attr('method','post')
            form.attr('action', mailInfo.action);
            // insert input
            $.each(params, function(key, val) {
                $('<input>').attr({
                    type: 'hidden',
                    name: key,
                    value: format(val, loginInfo)
                }).appendTo(form);
            });
            // submit
            form.appendTo("body")
            form.css('display','none')
            form.submit();
            resetEmailForm();
        }
        e.preventDefault();
    });
    function format(param, loginInfo) {
        return param.replace("#{p}", loginInfo.p).replace("#{u}", loginInfo.u);
    }
    function resetEmailForm() {
        $('#nav_username').val("");
        $('#nav_password').val("");
        $('#mailtype').val(-1);
    }
});
$(function($) {
    /*未阅读邮件数字*/
    var domain = "";
    if(typeof(scope) == 'undefined'){
        return;
    }else{
        domain = scope.username.match(/\w*@(.*)$/)[1];
    }
    window.userdefinedcallback = function (unreadNum,mails,uname){
        if($("a.email").length){
            $("a.email").text(unreadNum);
        }
    };
    var url = "http://msg.mail." + domain + "/cgi/mc?funcid=getusrnewmsgcnt&fid=0&template=newmsgres_setcookie.htm&callback=userdefinedcallback&username=" + scope.username;
    function getUnread(){
        $.ajax({
            url : url,
            type : "get",
            dataType : "jsonp"
        });
    }
    getUnread();
    setInterval(getUnread,1000*60*3);   /*每隔3分钟请求未阅读数*/
});
$(function($) {
    // 搜索部分的逻辑 - 数据部分见 sejson.js 文件

    var $wrap = $('#searchWrap');
    var $tabs = $wrap.find('ul.cnav-list');
    var $form = $wrap.find('form');

    // 搜索form数据
    var searchUrl = [
        "http://www.baidu.com/s",
        "http://image.baidu.com/i",
        "http://video.baidu.com/v",
        "http://gouwu.baidu.com/s",
        "http://news.baidu.com/ns",
        "http://dict.baidu.com/s"
    ];
    // ---------------------------------------------------------------
    // 切换垂搜 tab
    $tabs.find("li a").click(function(e) {
        // update look
        var li = $(this).parent();
        if (li.hasClass("current")) return;
        $tabs.find(".current").removeClass("current");
        li.addClass("current");
        // get index
        var index = $tabs.find("li a").index($(this));
        // get info
        var url = searchUrl[index];
        // update form
        $form.attr("action", url);
        e.preventDefault();
    });
    // init form
    $form.attr("action", searchUrl[0]);
});
$(function($) {
    function setHomePage(obj) {
        var url = location.href;
        try {
            obj.style.behavior = 'url(#default#homepage)';
            obj.setHomePage(url);
        } catch(e) {
                if (window.netscape) {
                    try {
                        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                    }catch (e)  {
                        alert("抱歉！您的浏览器不支持直接设为首页。请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为“true”，点击“加入收藏”后忽略安全提示，即可设置成功。");
                    }
                    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                    prefs.setCharPref('browser.startup.homepage',url);
                }else{
                    alert("您的浏览器不支持此功能，请手动设置。");
                }
        }
    }
    $("#setHomePage").bind("click",function(){setHomePage(this);});
});
/*用户名下拉列表*/
$(function($){
    var timeid;
    $(".username").bind("click",function(){
         $("#user163List").toggle();
    });
    $(".username").bind("mouseover",function(){
        if(timeid){
             clearTimeout(timeid);
        }
    });
    $(".username").bind("mouseout",function(){
        timeid = setTimeout(function(){$("#user163List").hide();},300);
    });
    $("#user163List").bind("mouseover",function(){
        if(timeid){
            clearTimeout(timeid);
        }
        $("#user163List").show();
    });
    $("#user163List").bind("mouseout",function(){
        timeid = setTimeout(function(){$("#user163List").hide();},300);
        $("#user163List").hide();
    });
});