sortDir = new Array ();

getValue = new Array ();
getValue.model = function ( v ) {
	//return v.match ( '^.*>(.*)</a.*$' )[1];
	return />([^<>]*)</.exec ( v )[1];
}
getValue.color = function ( v ) {
	return v;
}
getValue.descr = function ( v ) {
	return v;
}
getValue.week = function ( v ) {
	return v;
}
getValue.price = function ( v ) {
	return v;
}
getValue.sprice = function ( v ) {
	return v;
}

var cols = new Array ( 'model', 'color', 'descr', 'week', 'price', 'sprice', 'eng', 'inner' );

function tsort ( t, cn ) {
	for ( var i = 0; i < t.length; i++ ) {
		for ( var j = i; j < t.length; j++ ) {
			if ( ( ( sortDir[cn] == 0 ) && ( getValue[cn] ( t[i][cn] ) < getValue[cn] ( t[j][cn] ) ) ) || ( ( sortDir[cn] == 1 ) && ( getValue[cn] ( t[i][cn] ) > getValue[cn] ( t[j][cn] ) ) ) ) {
				var row = t[i];
				t[i] = t[j];
				t[j] = row;
			}
		}
	}

	return t;
}

function getRow ( tn, n ) {
	var row = new Array ();
	for ( k in cols ) {
		var cell = document.getElementById ( tn + cols[k] + n );
		if ( cell ) {
			row[cols[k]] = cell.innerHTML
		}
	}

	return row;
}

function setRow ( tn, n, row ) {
	for ( k in cols ) {
		var el = document.getElementById ( tn + cols[k] + n );
		if ( el ) {
			el.innerHTML = row[cols[k]];
		}
	}
}

function tableSort ( tn, cn ) {
	var table = new Array ();
	var i = 1;
	var row = getRow ( tn, i );

	while ( row.model ) {
		table.push ( row );
		i = i + 1;
		row = getRow ( tn, i );
	}

	if ( sortDir[cn] == 0 ) {
		sortDir[cn] = 1;
	} else {
		sortDir[cn] = 0;
	}

	table = tsort ( table, cn );

	/*
	table.sort ( function ( a, b ) {
		if ( sortDir[cn] == 0 ) {
			return getValue[cn] ( a[cn] ) > getValue[cn] ( b[cn] );
		} else {
			return getValue[cn] ( a[cn] ) < getValue[cn] ( b[cn] );
		}
	} )
	*/

	j = i;
	for ( var i = 1; i < j; i++ ) {
		setRow ( tn, i, table[i - 1] );
	}
}

var checkHash = function () {
	if ( document.location.hash == '#tab1' ) {
		document.getElementById ( 'tab1' ).style.display = 'block';
	};
	if ( document.location.hash == '#tab2' ) {
		document.getElementById ( 'tab2' ).style.display = 'block';
	};
	if ( document.location.hash == '#tab3' ) {
		document.getElementById ( 'tab3' ).style.display = 'block';
	};
};

//document.getElementsByTagName ( "body" )[0].addEventListener ( "load", function () {} );

/*
( function () {
	if ( document.location.hash == '#tab1' ) {
		document.getElementById ( 'tab1' ).style.display = 'block';
	};
} ) ().defer ( 1000 );
*/

