$(function(){
    "use strict";

    var shade;
    var fn ={};
    var STATE_NOLOGIN = 1;
    var STATE_ALREADY_VOTED = 2;
    var STATE_LOGIN_OK = 3;
    var STATE_LOGIN_FAILED = 2;
    var STATE_SUCCESS = 5;
    var STATE_OVER_MAX = 6;
    var STATE_OTHERS = 0;

    fn[STATE_NOLOGIN] = function noLogin(){
        showDialog($("#login-dialog"));
    };
    fn[STATE_ALREADY_VOTED] = function alreadyVoted(){ 
        alert("您已投过票");
    };
    fn[STATE_LOGIN_OK] = function(){
        hideDialog($("#login-dialog"));
    };
    fn[STATE_LOGIN_FAILED] = function(){
        alert("登录失败");
    };
    fn[STATE_SUCCESS] = function lucky(){
    	var times = 3 - arguments[1];
    	$("#times").html(times);
    	
        showDialog($("#dialog"));
        var btn = arguments[0];
        var span = btn.siblings(".number");
        var number = parseInt(span.html());
        span.html(++number);
        btn.addClass("liked");
    };
    fn[STATE_OVER_MAX] = function(){
    	alert("您已经投过2张票，不能再投了");
    }

    $(".like-btn").click(function(){ iLikeIt($(this)); });
    $("#login").click(login);

    $(".ele").hover(function(){
        var img = $(this).find(".banner img")
        img.addClass("likeit-hover");
        if(!$(this).hasClass("out")){
        	$(this).children(".out-line").hide();
        }
    }, function(){
        var img = $(this).find(".banner img");
        img.removeClass("likeit-hover");
    });

    function showDialog(dialog){
        calPos(dialog);
        dialog.fadeIn();

        shade = $("<div></div>").appendTo($("body"));
        shade.css("background", "transparent");
        shade.css("position", "fixed");
        shade.css("top", 0);
        shade.css("left", 0);
        shade.css("width", "100%");
        shade.css("height", "100%");
        shade.css("zIndex", 9);

        dialog.find(".closable").on("click", function(){
            hideDialog(dialog);
            if(shade){
                shade.remove();
            }
        });
    }

    function hideDialog(dialog){
        dialog.fadeOut();
        shade.fadeOut();
        shade.remove();
        shade = undefined;
        dialog.find(".closable").off("click");
    }

    function iLikeIt(btn){
        if(btn.hasClass("liked") || btn.parents().parents().hasClass("out")){
            return false;
        }
        var gid = btn.data("girl");
        $.getJSON("././findGirlAction.do?action=updateSupport",{"gid":gid}, function(json){
        	var result = json.result;
            fn[result] && fn[result](btn, json.times);
        });
    }

    function calPos(div){
        var width = div.width();
        var height = div.height();
        var winWidth = $(window).width();
        var winHeight = $(window).height();

        div.css("left", (winWidth - width) / 2);
        div.css("top", (winHeight - height) / 7);
    }
    


    function login()
    {
    	var username=$("#username").val();
    	var password=$("#password").val();
    	$.getJSON("././findGirlAction.do?action=login",
                  {
                      "username":username,
                      "password":password
                  },
                  function(items)
                  {
                      fn[items.result]();
                  }
                 );
    }
    
});

$(function(){
    "use strict";

    var users = [
        {
            name: '138***',
            goal: 10
        },
        {
            name: 'xiao***',
            goal: 5
        },
        {
            name: '150***',
            goal: 5
        },
        {
            name: 'zhan***',
            goal: 20
        },
        {
            name: '150***',
            goal: 5
        },
        {
            name: 'chen***',
            goal: 10
        },
        {
            name: 'jin1***',
            goal: 20
        },
        {
            name: '159***',
            goal: 5
        },
        {
            name: '大力***',
            goal: 10
        },
        {
            name: '138***',
            goal: 10
        }
    ];

    var userSort = users.sort(function(){
        return Math.random() > 0.5 ? 1 : -1;
    });

    var length = userSort.length;
    var html = "";
    for(var i = 0; i < length; ++i){
        html += "<tr>";
        html += "<td>" + userSort[i].name + "</td>" +
            "<td>" + userSort[i].goal + "元</td>";
        html += "</tr>";
    }

    var table = $("#prized");
    table.find("tr").eq(1).remove();
    table.append(html);
});

$(function(){
    "use strick";
    var disabledTeams = [16,6,11,12,1,14,2,15,9,7,4,8,5,13,3];
    (function disable(teams){
        var length = teams.length;
        for(var i = 0; i < length; ++i){
            var id = teams[i];
            var ele = $(".ele[data-name='" + id + "']");
            console.log(ele);
            ele.addClass("out");
        }
        attachEvents();
    })(disabledTeams);

    function attachEvents(){
        $(".ele.out").hover(function(){
            $(this).children().addClass("hover");
        }, function(){
            $(this).children().removeClass("hover");
        });
    }
});