function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( $.param.fragment() );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

$(function() {
	
	Cufon.replace('#header .count p span');
	Cufon.replace('.extra_center a span', { hover: true, hoverables: { a: true } });
	Cufon.now();
	
	// listen for pet type to change so we can populate breed
	$('#options_pet_type li').live('click', function() {
		// populate breed dropdown based on value clicked
		var pet_types = new Array();
		pet_types["Dog"] = "20";
		pet_types["Cats"] = "21";
		pet_types["Bunnies"] = "22";
		pet_types["Birds and Critters"] = "23";
		
		var category_group = pet_types[$(this).text()];

		$("#options_breed").load("/site/breeds/"+category_group, function(response) {
			// add mouseover and mouseout
			$("#options_breed li").mouseover(function() {
				$(this).removeClass("open");
				$(this).addClass("open_hover");
			}).mouseout(function() {
				$(this).removeClass("open_hover");
				if ($(this).attr("id") == 'selected_breed')
				{
					$(this).addClass("open_selected");
				}else
				{
					$(this).addClass("open");					
				}
			}).click(function(e) {
				var opt_index = $("#options_breed li").index(e.target);
				clickOptions(3, opt_index, "breed");
			});		
		});
		
		$("#breed").load("/site/breeds_options/"+category_group);	
		$("#select_info_breed").text("Any");
	});	
		
	//fix for & char code issue
	$('div.select_box').live('click', function() {
		$(this).find('div.tag_select').text($(this).find('div.tag_select').text().replace(/&amp;/,'&'));
	});
	
	// BBQ Goodness
	var cache = { '' : $('.default') };
	
	$(window).bind('hashchange', function(e) {
		// get the hash fragment
		var url = e.fragment;
		// hide any visible ajax content
		$('.list').children(':visible').hide();
		
		if ( cache[url] )
		{
			cache[url].show();
		}else
		{
			$('.loading').show();
			cache[url] = $('<div class="item" />')
				.appendTo('.list')
				.load( url, function() {
					var prod_paging = $(".product_paging a");
					if (prod_paging.length > 0)
					{
						prod_paging.each(function(index) {
							// get the host
							var host = location.hostname;
							var link = $(this).attr("href");
							link = link.replace("http://"+host+"/", "/pets/#/");
							$(this).attr("href",link);
						});
					}
					$('.loading').hide();
				});
				
			var selected_pet_type = $("#pet_type");
			if (selected_pet_type.val().length > 0)
			{
				if ($("#breed option").length == 1)
				{
					var selected_pet = getParameterByName("exact-pet");
					var pet_type_ids = new Array();
					pet_type_ids["D"] = "20";
					pet_type_ids["C"] = "21";
					pet_type_ids["B"] = "22";
					pet_type_ids["BC"] = "23";
					var cat_group = pet_type_ids[selected_pet];	
					var selected_breed = getParameterByName("breed-sel");
					$("#breed").load("/site/breeds_options/"+cat_group+"/"+selected_breed, function(response) {
						var all_selects = $("select");
						reload_breeds(all_selects.index($("#breed")));						
					});	
								
				}
			}			
		}
	});
	
	$(window).trigger('hashchange');	
	
	// PETS -> search
	$("#pet_search").submit(function(e) {
		e.preventDefault();
		// search&gender=F&age=3&exact-pet=D&adult_size=M&shelter=8582
		var gender = $("#gender").val();
		var age = $("#age").val();
		var breed = $("#breed").val()
		var pet_type = $("#pet_type").val();
		var adult_size = $("#adult_size").val();
		var base_uri = "/pets/#/pets/results/search&"
		var search_uri = base_uri + "gender="+gender+"&age="+age+"&category="+breed+"&exact-pet="+pet_type+"&adult_size="+adult_size+"&breed-sel="+breed;
		
		var zip = $("#geoquery2").val();
		var org = $("#org").val();
		
		if (zip.length > 0)
		{		
			// hide any visible ajax content
			$('.list').children(':visible').hide();
			$('.loading').show();
			// do a proximity search for organizations, get the entry_id's
			var radius = $("#radius2").val();
			var geo_uri = "/pets/org/"+zip+"/"+radius;			
			
			$.getJSON( geo_uri, function(json) {
				var shelter_ids = json.shelter_ids;
				search_uri += "&shelter="+shelter_ids;
				if (shelter_ids.length > 0)
				{
					window.location = search_uri;
				}else
				{
					window.location = base_uri + "age=X";
				}				
			});
		}else
		{
			// add the org
			search_uri += "&shelter="+org;
			// no need to worry with prox search
			window.location = search_uri;
		}
	});	
	
	// HOME -> hero
	$('ul#hero').innerfade({
		speed: 2000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '251px'
	});
	// HOME -> events scroll
	$('.headline_events ul').cycle({ 
    	fx:      'scrollUp', 
    	speed:    2000, 
    	timeout:  6000 
	});	
	// events - show more
	$(".show_more").click(function(e) {
		e.preventDefault()
		var parent = $(this).parent();
		$("span", parent).toggle();
	});
	// EVENTS -> archives
	$("#events_archive").submit(function(e){
		e.preventDefault(e);
		window.location = $("#mouth").val();
	});
	// EVENTS -> geo search on events page
	$("#geo_search_form").submit(function(e){
		e.preventDefault(e);
		var zip_code = $("#geoquery2").val();
		var radius = $("#radius2").val();
		
		if (zip_code.length < 1)
		{
			alert("Please enter a zip code.");
			return false;
		}
		
		window.location = $(this).attr("action") + "events/search/" + zip_code + "/" + radius + "/";
	});
	// EVENTS -> area code search
	$("#area_search").submit(function(e){
		e.preventDefault(e);
		var area_code = $("#area_code").val();
		if (area_code.length < 1)
		{
			alert("Please enter an area code.");
			return false;
		}
		
		window.location = $(this).attr("action") + area_code;
	});
	// EVENTS -> handle searching for area codes
	var area_search = $(".area_code_search");
	if (area_search.length > 0)
	{
		var path = window.location.pathname.substring(1,window.location.pathname.length);
		var host = "/assets/content-proxy.php?url=http://pfe.dev/news/adoption-"+path+"&mode=native";
		// var host = "/assets/content-proxy.php?url=http://petfood.ehclients.com/news/adoption-"+path+"&mode=native";
		$.get(host, function(data) {
			$("ul",area_search).html(data);
			area_search.removeClass("loading");
			if (data.length < 7)
			{
				$("ul",area_search).html("<li>No events found.</li>");
			}
		});
	}
	// PRESS ROOM -> archives
	$("#news_archive").submit(function(e){
		e.preventDefault(e);
		window.location = $("#mouth").val();
	});
	// EVENTS -> handle searching for locations/proximity
	var geo_search = $(".geo_events");
	if (geo_search.length > 0)
	{
		var event_ids = jQuery.trim($("#geo_event_ids").val());
		var host = "http://" + window.location.hostname + "/events/geo/" + event_ids;
		
		if (event_ids.length == 1)
		{
			geo_search.removeClass("loading");
			$("ul",geo_search).html("<li>No events found.</li>");
		}else
		{
			$.get(host, function(data) {
				$("ul",geo_search).html(data);
				geo_search.removeClass("loading");
				if (data.length < 7)
				{
					$("ul",geo_search).html("<li>No events found.</li>");
				}
			});			
		}
	}
	// ORGS -> proximity search
	$("#geofinder_org").submit(function(e){
		e.preventDefault();
		var geoquery = $("#geoquery2");
		var city = $("#select2");
		var radius = $("#geofinder_org input[name='radius']:checked");

		var path = $(this).attr("action") + "organizations/find/";
		
		if (geoquery.val().length > 1)
		{ // do zip search
			path += geoquery.val() + "/" + radius.val();
		}else
		{
			path += city.val().replace(" ", "+") + "/" + radius.val();
		}
		
		window.location = path;
	});	

});
