var timeoutId;
$(document).ready(function() {
	
	set_states();
	
	function get_states(){
		hash = '';
		$('.states_list li').addClass('print_hide');
		$('.state_input:checked').each(function(){
			$('.'+this.id+'_state_li').removeClass('print_hide');
			if (hash.length && hash.length != 0){
				hash += ":"+this.id;
			} else {
				hash = this.id;
			};
		});
		$('#id_state_selection').attr('value',hash);
		if ($('#id_state_selection').val() == ''){
			$('#all_states').attr('checked','checked');
			$('.all_state_li').removeClass('print_hide');
		}else{
			$('#all_states').removeAttr("checked");
			$('.all_state_li').addClass('print_hide');
		};
		set_timmer();
	};
	
	$('#all_states').change(function() {
		if ($('#all_states').attr('checked') != 'checked'){
			$('.state_input:checked').removeAttr("checked");
			$('#id_state_selection').attr('value','');
			$('#all_states').attr('checked','checked');
			get_states();
		}else{
			get_states();
		};
	});


        function change_state_selection() {
            jQuery.ajax({url: '/provider/filters/get_state_from_zip?zip_code=' + $('#id_zip_code').val() + '&listing_type=' + get_listing_type(),
                         success: function(data) {
                            $('#id_state_selection').val(data);
                            return data;
                         },
                         async: false
                        });
            return "";
        }

        $('#id_zip_code').keydown(function(e) {
            key = e.keyCode || e.which || null;
            if (key == 13 || key == 10) {
                change_state_selection();
                set_states();
                set_timmer();
            }
            return true;
        });

	function set_states() {
		hash = $('#id_state_selection').val();
		$('.states_list li').addClass('print_hide');
		$('.state_input').removeAttr("checked");
		if(hash.length && hash.length != 0){
                    state_ids = hash.split(':');
                } else {
                    state_ids = change_state_selection().split(':');
                }
                $('.state_input').removeAttr("checked");
                for(i=0; i < state_ids.length; i++){
                        $('#'+state_ids[i]).attr("checked","checked");
                        $('.'+state_ids[i]+'_state_li').removeClass('print_hide');
		}
		if ($('#id_state_selection').val() == ''){
			$('#all_states').attr('checked','checked');
			$('.all_state_li').removeClass('print_hide');
		}else{
			$('#all_states').removeAttr("checked");
			$('.all_state_li').addClass('print_hide');
		}
	}
	
	$('.state_input').click(function() {
		get_states();
	});
	
	$(".state_image").click(function () {
		if($(".state_box").hasClass('hidden')){
			$(".state_box").removeClass('hidden');
		}else{
			$(".state_box").addClass('hidden');
		}
        });
	
	$("#activity_providers_tab").click(function () {
		$("#camp_providers_tab").removeClass("active");
		$('#id_search_camps').attr('checked', false);
		$(this).addClass("active");
		$('#id_search_activity').attr('checked', true);
		$(".state_filter").addClass('hidden');
		$(".state_box").addClass('hidden');
		$(".state_filter").addClass('print_hide');
		$(".state_box").addClass('print_hide');
		set_timmer();
		return false;
    });
	$("#camp_providers_tab").click(function () {
		$("#activity_providers_tab").removeClass("active");
		$('#id_search_activity').attr('checked', false);
		$(this).addClass("active");
		$('#id_search_camps').attr('checked', true);
		$(".state_filter").removeClass('hidden');
		$(".state_filter").removeClass('print_hide');
		$(".state_box").removeClass('print_hide');
		get_states();
		return false;
    });
	
	//share thickbox
	$('#share_button').click(function() {
		thickbox.show('Share Provider Listing', '/provider/share/');
		return false;
	});

        $('#id_zip_code').change(function() { 
            change_state_selection();
            set_states();
            set_timmer();
        });

	/* load states on load */
	if ($('#id_zip_code').length > 0) {
	    var zip_code = $('#id_zip_code').val()
	    if (zip_code != 'undefined' && zip_code != '') {
	        change_state_selection();
		set_states();
	    }
	}

	$('#id_providers, #id_activity_type').change(function () {
            set_timmer();
	});
	
	
	function set_id_search() {
		var $this = $(this);
		if($this.val() == '') {
			$this.val('Provider Name');
			$this.addClass('prompt_text');
		}
	}
	$('#id_search').blur(set_id_search).each(set_id_search).focus(function() {
		if($(this).val() == 'Provider Name') {
			$(this).removeClass('prompt_text');
			$(this).val('');
			
		}
	});
	
	apply_events_listing_jq();
});

function apply_events_listing_jq(){
	$('#id_order').change(function () {
		set_timmer()
	});
	$('#program_detail_content ul li.listing_row').click(function() {
		window.location = $($(this).find('a')[0]).attr('href');
	});
}

function set_timmer(){
	$('#program_detail_content').empty()
	$('#program_detail_content').append('<div id="loading_holder" style="text-align:center;"><img style="margin-top:100px;margin-bottom:100px;" title="Loading" src="/public/images/ajax-loader.gif" /></div>')
	if(timeoutId != 0) {
		clearTimeout(timeoutId);
		timeoutId = 0;
	}
	timeoutId = setTimeout('provider_update_search(0)', 2000);
};

function get_listing_type() {
    if ($('#camp_providers_tab').hasClass('active')) {
	return 'camp';
    } else {
	return 'activity';
    }
}

function _update_providers_dropdown(type) {
    var listing_type = get_listing_type();
    var params = {
	zip_code: $('#id_zip_code').val(),
	state_selection: $('#id_state_selection').val(),
	type: type,
	listing_type: listing_type
    }
    var url = "/provider/filters/";
    $.get(url, params, function(data) {
	var myObject = eval('(' + data + ')');
	$('#id_providers').empty();
	$('#id_providers').append(myObject.providers);
    });
    update_analytics(build_url(url, params));
}

function provider_update_search(page){
	params = {partial:'True'};
	url = '/provider/'
	if (page != 0){
		params.page = page
	};
	if ($('#id_zip_code').length != 0){
		params.zip_code = $('#id_zip_code').val();
	}
	if($('#id_search_activity').attr('checked') == true){
		params.search_activity = 'on';
	};
	if($('#id_search_camps').attr('checked') == true){
		params.search_camps = 'on';
	};
	params.state_selection = $('#id_state_selection').val();
	params.activity_type = $('#id_activity_type').val();
	params.providers = $('#id_providers').val();
	params.search = $('#id_search').val()
	params.order = $('#id_order').val();
        _update_providers_dropdown(params.activity_type);
        update_analytics(build_url(url, params));
	$.post(url, params,
		function(data){
			re = new RegExp("^window.location");
			if (data.match(re)) {
				data = data.replace('window.location = ', '');
				data = data.replace('\"', '');
				data = data.replace('\"', '');
				window.location = data;
				return;
			} else {
				$('#search_area').empty();
				$('#search_area').append(data);
				apply_events_listing_jq();
                                
			}
	});
        external_refresh_marquee('forward');
        external_refresh_side_marketing();
	return false;
};
