

BDPMap = function () {};

BDPMap.prototype.RegStreet = '[-\\w., äöüß]';
BDPMap.prototype.RegCity = '[-\\w., äöüß]';
BDPMap.prototype.provider = 'GoogleMaps';
BDPMap.prototype.windows = new ArrayA();
BDPMap.prototype.resizeables = [];
BDPMap.prototype.minHeight = 480;
BDPMap.prototype.mainContainer = null;
BDPMap.prototype.searchFormContainer = null;
BDPMap.prototype.searchResultsContainer = null;
BDPMap.prototype.featuresControl = null;
BDPMap.prototype.searchRequest = window.location.getParameter('search');
BDPMap.prototype.startCLoc = window.location.getParameter('cLoc');
BDPMap.prototype.startBookmark = (window.location.getParameter('longitude') && window.location.getParameter('latitude') && window.location.getParameter('zoom'));

BDPMap.prototype.setContainerSize = function () {
	var windowSize = renderer.getWindowSize();
	var wrap = renderer.assimilate(document.getElementById('wrap'));
	var screenOffsetTop;
	var borderWidth = this.mainContainer.getCSSValue('borderTopWidth', 'int') + this.mainContainer.getCSSValue('borderBottomWidth', 'int');
	var footHeight = cssHandler.getCSSValue(document.getElementById('footer'), 'height', 'int');
	var navi = document.getElementById('navi');
	var naviWidth = navi.offsetWidth;
	var naviMarginRight = cssHandler.getCSSValue(navi, 'marginRight', 'int');
	var content = renderer.assimilate(document.getElementById('right'));
	var contentMarginBottom = content.getCSSValue('marginBottom', 'int');
	var contentPaddingRight = content.getCSSValue('paddingRight', 'int');
	var contentPaddingLeft = content.getCSSValue('paddingLeft', 'int');
	var contentPaddingBottom = content.getCSSValue('paddingBottom', 'int');
	var contentPaddingTop = content.getCSSValue('paddingTop', 'int');
	var scrollBarSpace = 0;
	
	document.body.style.width = windowSize.width + 'px';
	document.body.style.height = windowSize.height + 'px';
	
	if (this.mainContainer != null){
		var coords = null; 
	    if (typeof this.map != 'undefined' && typeof this.map.map != 'undefined') {
	    	coords = this.map.getCoordinates();
	        if (this.map.initialLoading == 0 && this.map.fullScreen == true) {
	            document.getElementById('head').style.display = 'none';
	            document.getElementById('footer').style.display = 'none';
	            document.getElementById('tips').style.display = 'none';
	            footHeight = 0;
	            wrap.style.width = windowSize.width + 'px';
	            content.style.width = this.mainContainer.style.width = windowSize.width - naviWidth - naviMarginRight - contentPaddingRight - contentPaddingLeft - scrollBarSpace + 'px';
	            document.getElementsByTagName('h1')[0].style.width = parseInt(content.style.width) - 10 + 'px';
	            content.style.marginTop = navi.style.top = 0;
	        } else {
	            this.searchResultsContainer.style.top = '';
	            document.getElementById('head').style.display = '';
	            document.getElementById('footer').style.display = '';
	            document.getElementById('tips').style.display = '';
	            document.getElementsByTagName('h1')[0].style.width = '';
	            content.style.width = content.style.marginTop = navi.style.top = wrap.style.width = '';
	            this.mainContainer.style.width = this.map.defaultContainerWidth;
	        }
	        if (typeof this.map.throbber != 'undefined') {
	            this.map.throbber.style.left = (this.map.throbber.visible ? this.mainContainer.offsetWidth - 43 : this.mainContainer.offsetWidth) + 'px';
	        }
	        if (typeof this.map.hint != 'undefined') {
	            this.map.hint.style.left = Math.round((this.mainContainer.offsetWidth-45)/2) + 'px';
	        }
	    }
	    screenOffsetTop = content.getScreenOffset().y;
	    var containerHeight = windowSize.height - screenOffsetTop - borderWidth - footHeight - contentPaddingTop - contentPaddingBottom - contentMarginBottom;
	    containerHeight = (containerHeight >= this.minHeight ? containerHeight > 1000 ? 1000 : containerHeight : this.minHeight) + 'px';
	    
	    if (parseInt(containerHeight) <= this.minHeight) {
	    	document.body.style.overflow = 'auto';
	    } else { 
	    	document.body.style.overflow = 'hidden';
	    };
	    
	    for (var  i=0; i<this.resizeables.length;i++) {
	        if (typeof this.resizeables[i].container != 'undefined' && this.resizeables[i].container != null) {
	            this.resizeables[i].container.style.height = containerHeight;
	        }
	        if (document.getElementById('contentarea')) {
	            document.getElementById('contentarea').style.height = containerHeight;
	        }
	    }
	    if (typeof this.map != 'undefined' && typeof this.map.map != 'undefined') {
	        google.maps.event.trigger(this.map.map, "resize");
	        this.map.zoomToCoordinateRect(coords);
	    }
	}
	if (this.searchResultsContainer != null) {
	    screenOffsetTop = this.searchResultsContainer.getScreenOffset().y;
	    this.searchResultsContainer.style.height = ((windowSize.height - screenOffsetTop) >= this.minHeight ? (windowSize.height - screenOffsetTop) : this.minHeight) + 'px';
	}
	if (this.tipsContainer != null) {
	    screenOffsetTop = this.tipsContainer.getScreenOffset().y;
	    this.tipsContainer.style.height = ((windowSize.height - screenOffsetTop) >= this.minHeight ? (windowSize.height - screenOffsetTop) : this.minHeight) + 'px';
	}
	if (this.featuresControl != null) {
	    var navteqBarHeight = 21;
	    this.featuresControl.style.top = (this.map.featuresControl.show ? (this.mainContainer.offsetHeight - this.featuresControl.offsetHeight - navteqBarHeight) : (this.mainContainer.offsetHeight - 20 - navteqBarHeight)) + 'px';
	    this.featuresControl.style.left = (this.map.featuresControl.show ? (this.mainContainer.offsetWidth - this.featuresControl.offsetWidth) : this.mainContainer.offsetWidth-20) + 'px';
	}
	if (typeof this.map != 'undefined' && typeof this.map.updateView == 'function' && this.map.initialLoading == false) {
	    if (typeof arguments[0] != 'undefined') {
	        this.map.updateView();
	    } else {
	        this.map.updateView();
	    }
	}
};

