/* Copyright (C) 2007 con terra GmbH (http://www.conterra.de)
 * All rights reserved
 * $Id: $
 */
/**************************************************/
// File contains base vboris classes and utility functions.
//
// package dependencies:
//          conterra.util       (ct-util.js)
//          conterra.lang       (ct-lang.js)
/**************************************************/
// Namespaces
/**************************************************/
/* define the conterra namespace */
if (!conterra) {
    var conterra = {};
}
/* define the conterra.util namespace */
if (!conterra.vboris) {
    conterra.vboris = {};
}

var mMapthemeValue_TopoMap     = "1";
var mMapthemeName_TopoMap      = "Totographische Karte";
var mMapthemeValue_CityMap     = "3";
var mMapthemeName_CityMap      = "Stadtplan";
var mMapthemeValue_AerialPhoto = "6";
var mMapthemeName_AerialPhoto  = "Luftbild";
var mBackgroundThemeMaxScaleCityMap = 20000;

function makeNextScale(pScale) {
    var scaleStr = pScale.substring(2).replace(/\./g, "");
    var scaleInt = parseInt(scaleStr);
    var nextScaleInt = scaleInt + scaleInt / 2 + 2;
    return "1:" + nextScaleInt;
}

/*
The Portal class is used to encapsulate methods common within the portal sites and frames.
The portal frames are the 'Gemeinde'-Search, 'Address'-Search and 'Flurstuecks'-Search.
A instance of Portal is created in the 'mainlayout.jsp' it is accessible through the variable 'portal'
*/
conterra.lang.defclass('conterra.vboris.Portal', {
// constructor (empty)
    ctor: function () {
    },
// the methods
    methods: {
    // a simple refresh method, it invokes a refresh of the map and of the tools frame.
        refreshMap : function() {
            var mapframe = conterra.util.dom.element('mapiframe', false);
            if (mapframe) {
                mapframe.contentWindow.frames['IMCTitleFrame'].map.refresh();
                mapframe.contentWindow.frames['ContentFrameSet'].document.forms[0].submit();
            }
        }
    }
});
/* simple helper to refresh the map from the contentframes (boris)*/
conterra.vboris.refreshMap = function() {
    mTitleFrame.map.refresh();
}
/* The map is instanciated in the m_Titleframe and will be updated frequently.
   It defines some usefull method to make it easier to use the mapclient scripts.
*/
conterra.lang.defclass('conterra.vboris.Map', {
    ctor: function (refreshurl) {
        this._refreshUrl = refreshurl;
    },
    methods: {
    // a simple refresh method.
        refresh : function() {
            mTitleFrame.refreshMap(this._refreshUrl);
        },
    // this is invoked by the click zoom buttons in the tool frame
    // it simple changes the selectedIndex through incrementation.
        zoomClick : function(aform, increment) {
            var aselect = aform.currentScale;
            var index = aselect.selectedIndex;
            var newindex = index + increment;
            var size = aselect.options.length;
            if (newindex > (size - 1) || newindex < 0) {
                return;
            }
            else {
                aselect.selectedIndex = newindex;
                aselect.onchange();
            }
        },
    // this is invoked by the click buttons in the tool frame
        fullExtentClick : function(aform) {
            if (aform.currentTheme.value == mMapthemeValue_CityMap)
                aform.currentTheme.value = mMapthemeValue_TopoMap;
            aform.cmd.value = 'changeBackgroundTheme|zoomFullExtent';
            aform.submit();
        },
    // this is invoked by the click buttons in the tool frame
        previousExtentClick : function(aform) {
            aform.cmd.value = 'zoomPreviousExtent';
            aform.submit();
        }
    }
});
/*BoRiFeatureCollection*/
conterra.lang.defclass('conterra.vboris.BoRiFC', {
    ctor: function (xTolerance, yTolerance) {
    	this._featureArray = new Array();
    	this._xTolerance = xTolerance;
    	this._yTolerance = yTolerance;
    },
// the methods
    methods: {
    // a method
        addFeature : function(feature) {
        	this._featureArray.push(feature);
        },
        getFeatureForPos : function(mouseX, mouseY) {
        	for (idx in this._featureArray) {
        		var feature = this._featureArray[idx];
        		if (feature.matchesPos(mouseX, mouseY, this._xTolerance, this._yTolerance)) {
        			return feature;
        		}
        	}
        	return null;
        }
    }
});
/*BoRiFeature*/
conterra.lang.defclass('conterra.vboris.BoRiFeature', {
    ctor: function (fID, fType, xCoord, yCoord) {
    	this._fID = fID;
    	this._fType = fType;
    	this._xCoord = xCoord;
    	this._yCoord = yCoord;
    },
// the methods
    methods: {
    // a method
        getID : function() {
            return this._fID;
        },
        getType : function() {
            return this._fType;
        },
        getXCoord : function() {
            return this._xCoord;
        },
        getYCoord : function() {
            return this._yCoord;
        },
        matchesPos : function(xCoord, yCoord, xTolerance, yTolerance) {
        	if (conterra.util.isInRange(xCoord, this._xCoord - xTolerance, this._xCoord + xTolerance) &&
        			conterra.util.isInRange(yCoord, this._yCoord - yTolerance, this._yCoord + yTolerance)) {
        		return true;
        	}
        	return false;
        }
    }
},
        /* Hover BG from startpage product table and video help table   */
        conterra.vboris.hoverTD = function() {
            var theTable = document.getElementsByTagName('TABLE');
            if (theTable) {
                for (var t = 0; t < theTable.length; t++) {
                    if (theTable[t].className == "hoverTable") {
                        theTable[t].getElementsByTagName("TD");
                        for (var u = 0;u < theTable[t].getElementsByTagName("TD").length; u++) {
                            if (theTable[t].getElementsByTagName("TD")[u].className != "noHover") {
                                theTable[t].getElementsByTagName("TD")[u].onmouseover = function () {
                                    this.className = "hoverTD";
                                }
                                theTable[t].getElementsByTagName("TD")[u].onmouseout = function () {
                                    this.className = "nohoverTD";
                                }
                            }
                        }
                    }
                }
            }
        },

        /* toggle login panel */

        conterra.vboris.toggleLoginPanel = function() {
            var loginLink = document.getElementById("loginLink");

            var loginWrapper = document.getElementById("loginDIV");

            if (loginLink !=undefined){

            loginLink.onclick = function (){
               loginWrapper.style.display = "block";
            } ;

            loginLink.onmouseover = function (){
               loginWrapper.style.display = "block";
            } ;


            loginLink.onblur = function (){
               loginWrapper.style.display = "none";
            } ;


           loginLink.onmouseout = function (){
               loginWrapper.style.display = "none";
            } ;

            }

        }
   );
