﻿/************************************************************************************************************
popupOverlay 

INFO:
used to show content ontop of other dom elements (including flash).
flash objects must have a wmode of either opaque or transparent in
order to layer div's over them properly.

a check has been added to ensure that safari users have the latest
flash component installed (older ones dont support dom elements displayed
ontop of it. in the event that a use is running safari, and its an older
version of flash, the content is simply displayed in a popup window.

also if the user is running safari and swfobject is for some reason
not available for flash version checking, a popup window is used by
default.

USAGE:

Simple popup with a message in it.
===================================================
var Settings = { top: "100px", 
left: "100px", 
width: "200px", 
height: "80px", 
background: "#FFFFFF", 
border: "1px solid #000000" 
};

popupOverlay('This is a popup!', Settings);
===================================================


Popup with external page loaded inside of it.		
===================================================
var Settings = { top: "100px", 
left: "100px", 
width: "200px", 
height: "80px", 
background: "#FFFFFF", 
border: "1px solid #000000",
type: "iframe"
};

popupOverlay('EXTERNAL_URL', Settings);
===================================================



anytime you can clear all of the popups with one command
===================================================
destroyOverlays();
===================================================



Overideable settings:
type = "div" or "iframe" (default is "div")
align = "center", "left", or "right" (default is "center")
margin = any typical margin values as a string e.x. "1px" (default is "0px")
border = any typical border values e.x. "1px solid #ffffff" (default is "opx none")
position = "relative" or "absolute" (default is "absolute") NOTE: be careful with this setting.
top = any typical top position as a string e.x. "100px" (default is "0px")
left = any typical left position as a string e.x. "100px" (default is "0px")
width = any typical width value as a string e.x. "100%" (default is "0px")
height = any typical height value as a string e.x. "100%" (default is "0px")
overflow = "scroll", "hidden", "visible", "auto" (default is "scroll") NOTE: be careful with this setting
background = any typical background settings e.x."#ffffff" (default is "transparent")
padding = any typical padding value as a string e.x. "10px" (default is "0px")

	
************************************************************************************************************/

var overlays;
overlays = new Array();
var compatabilityVerified = false;

function resizeOverlay(newHeight) {
    for (var i = 0; i < overlays.length; i++) {

        var theDiv = overlays[i];

        if (theDiv.style.minHeight < newHeight) {
            theDiv.style.height = newHeight;
            document.getElementById("innerFrame").style.height = (newHeight + 100) + "px";
        }
        else {
            theDiv.style.height = theDiv.style.minHeight;
            document.getElementById("innerFrame").style.height = (newHeight + 100) + "px";
        }
    }
}