BDPMap.prototype.addCompaniesAction = function (callback) {
    callback = callback || 'initCLoc';
    var searchResults = getElementsByClassName('searchResult'), links, link;
    if (searchResults && searchResults.length > 0) {
        for (var i=0;i<searchResults.length;i++) {
            links = searchResults[i].getElementsByTagName('a');
            for (var n=0;n<links.length;n++) {
                link = links[n];
                eventHandler.addEvent(link, 'click', function (e, link) {
                    e = eventHandler.getEvent(e);
                    e.preventDefault();
                    if (link.href.match(/^.*?([0-9]+)\.html$/)) {
                        if (typeof callback == 'function') callback(RegExp.$1);
                        else this.map.call(callback, RegExp.$1);
                    }
                }.bindTo(this, link));
            }
        }
    }
};

BDPMap.prototype.loadMap = function () {
    if (document.getElementById('indexMapBlank')) {
        document.getElementById('indexMapBlank').title = 'Stadtplansuche';
        document.getElementById('indexMapBlank').onclick = function () {
            BDPMap.Search.Address.init();
        };
        var height = this.mainContainer.getClientSize().Height;
        var mapHeight = Math.ceil(height/100)*100;
        var mapOffset = Math.floor((mapHeight-height)/2);
        var indexMap = document.getElementById('indexMap');
        var mapData = [];
        this.mainContainer.style.backgroundImage = this.mainContainer.getCSSValue('backgroundImage').replace(/x[0-9]{3,4}/, 'x'+mapHeight);
        this.mainContainer.style.backgroundPosition = '0 -'+mapOffset+'px';
        renderer.empty(indexMap);
        BDPMap.mapRequest = new HttpRequest();
        BDPMap.mapRequest.init(function (response) {
            response = JSON.parse(response);
            for (var i=0;i<response.length;i++) {
                mapData[i] = {};
                mapData[i].coords = response[i].coords;
                mapData[i].name = response[i].name;
                mapData[i].lon = response[i].Longitude;
                mapData[i].lat = response[i].Latitude;
                mapData[i].width = response[i].width;                       
                var area = renderer.createElement('area', {
                    "shape":"rect",
                    "coords":mapData[i].coords,
                    "href":"javascript:void(0)",
                    "alt":"Stadtplan \""+mapData[i].name+" aufrufen",
                    "title":"Stadtplan \""+mapData[i].name+"\" aufrufen",
                    "onclick":function (i) {
                        return function (e) {
                            e=eventHandler.getEvent(e);
                            e.preventDefault();        
                            e.stopPropagation();
                            BDPMap.map.simpleLocation({'tileState':'HYBRID','Longitude':parseFloat(mapData[i].lon),'Latitude':parseFloat(mapData[i].lat),'width':parseInt(mapData[i].width)});
                        };
                    }.call(this, i)
                });
                area.renderIn(indexMap);
            }
        }.bindTo(this));
        this.mainContainer.getCSSValue('backgroundImage').match(/([0-9]{3})x([0-9]{3,4})/);
        var x = RegExp.$1, y=RegExp.$2;
        this.indexMapName = x+'x'+y;
        BDPMap.mapRequest.load('indexMap_'+this.indexMapName+'.html');
        document.getElementById('indexMapBlank').style.width = x + 'px';
        document.getElementById('indexMapBlank').style.height = y + 'px';
        document.getElementById('indexMapBlank').style.top = '-'+mapOffset+'px';
        document.getElementById('indexMapBlank').style.border = 'none';
    }
};


