﻿
//脚本对象 GAGS
var GAGS = function( controlId, selectedArea ,selectedGroup ,url ,forbidAreas )
{
    this.SelectedArea = selectedArea;
    this.SelectedGroup = selectedGroup ;
    this.AreaListId = controlId + "_areaList" ;
    this.GroupListId = controlId + "_groupList";
    this.ForbidAreas = forbidAreas;
    this.Url = url;
    this.Json = null;
    
    //GSGA_Ini( this);
}

function GSGA_Ini( GAGS )
{      
    $.getJSON( GAGS.Url ,function(json ){     
        
        if ( json )
        {                       
            GAGS.Json = json; 
            
            InitialAreaList( json ,GAGS.AreaListId,GAGS.SelectedArea ,GAGS.ForbidAreas);
            
            InitialGroupList( GAGS );
            
            return ;
        }
    });
}


//设置区列表
function InitialAreaList( json , areaListId ,selectedArea ,forbidAreas )
{      
    var selectedIndex = 0;            
            
    //拿到JSON对象   
    if ( json )
    {   
        var fList = null;    
        if ( forbidAreas != "")
            fList = forbidAreas.split('|');
      
        //var index = 0;
        $.each(json.Games[0].Areas, function(i,item){
            
            if (! IsForbidArea( fList , item.Id ))
            {
                $("<option></option>").attr("value", item.Id).text(item.Name).appendTo("#" + areaListId);    
                
                
//                
//                if ( selectedArea != -1 && selectedArea == item.Id )
//                {
//                    selectedIndex = index - 1 ; 
//                }
//                
              
            }
        });
    }        
    
    
    $("#" + areaListId)[0][0].selected = true;	      
}

//设置服列表
function InitialGroupList (GAGS)
{    
   
    if ($( "#" + GAGS.GroupListId )[0] == undefined)
        return;
   
    var area = $("#" + GAGS.AreaListId)[0].value ;
    
    var selectedIndex = 0; 
    
    $( "#" + GAGS.GroupListId ).html("<option value='-1' selected>请选择服务器</option>");	
    
    var json = GAGS.Json;
    
    if ( json )
    {
        // var index = 0;
        for(i = 0;i < json.Games[0].Areas.length; i ++)
        {
            if(json.Games[0].Areas[i].Id == area)
            {
                $.each(json.Games[0].Areas[i].Groups, function(j,item){
        
                    $("<option></option>").attr("value", item.Id).text(item.Name).appendTo("#" + GAGS.GroupListId);    
                    
//                    index = index + 1;
//                    
//                    if ( GAGS.SelectedGroup != -1 && GAGS.SelectedGroup == item.Id )
//                    {
//                        selectedIndex = index - 1 ;
//                        
//                    }
                    
                }); 
            }
        }
    }
        
    $("#" + GAGS.GroupListId)[0][0].selected = true;
    
}    

function IsForbidArea( fList , areaId)
{
    
    if ( fList == null ) 
        return false;

    for (var i=0; i < fList.length ; i ++)
    {
        
        if ( fList[i] == areaId )
        {            
           return true;
           
        }
            
    }
    
    return false;
}
