﻿/*
#Region "History"
'20100203 - RH - Script file added
'20100302 - RH - IE fix
'20100304 - RH - Fix, Object instantiated after definition, fixes main is not defined errors
#End Region
*/

Event.observe(window, 'load', function() {
    main.startUp();

    Event.observe($('destinationsLink'), 'mouseover', main.overDestinationsLink);
    Event.observe($('destinationsLink'), 'mouseout', main.outDestinationsLink);
    Event.observe($('destinationsList'), 'mouseover', main.overDestinationsList);
    Event.observe($('destinationsList'), 'mouseout', main.outDestinationsList);
});

var Main = Class.create({
    destinationsLoaded: false,
    destinationsListHideTimeout: null,
    destinationsListShowTimeout: null,
    startUp: function() {
        if ($('txtWarning')) {
            this.warning('Warning', $F('txtWarning'));
        };

        if ($('txtMessage')) {
            this.message('Message', $F('txtMessage'));
        };
    },
    getContent: function(s) {
        return s.substring(s.indexOf('<!--START-->'), s.indexOf('<!--END-->') + 10);
    },
    overDestinationsLink: function() {
        clearTimeout(main.destinationsListHideTimeout);
        main.destinationsListShowTimeout = setTimeout('$(\'destinationsList\').appear({duration: 0.2});if($(\'map\')){$(\'map\').hide()}', 333); //appear({ duration: 100 });

        //$$('select').each(function(item) { item.setStyle({ 'visibility': 'hidden' }) });
        //$$('input').each(function(item) { item.setStyle({ 'visibility': 'hidden' }) });
    },
    outDestinationsLink: function() {
        main.destinationsListHideTimeout = setTimeout('$(\'destinationsList\').fade({duration: 0.2});;if($(\'map\')){$(\'map\').show()}', 333);
        clearTimeout(main.destinationsListShowTimeout);
    },
    overDestinationsList: function() {
        clearTimeout(main.destinationsListHideTimeout);
    },
    outDestinationsList: function() {
        main.destinationsListHideTimeout = setTimeout('$(\'destinationsList\').fade({duration: 0.2});;if($(\'map\')){$(\'map\').show()}', 333);
    },
    formatCurrency: function(num, includeplus) {
        if (num < 0) {
            return '- £' + (num * -1).toFixed(2);
        };

        if (num > 0) {
            return (includeplus ? '+ ' : '') + '£' + num.toFixed(2);
        };

        return '£0.00';
    },
    roundNumber: function(num, dec) {
        return num.toFixed(dec);
    },
    getTicks: function() {
        return new Date().getTime();
    },
    addLeadingZeros: function(number, desiredLength) {
        var s = '0000000000000000000000000' + number;

        return s.substr(s.length - desiredLength);
    },
    lightsOff: function() {
        document.body.scrollIntoView();
        if (!$('BlackOut')) {
            Element.insert(document.body, { bottom: '<div id="BlackOut" style="display:none;"></div>' });
        };

        $('BlackOut').setStyle({ height: this.getSize().height + 'px' });
        $('BlackOut').appear({ from: 0, to: 0.7, duration: 0.2 });
    },
    lightsOn: function() {
        $('BlackOut').fade({ from: 0.7, to: 0, duration: 0.2 });
    },
    wait: function(title, message) {

        this.lightsOff();

        if (!$('Wait')) {
            Element.insert(document.body, { bottom: '<div id="Wait"> <div class="Head c b">' + title + '</div><div class="Body c">' + message + '<br/><br/><img src="/gfx/loader.gif" alt="Loading"/><br/><br/>All our holiday packages include return private transfers to and from your accommodation.<br/><br/><img src="/gfx/aito.png" alt="AITO" /></div><div class="Foot"></div></div>' });
        } else {

        };
        $('Wait').appear({ duration: 0.2 });
    },
    warning: function(title, warning) {
        this.lightsOff();

        if (!$('Warning')) {
            Element.insert(document.body, { bottom: '<div id="Warning" style="display:none;"><div class="Head" id="WarningTitle"></div><div class="Body" id="WarningMessage"></div><div class="Foot"><a class="button" href="javascript: main.dismiss();">Ok</a></div></div>' });
        };

        $('WarningTitle').update(title);
        $('WarningMessage').update(warning);

        $('Warning').appear({ duration: 0.2 });
    },
    message: function(title, message) {
        this.lightsOff();
        if (!$('Message')) {
            Element.insert(document.body, { bottom: '<div id="Message" style="display:none;"><div class="Head" id="MessageTitle"></div><div class="Body" id="MessageMessage"></div><div class="Foot"><a class="button" href="javascript: main.dismiss();">Ok</a></div></div>' });
        };

        $('MessageTitle').update(title);
        $('MessageMessage').update(message);

        $('Message').appear({ duration: 0.2 });
    },
    Warning: function(warning) {
        this.warning('Warning', warning);
    },
    Message: function(message) {
        this.message('Message', message);
    },
    dismiss: function() {
        this.lightsOn();

        if ($('Wait')) {
            $('Wait').fade({ duration: 0.2 });
        };

        if ($('Message')) {
            $('Message').fade({ duration: 0.2 });
        };

        if ($('Warning')) {
            $('Warning').fade({ duration: 0.2 });
        };
    },
    arrayOverlap: function(a1, a2) {
        var r = 0;

        for (var i = 0; i < a1.size(); i++) {
            for (var j = 0; j < a2.size(); j++) {
                if (a1[i] == a2[j]) {
                    r++;
                };
            };
        };

        return r;
    },
    getXY: function(e) {
        myMouseX = (e || event).clientX;
        myMouseY = (e || event).clientY;

        if (document.documentElement.scrollTop > 0) {
            myMouseY = myMouseY + document.documentElement.scrollTop;
        };

        return { "x": myMouseX, "y": myMouseY };
    },
    getSize: function() {
        var xScroll, yScroll;

        if (window.innerHeight && window.scrollMaxY) {
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;

        if (self.innerHeight) {	// all except Explorer
            if (document.documentElement.clientWidth) {
                windowWidth = document.documentElement.clientWidth;
            } else {
                windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }




        // for small pages with total height less then height of the viewport
        if (yScroll < windowHeight) {
            pageHeight = windowHeight;
        } else {
            pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if (xScroll < windowWidth) {
            pageWidth = xScroll;
        } else {
            pageWidth = windowWidth;
        }

        if ($('float_contents')) {
            if ($('float_contents').clientHeight > pageHeight) {
                pageHeight = $('float_contents').clientHeight + 200;
            }
        }

        if ((pageHeight > 3000) && (pageHeight < 3500)) {
            pageHeight = 4000;
        }

        return { width: pageWidth, height: pageHeight };
    }
});

var main = new Main();
var Website = new Main();