BDPMap.prototype.unload = function () {
    BDPMap.map = null;
    Map24 = null;
    BDPMap = null;
    contentLoader = null;
    SubPageLinkProcessor = null;
    renderer = null;
    eventHandler.flushAll();
    eventHandler = null;
};
/**
 * Parse the Ajax response and hide the throbber
 *
 * @param  response   STRING    JSON response
 * @param throbber   Object    the throbber to hide on fault
 */
BDPMap.prototype.parseAjaxResponse = function (response, throbber) {
    throbber = throbber || this.map.throbber;
    try {
        response = JSON.parse(response);
        if (typeof response == 'object' &&
            (   response.status == 200 ||
                response.status == 'ok' ||
                (   typeof response.count != 'undefined' &&
                    response.count > 0
                )
            )
        ) {
            throw true;
        } else {
            throw false;
        }
    } catch (e) {
        if (e !== true) {
            throbber.hide();
            return false;
        } else {
            return response;
        }
    }
};


BDPMap.prototype.teaseMap = function () {};

BDPMap.prototype.MapTeaser = function () {};
BDPMap.prototype.MapTeaser.prototype.header = renderer.createElement('h4', {
    'id':'mapTeaserHeader',
    'innerHTML':'Ihre virtuelle Rundreise...'
});
BDPMap.prototype.MapTeaser.prototype.container = renderer.createElement('div', {'id':'mapTeaser'});
BDPMap.prototype.MapTeaser.prototype.textBox = renderer.createElement('div', {'id':'mapTeaserText'});
BDPMap.prototype.MapTeaser.prototype.text = new ArrayA(
    'führt Sie über Firmen in Ihrer Nachbarschaft...<br />oder Branchen Ihrer Umgebung...',
    'über Bilder Ihres Wohnortes...<br />zu Fotos aus Ihrer Umgebung.',
    'Sehen Sie Ihre Straße aus der Luft<br />und Ihren Arbeitsplatz von oben.'
);
BDPMap.prototype.MapTeaser.prototype.init = function () {
    var i = 0, fr = null;
    this.header.renderIn(this.container);
    this.textBox.innerHTML = this.text[i];
    this.textBox.renderIn(this.container);
    this.container.style.width = BDPMap.mainContainer.offsetWidth + 'px';
    this.container.renderIn(BDPMap.mainContainer);
    this.interval = window.setInterval(function () {
        this.textBox.onfadeready = function () {
            if (this.textBox.getOpacity() === 0) {
                i = i+1>=this.text.length?0:i+1;
                this.textBox.innerHTML = this.text[i];
            }
            this.textBox.onfadeready = undefined;
            this.textBox.fade();
        }.bindTo(this);
        this.textBox.fade();
    }.bindTo(this), 6000);
};

