
ISCPriceLevels = Class.create( ISCPriceDisplay, {
tlog: function () {},
initialize: function( data ) {
this.search_constraints = data.search_constraints;
this.search_constraints.price_levels = this;
this.choice_form = $('price_levels_choice_form');
document.body.appendChild( Element.remove( this.choice_form ) );
},
setup: function() {
$('price_level_dropdown_display').show();
this.setup_price_levels();
this.check_all_prices();
new Popup( 'price_levels_info_popup', {
close_id: 'pricelevelsPopupClose',
zIndex: 105,
links: [{
group: 'edp',
link_id: 'price_levels_info_link',
toggle: true,
anchor_align:  { x:  1, y: -1 },
popup_align: { x: -1, y: -1 },
offset: { x: 5, y: 0}
}]
});
this.dropdown = new EDPWidget( 'price_levels_select', {
width: 185,
ellipsis: true,
choice_form: 'price_levels_choice_form',
get_choice_text: this.get_form_value.bind( this ),
display_choice_form: this.display_choice_form.bind( this )
} );
this._dropdown_expand = this.dropdown.expand.bind( this.dropdown );
this.dropdown.expand = this.dropdown_expand.bind( this );
},
display_choice_form: function() {
if ( this.options.length > 9 ) {
var container = $("price_level_options_container");
if ( !Prototype.Browser.IE ) {
container.style.height = this.container_height - 5 + 'px';
container.style.overflow = 'auto';
}
if ( this.choice_form.style.width ) {
container.style.width = '100%';
}
else {
var width = Element.getWidth( container );
var scrollbar_width = width - Element.getWidth( $('price_level_options') );
if ( scrollbar_width <= 0 )
scrollbar_width = 20;
this.choice_form.style.width = width + scrollbar_width + 'px';
}
if ( Prototype.Browser.IE ) {
container.style.height = this.container_height - 5 + 'px';
container.style.overflow = 'auto';
}
}
},
dropdown_expand: function() {
var container = $("price_level_options_container");
container.style.height = '';
container.style.overflow = '';
$("price_level_options_container").style.width = '';
this._dropdown_expand();
},
sort_prices: function( a, b ) {
var a_price = a.display_charges.total_price;
var b_price = b.display_charges.total_price;
if ( a_price < b_price )
return 1;
if ( a_price > b_price )
return -1;
a_price = a.display_charges.price;
b_price = b.display_charges.price;
if ( a_price < b_price )
return 1;
if ( a_price > b_price )
return -1;
a_price = a.display_charges.sum_fees_and_taxes;
b_price = b.display_charges.sum_fees_and_taxes;
if ( a_price < b_price )
return 1;
if ( a_price > b_price )
return -1;
return 0;
},
setup_price_levels: function() {
this.options = [];
var source_tt = this.search_constraints.preferred;
var prices = $A($H(source_tt.price_breakdown).values()).sort( this.sort_prices.bind( this ) );
var real_prices = $A($H(source_tt.real_price_breakdown).values()).sort( this.sort_prices.bind( this ) );
$('price_levels_choice_form').style.left = '-10000px';
$('price_levels_choice_form').style.top = '0px';
$('price_levels_choice_form').show();
var container;
var last_charges;
var i;
for ( i = 0 ; i < prices.length ; i ++ ) {
var display_charges = prices[i].display_charges;
if ( typeof(last_charges) == 'undefined' ||
last_charges.price != display_charges.price ||
last_charges.sum_fees_and_taxes != display_charges.sum_fees_and_taxes ) {
if ( this.options.length == 9 ) {
container = $("price_level_options_container");
this.container_height = Element.getHeight( container );
}
var option = new PriceLevelOption( prices[i], real_prices[i] );
option.attach( 'price_level_options' );
option.show();
Event.observe( option.$('checkbox'), 'click', this.on_option_change.bind( this, option ) );
this.options.unshift( option );
}
else {
var option = this.options[0];
option.add_price_breakdown( prices[i], real_prices[i] );
}
last_charges = display_charges;
}
var ticket_types = this.search_constraints.isc.edp.event.get_enabled_ticket_types();
this.all_price_levels = {};
for ( i = 0 ; i < ticket_types.length ; i ++ ) {
var tt = ticket_types[i];
for ( var price_level in tt.price_breakdown ) {
this.all_price_levels[price_level] = true;
}
}
Event.observe( $('plo_all_prices_checkbox'), 'click', this.on_all_prices_change.bind( this ) );
},
on_ticket_type_unlock: function( tt ) {
if ( tt == this.source_tt ) {
for ( var i = 0 ; i < this.options.length ; i ++ )
this.options[i].switch_to_real_price();
this.dropdown.update_dropdown();
}
},
reset: function() {
this.check_all_prices();
this.on_all_prices_change();
},
register_on_price_change_handler: function( handler ) {
Event.observe( $('price_levels_info_popup'), 'isc:price-change', handler );
},
get_form_value: function() {
if ( $('plo_all_prices_checkbox').checked )
return [ this.options.first().price_breakdown[0].display_charges.formatted_total_price,
this.options.last().price_breakdown[0].display_charges.formatted_total_price ].join( ' - ' );
else {
var prices = [];
for ( var i = 0 ; i < this.options.length ; i ++ )
if ( this.options[i].$('checkbox').checked )
prices.push( this.options[i].price_breakdown[0].display_charges.formatted_total_price );
return prices.join( ', ' );
}
},
get_price_levels: function () {
if ( $('plo_all_prices_checkbox').checked ) {
return this.all_price_levels;
}
else {
var price_levels = {};
for ( var o = 0 ; o < this.options.length ; o ++ ) {
var option = this.options[o];
if ( option.$('checkbox').checked  )
for ( var i = 0 ; i < option.price_breakdown.length ; i ++ )
price_levels[option.price_breakdown[i].price_level] = true;
}
return price_levels;
}
},
on_option_change: function( option ) {
false;
option.on_checkbox_change();
var has_checked = false;
for ( var i = 0 ; i < this.options.length ; i ++ )
if ( this.options[i].$('checkbox').checked ) {
has_checked = true;
break;
}
if ( has_checked )
this.uncheck_all_prices();
else
this.check_all_prices();
this.dropdown.update_dropdown();
click_track.log_datapoint( { price_dropdown_used: '', __log_only_once__: 'price_dropdown_used' } );
$('price_levels_info_popup').fire( 'isc:price-change' );
},
check_all_prices: function() {
false;
$('plo_all_prices').className = 'widget-selected';
$('plo_all_prices_checkbox').checked = true;
},
uncheck_all_prices: function() {
false;
$('plo_all_prices').className = '';
$('plo_all_prices_checkbox').checked = false;
},
on_all_prices_change: function() {
false;
if ( $('plo_all_prices_checkbox').checked )
for ( var i = 0 ; i < this.options.length ; i ++ )
this.options[i].uncheck();
this.check_all_prices();
this.dropdown.update_dropdown();
$('price_levels_info_popup').fire( 'isc:price-change' );
}
} );
PriceLevelOption = Class.create( Module, {
tlog: function(){},
initialize: function( $super, price_breakdown, real_price_breakdown ) {
$super( { dom: 'price_level_option', removeParent: true } );
this.price_breakdown = [ price_breakdown ];
this.real_price_breakdown = [ real_price_breakdown ];
this.update_price_display( this.price_breakdown[0] );
this.$('checkbox').checked = false;
},
update_price_display: function( price_breakdown ) {
var display_charges = price_breakdown.display_charges;
this.$('total_price').innerHTML = display_charges.formatted_total_price;
if ( display_charges.sum_fees_and_taxes+0 > 0 ) {
this.$('face_value').innerHTML = display_charges.formatted_price;
this.$('sum_fees_and_taxes').innerHTML = display_charges.formatted_sum_fees_and_taxes;
this.$('fees').hide();
this.$('taxes').hide();
this.$('fees_taxes').hide();
if ( display_charges.has_fees && display_charges.has_taxes )
this.$('fees_taxes').show();
else if ( display_charges.has_fees )
this.$('fees').show();
else if ( display_charges.has_taxes )
this.$('taxes').show();
this.$('price_breakdown').show();
}
else
this.$('price_breakdown').hide();
},
switch_to_real_price: function() {
this.price_breakdown = this.real_price_breakdown;
this.update_price_display( this.price_breakdown[0] );
},
add_price_breakdown: function( price_breakdown, real_price_breakdown ) {
this.price_breakdown.push( price_breakdown );
this.real_price_breakdown.push( real_price_breakdown );
},
on_checkbox_change: function( option ) {
false;
if ( this.$('checkbox').checked )
this.check();
else
this.uncheck();
},
check: function() {
this.$('price_level_option').className = 'widget-selected';
this.$('checkbox').checked = true;
},
uncheck: function() {
this.$('price_level_option').className = '';
this.$('checkbox').checked = false;
}
} );

