/**
 * The browser manager class implementation.
 * This class can be used to detect the browser type and version.
 */
 
/**
 * The browser manager class
 */
function MMPBrowserManager() {
	
	// Define the supported browsers
    /**
     * Unknown browser
     */
    this.BROWSER_UNKNOWN = 0;
    
    /**
     * Internet explorer
     */
    this.BROWSER_INTERNET_EXPLORER = 1;
    
    /**
     * Mozilla Firefox
     */
    this.BROWSER_FIREFOX = 2;
    
    /**
     * Opera
     */
    this.BROWSER_OPERA = 3;
    
    /**
     * Safari
     */
    this.BROWSER_SAFARI = 4;
    
    /**
     * Google Chrome
     */
    this.BROWSER_CHROME = 5;
    
    /**
     * Konqueror
     */
    this.BROWSER_KONQUEROR = 6;
        
    /**
     * Extracts a number from the start of the specified text.
     * @param text the text to extract the number from
     * @return the number that was extracted from the text
     */
    this.GetNumberFromString = function(text) {

    	// Find the first whitespace
    	var pos = text.search(/\D+/);  

    	// If any whitespace was found, take the number up to this char
    	if(pos>=0) {

    		// Take only the first part of the text
    		var numberText = text.substr(0, pos);

    	} else {

            // No whitespace, take the whole text
            var numberText = text;

    	}
    	
        return new Number(numberText);
    } 

	/**
	 * This functions returns the name of the browser as specified by the browser
	 */
	this.GetName = function() {
		return this.browserName;
	}

    /**
     * This functions returns the name of the browser engine as specified by the browser
     */
    this.GetEngine = function() {
        return this.browserEngine;
    }

    /**
     * This functions returns the name of the browser version as specified by the browser
     */
    this.GetVersion = function() {
        return this.browserVersion;
    }

    /**
     * Returns the browser type as determined from the userAgent field 
     */	
	this.GetBrowserType = function() {
		return this.browserType;
	}

    /**
     * Returns the browser major version as determined from the userAgent field 
     */ 
    this.GetBrowserMajorVersion = function() {
        return this.browserMajorVersion;
    }

    /**
     * Returns the browser minor version as determined from the userAgent field 
     */ 
    this.GetBrowserMinorVersion = function() {
        return this.browserMinorVersion;
    }

    /**
     * Returns a string with the name and version of the browser 
     */ 
    this.GetBrowserString = function() {
    	
    	var nameString = '';
    	
    	// Check the name
    	if(this.browserType==this.BROWSER_INTERNET_EXPLORER) {
    		nameString += 'Internet Explorer'
        } else if(this.browserType==this.BROWSER_FIREFOX) {
            nameString += 'Mozilla Firefox'
        } else if(this.browserType==this.BROWSER_OPERA) {
            nameString += 'Opera'
        } else if(this.browserType==this.BROWSER_SAFARI) {
            nameString += 'Safari'
        } else if(this.browserType==this.BROWSER_CHROME) {
            nameString += 'Google Chrome'
        } else if(this.browserType==this.BROWSER_KONQUEROR) {
            nameString += 'Konqueror'
        } else {
            nameString += 'Unknown Browser'
        }
        
        // Add the version number
        nameString += ' ';
        nameString += this.browserMajorVersion;
        nameString += '.';
        nameString += this.browserMinorVersion;
        
        // Append the revision, if > 0
        if(this.browserRevision>0) {
            nameString += '.';
            nameString += this.browserRevision;
        }
    	
    	// Return the full string
        return nameString;
    }
    
    /**
     * Returns the top position of the current page in pixels.
     */
    this.GetPageStart = function() {
        
        // Assume we are at the page start
        var topValue = 0;

        // Read the value depending on the browser
        if(this.browserType==this.BROWSER_INTERNET_EXPLORER) {
            
            // IE
            topValue = document.documentElement.scrollTop;
            
        } else {
            
            // All other browsers
            topValue = window.pageYOffset;
            
        }
        
        // Ensure a valid value
        if(topValue==null) topValue = 0;
        
        // Return the top value
        return topValue;
    }
    
    /**
     * Returns the height of the current page in pixels.
     */
    this.GetPageHeight = function() {
        
        // Assume the page has no height
        var heightValue = 0;

        // Read the value depending on the browser
        if(this.browserType==this.BROWSER_INTERNET_EXPLORER) {
            
            // IE
            heightValue = document.documentElement.clientHeight;
            
        } else {
            
            // All other browsers
            heightValue = window.innerHeight;
            
        }
        
        // Ensure a valid value
        if(heightValue==null) heightValue = 0;
        
        // Return the height value
        return heightValue;
    }
    
    /**
     * Sets the specified style of the specified element to the specfied value.
     * @param element the element to set the style
     * @param style the name of the style to set
     * @param value the value to set the style to
     */
    this.SetStyle = function(element, style, value) {

        // Create the command
        var command = 'element.style.' + style + ' = \'' + value + '\';';

        // Execute the command  
        eval(command);
    }

    /**
     * Sets the specified attribute of the specified element to the specfied value.
     * @param element the element to set the style
     * @param attribute the name of the attribute to set
     * @param value the value to set the style to
     */
    this.SetAttribute = function(element, attribute, value) {

        // Create the command
        var command = 'element.' + attribute + ' = \'' + value + '\';';

        // Execute the command  
        eval(command);
    }

    /**
     * Sets the event handler for the specified event.
     * @param event the event that should be handled
     * @param handler the function that handles the event
     */
    this.SetEventHandler = function(event, handler) {

        // Create the command
        var command = 'window.' + event + ' = handler;';

        // Execute the command  
        eval(command);
    }
    
    /**
     * Checks, if the browser supports external fonts. 
     * @return true, if the browser supports the @font-face tag, false otherwise
     */
    this.FontsAreSupported = function() {
        
        // Check IE
        if(this.browserType==this.BROWSER_INTERNET_EXPLORER) {
            
            // IE supports this since 4.0
            if(this.browserMajorVersion>3) return true;
            return false;
        }
        
        // Check Firefox
        if(this.browserType==this.BROWSER_FIREFOX) {
            
            // Safari supports this since 3.5
            if(this.browserMajorVersion>3) return true;
            if((this.browserMajorVersion==3)&&(this.browserMinorVersion>4)) return true;
            return false;
        }
        
        // Check Opera
        if(this.browserType==this.BROWSER_OPERA) {
            
            // Opera supports this since 10.0
            if(this.browserMajorVersion>9) return true;
            return false;
        }
        
        // Check Chrome
        if(this.browserType==this.BROWSER_CHROME) {
            
            // Chrome supports this since 4.0
            if(this.browserMajorVersion>3) return true;
            return false;
        }
        
        // Check Safari
        if(this.browserType==this.BROWSER_SAFARI) {
            
            // Safari supports this since 3.1
            if(this.browserMajorVersion>3) return true;
            if((this.browserMajorVersion==3)&&(this.browserMinorVersion>0)) return true;
            return false;
        }

        // Assume fonts are not supported    	
    	return false;
    }

    // ------------------------------------------------------
    // Initialize the browser manager
    // ------------------------------------------------------

    // Read out the data from the navigator array if present
    if(navigator!=null) {

        this.browserName = navigator.appName;
        this.browserEngine = navigator.userAgent; 
        this.browserVersion = navigator.appVersion;

    } else {

        this.browserName = 'Unknown';
        this.browserEngine = 'Unknown'; 
        this.browserVersion = 'Unknown';
    }

    // The browser is not yet known
    this.browserType = this.BROWSER_UNKNOWN;
    this.browserMajorVersion = 0;
    this.browserMinorVersion = 0;
    this.browserRevision = 0;
    
    // Check browser types
    if(this.browserEngine.indexOf('Opera')!=-1) {
        
        // Get the index of the Engine name
        index = this.browserEngine.indexOf('Version');
        
        // Go to the index of the major version
        index += 8;
        
        // Extract the major version
        this.browserMajorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Go to the index of the minor version
        index += (this.browserMajorVersion.toString().length + 1);
        
        // Extract the minor version
        this.browserMinorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Set the type
        this.browserType = this.BROWSER_OPERA;
        
    } else if(this.browserEngine.indexOf('MSIE')!=-1) {
        
        // Get the index of the Engine name
        index = this.browserEngine.indexOf('MSIE');
        
        // Go to the index of the major version
        index += 5;
        
        // Extract the major version
        this.browserMajorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Go to the index of the minor version
        index += (this.browserMajorVersion.toString().length + 1);
        
        // Extract the minor version
        this.browserMinorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Set the type
        this.browserType = this.BROWSER_INTERNET_EXPLORER;
        
    } else if(this.browserEngine.indexOf('Firefox')!=-1) {
        
        // Get the index of the Engine name
        index = this.browserEngine.indexOf('Firefox');
        
        // Go to the index of the major version
        index += 8;
        
        // Extract the major version
        this.browserMajorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Go to the index of the minor version
        index += (this.browserMajorVersion.toString().length + 1);
        
        // Extract the minor version
        this.browserMinorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Go to the index of the revision
        index += (this.browserMinorVersion.toString().length + 1);
        
        // Extract the minor version
        this.browserRevision = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Set the type
        this.browserType = this.BROWSER_FIREFOX;

    } else if(this.browserEngine.indexOf('Chrome')!=-1) {
        
        // Get the index of the Engine name
        index = this.browserEngine.indexOf('Chrome');
        
        // Go to the index of the major version
        index += 7;
        
        // Extract the major version
        this.browserMajorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Go to the index of the minor version
        index += (this.browserMajorVersion.toString().length + 1);
        
        // Extract the minor version
        this.browserMinorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Go to the index of the revision
        index += (this.browserMinorVersion.toString().length + 1);
        
        // Extract the minor version
        this.browserRevision = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Set the type
        this.browserType = this.BROWSER_CHROME;
        
    } else if(this.browserEngine.indexOf('Safari')!=-1) {
        
        // Get the index of the Engine name
        index = this.browserEngine.indexOf('Version');
        
        // Go to the index of the major version
        index += 8;
        
        // Extract the major version
        this.browserMajorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Go to the index of the minor version
        index += (this.browserMajorVersion.toString().length + 1);
        
        // Extract the minor version
        this.browserMinorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Go to the index of the revision
        index += (this.browserMinorVersion.toString().length + 1);
        
        // Extract the minor version
        this.browserRevision = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Set the type
        this.browserType = this.BROWSER_SAFARI;
        
    } else if(this.browserEngine.indexOf('Konqueror')!=-1) {
        
        // Get the index of the Engine name
        index = this.browserEngine.indexOf('Konqueror');
        
        // Go to the index of the major version
        index += 10;
        
        // Extract the major version
        this.browserMajorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Go to the index of the minor version
        index += (this.browserMajorVersion.toString().length + 1);
        
        // Extract the minor version
        this.browserMinorVersion = this.GetNumberFromString(this.browserEngine.substr(index));
        
        // Set the type
        this.browserType = this.BROWSER_KONQUEROR;
        
    } 
};


