﻿if (!shadowBox) {

    (function() {

        var DomReady = window.DomReady = {};

        // Everything that has to do with properly supporting our document ready event. Brought over from the most awesome jQuery. 

        var userAgent = navigator.userAgent.toLowerCase();

        // Figure out what browser is being used
        var browser = {
            version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
            safari: /webkit/.test(userAgent),
            opera: /opera/.test(userAgent),
            msie: (/msie/.test(userAgent)) && (!/opera/.test(userAgent)),
            mozilla: (/mozilla/.test(userAgent)) && (!/(compatible|webkit)/.test(userAgent))
        };

        var readyBound = false;
        var isReady = false;
        var readyList = [];

        // Handle when the DOM is ready
        function domReady() {
            // Make sure that the DOM is not already loaded
            if (!isReady) {
                // Remember that the DOM is ready
                isReady = true;

                if (readyList) {
                    for (var fn = 0; fn < readyList.length; fn++) {
                        readyList[fn].call(window, []);
                    }

                    readyList = [];
                }
            }
        };

        // From Simon Willison. A safe way to fire onload w/o screwing up everyone else.
        function addLoadEvent(func) {
            var oldonload = window.onload;
            if (typeof window.onload != 'function') {
                window.onload = func;
            } else {
                window.onload = function() {
                    if (oldonload) {
                        oldonload();
                    }
                    func();
                }
            }
        };

        // does the heavy work of working through the browsers idiosyncracies (let's call them that) to hook onload.
        function bindReady() {
				if (typeof jQuery == 'undefined') {
					if (readyBound) {
						 return;
					}

					readyBound = true;

					// Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
					if (document.addEventListener && !browser.opera) {
						 // Use the handy event callback
						 document.addEventListener("DOMContentLoaded", domReady, false);
					}

					// If IE is used and is not in a frame
					// Continually check to see if the document is ready
					if (browser.msie && window == top) (function() {
						 if (isReady) return;
						 try {
							  // If IE is used, use the trick by Diego Perini
							  // http://javascript.nwbox.com/IEContentLoaded/
							  document.documentElement.doScroll("left");
						 } catch (error) {
							  setTimeout(arguments.callee, 0);
							  return;
						 }
						 // and execute any waiting functions
						 domReady();
					})();

					if (browser.opera) {
						 document.addEventListener("DOMContentLoaded", function() {
							  if (isReady) return;
							  for (var i = 0; i < document.styleSheets.length; i++)
									if (document.styleSheets[i].disabled) {
									setTimeout(arguments.callee, 0);
									return;
							  }
							  // and execute any waiting functions
							  domReady();
						 }, false);
					}

					if (browser.safari) {
						 var numStyles;
						 (function() {
							  if (isReady) return;
							  if (document.readyState != "loaded" && document.readyState != "complete") {
									setTimeout(arguments.callee, 0);
									return;
							  }
							  if (numStyles === undefined) {
									var links = document.getElementsByTagName("link");
									for (var i = 0; i < links.length; i++) {
										 if (links[i].getAttribute('rel') == 'stylesheet') {
											  numStyles++;
										 }
									}
									var styles = document.getElementsByTagName("style");
									numStyles += styles.length;
							  }
							  if (document.styleSheets.length != numStyles) {
									setTimeout(arguments.callee, 0);
									return;
							  }

							  // and execute any waiting functions
							  domReady();
						 })();
					}

					// A fallback to window.onload, that will always work
					addLoadEvent(domReady);

				} else {
					jQuery(document).ready(function()
					{
						domReady();
					});
				}
        };

        // This is the public function that people can use to hook up ready.
        DomReady.ready = function(fn, args) {
            // Attach the listeners
            bindReady();

            // If the DOM is already ready
            if (isReady) {
                // Execute the function immediately
                fn.call(window, []);
            } else {
                // Add the function to the wait list
                readyList.push(function() { return fn.call(window, []); });
            }
        };

        bindReady();

    })();

    var shadowBox = function() {

        var iPageLoader = null; // timer for fade and resize events
        var pageLoader = null;
        var ifLoader = null;
        var pageBlocker = null;
        var pageCloser = null;
        var isURLLoaded = false;
        var isDocumentLoaded = false;
        var isMoving = false;
        var divLoader = null;

        var aeOL = []; function addEvent(o, n, f, l) { var a = "addEventListener", h = "on" + n, b = "", s = ""; if (o[a] && !l) { return o[a](n, f, false); } o._c |= 0; if (o[h]) { b = "_f" + o._c++; o[b] = o[h]; } s = "_f" + o._c++; o[s] = f; o[h] = function(e) { e = e || window.event; var r = true; if (b) { try { r = o[b](e) != false && r; } catch (ex) { r = false; } } r = o[s](e) != false && r; return r; }; aeOL[aeOL.length] = { o: o, h: h }; }

        DomReady.ready(function() {
            pageLoader = document.createElement('div');
            {
                pageLoader.id = 'pageLoader';
                pageLoader.style.display = 'none';
                pageLoader.style.backgroundColor = '#fff';
                pageLoader.style.zIndex = '9999999';
                pageLoader.style.position = 'absolute';
                pageLoader.style.width = '10px';
                pageLoader.style.height = '10px';
                pageLoader.style.overflow = 'hidden';

                divLoader = document.createElement('div')
                {
                    divLoader.id = 'divLoader';
                    divLoader.style.display = 'none';
                    divLoader.style.width = '100%';
                    divLoader.style.height = '100%';
                    divLoader.style.background = '#fff url("' + (('https:' == document.location.protocol) ? 'https://' : 'http://') + 'www.bookeasy.com.au/images/ajax-loader-large.gif") no-repeat center center';

                    pageLoader.appendChild(divLoader);
                }

                ifLoader = document.createElement('iframe');
                {
                    ifLoader.id = 'ifLoader';
                    ifLoader.name = 'ifLoader';
                    ifLoader.frameBorder = 0;
                    ifLoader.src = 'about:blank';
                    ifLoader.style.display = 'none';
                    ifLoader.style.backgroundColor = '#fff';
                    ifLoader.style.zIndex = '9999999';
                    ifLoader.style.width = '100%';
                    ifLoader.style.overflow = 'auto';
                    ifLoader.style.height = '100%';
                    ifLoader.style.border = 'none';
                    ifLoader.style.margin = '0';
                    ifLoader.style.padding = '0';

                    pageLoader.appendChild(ifLoader);
                }

                document.body.appendChild(pageLoader);
            }

            pageBlocker = document.createElement('div');
            {
                pageBlocker.id = 'pageBlocker';
                pageBlocker.onclick = function() { shadowBox.hide() };
                pageBlocker.style.display = 'none';
                pageBlocker.style.zIndex = '999999';
                pageBlocker.style.backgroundColor = '#000';
                pageBlocker.style.position = 'absolute';
                pageBlocker.style.top = '0';
                pageBlocker.style.left = '0';
                pageBlocker.style.width = '100%';
                pageBlocker.style.height = '100%';
                pageBlocker.style.filter = 'alpha(opacity = 50)';
                pageBlocker.style.opacity = 0.5;

                document.body.appendChild(pageBlocker);
            }
        });

        addEvent(window, 'resize', function() { window.setTimeout(onResize, 100); });
        addEvent(window, 'scroll', function() { window.setTimeout(onResize, 100); });

        var getPLSize = function() { return { w: pageLoader.offsetWidth, h: pageLoader.offsetHeight} };
        var getSize = function(doc) {
            var viewportwidth;
            var viewportheight;

            // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

            if (typeof window.innerWidth != 'undefined') {
                viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
            }

            // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

            else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0) {
                viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
            }

            // older versions of IE

            else {
                viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
            }
            return { w: viewportwidth, h: viewportheight }
        };
        var getScroll = function(doc) {
            var de = (doc || document).documentElement;
            var db = (doc || document).body;

            return {
                x: ((de.scrollLeft || 0) < (db.scrollLeft || 0)) ? db.scrollLeft : de.scrollLeft,
                y: ((de.scrollTop || 0) < (db.scrollTop || 0)) ? db.scrollTop : de.scrollTop,
                w: ((de.scrollWidth || 0) < (db.scrollWidth || 0)) ? db.scrollWidth : de.scrollWidth,
                h: ((de.scrollHeight || 0) < (db.scrollHeight || 0)) ? db.scrollHeight : de.scrollHeight
            }
        };

        var setPLSize = function(size, scroll, plSize) {
            pageLoader.style.width = plSize.w + 'px';
            pageLoader.style.height = plSize.h + 'px';
            pageLoader.style.left = scroll.x + (((size.w / 2) + scroll.x) - (plSize.w / 2)) + 'px';
            pageLoader.style.top = scroll.y + (((size.h / 2) - (plSize.h / 2))) + 'px';
        }

        function onResize() {
            if (pageLoader) {
                if (!isMoving) {
                    var size = getSize();
                    var scroll = getScroll();
                    var plSize = { w: (maxWidth || (size.w * 0.8)), h: (maxHeight || (size.h * 0.8)) };

                    setPLSize(size, scroll, plSize);

                    pageBlocker.style.top = scroll.y + 'px';
                    pageBlocker.style.left = scroll.x + 'px';

                    var plSize = getPLSize();
                    var size = getSize();
                    var scroll = getScroll();

                    if (pageCloser) {
                        pageCloser.style.top = scroll.y + (((size.h / 2) - (plSize.h / 2))) + 'px';
                        pageCloser.style.left = ((((((size.w / 2) + scroll.x) - (plSize.w / 2))) + plSize.w) + 2) + 'px';
                    }
                }
            }
        }

        function loadUrl(url) {
				if (pageLoader === null) {
					domReady();
				}
            window.clearInterval(iPageLoader);
            iPageLoader = null;

            pageLoader.removeChild(ifLoader);

            pageLoader.style.display = 'block';
            divLoader.style.display = 'block';
            pageBlocker.style.display = 'block';

            isURLLoaded = false;
            isDocumentLoaded = false;

            setPLSize(getSize(), getScroll(), { w: 52, h: 52 });

            ifLoader = document.createElement('iframe');
            {
                ifLoader.id = 'ifLoader';
                ifLoader.name = 'ifLoader';
                ifLoader.frameBorder = 0;
                ifLoader.src = 'about:blank';
                ifLoader.style.display = 'none';
                ifLoader.style.backgroundColor = '#fff';
                ifLoader.style.zIndex = '9999999';
                ifLoader.style.width = '100%';
                ifLoader.style.overflow = 'auto';
                ifLoader.style.height = '100%';
                ifLoader.style.border = 'none';
                ifLoader.style.margin = '0';
                ifLoader.style.padding = '0';

                pageLoader.appendChild(ifLoader);
            }

            ifLoader = document.getElementById('ifLoader');

            ifLoader.contentWindow.DomReady = DomReady;
            ifLoader.contentWindow.DomReady.ready(function() {
                if (!doAutoSize) {
                    ifLoader.style.visibility = 'hidden';
                    ifLoader.style.display = 'block';
                }
                resizePageLoader(null, 1, 1);
            });

            if (ifLoader.addEventListener)
                ifLoader.addEventListener('load', doFinalLoad, true);
            else if (ifLoader.attachEvent)
                ifLoader.attachEvent('onload', doFinalLoad);
            else
                ifLoader.onload = doFinalLoad;

            try {
            if (ifLoader.contentWindow)
                    ifLoader.contentWindow.location.replace(url);
            else
                window['ifLoader'].location = url;
        }
            catch (ex) {
                var l = location.toString();
                l = l.substr(0, l.lastIndexOf('/') + 1);
                url = l + url;

                if (ifLoader.contentWindow)
                    ifLoader.contentWindow.location.replace(url);
                else
                    window['ifLoader'].location = url;
            }
        }

        var fl = null;

        function doFinalLoad() {
            fl = window.setInterval(function() {
                if (isDocumentLoaded) {
                    window.clearInterval(fl);
                    fl = null;
                    window.clearTimeout(iPageLoader);
                    iPageLoader = null;

                    isURLLoaded = true;
                    ifLoader.style.visibility = 'visible';
                    ifLoader.style.display = 'block';
                    divLoader.style.display = 'none';

                    resizePageLoader(null, 1, 1, doAutoSize ? autoSize : null);
                }
            }, 50);
        }

        function closeLoader(url) {

            var onCloseResult = true;

            if (onClose != null) {
                try {
                    onCloseResult = onClose();
                }
                catch (ex) { }
            }

            if (onCloseResult) {
                window.clearTimeout(iPageLoader);
                iPageLoader = null;

                pageCloser.style.display = 'none';
                ifLoader.style.display = 'none';
                resizeCloser(20, 1);
            }

        }

        function resizeCloser(iteration, timeout) {
            var plSize = getPLSize();
            var size = getSize();

            if (plSize.h > 10) {
                isMoving = true;
                plSize.h -= iteration;
                if (plSize.h < 10)
                    plSize.h = 10;

                setPLSize(size, getScroll(), plSize);
                window.clearTimeout(iPageLoader);
                iPageLoader = window.setTimeout(function() { resizeCloser(iteration + 2, timeout); }, timeout);
            }
            else if (plSize.w > 10) {
                isMoving = true;
                plSize.w -= iteration;

                if (plSize.w < 10)
                    plSize.w = 10;

                setPLSize(size, getScroll(), plSize);
                window.clearTimeout(iPageLoader);
                iPageLoader = window.setTimeout(function() { resizeCloser(iteration + 2, timeout); }, timeout);
            }
            else {

                window.clearTimeout(iPageLoader);
                iPageLoader = null;
                pageLoader.style.display = 'none';
                pageBlocker.style.display = 'none';

                if (refreshOnClose) {
                    if (document.all)
                        history.go(0);
                    else {
                        addEvent(ifLoader, 'load', function() {
                            top.location.reload();
                        });

                        window['ifLoader'].location = 'about:blank';
                    }
                }
                else if (functionOnClose) {
                    functionOnClose();
                }

                isMoving = false;
            }
            /*
            if (pageCloser) {
            var size = getSize();
            var scroll = getScroll();
            var plSize = getPLSize();

            pageCloser.style.top = scroll.y + (((size.h / 2) - (plSize.h / 2))) + 'px';
            pageCloser.style.left = ((((((size.w / 2) + scroll.x) - (plSize.w / 2))) + plSize.w) + 2) + 'px';
            }

        if (pageBlocker) {
            var scroll = getScroll();

            pageBlocker.style.top = scroll.y + 'px';
            pageBlocker.style.left = scroll.x + 'px';
            }*/
        }

        function resizePageLoader(url, iteration, timeout, onEndFn) {

            if (pageCloser) {
                pageCloser.style.display = 'none';
            }

            var plSize = getPLSize();
            var size = getSize();

            if (plSize.w < (maxWidth || (size.w * 0.8))) {
                isMoving = true;
                plSize.w += iteration;
                if (plSize.w > (maxWidth || (size.w * 0.8)))
                    plSize.w = Math.ceil((maxWidth || (size.w * 0.8)));

                setPLSize(size, getScroll(), plSize);
                if (false == (plSize.w < (maxWidth || (size.w * 0.8))))
                    iteration = 1;

                window.clearTimeout(iPageLoader);
                iPageLoader = window.setTimeout(function() { resizePageLoader(url, iteration + 2, timeout); }, timeout);
            }
            else if (plSize.h < (maxHeight || (size.h * 0.8))) {
                isMoving = true;
                plSize.h += iteration;
                if (plSize.h > (maxHeight || (size.h * 0.8)))
                    plSize.h = Math.ceil((maxHeight || (size.h * 0.8)));

                setPLSize(size, getScroll(), plSize);
                window.clearTimeout(iPageLoader);
                iPageLoader = window.setTimeout(function() { resizePageLoader(url, iteration + 2, timeout); }, timeout);
            }
            else {

                window.clearTimeout(iPageLoader);
                iPageLoader = null;

                var plSize = getPLSize();
                var size = getSize();
                var scroll = getScroll();

                if (!pageCloser) {
                    pageCloser = document.createElement('div');
                    {
                        pageCloser.id = 'pageCloser';
                        pageCloser.innerHTML = '<img src="' + (('https:' == document.location.protocol) ? 'https://' : 'http://') + 'www.bookeasy.com.au/images/shadowbox-close.png" style="cursor: pointer" />';
                        pageCloser.style.width = '20px';
                        pageCloser.style.height = '20px';
                        pageCloser.style.position = 'absolute';
                        pageCloser.style.zIndex = '9999999'
                        pageCloser.onclick = function() { shadowBox.hide() };

                        document.body.appendChild(pageCloser);
                    }
                }

                pageCloser.style.top = scroll.y + (((size.h / 2) - (plSize.h / 2))) + 'px';
                pageCloser.style.left = ((((((size.w / 2) + scroll.x) - (plSize.w / 2))) + plSize.w) + 2) + 'px';
                pageCloser.style.display = 'block';

                isDocumentLoaded = true;

                if (isURLLoaded) {
                    ifLoader.style.display = 'block';
                    ifLoader.style.visibility = 'visible';
                    ifLoader.focus();
                }

                isMoving = false;

                if (onEndFn)
                    onEndFn();
            }

            /*
            if (pageCloser) {
            var size = getSize();
            var scroll = getScroll();
            var plSize = getPLSize();

            pageCloser.style.top = scroll.y + (((size.h / 2) - (plSize.h / 2))) + 'px';
            pageCloser.style.left = ((((((size.w / 2) + scroll.x) - (plSize.w / 2))) + plSize.w) + 2) + 'px';
            }

        if (pageBlocker) {
            var scroll = getScroll();

            pageBlocker.style.top = scroll.y + 'px';
            pageBlocker.style.left = scroll.x + 'px';
            }*/
        }

        var isHeightDone = false;
        var isWidthDone = false;

        function autoSize() {
            window.clearInterval(ifl);
            ifl = null;

            window.clearTimeout(iPageLoader);
            iPageLoader = null;

            try {
            if (ifLoader.contentWindow)
                ifLoader.contentWindow.document.body.style.overflow = 'hidden';
            else
                window['ifLoader'].document.body.style.overflow = 'hidden';
            }
            catch (ex) { }

            var it = 1;
            ifl = window.setInterval(function() {

                var s;

                if (ifLoader.contentWindow)
                    s = getScroll(ifLoader.contentWindow.document);
                else
                    s = getScroll(window['ifLoader'].document);

                var scroll = getScroll();
                var plSize = getPLSize();
                var size = getSize();

                if ((!isWidthDone && plSize.w >= s.w) || (!isHeightDone && plSize.h >= s.h)) {

                    if (plSize.h >= s.h) {
                        isMoving = true;
                        plSize.h -= it;

                        if (plSize.h < 10) {
                            plSize.h = 10;
                            isHeightDone = true;
                        }

                        setPLSize(size, scroll, plSize);

                        if (getPLSize().h != plSize.h)
                            isHeightDone = true;
                    }
                    else
                        isHeightDone = true;

                    if (plSize.w >= s.w) {
                        isMoving = true;
                        plSize.w -= it;

                        if (plSize.w < 10) {
                            plSize.w = 10;
                            isWidthDone = true;
                        }

                        setPLSize(size, scroll, plSize);

                        if (getPLSize().w != plSize.w)
                            isWidthDone = true;
                    }
                    else
                        isWidthDone = true;

                    it = it + 2;
                }
                else {
                    window.clearInterval(ifl);

                    isHeightDone = false;
                    isWidthDone = false;

                    maxWidth = s.w;
                    maxHeight = s.h;

                    resizePageLoader('', 1, 1);

                    return;
                }
            }, 1);
        }

        var maxWidth = null;
        var maxHeight = null;
        var maxOpacity = null;
        var onClose = null;
        var refreshOnClose = false;
        var functionOnClose = null;
        var ifl = null;
        var doAutoSize = false;

        return {
            show: function(url, w, h, op, oc, roc, foc) {
                maxWidth = w;
                maxHeight = h;
                maxOpacity = 50; // set opacity to 50%
                onClose = oc;
                refreshOnClose = roc;
                functionOnClose = foc;

                if (maxWidth < 0 || maxHeight < 0) {
                    if (maxWidth < 0)
                        maxWidth = 52;

                    if (maxHeight < 0)
                        maxHeight = 52;

                    doAutoSize = true;
                }
                else
                    doAutoSize = false;

                loadUrl(url);
            },
            hide: closeLoader,
            autoSize: autoSize,
            closeFunction: function(fn) { if (fn) onClose = fn; else return onClose; },
            doRefreshOnClose: function(v) { if (v) refreshOnClose = v; else return refreshOnClose; },
            doFunctionOnClose: function(v) { if (v) functionOnClose = v; else return functionOnClose; }
        }
    } ();
}
