﻿$(document).ready(function () {
    var jqmInit = function (trigger) {
        $('#modalWindow').jqm({
            modal: true,
            trigger: trigger,
            target: '#jqmContent',
            onHide: closeModal,
            onShow:  loadInIframeModal
        });
    };
    
    var loadInIframeModal = function (hash) {
        var $trigger = $(hash.t),
            $modal = $(hash.w),
            $modalContent = $('iframe', $modal),
            url = $trigger.is('a') ? $trigger.attr('href') : '',
            title = $trigger.is('a') ? $trigger.attr('title') : '',
            style = $.evalJSON($trigger.attr('modal_style')),
            options = {};
        
        //if the target is a submit button, retrieve it's form's attributes (all inputs, action...)
        if ($trigger.is('input[type=submit]')) {
            var $form = $trigger.parents('form:first');
            
            if ($form.length) {
                url = $form.attr('action');
            };

            $form.find('input,textarea,select').each(function () {
                if (this.name) {
                    options[this.name] = this.value;
                };
            });
        };
        url += '{0}js_enabled=1'.format(url.indexOf('?') > 0 ? '&' : '?');
        
        if ($trigger.is('.jqmAutoSubmit')) {
            $modalContent[0].autoSubmit = 1;
            $modalContent[0].submitOptions = options;
            $modalContent[0].submitMethod = $trigger.is('input[type=submit]') ? 'post' : 'get';
            
            if ($trigger.is('.jqmRefresh')) {
                $(':hidden#jqmRefresh').val('1');
            };
        };
        
        $('div#jqmLoader').show();
        $modalContent.html('').attr('src', url);
        //let's use the anchor "title" attribute as modal window title
        $('#jqmTitleText').text(title);
        $('iframe#jqmContent').css(style);
        $modal.jqmShow();
        $('#modalWindow').show();
    };
    
    var closeModal = function (hash) {
        var $modal = $(hash.w),
            $modalContent = $('iframe', $modal),
            iframe = $modalContent[0],
            iframeDoc = iframe.contentWindow.document;
        
        hash.o.remove();
        if ($(':hidden#jqmRefresh').val()) {
            window.location.href = document.location.href;
        };
        $('#modalWindow').hide();
        $('body', iframeDoc).html('');
        $modalContent.attr('src', '');
        iframe.autoSubmit = null;
        iframe.submitOptions = null;
        iframe.submitMethod = null;
    };
    
    $('iframe#jqmContent').load(function () {
        if (this.autoSubmit && this.submitMethod == 'post') {
            var $body = $('body', this.contentWindow.document);
            
            $body.html('<form action="{0}" method="post"></form>'.format(this.src));
            var $form = $body.find('form');
            
            for (var option in this.submitOptions) {
                $form.append('<input type="hidden" name="{0}" value="{1}" />'.format(option, this.submitOptions[option]));
            };
            
            this.autoSubmit = null;
            this.submitOptions = null;
            
            $form.submit();
        } else {
            var iframeDoc = this.contentWindow.document,
                elements = $('input:visible,textarea:visible,select:visible', iframeDoc),
                closers = $('.jqmClose', iframeDoc),
                refreshers = $('.jqmRefresh', iframeDoc);
            
            $('div#jqmLoader').hide();
            
            if (elements.length) {
                elements[0].focus();
            };
            
            closers.click(function () {
                top.$('#modalWindow').jqmHide({ refresh: false, krneki: 1 });
                return false; 
            });
            
            refreshers.click(function () {
                $(':hidden#jqmRefresh').val('1');
            });
        };
    });
    
    jqmInit('.modal');
});