/*IrwFeatureCollection*/
conterra.lang.defclass('conterra.vboris.IrwFC', {
    ctor: function (xTolerance, yTolerance) {
    	this._featureArray = new Array();
    	this._xTolerance = xTolerance;
    	this._yTolerance = yTolerance;
    },
// the methods
    methods: {
    // a method
        addFeature : function(feature) {
        	this._featureArray.push(feature);
        },
        getFeatureForPos : function(mouseX, mouseY) {
        	for (idx in this._featureArray) {
        		var feature = this._featureArray[idx];
        		if (feature.matchesPos(mouseX, mouseY, this._xTolerance, this._yTolerance)) {
        			return feature;
        		}
        	}
        	return null;
        }
    }
});
/*IrwFeature*/
conterra.lang.defclass('conterra.vboris.IrwFeature', {
    ctor: function (fID, fType, xCoord, yCoord) {
    	this._fID = fID;
    	this._fType = fType;
    	this._xCoord = xCoord;
    	this._yCoord = yCoord;
    },
// the methods
    methods: {
    // a method
        getID : function() {
            return this._fID;
        },
        getType : function() {
            return this._fType;
        },
        getXCoord : function() {
            return this._xCoord;
        },
        getYCoord : function() {
            return this._yCoord;
        },
        matchesPos : function(xCoord, yCoord, xTolerance, yTolerance) {
        	if (conterra.util.isInRange(xCoord, this._xCoord - xTolerance, this._xCoord + xTolerance) &&
        			conterra.util.isInRange(yCoord, this._yCoord - yTolerance, this._yCoord + yTolerance)) {
        		return true;
        	}
        	return false;
        }
    }
},
        /* Hover BG from startpage product table and video help table   */
        conterra.vboris.hoverTD = function() {
            var theTable = document.getElementsByTagName('TABLE');
            if (theTable) {
                for (var t = 0; t < theTable.length; t++) {
                    if (theTable[t].className == "hoverTable") {
                        theTable[t].getElementsByTagName("TD");
                        for (var u = 0;u < theTable[t].getElementsByTagName("TD").length; u++) {
                            if (theTable[t].getElementsByTagName("TD")[u].className != "noHover") {
                                theTable[t].getElementsByTagName("TD")[u].onmouseover = function () {
                                    this.className = "hoverTD";
                                }
                                theTable[t].getElementsByTagName("TD")[u].onmouseout = function () {
                                    this.className = "nohoverTD";
                                }
                            }
                        }
                    }
                }
            }
        },

        /* toggle login panel */

        conterra.vboris.toggleLoginPanel = function() {
            var loginLink = document.getElementById("loginLink");

            var loginWrapper = document.getElementById("loginDIV");

            if (loginLink !=undefined){

            loginLink.onclick = function (){
               loginWrapper.style.display = "block";
            } ;

            loginLink.onmouseover = function (){
               loginWrapper.style.display = "block";
            } ;


            loginLink.onblur = function (){
               loginWrapper.style.display = "none";
            } ;


           loginLink.onmouseout = function (){
               loginWrapper.style.display = "none";
            } ;

            }

        }
   );

