jQuery.fn.alternateRowColors = function() {
	jQuery('tbody tr:odd', this).removeClass('even').addClass('odd');
	jQuery('tbody tr:even', this).removeClass('odd').addClass('even');
	return this;
};

var inactiveAntetColor = '#000000';
var activeAntetColor = '#9b0005';
var containerWidth = '470px';

function showThickboxWindow(link) {
	var container = jQuery(link).parents('.spectable');
	var table = jQuery('table.sortable', container);
	var tableWidth = jQuery(table).width() + 19;
	
	var t = link.title || link.name || null;
	var a = link.href + '&width=' + tableWidth;
	var g = link.rel || false;
		
	tb_show(t,a,g);
	link.blur();
	return false;
}

jQuery(document).ready(function() {
	jQuery('.sortable').each(function(i) {
		var tableId = 'sTable' + i;
		var tHeight = jQuery(this).height();
		var hNobr, sortUp, sortDown, arrDown, hAntet, defarrset = 0;
		
		jQuery('th', this).each(function() {
			hNobr = document.createElement('nobr');
			
			if (jQuery(this).hasClass('sort-alpha') || jQuery(this).hasClass('sort-numeric')) {
				hAntet = jQuery(document.createElement('span')).append(jQuery(this).text());
				jQuery(hNobr).append(hAntet[0]);
				
				// up ico
				sortUp = document.createElement('img');
				if (!defarrset) {
					arrDown = '/i/arr_red_down.gif';
				} else {
					arrDown = '/i/arr_black_down.gif';
				}
				jQuery(sortUp).attr('src', arrDown).attr('alt', '').addClass('down');
				jQuery(hNobr).append(sortUp);
				
				// down ico
				sortDown = document.createElement('img');
				jQuery(sortDown).attr('src', '/i/arr_black_up.gif').attr('alt', '').addClass('up');
				jQuery(hNobr).append(sortDown);
				
				jQuery(this).html(hNobr);
				
				jQuery(this).width(jQuery(hNobr).width());
				defarrset = 1;
				
				jQuery('span, img', this).hover(function() {
					jQuery(this).css('cursor', 'pointer');
				}, function() {
					jQuery(this).css('cursor', 'arrow');
				});
			
			} else {
				jQuery(hNobr).append(jQuery(this).html());
				jQuery(this).html(hNobr);
			}
		});
		var tWidth = jQuery(this).width();
				
		wHeight = 0;
		if (window.innerHeight) {
			wHeight = window.innerHeight;
		} else {
			wHeight = screen.Height;
		}
		
		if (tHeight > wHeight) {
			tHeight = wHeight - 90;
		}
		var caption = jQuery('caption', this);
		var closebutton = document.createElement('span');
		jQuery(closebutton).html('<a href="javascript:void(0);" id="TB_closeWindowButton" title="Затваряне"><img src="/i/x.jpg" alt="Затваряне" /></a>');
		jQuery(caption).prepend(closebutton);
		var capt = document.createElement('p');
		jQuery(capt).addClass('caption');
		jQuery(capt).append(jQuery(caption).text());
			
		var info = '<p class="seemore"><a href="#TB_inline?t=1&height=' + tHeight + '&inlineId=' + tableId + '" onclick="javascript:showThickboxWindow(this)">Виж цялата таблица</a></p>';
		
		var container = document.createElement('div');
		jQuery(container).attr('id', tableId);
		
		var outer = document.createElement('div');
		jQuery(outer).addClass('spectable');
		
		var table = jQuery(this).clone(true);
		jQuery(container).append(table);
		
		jQuery(outer).append(container);
												
		if (tWidth > parseInt(containerWidth)) {
			jQuery(info).appendTo(outer);
			jQuery(info).prependTo(outer);
		}
		jQuery(capt).prependTo(outer);
		jQuery(this).replaceWith(outer);
	});
	
	jQuery('table.sortable').each(function() {	
		var $table = jQuery(this);
		$table.alternateRowColors();
		var $headrows = $table.find('thead').children();
		var sortType;
		
		jQuery($headrows[$headrows.length - 1]).find('th').each(function(column) {
			var findSortKey;

			if (jQuery(this).is('.sort-alpha')) {
				sortType = 'sort-alpha';
				findSortKey = function($cell) {
					return $cell.find('.sort-key').text().toUpperCase() + ' ' + $cell.text().toUpperCase();
				};
		
			} else if (jQuery(this).is('.sort-numeric')) {
				sortType = 'sort-numeric';
				findSortKey = function($cell) {
					var key = parseFloat($cell.text().replace(/,/g, '').replace(/^[^\d.,-]*/, ''));
					return isNaN(key) ? 0 : key;
				};
			
			} else if (jQuery(this).is('.sort-date')) {
				sortType = 'sort-date';
				findSortKey = function($cell) {
					return Date.parse('1 ' + $cell.text());
				};
			}
			
			if (findSortKey) {
				jQuery('span, img', this).addClass('clickable').hover(function() {
					jQuery('span', this).addClass('hover');
	
				}, function() {
					jQuery('span', this).removeClass('hover');
				
				}).click(function() {
					var newDirection = 1;
					
					if (jQuery(this).is('img')) {
						if (jQuery(this).hasClass('up')) {
							newDirection = -1;
						}
					
					} else {
						var th = (jQuery(this).parents('th'))[0];
						if (jQuery(th).is('.sorted-asc')) {
							newDirection = -1;
						}
					}

					var rows = $table.find('tbody > tr').get();

					jQuery.each(rows, function(index, row) {
						row.sortKey = findSortKey(jQuery(row).children('td').eq(column));
					});
					rows.sort(function(a, b) {
						if (a.sortKey < b.sortKey) return -newDirection;
						if (a.sortKey > b.sortKey) return newDirection;
						return 0;
					});
					jQuery.each(rows, function(index, row) {
						$table.children('tbody').append(row);
						row.sortKey = null;
					});

					$table.find('th').removeClass('sorted-asc').removeClass('sorted-desc');
					var $sortHead = $table.find('th').filter(':nth-child(' + (column + 1) + ')');
					if (newDirection == 1) {
						$sortHead.addClass('sorted-asc');
					} else {
						$sortHead.addClass('sorted-desc');
					}
					$table.find('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');

					$table.alternateRowColors();
				
					// icons:
					jQuery(this).parents('tr').find('img.down').each(function() {
						jQuery(this).attr('src', '/i/arr_black_down.gif');
					});
					jQuery(this).parents('tr').find('img.up').each(function() {
						jQuery(this).attr('src', '/i/arr_black_up.gif');
					});
					
					if (jQuery(this).parents('th').hasClass('sorted-asc')) {
						jQuery(this).parents('th').find('img.down').attr('src', '/i/arr_red_down.gif');
						jQuery(this).parents('th').find('img.up').attr('src', '/i/arr_black_up.gif');
					
					} else if (jQuery(this).parents('th').hasClass('sorted-desc')) {
						jQuery(this).parents('th').find('img.down').attr('src', '/i/arr_black_down.gif');
						jQuery(this).parents('th').find('img.up').attr('src', '/i/arr_red_up.gif');
					}
				});
			}
		});
		
		// antets:
		jQuery(this).parents('tr').find('span').css('color', inactiveAntetColor);
		jQuery(this).parents('th').find('span').css('color', activeAntetColor);
		
		
	});
	
});

jQuery(document).ready(function() {
	jQuery('table.striped').each(function() {
		jQuery(this).bind('stripe', function() {
			var rowIndex = 0;
			jQuery('tbody tr:not(.filtered)', this).each(function(index) {
				if (jQuery('th',this).length) {
					jQuery(this).addClass('subhead');
					rowIndex = -1;
				} else {
					if (rowIndex % 6 < 3) {
						jQuery(this).removeClass('odd').addClass('even');
					
					} else {
						jQuery(this).removeClass('even').addClass('odd');
					}
				};
				rowIndex++;
			});
		});
		jQuery(this).trigger('stripe');
	});
});
