$(document).ready(function(){var currency_code=$("#currency_code").val();var currency_symbol=$("#currency_symbol").val();var current_city="";if(typeof(console)=='undefined'){console={info:function(){},debug:function(){},log:function(){}};}
function get_amount_with_discount(amount,discount){console.info('get_amount_with_discount');if(discount>0){amount=amount-(amount*discount/100);}
return amount;}
function update_departure_information(data,currency_code,currency_symbol){console.info('update_departure_information');var pricing_details=data.pricing_details;var departure_info=pricing_details[0];var departure_extras=departure_info['extras'];var departure_fields=departure_info['fields'];var my_own_room_details=data.my_own_room_details;var promotion_details=data.promotion_details;if(promotion_details){if(promotion_details.has_discount){var promotion_message=""
if(promotion_details.discount_amount>0){promotion_message="Save "+currency_symbol+parseFloat(promotion_details.discount_amount).toFixed(0);}else{promotion_message="Save "+promotion_details.discount_percent+"%";}
promotion_message=promotion_message+" with the "+promotion_details.title;}else{promotion_message=promotion_details.title;}
if(promotion_details.badge_url){$("#promotional_badge").attr('src',promotion_details.badge_url);$("#promotional_badge").show();}else{$("#promotional_badge").hide();}
$("#promotional_message").html(promotion_message);$("#promotional_details").attr('title',promotion_details.description);$("#promotional_details").show();}else{$("#promotional_details").hide();}
if(departure_fields.is_discounted){if(departure_fields.discount_percent>0){discount_message="Save "+departure_fields.discount_percent+"% on this Last Minute Departure"
$("#promotional_message").html(discount_message);$("#promotional_details").attr('title',"Last Minute Departure");$("#promotional_details").show();}}
availability_message=departure_extras.get_availability_message;$("#available_spaces").html(availability_message);var inca_trail_status=departure_extras.get_inca_trail_status;$("#inca_trail_status").html(inca_trail_status);var departure_id=departure_info.pk;$("#departure_id_map").val(departure_id);var local_payments=''
for(i=1;i<pricing_details.length;i++){if(pricing_details[i]['model']=='trips.localpayment'){local_payment=pricing_details[i]['fields'];local_payments+="<dt>"+local_payment['label']+"</dt>"+"<dd>"+Math.ceil(local_payment['amount'])+"&nbsp;"+local_payment['currency']+"</dd>";}}
if(local_payments==''){$('#local_payment_help').hide();}else{$('#local_payment_help').show();}
if(my_own_room_details!=null){var my_own_room_price=my_own_room_details.price;if(my_own_room_price!=0){my_own_room_price=currency_symbol+my_own_room_price+" "+currency_code.toUpperCase();$('#my_own_room').show();$('#my_own_room_info').show();$('#my_own_room_price').html(my_own_room_price);}else{$('#my_own_room').hide();}}
var pricing_details=data.pricing_details
var prices=[];var price_counter=0;var dominant_price_index=price_counter
for(i=1;i<pricing_details.length;i++){if(pricing_details[i]['model']=='trips.price'){prices[price_counter]=pricing_details[i]['fields'];price_label=prices[price_counter]['label'];price_label=price_label.replace('1 Any','1 Adult');price_label=price_label.replace('Anys','Adults');prices[price_counter]['label']=price_label;if(price_label=='1 Adult'||price_label=='1 Person'){dominant_price_index=price_counter;}
if(price_label=='2 Adults'||price_label=='2 People'){dominant_price_index=price_counter;}
price_counter++;}}
if(prices.length==1){dominant_price_index=0;$("#list-line").hide();}else{$("#list-line").show();}
var flight_price=parseFloat(data.flight_price);var pricing='';if(data.cities)
$("#flight_cities").html(data.cities);var selected_city=$("#flight_cities :selected");var flight_id=selected_city.val();current_city=selected_city.text();if(flight_id==undefined){flight_id=0;}
if(current_city==undefined){current_city="";}
$("#flight_id").val(flight_id);$("#flight_city").val(current_city);var flight_html="";var price_label="";var acv_asterik="";for(i=0;i<prices.length;i++){price=prices[i];var original_amount=price[currency_code];var amount=original_amount;var departure_is_discounted=false;var has_promotion=false;if(i==dominant_price_index){prefix="<dt id='dominant'>";postfix="</dt>";}else{prefix="<dd class='pricing-micro'>";postfix="</dd>";}
if(departure_fields.discount_percent>0){departure_is_discounted=true;amount=Math.ceil(get_amount_with_discount(original_amount,departure_fields.discount_percent));}
if(promotion_details){has_promotion=true;if(promotion_details.has_discount){departure_is_discounted=true;if(promotion_details.discount_percent>0){amount=Math.ceil(get_amount_with_discount(original_amount,promotion_details.discount_percent));}else{amount=Math.ceil(original_amount-promotion_details.discount_amount);}}}
amount=parseFloat(amount).toFixed(0);original_amount=parseFloat(original_amount).toFixed(0);discount_label=""
if(departure_is_discounted){discount_label="<br/>&nbsp;Was "+currency_symbol+original_amount;}
if(flight_price>0)
{acv_asterik="*";amount=parseFloat(amount)+parseFloat(flight_price);amount=parseFloat(amount).toFixed(2);$("#leaving").show();$("#flight-include").show();$("#flight-exclude").hide();if(typeof(acv_pricing_box)!="undefined")
{flight_html=acv_pricing_box(current_city);}}
else
{price_label="&nbsp;<span class='micro break'>( pp for "+price['label']+"&nbsp;)"+discount_label+"</span>";$("#leaving").hide();$("#flight-include").hide();$("#flight-exclude").show();}
pricing+=prefix+"<span class='micro currency'>"+currency_symbol+"</span>"+amount+"&nbsp;"+"<span class='micro'>"+currency_code.toUpperCase()+acv_asterik+"</span>"+price_label+flight_html+postfix;}
$("#departure_price").html(pricing);if(departure_is_discounted){$("#departure_price").css({color:'#FF8D00'});}else{$("#departure_price").css({color:'white'});}
if(local_payments){$("#local_payments").html(local_payments);$("#local_payments").show();}else{$("#local_payments").hide();}}
function update_departure_information_ajax(currency_code,currency_symbol,flight_id,live){console.info('update_departure_information_ajax');if(live!='true'){live='false';}
var departure_id=$("#id_departure_id").val();if(departure_id==undefined){departure_id="";}
currency_code=currency_code||'cad';currency_code=currency_code.toLowerCase();if(live=='false'){var spinning_wheel='<img src="'+MEDIA_URL+'static/images/small-loading.gif" />';$("#book_now_button").hide();$("#available_spaces").html(spinning_wheel);$("#departure_price").html(spinning_wheel);}
$.ajax({url:"/trips/departure/",dataType:'json',data:{'id':departure_id,'flight_id':flight_id,'selected_city':current_city,'currency':currency_code,'live':live,'ajax':'true'},success:function(data){console.info('received /trips/departure/');update_departure_information(data,currency_code,currency_symbol);if(live=='false'){update_departure_information_ajax(currency_code,currency_symbol,flight_id,'true');}
var availability_status=data.pricing_details[0].extras.get_availability_message;if(availability_status!='Sold Out'){$("#book_now_button").show();}},error:function(XMLHttpRequest,textStatus,errorThrown){console.error('Error getting /trips/departure');console.error(errorThrown);}});}
function update_departures_dropdown_and_price(currency_code,currency_symbol,flight_id,live){console.info('update_departures_dropdown_and_price');var data={"dossier_code":$("#dossier_code").val(),"year":$("#year").val(),"room_code":$("#id_room").val()||"","departure_id":$("#id_departure_id").val()}
$("#id_departure_id").load("/trips/trip/departure_dates_options/",data,function(){console.info('loaded /trips/trip/departure_dates_options/');update_departure_information_ajax(currency_code,currency_symbol,flight_id,live);});}
function update_dates_and_pricing_list(){console.info('update_dates_and_pricing_list');var dossier_code=$("#dossier_code").val();var year=$("#year").val();var start_date=$("#id_departure_date").val();var room_code=$("#id_pricing_room").val();if(room_code==undefined){room_code="";}
if(start_date==undefined){start_date="";}
var spinning_wheel='<img src="'+MEDIA_URL+'static/images/small-loading.gif" />';$("#dates_and_pricing_list").html(spinning_wheel);$("#dates_and_pricing_list").load("/trips/trip/dates_and_pricing/",{"dossier_code":dossier_code,"year":year,"room_code":room_code});}
function view_hotels_and_transfers_info(currency_code){console.info('view_hotels_and_transfers_info');var departure_id=$("#id_departure_id").val();var parameters={'id':departure_id,'currency':currency_code,'ajax':'true'};$.get("/trips/hotels-transfers/",parameters,function(data){$.facebox(data);});}
$("#flight_cities").change(function(){var selected=$("#flight_cities option:selected");var flight_id=selected.val();current_city=selected.text();update_departure_information_ajax(currency_code,currency_symbol,flight_id,'false');});$("#id_departure_id").change(function(){update_departure_information_ajax(currency_code,currency_symbol,0,'false');});$("#id_room").change(function(){update_departures_dropdown_and_price(currency_code,currency_symbol,0,'false');});$("#id_pricing_room").change(function(){update_dates_and_pricing_list();});$("#brief_itinerary_link").click(function(){$("#detailed_itinerary").hide("normal",function(){$("#brief_itinerary").show("normal");});$('#itinerary_title').html("Brief Itinerary");});$("#detailed_itinerary_link").click(function(){$("#brief_itinerary").hide("normal",function(){$("#detailed_itinerary").show("normal");});$('#itinerary_title').html("Detail Itinerary");});$("#detailed_itinerary").hide();$('#itinerary_title').html("Brief Itinerary");update_departures_dropdown_and_price(currency_code,currency_symbol,0,'false');update_dates_and_pricing_list();$("#hotels_and_transfers").click(function(e){e.preventDefault();view_hotels_and_transfers_info(currency_code=currency_code);});});$(document).ready(function(){function wishlist_button_status(){var dossier_code=$('#dossier_code').val();var year=$('#year').val();var url='/profiles/json/wishlist/trip_in_wishlist/'+dossier_code+'/'+year+'/';$.getJSON(url,function(data){$('#trip-summary-temporary-label').remove();if(data.status=="true"){$('#remove_from_wishlist').show();}else{$('#add_to_wishlist').show();}});}
wishlist_button_status();function update_wishlist(){var $wishlist=$('#user_wishlist > ul');$.getJSON('/profiles/json/wishlist/',function(data){$wishlist.empty();$.each(data,function(index,item){$wishlist.append("<li class='wishlist_item'><a href='"+item.url+"'>"+item.name+" ("+item.dossier_code+")</a></li>");});});}
update_wishlist();$('#add_to_wishlist').click(function(e){var $wishlist_button=$(this);var dossier_code=$('#dossier_code').val();var year=$('#year').val();var url='/profiles/wishlist/add/'+dossier_code+'/'+year+'/';$.ajax({type:'GET',dataType:'json',url:url,data:{},success:function(data,textStatus,xhr){var status=data.status;if(status=='OK'){update_wishlist();$wishlist_button.hide();$('#remove_from_wishlist').fadeIn('slow');}
else{alert('There was a problem adding trip to your wishlist. Please try again.');}},error:function(xhr,textStatus,error){alert('Communication error.');}});});$('#remove_from_wishlist').click(function(e){var $remove_button=$(this);var dossier_code=$('#dossier_code').val();var year=$('#year').val();var url='/profiles/wishlist/remove/'+dossier_code+'/'+year+'/';$.ajax({type:'GET',dataType:'json',url:url,data:{},success:function(data,textStatus,xhr){var status=data.status;if(status=='OK'){update_wishlist();$remove_button.hide();$('#add_to_wishlist').fadeIn('slow');}
else{alert('There was a problem removing trip from your wishlist. Please try again.');}},error:function(xhr,textStatus,error){alert('Communication error.');}});});});