reservations.js 23.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
( function( $, qt ) {
	var tableName;

	QS.Reservations = ( function() {
		var defs = {
					nonce: '',
					edata: {},
					ajaxurl: '/wp-admin/admin-ajax.php',
					templates: {},
					messages: {},
					owns: {}
				},
				has = 'hasOwnProperty';

		function _generic_efunc( r ) {
			if ( ! qt.is( console ) || ! qt.isF( console.log ) ) return;

			if ( qt.isO( r ) && qt.isA( r.e ) && r.e.length ) {
				for ( var i = 0; i < r.e.length; i++ )
					console.log( 'AJAX Error: ', r.e[ i ] );
			} else {
				console.log( 'AJAX Error: Unexpected ajax response.', r );
			}
		}

		function _aj( data, func, efunc ) {
			var me = this;
					data = $.extend( { sa:'unknown' }, data, { action:'qsot-frontend-ajax', _n:me.o.nonce, event_id:me.o.event_id } );

			if (data.items[0].name){
				tableName = data.items[0].name;
			}
			

			$.ajax( {
				url: me.o.ajaxurl,
				data: data,
				dataType: 'json',
				error: efunc,
				method: 'POST',
				success: func,
				xhrFields: { withCredentials: true }
			} );
		}

		function _name_lookup() {
			this.names = {};
			this.unknown = this.msg( '(unknown)' );
			var i;
			for ( i in this.o.edata.zones )
				if ( this.o.edata.zones[ has ]( i ) )
					this.names[ this.o.edata.zones[ i ].id + '' ] = this.o.edata.zones[ i ].name;
		}

		function _setup_ui() {
			var me = this;

			me.e.main_wrap = me.e.main.parent();

			me.e.loading = $( me.tmpl( 'loading' ) ).insertAfter( me.e.main );

			me.e.psui = $( me.tmpl( 'price-selection-ui' ) ).insertAfter( me.e.main );
			me.e.ps_box = me.e.psui.find( '[rel="box"]' );
			me.e.ps_error = me.e.psui.find( '[rel="error"]' );
			me.e.ps_backdrop = me.e.psui.find( '[rel="backdrop"]' );
			me.e.ps_list = me.e.psui.find( '[rel="price-list"]' );
			me.e.ps_qty_ui = me.e.psui.find( '[rel="qty-ui"]' );
			me.e.ps_qty = me.e.psui.find( '[rel="quantity"]' );
			me.e.ps_sel_list = me.e.psui.find( '[rel="sel-list"]' );

			me.e.ui = $( me.tmpl( 'ticket-selection' ) )[ 'below' == this.o.options['chart-position'] ? 'insertBefore' : 'insertAfter' ]( me.e.main );
			me.e.ui_title = me.e.ui.find( '[rel="title"]' );
			me.e.owns_cont = me.e.ui.find( '[rel="owns"]' );
			me.e.nosvg = me.e.ui.find( '[rel="nosvg"]' );
			me.e.actions = me.e.ui.find( '[rel="actions"]' );

			me.e.msgs = $( me.tmpl( 'msg-block' ) ).insertBefore( me.e.ui );
			me.e.errors = me.e.msgs.find( '[rel="errors"]' );
			me.e.confirms = me.e.msgs.find( '[rel="confirms"]' );

			me.e.owns_wrap = $( me.tmpl( 'owns-wrap' ) ).appendTo( me.e.owns_cont );
			me.e.owns = me.e.owns_wrap.find( '[rel="owns-list"]' );
			me.e.ubtn = me.e.owns_wrap.find( '[rel="update-btn"]' );
		}

		function _setup_events() {
			var me = this;

			me.e.ubtn.on( 'click', function( e ) {
				e.preventDefault();
				var data = { items:[] };
				me.e.owns.find( '> .item' ).each( function() {
					var ele = $( this );
					data.items.push( {
						state: ele.is( '[rel="own-item"]' ) ? 'r' : 'i',
						zone: ele.find( '[rel="zone"]' ).val(),
						'ticket-type': ele.find( '[rel="ticket-type"]' ).val(),
						quantity: ele.find( '[rel="qty"]' ).val() || 0
					} );
				} );
				me.reserve( data );
			} );

			me.e.owns.on( 'click', '[rel="remove-btn"]', function( e ) {
				e.preventDefault();

				
				var ele = $( this ), par = ele.parents( '.item' ), data = { items:[] };
				data.items.push( {
					state: par.is( '[rel="own-item"]' ) ? 'r' : 'i',
					zone: par.find( '[rel="zone"]' ).val(),
					'ticket-type': par.find( '[rel="ticket-type"]' ).val() || 0
				} );
				me.remove( data, function() { par.fadeOut( { duration:300, complete:function() { par.remove() } } ); } );
			} );

			me.e.owns.on( 'click', '[rel="continue-btn"]', function( e ) {
				var data = { items:[] }, par = $( this ).closest( '.item[key]' );
				if ( ! par.length ) return;

				data.items.push( {
					zone: par.find( '[rel="zone"]' ).val(),
					'ticket-type': 0,
					quantity: 1
				} );
				QS.cbs.trigger( 'continue-interest', [ data, me ] );
				me.price_selection( data.items, function( selected_price, qty, after_resp ) {
					var i;
					for ( i = 0; i < data.items.length; i++ ) {
						data.items[ i ]['ticket-type'] = selected_price;
						data.items[ i ]['quantity'] = qty;
					}
					me.reserve( data );
				} );
			} );

			me.e.psui.on( 'click', '[rel="close"]', function( e ) {
				e.preventDefault();
				me.e.psui.trigger( 'early-close' );
				_reset_ps.call( me );
			} );
		}

		function _reset_ps() {
			var me = this;
			me.e.psui.hide();
			me.e.ps_sel_list.empty();
			me.e.ps_list.empty();
			me.e.ps_qty.val( '1' ).attr( 'max', '' );
		}

		// setup the initial list of reservations on page load
		function _setup_existing() {
			// if there are no reservations, then skip this
			if ( ! qt.isO( this.o.owns ) )
				return;

			// cycle through the zone-indexed list of reservations, and add reservation rows for each zone-tickettype-state combo
			for ( zone_id in this.o.owns ) {
				for ( ticket_type_id in this.o.owns[ zone_id ] ) {
					for ( state in this.o.owns[ zone_id ][ ticket_type_id ] ) {
						var item = {
							z: zone_id,
							t: ticket_type_id,
							q: this.o.owns[ zone_id ][ ticket_type_id ][ state ],
							c: qt.is( this.o.edata.stati[ zone_id ] ) && qt.is( this.o.edata.stati[ zone_id ][1] ) ? qt.toInt( this.o.edata.stati[ zone_id ][1] ) : 0
						};

						if ( 'interest' == state ) {
							_add_interest_row.call( this, item );
						} else if ( 'reserved' == state ) {
							_add_reserve_row.call( this, item );
						}
					}
				}
			}
		}

		function _clean( data ) {
			var out = $.extend( true, { items:[] }, data ), i;
			if ( ! qt.isA( out.items ) ) out.items = [];

			for ( i = 0; i < out.items.length; i++ )
				out.items[ i ] = $.extend( { zone:0, 'ticket-type':0, quantity:0 }, out.items[ i ] )

			return out;
		}

		function _req_from_data( data, extra ) {
			var out = $.extend( {}, extra, { items:[] } ), i;

			for ( i = 0; i < data.items.length; i++ ) {
				var tmp = { z:data.items[ i ].zone, t:data.items[ i ]['ticket-type'], q:data.items[ i ].quantity, name:data.items[i].zoneName };
				if ( qt.is( data.items[ i ].state ) ) tmp.st = data.items[ i ].state;
				out.items.push( tmp )
			}

			return out;
		}

		function _call_as( done_as, func, prepend_params ) {
			var prepend_params = qt.isA( prepend_params ) ? prepend_params : [];
			return function() {
				var a = [].slice.call( arguments );
				return func.apply( done_as, prepend_params.concat( a ) );
			};
		}

		function _multi_call() {
			var args = [].slice.call( arguments ), i;
			return function() {
				var a = [].slice.call( arguments ), res;
				for ( i = 0; i < args.length; i++ ) {
					if ( qt.isF( args[ i ] ) ) {
						res = args[ i ].apply( args[ i ], a );
						if ( false === res )
							break;
					}
				}
			};
		}

		function _add_interest( req, r ) {
			if ( r.s && qt.is( r.r ) ) {
				for ( var i = 0; i < r.r.length; i++ ) {
					var item = r.r[ i ];
					_add_interest_row.call( this, item );
				}
				if ( qt.is( r.nn ) && r.nn )
					this.o.nonce = r.nn;
				_update_ui.call( this );
				QS.cbs.trigger( 'added-interest', [ r, req, this ] )
			} else if ( qt.isA( r.e ) && r.e.length ) {
				_error_msg.apply( this, r.e );
			}
		}

		function _add_interest_row( item ) {
			var k = item.z + ':' + item.t, kq = k + ':' + item.q,
					tmp = $( this.tmpl( 'interest-item' ) ).attr( { key:k, keyq:kq } ).appendTo( this.e.owns );
			_add_tt_to_row.call( this, tmp, item );
		}

		function _fail_interest( req, r ) {
			_error_msg.call( this, this.msg( 'Could not show interest in those tickets.' ) );
		}



		function _add_reserve( req, r ) {
			//// Robert Edit for Time Expire  ///////
			_time_expired_robert_edit(r);

			/////////////////////////////////////////

			if ( r.s && qt.is( r.data ) ) {
				for ( var i = 0; i < r.r.length; i++ ) {
					var item = r.r[ i ], k = item.z + ':' + item.t, kq = k + ':' + item.q, e;
					if ( item.s ) {
						_add_reserve_row.call( this, item )
					} else {
						_error_msg.call( this, this.msg( 'Could not reserve a ticket for %s.', this._name( item.z ) ) );
						if ( qt.isA( item.e ) && item.e.length )
							for ( e = 0; e < item.e.length; e++ )
								_error_msg.call( this, item.e[ e ] + ' (' + this._name( item.z ) + ')' );
						if ( item.rm ) {
							var k = item.z + ':' + item.t, k2 = item.z + ':0', kq = k + ':' + item.q, to_remove = $( '[key="' + k +'"], [key="' + k2 + '"]' );
							to_remove().remove();
						} else {
							_add_reserve_row.call( this, item )
						}
					}
				}
				_update_ui.call( this );
				QS.cbs.trigger( 'added-reserve', [ r, req, this ] )
			} else if ( qt.isA( r.e ) && r.e.length ) {
				_error_msg.apply( this, r.e );
				if ( qt.is( r.data ) && qt.is( r.data.rm ) ) {
					for ( var i = 0; i < r.data.rm.length; i++ ) {
						var item = r.data.rm[i], k = item.z + ':' + item.t, k2 = item.z + ':0', kq = k + ':' + item.q, to_remove = $( '[key="' + k +'"], [key="' + k2 + '"]' );
						to_remove.remove();
					}
					QS.cbs.trigger( 'removed-res-int-raw', [ { r:r.data.rm }, req, this ] )
				}
			}
		}

		function _time_expired_robert_edit(r){
			if (r.time_expired){
				if ($('.time-warning').length){
					$('.time-warning').remove();
				}

				var warningDiv = $("<div class='time-warning row' style='background: rgba(30, 95, 174, 0.7); z-index: 1000; text-align:center; height: 60px; position: fixed; top: 0; left: 10px; right:10px; border: solid 1px #555; box-shadow: 3px 0 10px rgba(100,90,90,0.6)'></div>");

				var subWarningString = "<div class='sub-time-warning col-xs-3' style='display: inline-block; float: none; text-align:center; vertical-align: middle;'></div>";
				var checkoutBtnDiv = $("<div class='col-xs-3 button' style='display: inline-block; float: none; text-align:center; vertical-align: top; margin-left: -40px; margin-top: 5px'><a href='/checkout' <button type='button' class='btn btn-warning' style='color:white;'>Check Out</button></a></div>");

				var d = new Date();
				var time = d.getTime();

				var expireObjArr = [];

				r.data.owns.forEach( function(element, index) {
					var state = element.state;
					if (state == "reserved"){
						var expireObj = {name, time, zone_id, event_id};
						var event_id = element.event_id;
						var zone_id = element.zone_id;
						var since = element.since;
						var name = _qsot_seating_loader.edata.zones[zone_id].name;

						expireObj.name = name;
						expireObj.time = (since + 18000 + r.time_expired)*1000;
						expireObj.zone_id = zone_id;
						expireObj.event_id = event_id;
						expireObjArr.push(expireObj);
					}
				});

				if (expireObjArr.length){
					$("body").append(warningDiv);
					expireObjArr.forEach( function() {
						$('.time-warning').append(subWarningString);
					});
					$('.time-warning').append(checkoutBtnDiv);
				}

				if (expireObjArr.length > 3){
					$('.time-warning').height(50 + 50 * (Math.floor(expireObjArr.length)/4));
				} else{
					$('.time-warning').height(50);
				}
				$('.sub-time-warning').each(function(index) {
					var name = expireObjArr[index].name;
					var $this = $(this), finalDate = expireObjArr[index].time, tableName = expireObjArr[index].name;
					$this.countdown(finalDate, function(event) {
						$this.html("<p class='warning-msg' style='color: white; font:50px'> You have " + event.strftime('%M:%S') + " to checkout <strong>" + tableName + "</strong>.</p>")
						}).on('finish.countdown', function(event){
							expireObjArr.splice(index,1);
							$.cookie("ticket_expire_time", JSON.stringify(expireObjArr), { path: '/' });
							$this.remove();

							var length = $('.sub-time-warning').length;

							var ratio = Math.floor((length) / 4);

							if (length){
								$('.time-warning').height(50 + 50 * ratio);
							}else{
								$('.time-warning').remove();
							}

						});
				});

				$.cookie("ticket_expire_time", JSON.stringify(expireObjArr), { path: '/' });
			}
		}

		function _add_reserve_row( item ) {
			var k = item.z + ':' + item.t,
					k2 = item.z + ':0',
					kq = k + ':' + item.q,
					to_remove = $( '[key="' + k +'"], [key="' + k2 + '"]' ),
					template = qt.toInt( item.c ) + qt.toInt( item.q ) > 1 ? 'owns-multiple' : 'owns',
					tmp = $( this.tmpl( template ) ).attr( { key:k, keyq:kq } );
			if ( to_remove.length ) tmp.insertBefore( to_remove.get( 0 ) );
			else tmp.appendTo( this.e.owns );
			to_remove.remove();
			_add_tt_to_row.call( this, tmp, item );
		}

		function _fail_reserve( req, r ) {
			_error_msg.call( this, this.msg( 'Could not reserve those tickets.' ) );
		}

		function _remove_all( skip_filters, req, r ) {
			/// Robert Edit For Time Expire /////
			_time_expired_robert_edit(r);
			/////////////////////////////////////

			var skip_filters = skip_filters || false;

			if ( r.s && qt.is( r.r ) ) {
				for ( var i = 0; i < r.r.length; i++ ) {
					var item = r.r[ i ], k = item.z + ':' + item.t, kq = k + ':' + item.q;
					if ( item.s ) {
						var to_remove = $( '[key="' + k +'"]' ), tmp = $( this.tmpl( 'interest-item' ) ).attr( { key:k, keyq:kq } );
						to_remove.remove();
					} else {
						_error_msg.call( this, this.msg( 'Could not remove the tickets for %s.', this._name( item.z ) ) );
					}
				}
				_update_ui.call( this );
				if ( ! skip_filters )
					QS.cbs.trigger( 'removed-res-int', [ r, req, this ] )
			} else if ( qt.isA( r.e ) && r.e.length ) {
				_error_msg.apply( this, r.e );
			}
			if ( ! skip_filters )
				QS.cbs.trigger( 'removed-res-int-raw', [ r, req, this ] )
		}

		function _fail_remove( req, r ) {
			_error_msg.call( this, this.msg( 'Could not remove those tickets.' ) );
		}

		function _add_tt_to_row( row, item ) {
			var tid = item.t, zid = item.z,
					zone = { name:this.msg( '(pending)' ) },
					tt = qt.is( this.o.edata.ticket_types[ tid + '' ] ) ? this.o.edata.ticket_types[ tid + '' ] : { product_name:this.msg( '(pending)' ), product_raw_price:this.msg( '(pending)' ) },
					ele = $( this.tmpl( 'ticket-type-display' ) ).appendTo( row.find( '[rel="tt_display"]' ) );

			zone = _zone_info.call( this, zid, zone );

			row.find( '[rel="quantity"]' ).html( item.q );
			row.find( '[rel="qty"]' ).val( item.q );
			row.find( '[rel="ticket-type"]' ).val( item.t );
			row.find( '[rel="zone"]' ).val( item.z );

			ele.find( '[rel="zone-name"]' ).html( zone.name || zone.abbr );
			if ( this.o.edata.zone_count < 1 )
				ele.find( '[rel="zone-name"]' ).parent().hide();
			ele.find( '[rel="ttname"]' ).html( tt.product_name );
			ele.find( '[rel="ttprice"]' ).html( tt.product_raw_price );
		}

		function _zone_info( zid, zone ) {
			if ( zid <= 0 ) return zone;
			if ( qt.is( this.z[ zid + '' ] ) ) return this.z[ zid + '' ];

			return zone;
		}

		function _error_msg() {
			var me = this, msgs = [].slice.call( arguments ), i = 0;
			for ( ; i < msgs.length; i++ ) {
				console.log( 'ERROR: ', msgs[ i ] );
				// do visual error
				var msg = $( '<div class="msg">' + msgs[ i ] + '</div>' ).appendTo( me.e.errors );
				setTimeout( function() { msg.fadeOut( { duration:1000, complete:function() {
					$( this ).remove();
					if ( 0 == me.e.errors.find( '.msg' ).length )
						me.e.errors.slideUp( { duration:250, complete:function() { _maybe_hide_msgs.call( me ); } } );
				} } ); }, 3500 );
			}

			me.e.msgs.show();
			me.e.errors.slideDown( 250 );
		}

		function _confirm_msg() {
			var me = this, msgs = [].slice.call( arguments ), i = 0;
			for ( ; i < msgs.length; i++ ) {
				console.log( 'CONFIRMS: ', msgs[ i ] );
				// do visual error
				var msg = $( '<div class="msg">' + msgs[ i ] + '</div>' ).appendTo( me.e.confirms );
				setTimeout( function() { msg.fadeOut( { duration:1000, complete:function() {
					$( this ).remove();
					if ( 0 == me.e.confirms.find( '.msg' ).length )
						me.e.confirms.slideUp( { duration:250, complete:function() { _maybe_hide_msgs.call( me ); } } );
				} } ); }, 3500 );
			}

			me.e.msgs.show();
			me.e.confirms.slideDown( 250 );
		}

		function _maybe_hide_msgs() {
			var me = this, any_shown = false;
			me.e.msgs.find( '> .inner' ).each( function() { if ( 'none' != $( this ).css( 'display' ) ) any_shown = true; } );
			if ( ! any_shown )
				me.e.msgs.slideUp( 250 );
		}

		function _update_ui() {
			if ( this.e.owns.find( '[rel="own-item"]' ).length ) {
				this.e.ui_title.html( this.tmpl( 'two-title' ) );
				this.e.owns_wrap.find( '.section-heading' ).show();
				this.e.actions.show();
			} else {
				this.e.ui_title.html( this.tmpl( 'one-title' ) );
				this.e.owns_wrap.find( '.section-heading' ).hide();
				this.e.actions.hide();
			}

			if ( this.e.owns.find( '.item.multiple' ).length ) {
				this.e.ubtn.show();
			} else {
				this.e.ubtn.hide();
			}
		}

		function _common_prices( items ) {
			var me = this, common_prices = {}, i, j;
			for ( i = 0; i < items.length; i++ ) {
				var item = items[ i ], prices = qt.is( me.o.edata.struct ) && qt.is( me.o.edata.struct.prices[ item.z + '' ] ) ? me.o.edata.struct.prices[ item.z + '' ] : me.o.edata.struct.prices['0'];
				if ( 0 == i ) {
					for ( j = 0; j < prices.length; j++ )
						common_prices[ prices[ j ].product_id + '' ] = prices[ j ];
				} else {
					var my_list = [], cp = common_prices, common_prices = {};
					for ( j = 0; j < prices.length; j++ )
						my_list.push( prices[ j ].product_id );
					for ( j in cp ) if ( cp[ has ]( j ) ) {
						if ( j in my_list ) common_prices[ j ] = cp[ j ];
					}
				}
			}

			return common_prices;
		}

		function res( e, o ) {
			this.e = { main:e };
			this.o = $.extend( true, {}, defs, o );
			this.r = [];
			this.z = $.extend( {}, this.o.edata.zones );
			this.loading_counter = 0;

			this.init();
		}

		res.prototype = {
			init: function() {
				if ( this.initialized ) return;
				this.initialized = true;

				_name_lookup.call( this );
				_setup_ui.call( this );
				_setup_events.call( this );
				_setup_existing.call( this );
				_update_ui.call( this );

				QS.cbs.trigger( 'qsot-seating-init', [ this, { close_dialog:_reset_ps, ajax:_aj, remove:_remove_all, confirms:_confirm_msg } ] );

				QS.cbs.add( 'canvas-start', function() {
					setTimeout( function() {
					$( '[rel="continue-btn"]:eq(0)' ).click();
					}, 50 );
				} )( 20000 );
			},

			msg: function( str ) {
				var args = [].slice.call( arguments, 1 ), i;
				if ( qt.is( this.o.messages[ str ] ) )
					str = this.o.messages[ str ];
				for ( i = 0; i < args.length; i++ )
					str = str.replace( /%s/, args[ i ] );
				return str;
			},

			tmpl: function( name ) {
				return qt.is( this.o.templates[ name ] ) ? this.o.templates[ name ] : '';
			},

			_name: function( zid ) { return qt.is( this.names[ zid + '' ] ) ? this.names[ zid + '' ] : this.unknown; },

			price_selection: function( items, upon_select_func, early_close ) {
				var me = this, req = _req_from_data( { items:items.slice(0) } ), common_prices = _common_prices.call( this, req.items ), znames = [], max_qty, i;
				
				for ( i = 0; i < items.length; i++ ) {
					if ( qt.is( me.z[ items[ i ].zone ] ) && qt.isA( me.o.edata.stati[ items[ i ].zone ] ) ) {
						znames.push( me.z[ items[ i ].zone ].name );
						// it is important that max_qty equals 1 here, for 'seats', because otherwise the UI will show a how many box
						if ( ! qt.is( max_qty ) ) max_qty = me.o.edata.stati[ items[ i ].zone ][1];
						else max_qty = Math.min( max_qty, me.o.edata.stati[ items[ i ].zone ][1] );
					}
				}
				me.e.ps_sel_list.text( znames.join( ', ' ) );
				me.e.ps_qty.attr( 'max', max_qty + '' );
				if ( max_qty > 1 ) me.e.ps_qty_ui.show();
				else {
					me.e.ps_qty_ui.hide();
					if ( max_qty <= 0 )
						_error_msg.call( me, me.msg( 'There are not enough %s tickets available.', znames.join( ', ' ) ) );
				}

				for ( i in common_prices ) if ( common_prices[ has ]( i ) ) ( function( price ) {
					var prd_id = price.product_id, li = $( me.tmpl( 'price-selection-ui-price' ) ).appendTo( me.e.ps_list ).on( 'click', function( e ) {
						e.preventDefault();
						var qty = me.e.ps_qty.val();
						_reset_ps.call( me );
						me.show_loading();
						if ( qt.isF( upon_select_func ) )
							upon_select_func( prd_id, qty, function() { me.hide_loading(); } );
					} );
					li.find( '[rel="name"]' ).html( price.product_name );
					li.find( '[rel="price"]' ).html( price.product_display_price );
					QS.cbs.trigger( 'qsots-price-list-item', [ price, li, $.extend( {}, me.o.edata.ticket_types ) ] );
				} )( common_prices[ i ] );

				var pdims = { width:me.e.main_wrap.width(), height:me.e.main_wrap.height() },
						chart = me.e.main_wrap.find( '#svgui' ),
						cdims = chart.length ? { width:chart.width(), height:chart.height() } : { width:pdims.width, height:100 };

				this.e.psui.off( 'early-close.psui' ).on( 'early-close.psui', function( e ) {
					if ( qt.isF( early_close ) ) {
						early_close( e, items );
					}
					var data = { items:items }, i;
					for ( i = 0; i < data.items.length; i++ ) {
						data.items[ i ]['state'] = 'i';
						data.items[ i ]['ticket-type'] = 0;
					}
					me.remove( data );
				} );

				this.e.psui.show();
				var dims = { width:me.e.ps_box.width(), height:me.e.ps_box.height() };
				me.e.ps_box.css( { top:( cdims.height - dims.height ) / 2, left:( cdims.width - dims.width ) / 2 } );
			},

			show_loading: function() {
				this.e.loading.show();
				this.loading_counter++;
			},

			hide_loading: function() {
				this.loading_counter--;
				if ( this.loading_counter <= 0 ) {
					this.loading_counter = 0;
					this.e.loading.hide();
				}
			},

			
			/**/
			interest: function( data, func, efunc ) {
				var req = _req_from_data( _clean( data ), { sa:'seating-interest' } ),
						me = this,
						func = qt.isF( func ) ? func : function( r ) {},
						efunc = qt.isF( efunc ) ? efunc : _generic_efunc;
				this.show_loading();


				// run the interest ajax. upon success run a series of functions. upon failure, run a different series of functions  成功
				_aj.call(
					this,
					req,
					// success functions  成功
					_multi_call(
						_call_as( me, _add_interest, [ req ] ),
						func,
						function( r ) { return QS.cbs.trigger( 'qsots-interest-success', [ r, req ] ); },
						_call_as( me, this.hide_loading )
					),
					// failure functions 失败
					_multi_call(
						_call_as( me, _fail_interest, [ req ] ),
						efunc,
						function( r ) { return QS.cbs.trigger( 'qsots-interest-failure', [ r, req ] ); },
						_call_as( me, this.hide_loading )
					)
				);
			},

			reserve: function( data, func, efunc ) {
				var req = _req_from_data( _clean( data ), { sa:'seating-reserve' } ),
						me = this,
						func = qt.isF( func ) ? func : function( r ) {},
						efunc = qt.isF( efunc ) ? efunc : _generic_efunc;
				this.show_loading();
				// run the reservation ajax. on success, run a series of functions. on failure, run a different series of function
				_aj.call(
					this,
					req,
					// success functions
					_multi_call(
						_call_as( me, _add_reserve, [ req ] ),
						func,
						function( r ) { return QS.cbs.trigger( 'qsots-reserve-success', [ r, req ] ); },
						_call_as( me, this.hide_loading )
					),
					// failure functions
					_multi_call(
						_call_as( me, _fail_reserve, [ req ] ),
						efunc,
						function( r ) { return QS.cbs.trigger( 'qsots-reserve-failure', [ r, req ] ); },
						_call_as( me, this.hide_loading )
					)
				);
			},

			remove: function( data, func, efunc ) {
				var req = _req_from_data( _clean( data ), { sa:'seating-remove' } ),
						me = this,
						func = qt.isF( func ) ? func : function( r ) {},
						efunc = qt.isF( efunc ) ? efunc : _generic_efunc;
				this.show_loading();
				// run the remove reservation ajax. upon success, run a list of functions. when the request failed, run a different series of functions
				_aj.call(
					this,
					req,
					// function on success
					_multi_call(
						_call_as( me, _remove_all, [ false, req ] ),
						func,
						function( r ) { return QS.cbs.trigger( 'qsots-remove-success', [ r, req ] ); },
						_call_as( me, this.hide_loading )
					),
					// function on failure
					_multi_call(
						_call_as( me, _fail_remove, [ req ] ),
						efunc,
						function( r ) { return QS.cbs.trigger( 'qsots-remove-failure', [ r, req ] ); },
						_call_as( me, this.hide_loading )
					)
				);
			}
		};
		return res;
	} )();
} )( jQuery, QS.Tools );