function Queue() {
    var data = new Array();
    this.size = function() { return data.length; };
    
    this.item = function() { return data; };

    this.remove = function(id) {//从队列中删除        
        data = $.grep(data, function(n, i) {
            return n != id;
        });
    };
    this.getData = function() {
        if (data.length > 0)
        //return $.grep(data, function(n, i) { return i < length; });
            return data[0];
        else return null;
    };
    this.moveToLast = function(id) {
        this.remove(id);
        data[data.length] = id;
        
    };
    this.pushData = function(id) {
        this.remove(id);
        data[data.length] = id;
        //触发发送程序    
        sendToServer(0);
    };

}


function testFlags(containerId) {
    var items = new Array(); var container = containerId;
    var _testFlags_finishCorlor = "green";
    var _testFlags_unFinishCorlor = "red";
    var _keyQueue = new Queue();
    this.getItems = function() { return items };
    this.getQueue = function() { return _keyQueue };
    this.reset = function () {
        
        $.each(items, function(i, n) {
            n.clientKey = []; n.serverKey = [];
        });

        var q = _keyQueue.item();
        while (q.length > 0)
            q.shift();
        $("div[id^=_testFlag_foucs_]").remove();

        $("div[id^=_testFlag_]").attr("class", "flag").css("backgroundColor", _testFlags_unFinishCorlor);

    };
    this.getItem=function (keyId) {

        for (var i = 0; i < items.length; i++) {

            var a = items[i]; 
            if (a.keyId == keyId) return a;
        };
        return null;
    };
    function testFlagsSetAnswerState(keyId, clientKeys) {
        $("#_testFlag_" + keyId).css("backgroundColor", _testFlags_finishCorlor);
        $.each(clientKeys, function(i, n) {       
            if ((n == null) || (n == "") || (n.length == 0)) { $("#_testFlag_" + keyId).css("backgroundColor", _testFlags_unFinishCorlor); return; };
        });

    };
   
    this.savedState = function(item, sendState) { setSavedState(item, sendState) };
     function setSavedState(item, sendState) {
         //设置显示区的保存状态
        
        item.sendState = sendState;var keyId = item.keyId;
        if (sendState == 0) {
            _keyQueue.pushData(keyId);
        };
        if ((sendState == 1)||(sendState == 3)) {
            _keyQueue.moveToLast(keyId);
            $("#_testFlag_" + keyId)
        };
        if ((sendState == 2) || (sendState == 4)) {
            _keyQueue.remove(keyId);
        };
        $("#_testFlag_" + keyId).removeClass("sendState_0").removeClass("sendState_1").removeClass("sendState_2").removeClass("sendState_3").addClass("sendState_" + sendState);

        /*return
        
        
        var f = $("#_testFlag_send_" + keyId);
        if (f.length == 0) {
            var tt = $("#_testFlag_" + keyId);

            var h = "<div id='_testFlag_send_" + keyId + "' class='sendState_" + sendState + "' />";
            $(h).appendTo(tt);
        } else { f.attr("class", "sendState_" + sendState); };*/
    };
    var _lastedFoucsTest = null;
    var _lastedFoucsFlagId = null;
    this.setFoucs = function (keyId, divName) {
        //$("div[class='flagFoucs'],div[class='flagFoucs_red']").remove();
        
        var f = $("#_testFlag_foucs_" + keyId);
        if (f.length == 0) {
            $("#" + _lastedFoucsFlagId).remove();
            var className = "flagFoucs";
            if ($("#_testFlag_" + keyId).css("backgroundColor") == _testFlags_finishCorlor) { className = "flagFoucs_red"; } else
                className = "flagFoucs";
            var h = "<div id='_testFlag_foucs_" + keyId + "' class='" + className + "' />";
            var cell = document.getElementById("_testFlag_" + keyId);
            //cell.scrollIntoView();
            $(h).appendTo(cell); _lastedFoucsFlagId = "_testFlag_foucs_" + keyId
        };

        if (divName != null) {
            var test = $("[name='" + divName + "']");
            //var tagName = test.get(0).tagName.toLowerCase()

            //$(tagName + "[name]").removeClass("testFocus");
            if (_lastedFoucsTest != null) { $(_lastedFoucsTest).removeClass("testFocus"); };
            test.addClass("testFocus");
            _lastedFoucsTest = test;
        };
        if ($.isFunction(startADraw)) {
            startADraw(keyId);
        };

    };
    function testFlagsSetSavedState(item) {
        
        var keyId = item.keyId; var serverKeys = item.serverKey; var clientKeys = item.clientKey;
        
        if (serverKeys.length == 0) {
            var none = false;
            for (var m = 0; m < clientKeys.length; m++) {
                if (clientKeys[m] == "") none = true;
            };
            if (none) { setSavedState(item, 4); return; }
        };
        if (clientKeys.length != serverKeys.length) {
            setSavedState(item, 0);
            return;
        };
        setSavedState(item, 2);
        $.each(clientKeys, function(i, n) {
            
            if (n != serverKeys[i]) { setSavedState(item, 0); return; };
        });
        
    };

    this.initKeyFromServer = function(keyId, keys) {
        var item = this.getItem(keyId);
        if (item == null) return;
        item.serverKey = keys;
        item.clientKey = keys;
        testFlagsSetAnswerState(keyId, keys); setSavedState(item, 2);
    };

    //添加一个单元
    this.add = function(km,keyId, text, testArea) {
       
        if (this.getItem(keyId) == null) {
            
            items[items.length] = {keyMode:km,
                allowSkip: true, testArea: testArea, keyId: keyId, serverKey: [], clientKey: [],
                text: text, //显示块的文本
                sendState: 2//发送到服务器状态  0未保存； 1正在保存； 2保存成功.append(h);3保存失败;4用户在保存前撤销答案
            };

            var h = "<div id='_testFlag_" + keyId + "' class='flag' onclick='moveTo(\"" + keyId + "\",\"" + testArea + "\")'>" + text + "</div>";

            var tt = document.getElementById(container);
            if(tt!=null)tt.innerHTML += h;
            
        };
    };
    this.setServerKey = function(keyId, keys) {
    var item = this.getItem(keyId); 
        if (item == null) return;
        item.serverKey = keys;
        testFlagsSetSavedState(item);
    };
    this.setClientKey = function(keyId, keys) {


    var item = this.getItem(keyId); 
        if (item == null) return;
        item.clientKey = keys;
        //设置标志块的颜色
        testFlagsSetAnswerState(keyId, keys);
        //this.setFoucs(keyId, item.testArea);
        
        //设置
        testFlagsSetSavedState(item);
    };
};
function moveTo(keyId,targetName) {
    if(!testFlags.prototype.testFlagsEnabled)return ;
  
    var test = $("[name='" + targetName + "']");
    if (test.length > 0) {
        test.get(0).scrollIntoView();
        _testFlags.setFoucs(keyId, targetName);
        
        
    } else alert("未发现相应试题");
};

