/*
 * 	
 * jQuery extensions by plentySystems
 * 
 *
 * 
 * HowTo ...
 * 
 * ...definine a custom function:
 * 
 * jQuery.fn.FUNCTIONNAME = function()
 * 							{
 * 								return $(this).each(
 * 									function()
 * 									{
 * 										// "$(this)" is the jquery object
 * 										$(this).doSomething();
 * 									});
 *							};
 * 
 */


/*
 * Lets element(s) blink
 */
jQuery.fn.blink =  	function(iBlickCount)
					{
						return $(this).each(
							function()
							{
								// speed in ms
								var speed = 250;
								
								// count of blinks
								if(iBlickCount > 0)
								{
									var count = iBlickCount;
								}
								else
								{
									var count = 3;
								}
								
								for(var i=0;i<count;i++)
								{
									this.fadeOut(speed).fadeIn(speed);
								}
							});
					};

/*
 * Move element(s) to center (chainable), centers both on param unset
 */			
jQuery.fn.center =	function (from)
					{
						return $(this).each(
							function()
							{
								// top pos
								var top		= ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px";
								// left pos
								var left	= ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px";
								
								switch(from)
								{
								// adjust top
								case 'top':
									this.css("top", top);
									break;
									// adjust left
								case 'left':
									this.css("left", left);
									break;
									// default adjust both
								default:
									this.css("top", top);
								this.css("left", left);
								break;
								}
							});
					};

/*
 * Applies a for the document unique id to element(s)
 */
jQuery.fn.setUniqueId =	function(prefix)
						{
							return $(this).each(
								function()
								{
									var sUniqueId = '';
									var sUniqueId = '';
									var iMultiplier = 3;
									var iRandomNum 	= 0;
									
									var elExists = true;
									
									// set default id
									if(!prefix)
									{
										prefix = 'id_';
									}
									
									do
									{
										// get more room for new IDs on repeat
										iMultiplier++;
										// get random number
										iRandomNum = Math.ceil(Math.random() * iMultiplier);
										
										// build new id
										sUniqueId = prefix + iRandomNum;
									}
									// repeat if id already exists
									while($('#'+sUniqueId).exists());
									
									// set id to element
									this.attr('id', sUniqueId);
								});
						};

/**
 * Extending the jQuery namespace by defining a new function jQuery.plentyRequest() for AJAX calls
 * (estafilarakis)
 */
jQuery.extend({
	plentyRequest : plentyAjaxRequest2
});

/**
 * 
 * Custom effect for tabs
 */
$.tools.tabs.addEffect(	"plenty_tabs",
						function(tabIndex, done)
						{
							this.getPanes()
								// slide up all panes
								.slideUp("slow")
								// get the selected tab
								.eq(tabIndex)
								// slide down the selected
								.slideDown("slow");
							
							// effect is done
							done.call();
						});

/*
 * Custom datepicker
 */
jQuery.fn.plentyDateInput = 	function() 
								{
									return this.each(	function()
														{
															 $(this).dateinput({
																 				format: "dd.mm.yyyy",
																 				selectors: true,             	
																 				min: -1
																 				});
														});
								};


/**
 * New plugin 'plentyPortlet'.
 * 
 * The HTML structur should be:
 * <div class="PlentyGuiHeaderPane">
 *     <div class="PlentyGuiHeader">...</div>
 *     <div class="PlentyGuiContent">...</div>
 * </div>
 * 
 * The javascript call to build the portlet is:
 * $(".PlentyGuiHeaderPane").plentyPortlet();
 * 
 * The plugin adds a button with the CSS class 'PlentyGuiPortletButton'
 * to open and close the content of the portlet.
 * 
 * @author estafilarakis@plentySystems
 * 
 */
jQuery.fn.plentyPortlet = function(conf) {
	if(typeof(conf) != "object")
	{
		conf = { opened : false };
	}
	return this.each(function(){
		var button = $('<div class="PlentyGuiPortletButton">&nbsp;</div>');
		var portlet = $(this);
		portlet.
			addClass('PlentyGuiPortlet').
			find('> .PlentyGuiContent').
				addClass('PlentyGuiPortletContent').
				end().
			find('> .PlentyGuiHeader').
				addClass('PlentyGuiPortletHeader').
				before(button);
		button.toggle(
				function(){
					$(this).addClass('PlentyGuiPortletOpened');
					portlet.find('> .PlentyGuiPortletContent').slideDown();
					if(typeof(conf.onOpen) == 'function')
					{
						conf.onOpen(portlet.find('> .PlentyGuiPortletContent'));
					}
				},
				function(){
					$(this).removeClass('PlentyGuiPortletOpened');
					portlet.find('> .PlentyGuiPortletContent').slideUp();
					if(typeof(conf.onClose) == 'function')
					{
						conf.onClose(portlet.find('> .PlentyGuiPortletContent'));
					}
				}
		);
		if(conf.opened == true || conf.opened == "true")
		{
			button.click();
		}
	});
}

/**
 * New Plugin 'plentyOverlay'.
 * 
 * This plugin combines the Tools-Plugin 'overlay' with the UI-Plugin 'draggable'.
 * So you get a draggable overlay.
 * 
 * @author estafilarakis@plentySystems
 * 
 */
jQuery.fn.plentyOverlay = function(conf){
	return this.each(function(){
		// Bilde Overlay (über den Trigger)
		jQuery(this).
			overlay({	onLoad : conf.onLoad,
						onBeforeLoad : conf.onBeforeLoad,
						oneInstance : false,
						left : '20%',
						top : '10%'});
		// Mache das Overlay verschiebbar
		jQuery(jQuery(this).attr("rel")).
			draggable({	appendTo : 'body',
						//containment : 'window',
						scroll : true,
						stack : '.plenty_overlay',
						handle : '> .title_bar'	});
	});
}

/**
 * New Plugin 'plentyDateInput'
 * 
 * This Plugin uses the jQuery Tools dateinput-Plugin and fixes
 * the problems with multiple dateinput instances in different
 * AJAX loaded contents (e.g. portlets).
 * 
 * @author estafilarakis@plentySystems
 * 
 */
jQuery.fn.plentyDateInput = function(conf){
	return this.each(function(){
		$(this).
			bind("focus",function(){
				$(this).after($("#calroot"));
			}).
			dateinput(conf);
	});
}