var jQuery_1_8_2 = jQuery_1_8_2 || $.noConflict();
(function ($, undefined) {
	$(function () {
		var $dialogStripeInfo = $("#dialogStripeInfo"),
			dialog = ($.fn.dialog !== undefined),
			datagrid = ($.fn.datagrid !== undefined);

		function formatBtn(str, obj) {
			return ['<input type="button" class="pj-button stripe-details" data-id="', obj.id, '" value="', myLabel.btn_view, '" />'].join("");
		}

		if ($("#grid").length > 0 && datagrid) {
			var $grid = $("#grid").datagrid({
				buttons: [],
				columns: [
				    {text: myLabel.session_id, type: "text", sortable: true, editable: false},
				    {text: myLabel.txn_id, type: "text", sortable: true, editable: false},
				    {text: myLabel.amount, type: "text", sortable: true, editable: false},
				    {text: myLabel.currency, type: "text", sortable: true, editable: false},
				    {text: myLabel.email, type: "text", sortable: true, editable: false},
				    {text: myLabel.dt, type: "text", sortable: true, editable: false},
				    {text: "", type: "text", sortable: false, editable: false, renderer: formatBtn}
				],
				dataUrl: "index.php?controller=pjStripe&action=pjActionGetStripe",
				dataType: "json",
				fields: ['session_id', 'txn_id', 'amount', 'currency', 'payer_email', 'dt', 'id'],
				paginator: {
					actions: [],
					gotoPage: true,
					paginate: true,
					total: true,
					rowCount: true
				},
				select: false
			});
		}

		$(document).on("click", ".stripe-details", function (e) {
			if (e && e.preventDefault) {
				e.preventDefault();
			}
			if ($dialogStripeInfo.length > 0 && dialog) {
				$dialogStripeInfo.data("id", $(this).data("id")).dialog("open");
			}
			return false;
		});

		if ($dialogStripeInfo.length > 0 && dialog) {
			$dialogStripeInfo.dialog({
				modal: true,
				autoOpen: false,
				draggable: false,
				resizable: false,
				width: 560,
				open: function () {
					$dialogStripeInfo.html("");
					$.get("index.php?controller=pjStripe&action=pjActionGetDetails", {
						"id": $dialogStripeInfo.data("id")
					}).done(function (data) {
						$dialogStripeInfo.html(data);
						$dialogStripeInfo.dialog("option", "position", "center");
					});
				},
				buttons: (function () {
					var btns = {};
					btns[myLabel.btn_close] = function () {
						$dialogStripeInfo.dialog("close");
					};
					return btns;
				})()
			});
		}
	});
})(jQuery_1_8_2);
