var businesses = [];

$(document).ready(function() {
  
  // Set header for javascript requests
  jQuery.ajaxSetup({
    "beforeSend": function(xhr)  {xhr.setRequestHeader("Accept", "text/javascript")}
  });
  
  $(document).ajaxSend(function(event, request, settings) {
  	if (typeof(AUTH_TOKEN) == "undefined") return;
  	settings.data = settings.data || "";
  	settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
  });
  
  $("#more-reviews").click(function() {
    
    $.ajax({
          type: "GET",
          url: "/shopping",
          data: {page:  $("#current-page").text()},
          success: function(data) {
            eval(data)
          }
        });
    
    return false;
  })
  
  $("#review-filter").change(function() {
    if ($(this).val() == "") {
      window.location.href = "/shopping";
    } else {
      window.location.href = "/shopping/" + $(this).val();
    }
  })
  
  $(".display-filter").live("click", function() {
    
    
    $.ajax({
          type: "GET",
          url: $(this).attr("href"),
          data: {page: 1},
          success: function(data) {
            eval(data)
          }
        });
    
    return false;
    
  })

  
  $("#profile_date_of_birth").datepick({minDate: new Date(1900, 1, 1), maxDate: -(18 * 365), yearRange: '-100:+0', showOn: 'both', buttonImageOnly: true, buttonImage: '/images/calendar.gif', dateFormat: $.datepick.COOKIE});

  $(".category-accordion .sector_check").click(function() {
    sector = $(this)[0];
    $(this).parent().find(".category_check").each(function(index, item) { item.checked = sector.checked });
    map_category_filter();
  });

  $(".sector .sector_check").click(function() {
    sector = $(this)[0];
    $(this).parents(".sector").find(".category_check").each(function(index, item) { item.checked = sector.checked });
  });

  $(".category_check").click(function() {
    map_category_filter();
  });
	
	//Show the first menu item on page load and add an active class
	$("ul.business_directory:first").show();
	$('a.business_directory_link:first').addClass('active');
	//The tab between them
  $(".business_directory_link").click(function() {
		$('.business_directory_link').removeClass('active');
		$(this).addClass('active');
    $(".business_directory").hide();
    $($(this)[0].rel).show();
  });

	//Map Page accordion
	$('ul.category-accordion').accordion();
	
	//$('.current').show();
	//$('.category-accordion').children(":first").addClass('active');

  $("#featured-business").jCarouselLite({
    auto: 5000,
    speed: 1000,
    circular: true,
		btnNext: "#featured-holder .next",
		btnPrev: "#featured-holder .prev",
		visible: 1
  });

	//The user sign up form on the homepage - we hide on page load then open
	//Once the user clicks the button
	//$('.user-signup').hide();
	//$(".join-link").click(function() {
	//	$('#user-intro').hide();
	//	$('.user-signup').show();
	//});
	
	//Open all external links that are not local in a new browser window
	//Just add a class of rel="external" to all these links
	$('a[rel*=external], #affiliate-mpu a').attr({
      target: "_blank"
    });
  
  //Newsletter form
  $('#newsletter-signup-box input[type="text"]').val('Enter your email address...');
  $('#newsletter-signup-box input[type="text"]').focus(function() {
				value=$(this).val();
				$(this).attr("value","");
			});
	$('#newsletter-signup-box input[type="text"]').blur(function() {
				if($(this).val()=="") {
					$(this).val(value);
			}
	});

  $(".opening_hours_check").click(function() {
    day = $(this)[0].id.replace("_open", "");
    $("#business_" + day + "_from")[0].disabled = $("#business_" + day + "_to")[0].disabled = !$(this)[0].checked;
  });

  $(".limit").limit(250);

  $(".x_business_delete_logo").click(function() {
    if (confirm("Are you sure you wish to delete the current logo for this business? Any other changes you've made on the form will not be retained.")) {
      $(".x_business_delete_logo_submit").click();
    }
    return false;
  });

  $(".x_business_clear_logo").click(function() {
    $("#business_logo").val("");
    return false;
  });

  $(".x_user_delete_avatar").click(function() {
    if (confirm("Are you sure you wish to delete the current avatar for this user? Any other changes you've made on the form will not be retained.")) {
      $(".x_user_delete_avatar_submit").click();
    }
    return false;
  });

  $(".x_ranking_pagination").click(function() {
    $(".ranking").hide();
    $("." + $(this)[0].rel).show();
    $(".x_ranking_pagination").removeClass("current");
    $(this).addClass("current");
    if (parseInt($(this)[0].title) != 1) {
      $(".x_ranking_pagination_prev").removeClass("disabled");
    } else {
      $(".x_ranking_pagination_prev").addClass("disabled");
    }
    if (parseInt($(this)[0].title) != $(".x_ranking_pagination").length) {
      $(".x_ranking_pagination_next").removeClass("disabled");
    } else {
      $(".x_ranking_pagination_next").addClass("disabled");
    }
    return false;
  });

  $(".x_ranking_pagination_prev").click(function() {
    $(".x_ranking_pagination.current").parents("li").prev().children("a").click();
    return false;
  });

  $(".x_ranking_pagination_next").click(function() {
    $(".x_ranking_pagination.current").parents("li").next().children("a").click();
    return false;
  });

  $("#x_local_favourites_filter").change(function() {
    window.location.href = "/localfavourites/" + $(this).val();
  });
});

// Map page
map_category_filter = function() {
  map.clearOverlays();
  markers = [];
  businesses = [];
  $(".category_check:checked").each(function(i1, category) {
    $(".x_category_" + $(category)[0].name.replace("category_", "")).each(function(i2, business) {
      already_on_map = false;
      $.each(businesses, function(i3, existing) {
        if (existing == $(business)[0].id) {
          already_on_map = true;
        }
      });
      if (!already_on_map) {
        add_business_marker(business);
        businesses[businesses.length] = $(business)[0].id;
      }
    });
  });
  refresh_map();
};