BDPMap.prototype.WindowManager = function () {};
BDPMap.prototype.WindowManager.prototype.buffer = new ArrayA();
BDPMap.prototype.WindowManager.prototype.minZindex = 5000;
BDPMap.prototype.WindowManager.prototype.register = function (newWin) {
    
    newWin.BDPWindowIndex = this.buffer.length;
    newWin.draggable.style.zIndex = this.minZindex+this.buffer.length+1;
    this.buffer.push(newWin);
    
    eventHandler.addEvent(newWin.draggable, 'remove', function () {
        BDPMap.WindowManager.unregister(this.BDPWindowIndex);
    }.bindTo(newWin));
    eventHandler.addEvent(newWin.draggable, 'mousedown', function () {
        BDPMap.WindowManager.focus(this.BDPWindowIndex);
    }.bindTo(newWin));
    BDPMap.WindowManager.focus(newWin.BDPWindowIndex);
};
BDPMap.prototype.WindowManager.prototype.unregister = function (index) {
    this.buffer.splice(index, 1);
    this.focus(0);
};
BDPMap.prototype.WindowManager.prototype.focus = function (index) {
    if (this.buffer.length == 0) {
        return;
    }
    var focusWin = this.buffer[index];
    this.buffer.splice(index, 1);
    var winTmp = new ArrayA;
    this.buffer.foreach(function (current) {
        if (current != focusWin && !current.dragHandle.hasClassName('backgroundWin')) {
            current.dragHandle.addClassName('backgroundWin');
        }
        current.BDPWindowIndex = winTmp.length;
        current.draggable.style.zIndex = this.minZindex+winTmp.length+1;
        winTmp.push(current);
    }.bindTo(this));
    focusWin.BDPWindowIndex = winTmp.length;
    focusWin.draggable.style.zIndex = this.minZindex+winTmp.length+1;
    focusWin.dragHandle.removeClassName('backgroundWin');
    winTmp.push(focusWin);
    this.buffer = winTmp;
    winTmp = undefined;
};
BDPMap.prototype.WindowManager.prototype.rearrange = function () {
    //var winSize = renderer.getWindowSize();
    this.buffer.foreach(function (current) {
        var pos = current.draggable.getScreenOffset();
        
        var dragHandleOffset = current.dragHandle.getCustomOffset(current.draggable);
        
        if (pos.x < current.borderPadding) pos.x = current.borderPadding;
        else if (pos.x + current.dragHandle.offsetWidth + dragHandleOffset.x + current.borderPadding > current.border.clientWidth)
            pos.x = current.border.clientWidth-current.dragHandle.offsetWidth-dragHandleOffset.x-current.borderPadding;
        if (pos.y < current.borderPadding) pos.y = current.borderPadding;
        else if (pos.y + current.dragHandle.offsetHeight + dragHandleOffset.y + current.borderPadding > current.border.clientHeight)
            pos.y = current.border.clientHeight-current.dragHandle.offsetHeight-dragHandleOffset.y-current.borderPadding;
        
        current.draggable.style.left = pos.x + 'px';
        current.draggable.style.top = pos.y + 'px';
    });
};

