/*
 * $Id: faba5.sectionQuickform.js 2243 2011-08-09 16:06:12Z markus.tripp@fabasoft.com $
 * 
 * Copyright © Fabasoft R&D GmbH, A-4020 Linz, 1995-2011.
 * 
 * All rights reserved. All hardware and software names used are registered trade names and/or registered trademarks of  
 * the respective manufacturers.
 * 
 * The user of this computer program acknowledges that the above copyright notice, which constitutes the Universal 
 * Copyright Convention, will be attached at the position in the function of the computer program which the author has 
 * deemed to sufficiently express the reservation of copyright. It is prohibited for customers, users and/or third  
 * parties to remove, modify or move this copyright notice.
 * 
 * This computer program is protected by copyright law and international treaties. Unauthorized reproduction or 
 * redistribution of this program, or any portion of it, may result in severe civil and criminal penalties.
 * 
 * The use of this software requires a valid license agreement for this software. This license agreement specifies 
 * especially permitted use through the licensing model. A license agreement must already exist prior to installing the 
 * software as the installation process of this software does not include the conclusion of a license agreement.
 */

(function ($) {
	var mindbreezeTemplates = {
		'website' : '<script type="text/x-mustache-template" class="mindbreeze renderedresult" data-selectbox="website" data-count="5" data-constraint="" data-extraconstraint=" NOT url:blog">\n<h2>\n  <a href="{{url}}">{{title}}</a>\n</h2>\n<p>\n  {{#Author}}\n    von <b>{{Author}}</b>\n  {{/Author}}\n  in {{breadcrumbs}}\n</p>\n<p>\n  {{modification_date}} | {{content}}\n</p>\n<hr>\n</script>',
		'blog' : '<script type="text/x-mustache-template" class="mindbreeze renderedresult" data-selectbox="blog" data-count="5" data-constraint="" data-extraconstraint=" url:blog">\n<h2>\n  <a href="{{url}}">{{title}}</a>\n</h2>\n<p>\n  {{#Author}}\n    von <b>{{Author}}</b>\n  {{/Author}}\n  in {{breadcrumbs}}\n</p>\n<p>\n  {{modification_date}} | {{content}}\n</p>\n<hr>\n</script>',
		'wikipedia' : '<script type="text/x-mustache-template" class="mindbreeze renderedresult" data-selectbox="wikipedia" data-dataSource="https://wikipedia.mindbreeze.com/en/search/" data-count="3" data-constraint="" data-extraconstraint=" extension:wikitext">\n<h2>\n{{title}}\n</h2>\n<p>{{modification_date}} | {{content}}</p>\n<p>\n  <b>Quelle:</b> {{site}}</p>\n</script>'
	};
	
    var sectionMindbreezeMethods = {
            
        type: function() {
            return 'mindbreeze';
        },
        
        supports: function(type) {
            return true;
        },
        
        edit: function(content, edit) {
        	if (window.searchApp === undefined) {
        		window.searchApp = new mindbreeze.search.SearchApp();
        	}
        	
            var dialog = $('#mindbreezeDialog');
            dialog.find('.ftabs').tabs();
            
            var left = edit.offset().left + edit.outerWidth() + 3;
            var top = edit.offset().top - $(document).scrollTop();
            
            dialog.dialog({
            	closeOnEscape: false,
            	autoOpen: false,
            	resizable: false,
            	width: 380,
            	position: [left, top],
            	open: function(event, ui) {
            		$(this).closest('.ui-dialog').find('.ui-dialog-titlebar-close').hide();
            	}
            });

            dialog.dialog('open');
            
            edit.html(content);
            
            var script = edit.find('script');
            if (script.length == 0) {
            	sectionMindbreezeMethods.select('website');
            } else {
            	$('#mindbreezeTemplate').val(script.data('selectbox'));
            	$('#mindbreezeEntries').val(script.data('count'));
            	
            	var constraint = script.data('constraint');
            	var extra = script.data('extraconstraint');
            	if (extra) {
            		constraint = constraint.replace(extra, '');
            	}
            	$('#mindbreezeQuery').val(constraint);
            	
            	var code = $('#mindbreezeCode');
            	var codeVal = $('<div/>').append(script.clone()).html();
            	code.val(codeVal);
            }
            
            $('.mindbreezeCodeApply').button().click(function() {
            	var code = $('#mindbreezeCode');
            	var query = $.trim($('#mindbreezeQuery').val());
            	var constraint = query;
            	var count = $('#mindbreezeEntries').val();
            	
            	edit.html(code.val());
            	
            	if ($(this).closest('.ftab').attr('id') == 'mindbreezeDialog-1') {
                	if (query.length == 0) {
                		constraint = 'ALL';
                	}

                	var script = edit.find('script');
                	
                	var extraConstraint = script.attr('data-extraconstraint');
                	if (!extraConstraint) {
                		extraConstraint = '';
                	}
            		
            		script.attr('data-constraint', constraint + extraConstraint);
            		script.attr('data-count', count);
            		
                    var codeVal = $('<div/>').append(script.clone()).html();
            		code.val(codeVal);
            	}
            	
            	searchApp.refresh();
            	
            	return false;
            });
            
            $('#mindbreezeTemplate').change(function() {
            	sectionMindbreezeMethods.select($(this).val());
            });
        },
        
        select: function(option) {
        	var code = $('#mindbreezeCode');
        	var template = mindbreezeTemplates[option];
        	
        	$('#mindbreezeQuery').val('');
        	$('#mindbreezeEntries').val('5');
        	
        	code.val(template);
        },
        
        val: function(edit) {
            return edit.html();
        },
        
        close: function(edit) {
            $('#mindbreezeDialog').dialog('close');
            
            return true;
        }
    };

    $.fn.sectionMindbreeze = function(method) {
        if (sectionMindbreezeMethods[method]) {
            return sectionMindbreezeMethods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
    };
}) (jQuery);

