/**
 *
 * Backend
 * skrypty ogólne
 * 
 * @lastmodified 
 * @requires JQuery 1.4.2+
 *
 */


/**
 * Rich multiselect
 * jQuery plugin
 *
 * @author MJ
 */
(function($){
	$.fn.richMultiselect = function() {
		return this.each( function() {
			
			var $this = $(this);
			var $select = $this.find('select');
			var pickerList_html = '<dl class="ms-picker"><dt>wybierz:</dt><dd><ul>';
			var pickedList_html = '<ul class="ms-picked">';
			$select.find('> option').each( function() {
				pickerList_html += '<li fieldvalue="'+
				$(this).val()+'"><label><input type="checkbox" name="'+$select.attr('name')+'" value="'+$(this).attr('value')+'" '+ 
				( $(this).is(':selected') ? 'checked="checked"' : '' )+' /> '+
				$(this).text()+'</label></li>';
				
				pickedList_html += '<li fieldvalue="'+
				$(this).val()+'"'+( !$(this).attr('selected') ? ' class="disabled"' : '' )+
				'> <a class="delete" href="#">usuń</a> <span>'+$(this).text()+'</span></li>';
			} );
			pickerList_html += '</ul></dd></dl>';
			pickedList_html += '</ul>';
			
			var $pickerList = $(pickerList_html);
			var $pickedList = $(pickedList_html);
			$select.after($pickerList, $pickedList).remove();
			
			
			/* Zaznaczanie/usuwanie na liście checkboxów */
			$pickerList.find('li').click( function (e) {
				var $checkbox = $(":checkbox", this); 
				if (e.target.type !== "checkbox") {
					e.preventDefault();
					$checkbox.attr('checked', !$checkbox.get(0).checked);
				} else {
					$checkbox = $(e.target); 
				}
				var $picked_LI = $pickedList.find('li[fieldvalue="'+ $checkbox.attr('value') +'"]');
				
				
				if ($checkbox.get(0).checked) {
					$picked_LI.fadeIn( function () {
						$(this).addClass('disabled');
					} )
				} else {
					$picked_LI.fadeOut( function () {
						$(this).removeClass('disabled');
					} )
				}
				
			} );
			
			/* Usuwanie z listy dodanych */
			$pickedList.find('a').click( function (e) {
				var $picked_LI = $(this).closest('li');
				$pickerList.find('li[fieldvalue="'+ $picked_LI.attr('fieldvalue') +'"] :checkbox').removeAttr('checked');
				$picked_LI.fadeOut( function () {
					$(this).addClass('disabled');
				} )
				e.preventDefault();
			} );
			
		});	// each
	}
})(jQuery); // $.fn.richMultiselect


/**
 * strip HTML tags
 * @url http://devkick.com/blog/parsing-strings-with-jquery/
 */
function stripTags(html) {
	var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
	return html.replace(regexp,"");
}
/**
 * stripTags
 * jQuery plugin
 */
(function($){
	$.fn.stripTags = function() {
		this.each(function() {
			$(this).html(
				stripTags( $(this).html() )
			);
		});
		return $(this);
	}
})(jQuery);
 
 
/* ************************************************
   Document ready 
*/
$(function(){

	
	/**
	 * Wywołanie "rich multiselecta" na elemencie <select multiple />
	 */
	$('div.multiselect').richMultiselect();
	
	
	/**
	 * Pokazywanie / ukrywanie - np. sekcji w formularzach
	 */
	(function(){
		var $toggler = $('.showhide-toggler');
		
		$toggler
		.click( function () {
			var $wrapper = $(this).closest('.showhide');
			$wrapper.find('.showhide-content').slideToggle('fast', function() {
				$wrapper.toggleClass('showhide_collapsed').toggleClass('showhide_expanded');
			} );
			return false
		} )
		.end()
		
	})(); 
	/**/

	
	/**
	 * Aktywacja colorboxa
	 * @requires jquery.colorbox
	 */
	if (typeof($.fn.colorbox) !== 'undefined') { 
		$("a[rel^='gallery']").colorbox(config.colorbox);
	}
	
	
	/**
	 * Tooltipy
	 * 
	 * @url http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips
	 * @todo przerobić na plugin
	 */
	var $liveTip = $('<div id="livetip"><i></i><div></div></div>').hide().appendTo('body');
	var tipTitle = '';

	$('.has-tooltip[title]')
	//.add('.mod_quicklaunch.ql_compact')
	.bind('mouseover mouseout mousemove', function(event) {
		var $link = $(event.target).closest('a');
		if (!$link.length) { $link = $(event.target).closest('.has-tooltip') }
		
		if (!$link.length) { return; }
		var link = $link[0];
		
		
		if (event.type == 'mouseover' || event.type == 'mousemove') {
			$liveTip.css({
			  top: event.pageY + 12,
			  left: event.pageX + 12
			});
		}

		if (event.type == 'mouseover') {
			tipTitle = link.title;
			link.title = '';
			//$liveTip.html('<div>' + tipTitle + '</div><div>' + link.href + '</div>')
			$liveTip.find('div').html(tipTitle)
			.end()
			.show();
		}

		if (event.type == 'mouseout') {
			$liveTip.hide();
			if (tipTitle) {
					link.title = tipTitle;
				}
		}
	}); // Tooltipy	

	
}) // Document ready


/**
 * Modal dialogs wrapper
 *
 * @requires jquery.simplemodal
 */
var ModalDialog = {
	open: function(el, opt) {
		if ( typeof($.fn.modal) === 'function' ) {
			if (typeof(opt) === 'object') {
				if (opt.onCloseEffect) {
					opt.onClose = function(dialog) {
						dialog.data.fadeOut('slow', function () {
							dialog.container.hide(0, function () {
								dialog.overlay.fadeOut('fast', function () {
									$.modal.close();
								});
							});
						});
					}
				}
			}
			$(el).modal( $.extend( '', opt, {  } ));
		}		
	},
	close: function() {
		$.modal.close();
	}
};
/**/


if (typeof(config) === 'undefined') var config = {};
config.colorbox = {
	opacity: .64,
	transition: 'none',
	current: "<b>{current}</b> z {total}"
}