//启用或禁用一个单元
testFlags.prototype.testFlagsEnabled = true;
var _testFlags = new testFlags("testFlagsDiv");
$.metadata.setType("attr", "data");

//启用或禁用一个单元

$().ready(
        function () {

            var loader = jQuery('<div><img src="/paperStyle/images/button_ok.png" /><img src="/paperStyle/images/message_warning.png" /><img src="/paperStyle/images/error.png" /><img src="/paperStyle/images/loading2.gif" /></div>')
			.appendTo("body")
			.hide();

            $("div[id^=test_d_],div[id^=test_r_],div[id^=test_c_],div[id^=test_t_],div[id^=test_i_],li[id^=test_]").each(function() {
                var flagData = $(this).metadata(); if (flagData == null) return; if (flagData.km == null) return;
                _testFlags.add(flagData.km, flagData.keyId, flagData.text, flagData.testArea)
            }).hover(
               function() {
                  // $(this).addClass("testFocus");
            var flagData = $(this).metadata(); if (flagData == null) return; if (flagData.km == null) return;
            _testFlags.setFoucs( flagData.keyId, flagData.testArea)
               },
               function() {
               
               }
); 
           
            $("div[id^='_testFlag_']").hover(
               function() {
                   $(this).addClass("hover");
               },
               function() {
                   $(this).removeClass("hover");
               }
);

        });
