// JavaScript Document

jQuery.fn.swapWith = function(to) {
    return this.each(function() {
        var copy_to = to.clone(true);
        var copy_from = $(this).clone(true);
        to.replaceWith(copy_from);
        $(this).replaceWith(copy_to);
    });
};


$(document).ready(function() {
	if($('table#hotelWrapper')) {
		var swap = Math.floor(Math.random() * 2) < 1 ? true : false;
		if (swap) {
			$('table#hotelWrapper > tbody > tr:eq(0) > td').eq(0).swapWith($('table#hotelWrapper > tbody > tr:eq(0) > td').eq(2));
			$('table#hotelWrapper > tbody > tr:eq(1) > td').eq(0).swapWith($('table#hotelWrapper > tbody > tr:eq(1) > td').eq(1));
		}
	}
	$('table.randomize').each(function() {
		var nCells = $('td:not(.empty):not(.fixed)', this).length;
		if (nCells > 1) {
			for (i=0; i<2*nCells; i++) {
				var c1 = Math.floor(Math.random() * nCells);
				var c2 = Math.floor(Math.random() * nCells);
				if (c1 >= 0 && c1 < nCells && c2 >= 0 && c2 < nCells && c1 != c2) {
					$('td:not(.empty):not(.fixed)', this).eq(c1).swapWith($('td:not(.empty):not(.fixed)', this).eq(c2));
				}
			}
		}
	} );	
});
