var isAbsoluteSnow = (document.domain == "www.absolute-snow.co.uk");

/* Page Size & Scroll - needed to resize overlays */
function ___getPageSize() {
    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;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
};

function ___getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }
    arrayPageScroll = new Array(xScroll, yScroll);
    return arrayPageScroll;
};

/* Transparent Overlay for helper forms */
function hideOverlay() {
    $('#overlayBackground').hide();
    $('#overlayPanel').hide();
    return false;
} //end function hideOverlay()

function showOverlay() {
    //set up the background
    var arrPageSizes = ___getPageSize();
    $('#overlayBackground').css({
        backgroundColor: "#000",
        opacity: "0.5",
        width: arrPageSizes[0],
        height: arrPageSizes[1]
    }).fadeIn();

    //set up the panel
    var arrPageSizes = ___getPageSize();
    var arrPageScroll = ___getPageScroll();
    $('#overlayPanel').css({
        top: arrPageScroll[1] + (arrPageSizes[3] / 20),
        left: ((arrPageSizes[2] - 560) / 2)
    }).show();

    //reposition panel when the window resizes
    $(window).resize(function () {
        // Get page sizes
        var arrPageSizes = ___getPageSize();
        // Style overlay and show it
        $('#overlayPanel').css({
            width: arrPageSizes[0],
            height: arrPageSizes[1]
        });
        // Get page scroll
        var arrPageScroll = ___getPageScroll();
        // Calculate top and left offset for the jquery-lightbox div object and show it
        $('#overlayPanel').css({
            top: arrPageScroll[1] + (arrPageSizes[3] / 20),
            left: ((arrPageSizes[2] - 560) / 2)
        });
    });

    //apply hover effects to images inside the panel
    $("#overlayPanel img.hover").hover(
        function () { $(this).src($(this).src().replace("_off","_on"); },
        function () { $(this).src($(this).src().replace("_on","_off"); }
    );
    return false;
} //end function showOverlay()

function setUpOverlay() {
    $('body').append('<div id="overlayBackground"></div>');
    $('#overlayBackground').click(function () { return hideOverlay(); });
    $('body').append('<div id="overlayPanel"></form>');
} //end function setUpOverlay()

/* Create 'select address' panel */
function setUpPostcodeLookup() {
    $('input#DeliveryPostcode').after('<a href="#" id="postcodeLookup"><img src="/images/' + (isAbsoluteSnow ? 'AbsoluteSnow/' : '') + 'Buttons/findaddressBTN_off.png" class="hover" alt="Find Address" /></a>');
    $('#overlayBackground').click(function () { return postcodeLookupPanel(); });
}

function postcodeLookupPanel() {
    $('#overlayPanel').html('<p><a href="#" id="overlayPanelCancel"><img src="/images/' + (isAbsoluteSnow ? 'AbsoluteSnow/' : '') + 'Buttons/cancelBTN_off.png" alt="Cancel" class="hover" /></a></p>');
    $('#overlayPanelCancel').click(function () { return hideOverlay(); });
    return showOverlay();
} //end function postcodeLookupPanel()

$(document).ready(function() { //do this stuff when the page has finished loading
    $('a[rel*=facebox]').facebox({});
    //Show/Hide text over 130 characters for descriptive text boxes
    var trim_length = 200;
    if ($('#descriptive_text').length && $('#descriptive_text').html().length > trim_length) {
        var first_description = $('#descriptive_text').html().substring(0, trim_length);
        var second_description = $('#descriptive_text').html().substring(trim_length);
        $('#descriptive_text').html(first_description);
        $('#descriptive_text').after('<span id="extra_descriptive_text" style="display: none;">' + second_description + '</span>');
        $('#descriptive_text').after('<span id="descriptive_text_ellipsis">...</span>');
        $('#extra_descriptive_text').after(' <a id="read_more_descriptive_text" href="#">Read More</a>');
        $('#read_more_descriptive_text').click(function() {
            if ($('#extra_descriptive_text').css('display') == 'none') {
                $('#read_more_descriptive_text').html('Read Less');
                $('#descriptive_text_ellipsis').css('display', 'none');
                $('#extra_descriptive_text').css('display', 'inline');
            } else {
                $('#read_more_descriptive_text').html('Read More');
                $('#descriptive_text_ellipsis').css('display', 'inline');
                $('#extra_descriptive_text').css('display', 'none');
            }
            return false;
        });
    } //end if ($('#descriptive_text').length && $('#descriptive_text').html().length>trim_length)

    //Set up overlay panel
    setUpOverlay();
    setUpPostcodeLookup();
});
