﻿/* cut and shut of facebox - require not to duplicate form values. */

(
function($) {

    $.fn.modalform = function(settings) {
        var main_opts = $.extend({}, $.fn.modalform.defaults, settings);
        return this.each(function() {
            return new $.ModalForm(this, main_opts);

        });
    }
    //allow_page_scroll was originally removing window scroll bars - now removed it instead defines layout as rounded transparent border style or lightbox
    $.fn.modalform.defaults = {
        modal_id: '#Modal',
        loading_image: '/images/v2/animation/wait.gif',
        close_image: '/images/v2/modal/closelabel.gif',
        image_types: ['png', 'jpg', 'jpeg', 'gif'],
        modal_html: '',
        allow_page_scroll: false,
        pos_middle: false,
        onEsc: function() { }

    };





    $.ModalForm = function(element, settings) {


        function loading() {

            if ($(settings.modal_id + ' .loading').length == 1) return true
            if (settings.modal_html != '') {

                $(settings.modal_id + ' .tdpg_modal_content').empty();
            }
            $(settings.modal_id + ' .body').children().hide().end()
            .append('<div class="loading"><img src="' + settings.loading_image + '"/></div>');

            var pageScroll = $.fn.modalform.getPageScroll()
            var pageHeight = $.fn.modalform.getPageHeight()

            var modalForm = $(settings.modal_id)

            if (settings.allow_page_scroll) {
                if (pageHeight < document.body.scrollHeight) pageHeight = document.body.scrollHeight;
                var popup = modalForm.find('.popup:eq(0)');

                modalForm.css({ top: 0, left: 0, height: document.body.scrollHeight }).addClass("fullModal").show();
                popup.css("margin-top", pageScroll[1]);

                if (settings.pos_middle == true) {
                    $.fn.modalform.centre(settings.modal_id);
                }


            }
            else {
                modalForm.css({ top: pageScroll[1] + (pageHeight / 10), left: pageScroll[0] }).show();
            }



            $(document).bind('keydown.modalform', function(e) {
                if (e.keyCode == 27) {
                    $.fn.modalform.close(settings.modal_id);
                    settings.onEsc.call();

                }
            })

        }

        function reveal(data, klass) {


            if (klass) $(settings.modal_id + ' .tdpg_modal_content').addClass(klass)
            if (settings.modal_html != '') {
                $(settings.modal_id + ' .tdpg_modal_content').append(data);
            }
            $(settings.modal_id + ' .loading').remove()
            $(settings.modal_id + ' .body').children().fadeIn('normal');

            if (settings.pos_middle == true) {
                $.fn.modalform.centre(settings.modal_id);

            }
        }


        function modalform(data, klass) {
            init()
            loading()
            $.isFunction(data) ? data.call($) : reveal(data, klass)
        }

        function init() {

            if (settings.inited) {
                return true
            } else {
                settings.inited = true;
            }

            // if (settings) $.extend(settings, settings)
            if (settings.modal_html != '') {
                $('body').append(settings.modal_html);
            }

            var preload = [new Image(), new Image()]
            preload[0].src = settings.close_image
            preload[1].src = settings.loading_image

            var element = $(settings.modal_id);

            element.find('.b:first, .bl, .br, .tl, .tr').each(function() {
                preload.push(new Image())
                preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
            })

            $('.close', element).click(function() { return $.fn.modalform.close(settings.modal_id); });
            $('.close_image', element).attr('src', settings.close_image)

            if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
                element.append($("<iframe />")
                                    .height(element.height())
                                    .width(element.width())
                                           );

            }

        }

        if (settings.modal_html == 'default') { settings.modal_html = '<div id="{{modal_id}}" style="display:none;" ><div class="popup"><table><tbody><tr><td class="tl"/><td class="b"/><td class="tr"/></tr><tr><td class="b"/><td class="body"><div class="tdpg_modal_window"><div class="tdpg_modal_content"></div><div class="footer"><a href="#" class="close">Close Window [x]</a></div></div></div></td><td class="b"/></tr><tr><td class="bl"/><td class="b"/><td class="br"/></tr></tbody></table></div></div>'.replace('{{modal_id}}', settings.modal_id.replace('#', '')); }

        init();

        var image_types = settings.image_types.join('|')
        image_types = new RegExp('\.' + image_types + '$', 'i')

        function click_handler() {

            loading(true);


            // support for rel="modalform[.inline_popup]" syntax, to add a class
            var klass = this.rel.match(/modalform\[\.(\w+)\]/)
            if (klass) klass = klass[1]

            // div
            if (this.href.match(/#/)) {
                var url = window.location.href.split('#')[0]
                var target = this.href.replace(url, '')

                reveal($(target).clone().show(), klass)

                // image
            } else if (this.href.match(image_types)) {
                var image = new Image()
                image.onload = function() {
                    reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
                }
                image.src = this.href

                // ajax
            } else {
                $.get(this.href, function(data) { reveal(data, klass) })
            }

            return false
        }

        $(element).click(click_handler);
        $(element).bind("show", function() { loading(true); reveal(null, null); });

        return this;

    }


    // getPageScroll() by quirksmode.com
    $.fn.modalform.getPageScroll = function() {
        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;
        }
        return new Array(xScroll, yScroll)
    }

    // adapter from getPageSize() by quirksmode.com
    $.fn.modalform.getPageHeight = function() {

        var windowHeight
        if (self.innerHeight) {	// all except Explorer
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowHeight = document.body.clientHeight;
        }
        return windowHeight;
    }

    $.fn.modalform.getWindowHeight = function() {
        // fix a jQuery/Opera bug with determining the window height
        var h = $.browser.opera && $.browser.version > "9.5" && $.fn.jquery <= "1.2.6" ? document.documentElement["clientHeight"] : $(window).height();

        return h;
    }

    $.fn.modalform.close = function(objId) {

        $(document).trigger('close.modalform', objId);
        return false;
    }

    $.fn.modalform.centre = function(objId) {
        var popup = $('.popup:eq(0)', objId);
        popup.css("padding-top", Math.floor((($.fn.modalform.getWindowHeight() - popup.height()) / 2)) + 'px');
    }

    $(document).bind('close.modalform', function(obj, objId) {

        $(document).unbind('keydown.modalform');

        $(objId).fadeOut(function() {
            $(objId + ' .tdpg_modal_content').removeClass().addClass('tdpg_modal_content');


        });
    });


})(jQuery);
