/* $Id: plugin-general.js,v 1.1 2011/03/12 08:32:21 chris Exp $ */

/* NATIVE-LIKE ENHANCEMENT & SUPPLEMENTS  *************************************/


/**	Debug function				// TODO: sprawdzić jak to właściwie działa
 *	http://markc.renta.net/jquery/, 206-06-11
 */
$.fn.dbg = function(obj) {
  var buf = '';
  var o = obj || this;
  if (o) {
    for (var i in o) {
      if ((o[i] instanceof Function)
       || (o[i] == null)
       || (i.toUpperCase() == i)) continue;
      buf += i + '=' + o[i] + ', ';
    }
  } else buf = 'Null Object';
  alert(buf);
  return o;
};


/**	Checks if given selector has found anything
 * 	by chris, 2006-06-15
 */
$.fn.exists = function()
{
	return this.size() > 0;
};


 
/** Replaces Old class with New one in all found elements
 * 	by chris, 2006-06-15 
 */

$.fn.replaceClass = function(oldClassName, newClassName)
{
	this.addClass(newClassName).removeClass(oldClassName);
	return this;
};


/**	Wrapper for DOM getAttribute method, returns attribute value for first element found
 * 	by chris, 2006-06-11
 */
$.fn.getAttribute = function(sAttrName) 
{
	var aR = new Array();
	this.each(function() { aR[aR.length] = this.getAttribute(sAttrName); });
	return aR[0];
};

/**	Wrapper for DOM getAttribute method, returns an array of attribute values for all elements found
 * 	by chris, 2006-06-11
 */
$.fn.getAttributes = function(sAttrName) 
{
	var aR = new Array();
	this.each(function() { aR[aR.length] = this.getAttribute(sAttrName); });
	return aR;
};


/** Workaround for DOM getAttribute('class') method (IE bug), returns 'class' value for fisrt found element
 * 	by chris, 2006-06-15
 */
$.fn.getClass = function()
{
	var aR = new Array();
	this.each(function() { aR[aR.length] = this.attributes['class'].value; });
	return aR[0];
};

/**	Workaround for DOM getAttribute method (IE bug), returns an array of 'class' values for all elements found
 * 	by chris, 2006-06-15
 */
$.fn.getClasses = function() 
{
	var aR = new Array();
	this.each(function() { aR[aR.length] = this.attributes['class'].value; });
	return aR;
};


$.fn.getContents = function() 
{
	var aR = new Array();
	this.each(function() { aR[aR.length] = this.innerHTML; });
	return aR;
};

$.fn.getContent = function() 
{
	var aR = new Array();
	this.each(function() { aR[aR.length] = this.innerHTML; });
	return aR[0];
};





$.fn.stripable = function()
{ 
	this.each(function()
	{	var even =  true;
		$('.data-row:visible', this).each(function() 
		{
			if(even) 
				$(this)
					.addClass('data-list-even');
			else 
				$(this)
					.addClass('data-list-odd');
			even = !even;
		});
	});
	return this;
};


$.fn.listable = function()
{ 
	this.each(function()
	{	var even =  true;
		$('.data-row:visible', this).each(function() 
		{
			if(even) 
				$(this)
					.addClass('data-list-even')
					.hover(
						function() { $(this).replaceClass('data-list-even', 'data-hover-even'); }, 
						function() { $(this).replaceClass('data-hover-even', 'data-list-even'); }
						);
			else 
				$(this)
					.addClass('data-list-odd')
					.hover(
						function() { $(this).replaceClass('data-list-odd', 'data-hover-odd'); }, 
						function() { $(this).replaceClass('data-hover-odd', 'data-list-odd'); }
						);
			even = !even;

		});
	});
	return this;
};



$.fn.hoverable = function()
{ 
	this.each(function()
	{	var even =  true;
		$('.data-row:visible', this).each(function() 
		{
			if(even) 
				$(this)
					.hover(
						function() { $(this).addClass('data-hover-even'); }, 
						function() { $(this).removeClass('data-hover-even'); }
						);
			else 
				$(this)
					.hover(
						function() { $(this).addClass('data-hover-odd'); }, 
						function() { $(this).removeClass('data-hover-odd'); }
						);
			even = !even;

		});
	});
	return this;
};



$.fn.clickable = function(fAOnClick, sATitle)
{ 
	this.each(function()
	{
		$('.data-row:visible', this)
			.click(function() { eval(fAOnClick); })
			.css('cursor', 'pointer')
			.attr("title", sATitle);
	});
	return this;
};



$.fn.pointable = function(sATitle)
{ 
	this.each(function()
	{
		$('.data-row:visible', this)
			.addClass('data-list-pointable')
			.attr("title", sATitle);
	});
	return this;
};




$.fn.showLoadingNormal = function()
{
	this.html('<span class="loading-normal">Pobieranie danych ...</span>');
	return this;
}

$.fn.showLoadingSmall = function()
{
	this.html('<span class="loading-small">Pobieranie danych ...</span>');
	return this;
}



/* ADDONS & OTHER PLUGINS *****************************************************/

/*	Tabs - jquery plugin for accessible, unobtrusive tabs 
 *	Free beer and free speech. Enjoy!
 * 	by Klaus Hartl, http://stilbuero.de/tabs/, added 2006-06-11
 */
$.tabs = function(containerId, start) {		// TODO: jakiś kod jeszcze został na tej stronie; 2006-06-11, chris
    var ON_CLASS = 'on';
    var id = '#' + containerId;
    var i = (typeof start == "number") ? start - 1 : 0;
    $(id + '>div:lt(' + i + ')').add(id + '>div:gt(' + i + ')').hide();
    $(id + '>ul>li:nth-child(' + i + ')').addClass(ON_CLASS);
    $(id + '>ul>li>a').click(function() {
        if (!$(this.parentNode).is('.' + ON_CLASS)) {
            var re = /([_\-\w]+$)/i;
            var target = $('#' + re.exec(this.href)[1]);
            if (target.size() > 0) {
                $(id + '>div:visible').hide();
                target.show();
                $(id + '>ul>li').removeClass(ON_CLASS);
                $(this.parentNode).addClass(ON_CLASS);
            } else {
                alert('There is no such container.');
            }
        }
        return false;
    });
};



$.stripDataList = function(className, modeOption, sortOption)
{
	if( $('table.data-list.'+ className +' tr.data-row').size() > 0 )
	{
		var tables = $('table.data-list.'+ className);
		var sortOption = sortOption == 'sortable';

		switch(modeOption) {
			case 'stripable':
				if(sortOption) tables.stripable().tableSorter();
				else tables.stripable();
				break;
				
			case 'pointable':
				if(sortOption) tables.listable().tableSorter();
				else tables.listable();
				break;

			case 'clickable':
				if(sortOption) tables.listable().pointable().tableSorter();
				else tables.listable().pointable();
				break;
		}
	}			
};	// paintDataListTable
	