BDPMap.Root = function (noPush) {
    if (!noPush)
        BDPMap.resizeables.push(this);
};

BDPMap.Root.prototype.key = '';

eventHandler.addLoadEvent(function () {
    BDPMap.WindowManager = new BDPMap.WindowManager();
    BDPMap.Search = new BDPMap.Search();
    BDPMap.Search.Business = new BDPMap.Search.Business();
    BDPMap.Search.Address = new BDPMap.Search.Address();
    BDPMap.MapTeaser = new BDPMap.MapTeaser();

    //contentLoader.processBreadCrumbs();
    SubPageLinkProcessor.init();
	document.getElementById('right').style.overflow = 'hidden';
    BDPMap.mainContainer = renderer.assimilate(document.getElementById('maparea'));
    BDPMap.searchResultsContainer = renderer.assimilate(document.getElementById('searchResults'));
    BDPMap.resizeables.push({'container':BDPMap.mainContainer});
    BDPMap.tipsContainer = renderer.assimilate(renderer.getElement('.resultListContainer', renderer.getElement('#tips')));
    BDPMap.setContainerSize();
    BDPMap.socialBookmarksContainer = document.getElementById('socialBookmarks');
    BDPMap.socialBookmarks = BDPMap.socialBookmarksContainer.innerHTML;
    document.body.className = 'bdpMapBody';
    eventHandler.addEvent(window, 'resize', function () {
        if (typeof setContainerSizeTimeout != 'undefined') window.clearTimeout(setContainerSizeTimeout);
        setContainerSizeTimeout = window.setTimeout(function () {
            BDPMap.setContainerSize();
            BDPMap.loadMap();
            BDPMap.WindowManager.rearrange();
            if (typeof BDPMap.map.map != 'undefined') {
            	google.maps.event.trigger(BDPMap.map.map, 'resize');
            }
        }, 500);
    });
    BDPMap.loadMap();
    BDPMap.addCompaniesAction();
    BDPMap.MapTeaser.init();
    BDPMap.map = new BDPMap[BDPMap.provider]();
    if (!document.getElementById('contentarea')) {
        if (BDPMap.searchRequest) {
            var oldSR;
            do {
                oldSR = BDPMap.searchRequest;
                BDPMap.searchRequest = decodeURIComponent(BDPMap.searchRequest);
            } while (oldSR!=BDPMap.searchRequest);
            BDPMap.map.call(BDPMap.map.search, BDPMap.searchRequest);
        } else if (BDPMap.startCLoc) {
            BDPMap.map.call(BDPMap.map.initCLoc, BDPMap.startCLoc);
        } else if (BDPMap.startBookmark) {
            BDPMap.map.init();
        }
        else {
            BDPMap.Search.Address.init();
        }
    } else if (BDPMap.RegCustomerDeepLink.test(location.pathname.replace(/\.html$/, '').replace(/^\/suche-/, ''))) {
        var cLocId = RegExp.$7, openerMap=undefined;
        try {
            openerMap = typeof window.opener.BDPMap;
        } catch (e) {}
        if (
            (
                window.opener == null ||
                openerMap == undefined
            ) &&
            cLocId != '' &&
            !isNaN(cLocId)
        ) {
            window.setTimeout(function () {
                BDPMap.map.call('initCLoc', cLocId);
            }, 1000);
        }
    };
    //BDPMap.map.init();
/*    
    var x=0;
    
    window.prefetchInterval = window.setInterval(function() {
        BDPMap.map.prefetch();
        x++;
        if (x==2000) {
            window.clearInterval(prefetchInterval);
        }
    }, 1000);
*/
});
BDPMap.prototype.error = function (a,b,c) {
    if (typeof this.map != 'undefined' && this.map instanceof this[this.provider]
        && typeof this.map.throbber != 'undefined' && this.map.throbber instanceof this.Throbber) {
        this.map.throbber.hide(true);
    }
};
eventHandler.addEvent(window, 'error', BDPMap.error);









