﻿var defDialogHeight = 120;
var defDialogWidth = 350;


// Show dialog : create a popup dialog to display an URL
function showDialog(url, title, width, height) {
    $('#dialogPlaceholder').dialog({
        modal: true,
        open: function () {
            $(this).load(url);
        },
        height: height,
        width: width,
        title: title,
        resizable: true,
        position: ['center', 'middle']
    });
}


// Truncate fields in data grids to the first line
function oneLineFormatter(cellvalue) {
    var i = cellvalue.indexOf('\n');
    if (i != -1)
        cellvalue = cellvalue.substring(0, i);
    return cellvalue;
}

// 
function imageFormatter(cellvalue) {
    if (cellvalue == null) return "";
    
    var i = cellvalue.indexOf('\n');
    if (i != -1)
        cellvalue = cellvalue.substring(0, i);
        return '<img width=70 height=70 src="/Home/GetImage?width=70&height=70&image=' + cellvalue + '"></img>';
}


// Delayed keyup plugin : execute a call back when a text box is changed with a delay
(function ($) {
    $.fn.delayedKeyup = function (options) {
        // Params : timer (millisec before callback fired), callback (callback function)
        var settings = {timer: 500, callback: function() { } };

        return this.each(function() {        
            if ( options ) { $.extend( settings, options ); };
            var $this = $(this);

            // Plugin body
            $this
            .data('timeout', null) // initially clean data
            .keyup(function() { // On key up set a timeout for the call callback in data
                $(this).data('timeout', setTimeout(settings.callback, settings.timer));
            })
            .keydown(function() { // New key pressed, clear the timeout
                clearTimeout($(this).data('timeout'));
                });
        });

    };
})(jQuery);


// Upload plugin : upload a single file
(function ($) {
    $.fn.uploadFile = function (options) {
        // Params : message, height
        var settings = { url: ".", statusId: "" };

        return this.each(function () {
            if (options) { $.extend(settings, options); };
            var $this = $(this);

            // Plugin body
            var uploader = new plupload.Uploader({
                url: settings.url,
                runtimes: 'html5,flash,silverlight',
                browse_button: this.id,
                chunk_size: '256kb',
                flash_swf_url: '/Scripts/plupload/plupload.flash.swf',
                silverlight_xap_url: '/Scripts/plupload/plupload.silverlight.xap',
                filters: [{ title: "Image files", extensions: "jpg,gif,png"}]
            });
            
            uploader.bind('QueueChanged', function () {
                uploader.start();
            });
            
            uploader.bind('FileUploaded', function (up, file, response) {
                $('#' + settings.statusId).html('<p>Uploading ' + file.name + ' status:'  + response.status + ' : ' + response.response + '</p>');
            });
            
            uploader.init();

            $this.data('up', uploader);
        });
    };
})(jQuery);


// Alert plugin : Display a simple alert modal popup
(function ($) {
    $.fn.alertDialog = function (options) {
        // Params : message, height
        var settings = { message: "Empty dialog", height: defDialogHeight, width: defDialogWidth };

        return this.each(function () {
            if (options) { $.extend(settings, options); };
            var $this = $(this);

            // Plugin body
            $this.html(settings.message).dialog({ resizable: false,
                height: settings.height,
                width: settings.width,
                open: function () { },
                modal: true,
                buttons: { "OK": function () { $(this).dialog("close"); $(this).dialog("destroy"); } }
            });
        });
    };
})(jQuery);

// Confirmation plugin : Display a simple confirmation modal dialog
(function ($) {
    $.fn.confirmDialog = function (options) {
        // Params : message, height, OK callback
        var settings = { message: "Empty dialog",height: defDialogHeight, width: defDialogWidth, ok: function() { }, okData: null };

        return this.each(function () {
            if (options) { $.extend(settings, options); };
            var $this = $(this);

            // Plugin body
            $this.html(settings.message).dialog({ 
                resizable: false,
                height: settings.height,
                width: settings.width,
                open: function () { },
                modal: true,
                position: ['center', 'middle'],
                buttons: {
                     "OK": function () { $(this).dialog("close");  $(this).dialog("destroy"); settings.ok(settings.okData); },
                     Cancel: function () { $(this).dialog("close"); $(this).dialog("destroy");  }                     
                }
            });
        });
    };
})(jQuery);

