/**
 * author: Dariusz Pobożniak | http://pobozniak.pl
 */
 
$(function() {
    Psmc.confirm();
    Psmc.dialog_answer();
	Psmc.roundImg($('#avatar'));
	Psmc.autocompleter();
})

var Psmc = {
    confirm: function () {
        $('.confirm').click(function() {
            var title = $(this).attr('title');
            var msg = $(this).attr('rel');
            var href = $(this).attr('href');
    
            $('<div id="dialog" title="'+title+'"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>'+msg+'</div>').appendTo('body');
    
            $('#dialog').dialog({
                bgiframe: true,
                resizable: false,
                height: 140,
                modal: true,
                overlay: {
                    backgroundColor: '#000',
                    opacity: 0.5
                },
                buttons: {
                    'OK': function() {
                        window.location.href = href;
                        $(this).dialog('destroy');
                        $('#dialog').remove();
                    },
                    'Anuluj': function() {
                        $(this).dialog('destroy');
                        $('#dialog').remove();
                    }
                }
            });
            $('.ui-dialog-titlebar-close').click(function() {
                $('#dialog').dialog('destroy');
                $('#dialog').remove();
            })
            return false;
        });
    },
    dialog_answer: function () {
        var form = $('#answer-form'),
            action = form.attr('action');
        form.find('button').button();
        form.dialog({
            autoOpen: false,
			height: 280,
			width: 370,
			modal: false,
			resizable: true
        });
        $('#answer-btn').click(function() {
			form.dialog('open');
			return false;
		});
    },
	roundImg: function (obj) {
		var img = obj.find('img');
		obj.css({
			background: 'url(' + img.attr('src') + ') 50% 50% no-repeat',
			height: img.height(),
			width: img.width()
		});
		img.remove();
	},
	autocompleter: function() {
		function split( val ) {
			return val.split( /,\s*/ );
		}
		function extractLast( term ) {
			return split( term ).pop();
		}

		$( "#mto" ).autocomplete({
			source: function( request, response ) {
				$.getJSON( "jquery_files/search.php", {
					term: extractLast( request.term )
				}, response );
			},
			search: function() {
				// custom minLength
				var term = extractLast( this.value );
				if ( term.length < 2 ) {
					return false;
				}
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function( event, ui ) {
				var terms = split( this.value );
				// remove the current input
				terms.pop();
				// add the selected item
				terms.push( ui.item.value );
				// add placeholder to get the comma-and-space at the end
				terms.push( "" );
				this.value = terms.join( ", " );
				return false;
			}
		});

	}
}
