﻿//Class for adding a tile on the map
//2 options for adding a tile":
//  + whereClause  
//  + (whereValues, whereColName, whereSeperator) this needed where the list is so long (URL too long problem)

function VMTile (theMap, id){
    var map = theMap;

    var tileLayerId = id;
    var tableName;
    var tableToUpdate;
    var whereClause;
    var themeColName;
    var shapeType;
    var opacity;
    var zIndex;
    var iconType;
    var sameIcon;
    var colorHexColName;
    
    var whereValues;
    var whereSeperator;
    var whereColName;
    
    this.addTile = addTile;
    this.deleteTile = deleteTile;
    this.setTileLayerId = setTileLayerId;
    this.setTableName = setTableName;
    this.setTableToUpdate = setTableToUpdate;
    
    this.setWhereClause = setWhereClause;
    this.setThemeColName = setThemeColName;
    this.setColorHexColName = setColorHexColName;
    this.setShapeType = setShapeType;
    this.setIconType = setIconType;
    this.setSameIcon = setSameIcon;
    
    this.setOpacity = setOpacity;
    this.setZIndex = setZIndex;
    
    this.setWhereValues = setWhereValues;
    this.setWhereSeperator = setWhereSeperator;
    this.setWhereColName = setWhereColName;
    
    
    this.addTile = addTile;
    this.deleteTile = deleteTile;
    this.getTileLayerId = getTileLayerId;
    this.getTableName = getTableName;
    this.getWhereClause = getWhereClause;
    this.getThemeColName = getThemeColName;
    this.getShapeType = getShapeType;
    this.getOpacity = getOpacity;
    this.getZIndex = getZIndex;
    
    var tileSourceSpec;
    
    function addTile(theMap, minZoom, maxZoom)         
    {          
	    try
	    {   				    
		    
		    tileSourceSpec = new VETileSourceSpecification(tileLayerId, "");   
		    
		    tileSourceSpec.ID = tileLayerId;		    
		    tileSourceSpec.GetTilePath = getTilePath;                                   
		    tileSourceSpec.NumServers = 1;
		    tileSourceSpec.MinZoom = minZoom;
		    tileSourceSpec.MaxZoom = maxZoom;       
    		tileSourceSpec.Opacity = opacity;
    		
    		if (zIndex) {    		    
    		    tileSourceSpec.ZIndex = zIndex;    		    
    		}
	        theMap.AddTileLayer(tileSourceSpec, true);	
	        
	        if (isIE6OrLower()){
	            fixTiles();
	        }
	        
	    }
	    catch(ex)
	    {
		    alert("Exception in addTile function:\n tile id:" + tileLayerId + " table name: " + tableName + "\n Error msg:'" + ex.message + "'");
	    }    
    }
    
    function getTilePath (tileContext)
    {                                            
       if(tileContext != null && tileContext != "undefined")
       {                                                 
          var cell = 0;
          var key = "X=" + tileContext.XPos + "&Y=" + tileContext.YPos + "&ZoomLevel=" + tileContext.ZoomLevel
          var path = "AspNet/VMTile.ashx?TABLENAME=" + tableName + "&TABLETOUPDATE=" + tableToUpdate + "&WHERECLAUSE=" + whereClause+ "&THEMECOLNAME=" + themeColName+ "&COLORHEXCOLNAME=" + colorHexColName + "&WHEREVALUES=" + whereValues+ "&WHERECOLNAME=" + whereColName+ "&WHERESEPERATOR=" + whereSeperator+ "&SHAPETYPE=" + shapeType+  "&ICONTYPE=" + iconType  + "&SAMEICON=" + sameIcon + "&TILEID=" + tileLayerId + "&"+ key;
          //alert (path);
          //displayMsgBox(path);
          return path;
       }
    }        
    
    function deleteTile(){       
        try {
        map.DeleteTileLayer(tileLayerId);        
        }
	    catch(ex)
	    {
		    //alert(ex.message);
	    }                
    }
    
    // set and gets method
    function setTileLayerId(id){
        tileLayerId = id;
    }
    function setTableName(name){
        tableName = name;
    }    
    function setTableToUpdate(name){
        tableToUpdate = name;
    }
    function setWhereClause(clause){
        whereClause = clause;
    }
    function setThemeColName(name){
        themeColName = name;
    }
    function setColorHexColName(name){
        colorHexColName = name;
    }
    function setShapeType (type){
        shapeType = type;
    }
    function setIconType (type){
        iconType = type;
    }
    function setSameIcon(b){
        sameIcon = b;
    }
    function setOpacity (op){
        opacity = op;
    }
    function setZIndex(index){
        zIndex = index;
        if (tileSourceSpec){
            alert ("set iz");
            tileSourceSpec.ZIndex = index;
        }
    }
    
    function setWhereValues(values){
        whereValues = values;
    }
    function setWhereSeperator( s){
        whereSeperator = s;
    }
    function setWhereColName (col){
        whereColName  = col;
    }
    
    function getTileLayerId(id){
        return tileLayerId ;
    }
    function getTableName(tableName){
        return tableName ;
    }    
    function getWhereClause(clause){
        return whereClause ;
    }
    function getThemeColName(colName){
        return themeColName ;
    }
    function getShapeType (type){
        return shapeType ;
    }
    function getOpacity (op){
        return opacity ;
    }
    function getZIndex(){
        //alert (zIndex);
        return zIndex ;
    }
    
    

}