// sort
var agSort =
	new Class(
	{
		initialize:
			function(table, idx)
			{
				this.table = $(table);
				this.idx = idx;
				this.tbody = $(this.table.getElementsByTagName("tbody")[0]);

				this.headers = new Hash;
				var thead = $(this.table.getElementsByTagName("thead")[0]);

				this.sort_orders = new Array();

    			$each(thead.getElementsByTagName("tr")[0].getElementsByTagName("th"),
    				function(header, index)
					{
						var header = $(header);

						this.headers.set(header.getText(), { column: index });
						this.sort_orders.push(0);
					}.bind(this));

				if (window.ie6)
				{
					rows = this.tbody.getElementsByTagName("tr");

					for (var i = 0; i < rows.length; i ++)
					{
						rows[i].onmouseover =
							function()
							{
								// this.className += " hover";
								this.className = "cz_row_on";
							};

						rows[i].onmouseout =
							function()
							{
								// this.className = this.className.replace(" hover", "");
								this.className = "cz_row";
							};
					}
				}

				this.preload();
				this.load_conversions();
			},

		preload:
			function()
			{
				path = _static_img + "/images/common/";

				this.images = new Array();

				this.images[0] = new Image();
				this.images[1] = new Image();
				this.images[2] = new Image();

				this.images[0].src = path + "sort_off.gif";
				this.images[1].src = path + "sort_asc.gif";
				this.images[2].src = path + "sort_desc.gif";
			},

		sort_by_header:
			function(header_text)
			{
				var trs = this.tbody.getElements("tr");

				this.rows = new Array;

				while (trs.length > 0)
				{
					var row = { row: trs.shift().remove() };

					this.rows.unshift(row);
				}

				var header = this.headers.get(header_text);

				if (this.sort_column >= 0 && this.sort_column == header.column)
				{
					// They were pulled off in reverse
				}
				else
				{
					this.sort_column = header.column;

					if (header.conversion_function)
					{
						this.conversion_function = header.conversion_function;
					}
					else
					{
						this.conversion_function = false;

						this.rows.some(
							function(row)
							{
								var to_match = $(row.row.getElementsByTagName("td")[this.sort_column]).getText();

								if (to_match == '')
								{
									return false;
								}

								this.conversions.some(
									function(conversion)
									{
										if (conversion.matcher.test(to_match))
										{
											this.conversion_function = conversion.conversion_function;

											return true;
										}

										return false;
									}.bind(this));

								if (this.conversion_function)
								{
									return true;
								}

								return false;
							}.bind(this));

						header.conversion_function = this.conversion_function.bind(this);

						this.headers.set(header_text, header);
					}

					this.rows.each(
						function(row)
						{
							row.compare_value = this.conversion_function(row);
						}.bind(this));

					this.rows.sort(this.compare_rows.bind(this));
				}

				var index = 0;

				while (this.rows.length > 0)
				{
					var row = this.rows.shift();

					row.row.injectInside( this.tbody );

					if (row.detail)
					{
						row.detail.injectInside(this.tbody);
					}

					index ++;
				}

				this.rows = false;
			
				// sets sort order
				for (var i = 0; i < this.sort_orders.length; i ++)
				{
					if (i == this.sort_column)
					{
						this.sort_orders[i] = (this.sort_orders[i] == 0 ? 1 : 0);

						if ($("sort_" + this.idx + "_" + i))
						{
							$("sort_" + this.idx + "_" + i).src = this.images[(this.sort_orders[i] == 0 ? 2 : 1)].src;
						}
					}
					else
					{
						this.sort_orders[i] = 0;

						if ($("sort_" + this.idx + "_" + i))
						{
							$("sort_" + this.idx + "_" + i).src = this.images[0].src;
						}
					}
				}
			},

		compare_rows:
			function( r1, r2 )
			{
				if (r1.compare_value > r2.compare_value)
				{
					return 1;
				}

				if (r1.compare_value < r2.compare_value)
				{
					return -1;
				}

				return 0;
			},
		
		load_conversions:
			function()
			{
				this.conversions = $A([
					// Currency
					{
						matcher: /((\d{1}\.\d{2}|\d{2}\.\d{2}|\d{3}\.\d{2}|\d{4}\.\d{2}|\d{5}\.\d{2}|\d{6}\.\d{2}))/,
						conversion_function:
							function(row)
							{
								var cell = $(row.row.getElementsByTagName("td")[this.sort_column]).getText();
								cell = cell.replace(/[^\d]/g, "");

								console.log("00000000000000000000000000000000".substr(0, 32 - cell.length).concat(cell));

								return "00000000000000000000000000000000".substr(0, 32 - cell.length).concat(cell);
							}
					},

					// czk price
					{
						matcher: /(\d{1,3}\s{1}){1,}[A-Za-z€č]{1,}/,
						conversion_function:
							function(row)
							{
								var cell = $(row.row.getElementsByTagName("td")[this.sort_column]).getText();
								cell = cell.replace(/[^\d]/g, "");

								return "00000000000000000000000000000000".substr(0, 32 - cell.length).concat(cell);
							}
					},

					// DD-MM-YYYY, d-m-YYYY
					{
  						matcher: /\d{1,2}\/\d{1,2}\/\d{4}/,
						conversion_function:
							function(row)
							{
								var cell = $(row.row.getElementsByTagName("td")[this.sort_column]).getText();
								var re = /(\d{1,2})\/(\d{1,2})\/(\d{4})/;

								cell = re.exec(cell);

								return new Date(parseInt(cell[3]), parseInt(cell[2], 10) - 1, parseInt(cell[1], 10));
							}
					},

					// Numbers
					{
						matcher: /^\d+$/,
						conversion_function:
							function(row)
							{
								var cell = $(row.row.getElementsByTagName("td")[this.sort_column]).getText();

								return "00000000000000000000000000000000".substr(0, 32 - cell.length).concat(cell);
							}
					},

					// Fallback
					{
						matcher: /.*/,
						conversion_function:
							function(row)
							{
								return $(row.row.getElementsByTagName("td")[this.sort_column]).getText();
							}
					}]);
			}
	});

// globals
t1 = {};
t2 = {};

// loader
window.addEvent("domready",
	function()
	{
		// premium ads
		if ($("ads_table_1"))
		{
			t1 = new agSort("ads_table_1", 1);
		}

		// new ads
		if ($("ads_table_2"))
		{
			t2 = new agSort("ads_table_2", 2);
		}
	}
);