
function CountryGroup(code,name,description,countryList){
    //countryList is an array of countries for this countryGroup

    //Methods
    this.setSelected        = _setGroupSelected;
    this.isSelected         = _isGroupSelected;
    this.addCountryList     = _addCountryList;
    this.getSelectedColor   = _getGroupSelectedColor;
    this.displayGroup       = _displayGroup;
    this.getStrokeWidth     = _getGroupStrokeWidth;
    this.getStroke          = _getGroupStroke;
    this.setStyle           = _setStyle;
    this.getStyle           = _getStyle;

    //properties
    this.name = name;
    this.code = code;
    this.description        = description;
    this.hidden_selected    = false;
    this.countryList        = countryList;
    this.addCountryList(countryList);
    this.hidden_style;
    this.hidden_selected_color = null;
}

function _getGroupStrokeWidth(){
    return 4;
}
function _getGroupStroke(){
    return "blue";
}
function _getGroupSelectedColor(){

    if( this.hidden_selected_color == null){
        var style = this.getStyle();
        var startIndex = style.indexOf('fill');
        startIndex+=5;
        var endIndex = style.indexOf(";",startIndex)
        var tmpColor = style.substr(startIndex,endIndex-startIndex);
        this.hidden_selected_color=tmpColor.replace(/\s+|:/g,"");

    }
    return this.hidden_selected_color;
}
function _isGroupSelected(){
    return this.hidden_selected;
}
function _setGroupSelected(selected){
    //if it is selected and it was not already selected. it adds itself
    // to the SeleectedGroups array
    if(selected && !this.isSelected()){
        //added to the selected list
        SelectedGroups.push(this);
         this.hidden_selected = true;
    }else if( this.isSelected()){
    //if it is deseledted and it was previosly selected it removes itself
    //from the SelectedGroups Array.
        //remove it from the selected list
        for( var i=0; i< SelectedGroups.length; i++){
            if( SelectedGroups[i].code == this.code){
                SelectedGroups.splice(i,1);
                 this.hidden_selected = false;
                continue;
            }
        }
    }

    //Notify to all country in the country list that
    //they must display theirself according to the new
    //selection of groups

    for( var i=0; i<this.countryList.length; i++){
        this.countryList[i].displayCountry();
    }
    //it display itself according to the new selection status

    this.displayGroup();
}
function _addCountryList(countryListArray){
    for( var i=0; i<countryListArray.length;i++)
        countryListArray[i].addedToGroup(this);
}
function _displayGroup(){
    if( this.isSelected())
        parent.setGroupOpacity(this.code,"1");
    else
        parent.setGroupOpacity(this.code,"0.5");

}
function _getStyle(){
    return this.hidden_style;
}
function _setStyle(newStyle){
    this.hidden_style = newStyle;
}
