﻿/* static helpers */

$(function() {
    //add hover states on the static widgets
    $('.ui-state-default:not(.ui-state-disabled, .ui-slider-range, .ui-progressbar-value), a.ui-datepicker-next, a.ui-datepicker-prev, .ui-dialog-titlebar-close').hover(
		function() { $(this).addClass('ui-state-hover'); },
		function() { $(this).removeClass('ui-state-hover'); }
	);
});


/* CUSTOM AJAX EVENTS */
/*
$('loadingDialog').ajaxStart(function() {
    showLoadingDialog();
}).ajaxStop(function() {
    hideLoadingDialog();
});
*/

/* CUSTOM OVERLAY EFFECT */

$.tools.overlay.addEffect('showAndHide',
    // load function 
    function(done) {
        /* 
        - the 'this' variable is a reference to the overlay API 
        - here we use jQuery's fadeIn() method to perform the effect 
        */
        this.getOverlay().show(0, done);
    },

    // close function 
    function(done) {
        this.getOverlay().hide(0);
        done.call();
    }
);
/*
    $('.overlay').overlay({
        oneInstance: false,  
        expose: { color: '#2d86b2', loadSpeed: 0 },
        effect: 'showAndHide'
    });
*/
/* LOADING DIALOG */
function prepareLoadingDialog() {
    loadingBox = $('#loadingDialog').overlay({
        oneInstance: false,
        expose: { color: '#2d86b2', loadSpeed: 0, zIndex: 10000 },
        effect: 'showAndHide'/*,
        onClose: function() {
            
        }*/
    });
}

function showLoadingDialog() {
    $('#loadingDialog').overlay().load();
}

function hideLoadingDialog() {
    $('#loadingDialog').overlay().close();
}

/* MODAL DIALOG */

function prepareOverlay(elements) {
    elements.click(function(e) {
        e.preventDefault();
        var link = $(this);

        showLoadingDialog();

        var href = link.attr('href');
        var box = $('#modalDialog');
        var wrap = box.find('.contentWrap');

        link.overlay({
            oneInstance: false,
            expose: { color: '#2d86b2', loadSpeed: 0 },
            effect: 'showAndHide'
        });

        wrap.load(href, function() {

            hideLoadingDialog();
            link.overlay().load();

            
        });
    });
}

/* FORM */

$.prepareButtons = function(target) {
    $(target).find('.ui-button').button();
    //$(target).find('.ui-button').button();
    //$(target).find('.ibutton').iButton({ labelOn: "WEL", labelOff: "NIET" });
}

$.prepareForm = function(target) {
    $(target).find('input[type=text], input[type=password], textarea, select').addClass('ui-widget-content ui-corner-all')
        .focus(function() {
            if ($(this).hasClass('ui-spinner-box')) {
                $(this).parent().addClass('ui-state-highlight');
            }
            $(this).addClass('ui-state-highlight');
        })
        .blur(function() {
            if ($(this).hasClass('ui-spinner-box')) {
                $(this).parent().removeClass('ui-state-highlight');
            }
            $(this).removeClass('ui-state-highlight');
        });
    $.prepareButtons(target);
    $(target).find('form').bind('submit', function() { showLoadingDialog(); });

    $(target).find('.formError').each(function() {
        var inputElem = $(this).prevAll('p:first');
        //var surround = $('<div class="formError"/>');
        $(this).css('top', (inputElem.position().top) + 'px');
        $(this).css('left', (inputElem.position().left + inputElem.width()) + 'px');
        $(this).click(function() { $(this).hide(); });
    });
}