function popupOverlay(htmlToShow, settings) {
    // default settings
    var controlStyle = "div";
    var alignStyle = "center";
    var borderStyle = "0px none";
    var positionStyle = "absolute";
    var topPosition = "0px";
    var leftPosition = "0px";
    var width = "";
    var height = "";
    var minheight = "";
    var overflowStyle = "scroll";
    var backgroundStyle = "transparent";
    var paddingStyle = "0px";
    var marginLeft = "0px";
    var marginRight = "0px";
    var marginTop = "0px";
    var marginBottom = "0px";

    if (settings) {
        if (settings.type) { controlStyle = settings.type; }
        if (settings.align) { alignStyle = settings.align; }
        if (settings.margin) { marginStyle = settings.align; }
        if (settings.border) { borderStyle = settings.border; }
        if (settings.position) { positionStyle = settings.position; }
        if (settings.top) { topPosition = settings.top; }
        if (settings.left) { leftPosition = settings.left; }
        if (settings.width) { width = settings.width; }
        if (settings.height) { height = settings.height; minheight = settings.height; }
        if (settings.overflow) { overflowStyle = settings.overflow; }
        if (settings.background) { backgroundStyle = settings.background; }
        if (settings.padding) { paddingStyle = settings.padding; }
        if (settings.marginLeft) { marginLeft = settings.marginLeft; }
        if (settings.marginRight) { marginRight = settings.marginRight; }
        if (settings.marginTop) { marginTop = settings.marginTop; }
        if (settings.marginBottom) { marginBottom = settings.marginBottom; }

        // special settings
        if (settings.autoCenter == true) {
            positionStyle = "absolute";
            marginLeft = -(fixNumber(width) / 2) + "px";
            marginRight = "0px";
            marginTop = -(fixNumber(height) / 2) + "px";
            marginBottom = "0px";
            topPosition = "50%";
            leftPosition = "50%";
        }
        if (settings.autoCenterWidth == true) {
            positionStyle = "absolute";
            marginLeft = -(fixNumber(width) / 2) + "px";
            marginRight = "0px";
            leftPosition = "50%";
        }
        if (settings.autoCenterHeight == true) {
            positionStyle = "absolute";
            marginTop = -(fixNumber(height) / 2) + "px";
            marginBottom = "0px";
            topPosition = "50%";
        }
    }

    // CHECK FOR FIREFOX, IF IT IS FIREFOX THEN CHECK IF THEIR USING THE LATEST VERSION OF FLASH, IF NOT THEN OPEN THE PAGE IN A POPUP INSTEAD.
    if (compatabilityVerified == false) {
        BrowserDetect.init();

        if (BrowserDetect.browser == "Safari") {
            var flashVersion = swfobject.getFlashPlayerVersion();

            // if swf object isnt accessible (doesnt exist) or doesnt support the swfobject.getFlashPlayerVersion command, then do nothing.
            if (!flashVersion["major"]) {
                if (controlStyle == "iframe") { window.open(htmlToShow); }
                if (controlStyle == "div") {
                    var popup = window.open("", "");
                    popup.document.body.innerHTML = htmlToShow;
                }
                return;
            }

            if (flashVersion["major"] <= 10) {
                if (flashVersion["minor"] <= 0) {
                    if (flashVersion["release"] < 32) {
                        if (controlStyle == "iframe") { document.getElementById("flashContentDiv").safariSucksOpenWindow(htmlToShow); }
                        if (controlStyle == "div") {
                            var popup = window.open("", "");
                            popup.document.body.innerHTML = htmlToShow;
                        }
                        return;
                    }
                }
            }
        }

        // no need to ever run these checks again.
        BrowserDetect = null;
        compatabilityVerified = true;
    }


    var newDiv = document.createElement("div");

    newDiv.id = "popupOverlayDiv" + overlays.length;

    newDiv.style.border = borderStyle;
    newDiv.style.position = positionStyle;
    newDiv.style.top = topPosition;
    newDiv.style.left = leftPosition;
    newDiv.style.width = width;
    newDiv.style.height = height;
    newDiv.style.minHeight = height;
    newDiv.style.background = backgroundStyle;
    newDiv.style.zindex = 1000;
    newDiv.style.padding = paddingStyle;
    newDiv.style.marginLeft = marginLeft;
    newDiv.style.marginRight = marginRight;
    newDiv.style.marginTop = marginTop;
    newDiv.style.marginBottom = marginBottom;

    if (controlStyle == "div") {
        newDiv.style.overflow = overflowStyle;
        newDiv.innerHTML = htmlToShow;
    }

    if (controlStyle == "iframe") { newDiv.innerHTML = "<iframe frameborder='0px' src='" + htmlToShow + "' style='width:100%; height:100%; overflow:" + overflowStyle + "; border:0px;' id='innerFrame'></iframe>"; }

    document.body.appendChild(newDiv);

    overlays.push(newDiv);
}

function destroyOverlays() {
    
    var refocus = overlays.length > 0;
    
    for (var i = 0; i < overlays.length; i++) {
        var theDiv = overlays[i];
        try {
            if (theDiv != null) { document.body.removeChild(theDiv); }
        }
        catch (err) {
        }
    }
    
    if (refocus)
        focusOnFlash();
}

// makes sure a number is actually a number, accepts string with or without the px at the end and returns a float
function fixNumber(str) { return parseFloat(str.replace(/px/gi, "")); }

// BROWSER DETECTION CODE FROM http://www.quirksmode.org/js/detect.html ////////////////////////////////
var BrowserDetect = {
    init: function() {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};