$(document).ready(function() {
	$('.listing_detail_photo_gallery ul li a').lightBox();
	//var activity_id;
	$('.activity_added').hide();
	
	star_rating.init();
	var activity_id = $('#activity_id').val();
	var participant_id = $('#participant_id').val();
	
	$('.print').click(function() {
		window.open('/activity/detail/' + activity_id + '/print/');
	});
	
	$('.r_listing').click(function(){
		window.location = $(this).children('.class_summary').children('h4').children('a').attr('href');
	});
	
	
	$('.add_to_cart').unbind('click').click(function() {
		var add_to_cart = $(this);
		if(participant_id) {
			
			var data = {
				'object_type': 'activity',
				'object_id': activity_id,
				'participant_id': participant_id
			};
			data = JSON.encode(data);
			
			$.ajax({
				'url': '/shoppingcart/ajax/add/',
				'type': 'POST',
				'data': data,
				'dataType': 'json',
				'success': function(data, textStatus) {
					
					if(data.result <= 0) {
						alert(data.error);
					} else {
						if(data.almost_full && !data.is_waitlist_alternative) {
							thickbox.confirm("We have added this activity to your cart, but this activity is almost full!",
								"We recommend that you make a second choice. " +
								"Both listings will be added to your cart. Registration for your first and second choice will show as pending for " +
								"48 hours while we confirm availability. Your account will only be charged for one activity. Priority will be given " +
								"to your first choice if both choices are available.",
								"Would you like to create a second choice for this activity?",
								function(button) {
									switch(button) {
										case 'no_button':
										    window.location.reload();
											break;
										case 'yes_button':
											window.location = '/shoppingcart/waitlist_mode/on/' + data.hold_id + '/'
											break;
									}
								}, ["no_button", "yes_button"], 720, 160);
						} else {
						    window.location.reload();
						}
						add_to_cart.hide();
						$('.rounded_border_box').hide();
						$('.activity_added').show();
                        $('#user_account_access').load('/shoppingcart/ajax/info_box/');
					}
				},
				'error': function (XMLHttpRequest, textStatus, errorThrown) {
					alert('There was an error on the server. Please refresh this page.');
				}
			});
		} else {
			alert(lang_msg['NO_PARTICIPANT']);
		}
	});
	
	$("#share_button").click(function() {
	        var provider_id = $('#provider_id').val();
		var location_id = $('#location_id').val();
		thickbox.show('Share Provider', '/provider/share/' + provider_id+'/' + location_id + '/?TB_iframe=True', false);
		return false;
    	});

	$('#cancellation_policy_link').click(function() {
		thickbox.show('Cancellation Policy', '/activity/cancellation_policy/' + activity_id, false);
		return false;
	});
	
	function set_activity_id() {
		var $this = $(this);
		if($this.val() == '') {
			$this.val('Activity Number');
			$this.addClass('prompt_text');
		}
	}
	$('#listings_search').blur(set_activity_id).each(set_activity_id).focus(function() {
		if($(this).val() == 'Activity Number') {
			$(this).removeClass('prompt_text');
			$(this).val('');
		};
	});
	$("#c_policy").click(function() {
		if($("#c_policy_text").hasClass('hidden')){
			$("#c_policy_text").removeClass('hidden');
			$(this).attr('class','policy_open');
		}else{
            $("#c_policy_text").addClass('hidden');
			$(this).attr('class','policy_closed');
		};
    });
	$("#r_policy").click(function() {
		if($("#r_policy_text").hasClass('hidden')){
			$("#r_policy_text").removeClass('hidden');
			$(this).attr('class','policy_open');
		}else{
			$("#r_policy_text").addClass('hidden');
			$(this).attr('class','policy_closed');
		};
    });
	$("#e_policy").click(function() {
		if($("#e_policy_text").hasClass('hidden')){
			$("#e_policy_text").removeClass('hidden');
			$(this).attr('class','policy_open');
		}else{
			$("#e_policy_text").addClass('hidden');
			$(this).attr('class','policy_closed');
		};
    });

    $('.provider-website_link').click(function () {
        var prov_id = $(this).attr('id').split('-')[1];
        register_ad_action('website', 'website_click', prov_id);
    });
        
        
});

function register_click_to_call() {
    if (!$('#phone_number').is(':visible')) {
        var prov_id = $('.provider-click_to_call').attr('id').split('-')[1];
        register_ad_action('click_to_call', 'click_to_call_click', prov_id);
    }
};

function register_email_click(provider_name, provider_location) {
    var prov_id = $('#provider_id').val();
    thickbox.show('Just fill out the form below to ask ' + provider_name + ' a question.', '/provider/direct_contact_form/' + $('#location_id').val() + '/' + prov_id + '/?TB_iframe=True', null,600,500,true); 
};



