﻿/// <reference assembly="System.Web.Extensions" name="MicrosoftAjax.js"/>
Type.registerNamespace('Geodata.Component.ADF');

Geodata.Component.ADF.FullScreenController = function(mapId, mapWidthOffset, mapHeightOffset) {
    this.mapWidthOffset = mapWidthOffset;
    this.mapHeightOffset = mapHeightOffset;

    this.MapId = mapId;
    this.map = null; 
	this.MapDisplay = null;
	this.LastMapWidth = 512;
	this.LastMapHeight = 512;
	this.CurrentMapWidth = 512;
	this.CurrentMapHeight = 512;
	this.reloadTimer = null;

	this.setup = function() {
	    var mapElement = document.getElementById(this.MapId);
	    if(!(mapElement)) {
	        alert('Error: Map with id ' + this.MapId + ' was not found by full screen controller.');
	        return;
	    }
	    var div = mapElement.parentNode;
	    if(!(div) || div.tagName != "DIV") {
	        alert('Error: Map control is not placed inside a DIV element. This is necessary for the full screen controller to work.');
	        return;
	    }
	    this.MapDisplay = div;
	    this.MapDisplay.style["overflow"] = "hidden";
	    this.MapDisplay.style["position"] = "absolute";
        this.setMapSize(); 
        // set body style 
        if (document.documentElement) {
            document.documentElement.style.overflow = "hidden";
            document.documentElement.style.height = "100%"; 
        } else {
            document.body.style.overflow = "hidden";
            document.body.style.height = "100%";
        }
        Sys.Application.add_init(function() {
            fullScreenController.map = $find(fullScreenController.MapId);
            window.setTimeout('$addHandler(window,"resize", fullScreenController.adjustMapSizeHandler);', 1000); 
        });
	}
	
    this.setMapSize = function() {
        this.LastMapWidth = this.CurrentMapWidth;
        this.LastMapHeight = this.CurrentMapHeight;
        // get browser window dimensions 
	    var sWidth = this.getPageWidth();
	    var sHeight = this.getPageHeight();
	    var mHeight = sHeight - this.mapHeightOffset;
	    var mWidth = sWidth - this.mapWidthOffset;
	    if (mWidth<5) mWidth = 5;
	    if (mHeight<5) mHeight = 5;  
        this.MapDisplay.style.width = mWidth + "px";
        this.CurrentMapWidth = mWidth; 
	    this.MapDisplay.style.height = mHeight  + "px";
        this.CurrentMapHeight = mHeight;
    }
    
	// function for adjusting element sizes when brower is resized
	this.adjustMapSize = function() {
        this.setMapSize();
	    if (this.LastMapWidth!=this.CurrentMapWidth || this.LastMapHeight!=this.CurrentMapHeight) {
	        // Raise map resized event for use by interested parties?
	    }
	    // refresh the map 
	    if (this.map!=null) {
	        if (this.LastMapWidth!=this.CurrentMapWidth || this.LastMapHeight!=this.CurrentMapHeight) {
	            this.map.checkMapsize();
	        }
	    } else 
	        window.setTimeout("fullScreenController.adjustMapSize();", 1000); 	  
	    return false;
	}

	// handler for window resize
	this.adjustMapSizeHandler = function(e) {
	    window.clearTimeout(this.reloadTimer);
		this.reloadTimer = window.setTimeout("fullScreenController.adjustMapSize();",1000);
	}

	// get the page width
	this.getPageWidth = function() {
		var width = window.innerWidth;
		if (width == null) {
			if (document.documentElement && document.documentElement.clientWidth)
				width = document.documentElement.clientWidth
			else	
				width = document.body.clientWidth;
		}
		return width;
	}
	
	 //get the page height
	this.getPageHeight = function() {
		var height = window.innerHeight;
		if (height == null) {
			if (document.documentElement && document.documentElement.clientHeight)
				height = document.documentElement.clientHeight;
			else
				height = document.body.clientHeight;
		}
		return height;	
	}

	Geodata.Component.ADF.FullScreenController.initializeBase(this);
}

Geodata.Component.ADF.FullScreenController.registerClass('Geodata.Component.ADF.FullScreenController');

if (typeof(Sys) !== 'undefined') { Sys.Application.notifyScriptLoaded(); }