﻿function deleteShapes(divId, checkBoxClass, hiddenTxtId, colId){
    var ids = getValuesOfCheckedBox(divId, checkBoxClass );
    
    for (var i = 0; i< ids.length; i++){
        vmShapeList.removeShape(ids[i]);     
    }
    //checkBoxesToWhereClause(divId, checkBoxClass, colName)
    document.getElementById(hiddenTxtId).value = checkBoxesToWhereClause(divId,checkBoxClass, colId);
}
function showShapes(divId, checkBoxClass){
    var ids = getValuesOfCheckedBox(divId, checkBoxClass );    
    for (var i = 0; i< ids.length; i++){
        vmShapeList.showShapeById(ids[i]);        
    }
}
function clearShapes(divId, checkBoxClass){
    var ids = getValuesOfCheckedBox(divId, checkBoxClass );    
    for (var i = 0; i< ids.length; i++){
        vmShapeList.clearShapeById(ids[i]);        
    }
}

// Wraper for the VEShape, used in drawing
function VMShape(theId, theName, theDetails, theWKT, theVEShapeType, theAppShapeType){
    var id = theId;
    var name = theName;
    var details = theDetails;
    var wkt = theWKT;
    var veShapeType = theVEShapeType;
    var appShapeType = theAppShapeType; //this is the menue icon
    
    var veShape = null;
    var visible = false;
    var iconPath = null;
    
    this.getId = getId;
    this.setVEShape = setVEShape;
    this.setVisibility = setVisibility;
    this.showShape = showShape;
    this.clearShape = clearShape;
    this.updateShapeInfo = updateShapeInfo;
    this.getBound = getBound;
    
    this.setIcon = setIcon;
    
    function getBound(){
        
    }
    function setIcon(path){
        iconPath = makeIcon(path);
    }
    
    
    function getId(){
        return id;
    }
    function setVisibility(theVisible){
        visible = theVisible;
    }
    function setVEShape(shape){
        veShape = shape;
    }
    
    function showShape(){
        if (visible == true){
            return;
        }
        // if veshape if null then parse wkt to create it
        if (veShape == null){
            var latLngArr = wkt2VELatLngArr(wkt);
            veShape = new VEShape(veShapeType, latLngArr);
            
        }
        
        updateShapeInfo();
        if (veShape.GetType() != VEShapeType.pushpin){
            veShape.HideIcon();
        }
        
        map.AddShape(veShape);
        visible = true;
        
       
        
    }
    
    function updateShapeInfo(){
        if (veShape){
            var clearLink = "<br><a href='javascript:vmShapeList.clearShapeById(\"" + id + "\");'>Clear</a>";    
            veShape.SetTitle(name);
            veShape.SetDescription(details + clearLink);
            if (iconPath){
                veShape.SetCustomIcon(iconPath);
            }
        }
    }
    
    function clearShape(){
        if (visible == false ){
            return;
        }
        if (veShape){
            //alert ("delete" + id);
            map.DeleteShape(veShape);
            veShape = null;
        }
        visible = false;
        //hide the infobox, this function in VMEvents
        hideInfoBox();
    }
    
    
}
function VMShapeList(){
    var list = new Vector(0);
    
    this.addShapeObj = addShapeObj;
    this.addShape = addShape;
    this.addShapeWithVEObj = addShapeWithVEObj;
    this.removeShape = removeShape;
    this.getIndexOf = getIndexOf;
    this.showShapeById = showShapeById;
    this.clearShapeById = clearShapeById;
    this.clearAll = clearAll;
    
    //add shape to list
    function addShapeObj(shapeObj){
              
        if (!contains(shapeObj)){
            list.addElement(shapeObj);
        }
    }
    function contains(shapeObj){
        var index = getIndexOf(shapeObj.getId());
        //alert ("contain" + (index > -1));
        return( index > -1);        
    }
    function addShape(theId, theName, theDetails, theWKT, theVEShapeType, theAppShapeType){
        //alert ("addShpe" + theId);
        addShapeObj(new VMShape(theId, theName, theDetails, theWKT, theVEShapeType, theAppShapeType));
    }
    function addShapeWithVEObj(theId, theName, theDetails, theWKT, theVEShape, theAppShapeType){
        var shape = new VMShape(theId, theName, theDetails, theWKT, theVEShape.GetType(), theAppShapeType);
        shape.setVEShape(theVEShape);
        shape.setVisibility(true);
        shape.updateShapeInfo();
        
        addShapeObj(shape);
    }
    //remove shape should also clear it from map
    function removeShape(shapeId){
        var index = getIndexOf(shapeId);
        if ( index== -1){
            return;
        }
        list.getElementAt(index).clearShape();
        list.removeElementAt(index);  
        
    }
    function getIndexOf(shapeId){
        for (var i = 0; i < list.getSize(); i++){
            var shape = list.getElementAt(i);
            if (shape.getId () == shapeId){
                //alert ("found" + i);
                return i;
            }
        }
        //alert ("not found, length" + i);
        return -1;
    }
    function showShapeById(id){
        var index = getIndexOf(id);
        if ( index== -1){
            return;
        }
        list.getElementAt(index).showShape();
    }
    function clearShapeById(id){
        var index = getIndexOf(id);
        if ( index== -1){
            return;
        }
        list.getElementAt(index).clearShape();
    }
    
    function clearAll(){
        //alert ("clear all" + list.getSize());
        for (var i = 0 ; i < list.getSize(); i++){
            list.getElementAt(i).clearShape();              
        }
    }
}
