var oTable;

$(document).ready(function(){
	$('span.sk').tooltip();
	$('#o_rent_time').timePicker();

	if ($('#rent_order')) {
		$('input[type="checkbox"]').change(function(){
			sessionCart($(this).attr('value'));
			countCashbail();
			countPrice();
		});

		$(".tabulka-obj input[type='text']").change(function(){ countCashbail(); countPrice(); })
		$('#o_rent_days').change(function(){ countPrice(); })

		countCashbail();
		countPrice();
	}

	$("fieldset legend").click(function() {
		$(this).parent().children().filter("p,img,table,ul,div,span").toggle("fast");
	});

	$("#dataman tbody").click(function(event) {
		fnReloadIDs();
		id = $(event.target.parentNode).attr('id');

		tb_show("Objednávka č."+id, "http://www.cestovnapozicovna.sk/global/ax.order-details.php?height=400&width=600&id="+id);
	});

	oTable = $('table#dataman').dataTable({
		"sAjaxSource"    : "http://www.cestovnapozicovna.sk/global/ax.orders.php",
		"aaSorting"      : [[ 1, "desc" ]],
		"sPaginationType": "full_numbers",
		"sDom"           : '<"top"i>rt<"bottom"flp<"clear">',
		"bStateSave"     : true,
		"bProcessing"    : true,
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
			if (aData[3]=="Nová") {
				$(nRow).removeClass('gradeA').addClass('gradeX');
			}
			if (aData[3]=="Prijatá") {
				$(nRow).removeClass('gradeA').addClass('gradeU');
			}
			return nRow;
		}
	});
	var oSettings = oTable.fnSettings();

});

function countCashbail () {
	var t = 0.00;
	$('input:checked').each(function() {
		$('tr#i'+$(this).val()).each(function() {
			t += parseFloat($(this).find('td:eq(2)').text().replace(" EUR", "")) * $(this).find("input[type='text']").val();
		});
	});
	t = t.toFixed(2);
	$('table tfoot tr td:eq(2)').html('<span class="skx" title="'+(t*30.126).toFixed(2)+' SKK">'+t+' EUR</span>');
	$('span.skx').tooltip();
}

function countPrice () {
	var t = 0.00;
	var d = 1;
	var col = false;
	if ($('#o_rent_days').val()!='') d = parseFloat($('#o_rent_days').val());

	if (d<8) col=3; else col=4;

	$('input:checked').each(function() {
		$('tr#i'+$(this).val()).each(function() {
			t += parseFloat($(this).find('td:eq('+col+')').text().replace(" EUR", "")) * $(this).find("input[type='text']").val();;
		});
	});
	t = (t*d).toFixed(2);
	$('table.tabulka-obj tfoot tr td:eq('+col+')').html('<span class="sky" title="'+(t*30.126).toFixed(2)+' SKK">'+t+' EUR</span>');
	if (col==3) $('table tfoot tr td:eq(4)').text(' ');
	if (col==4) $('table tfoot tr td:eq(3)').text(' ');
	$('span.sky').tooltip();
}

function sessionCart (id) {
	setTimeout(function() {
		if ( $("input:checked[value='"+id+"']").val() == id ) {
			$('input#inp'+id).removeAttr('disabled').val('1');
			toCart(parseInt(id));
		} else {
			$('input#inp'+id).attr('disabled', 'disabled').val('0');
			rmFromCart(parseInt(id));
		}
		countCashbail();
		countPrice();
	}, 500);
}

function toCart (id) {
	if (typeof(id)=="number") {
		$.post("global/ax.cart-add.php?id="+id,
			function(data) {
				if (data=='1') {
					$('#ostatus'+id).text("v objednávke");
				}
			}
		);
	}
}

function rmFromCart (id) {
	if (typeof(id)=="number") {
		$.post("global/ax.cart-remove.php?id="+id);
	}
}

function fnReloadIDs () {
	$('table#dataman tbody tr').each ( function() {
		$(this).attr('id', $(this).children("td:eq(0)").text());
	});
}

function fnGetSelected (oTableLocal) {
	var aReturn = new Array();
	var aTrs = oTableLocal.fnGetNodes();

	for ( var i=0 ; i<aTrs.length ; i++ ) {
		if ( $(aTrs[i]).hasClass('row_selected') ) {
			aReturn.push( aTrs[i] );
		}
	}

	return aReturn;
}

$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource ) {
	if ( typeof sNewSource != 'undefined' ) {
		oSettings.sAjaxSource = sNewSource;
	}
	this.fnClearTable( this );
	this.oApi._fnProcessingDisplay( oSettings, true );
	var that = this;

	$.getJSON( oSettings.sAjaxSource, null, function(json) {
		for ( var i=0 ; i<json.aaData.length ; i++ ) {
			that.oApi._fnAddData( oSettings, json.aaData[i] );
		}

		oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
		that.fnDraw( that );
		that.oApi._fnProcessingDisplay( oSettings, false );
	} );
}
