//Only need one document ready function, throw it in here. 
 
function my_resources(where){
    window.location = '/resources/page/1?search_type='+where;
} 
 

$(document).ready(function() {
	
	//Scroll for Find a Member
	 var msie6 = $.browser == 'msie' && $.browser.version < 7;
	  
	 if ($('#map').length != 0) {
	  if (!msie6 ) {
	    var top = $('#map').offset().top - parseFloat($('#map').css('margin-top').replace(/auto/, 0));
	    $(window).scroll(function (event) {
	      // what the y position of the scroll is
	      var y = $(this).scrollTop();
	      
	      // whether that's below the form
	      if (y >= top) {
	        // if so, ad the fixed class
	        $('#map').addClass('fixed');
	      } else {
	        // otherwise remove it
	        $('#map').removeClass('fixed');
	      }
	    });
	  }  
 	}
	// set flag so it doesn't increment infinitely
	var rating_incremented = false;
	
	$("#vertMenu").accordion({
		active: false, 
		autoHeight: false,
		collapsible: true
	});
	
	/////////////////////
	// RATINGS RESOURCE//
	/////////////////////
	$(function(){
	 // $resource_id:$rating_amount
	 $('.auto-submit-star').rating({
	  callback: function(value, link) {
		var split = value.split(':');
		var rating = split[0];
		var resource_id = split[1];

		// Ajax request
		$.ajax({
			type: "POST", 
			url: "/resources/rate/" + resource_id + "/" + rating,
			success: function(data) { 
				var dom_element = "#ratings_count_" + resource_id;

				if (data > 0) {
					// change flag
					if (rating_incremented == false) { 
						rating_incremented = true;

						$(dom_element).text(
							function(i, text) { 
								return ++[+text][0];  
							}
						);
					}
				} else {
					$(dom_element).text(
						function(i, text) { 
							var subtracted_dom = --[-text][0]; 

							if (subtracted_dom < 0) {
								return '0';
							} else {
								return subtracted_dom;  
							}  // if
						}
					); // text
				} // else
			} //function(data)
		}); //ajax
	  } // callback
	 }); //.rating()
	}); // $(function) 
	////////////////////////
	// END RATING Resource//
	////////////////////////
	
	/////////////////////////
	// RATINGS Testimonials//
	/////////////////////////
	$(function(){
	 // $testimonial_id:$rating_amount
	 $('.auto-submit-star-testimonial').rating({
	  callback: function(value, link) {
		var split = value.split(':');
		var rating = split[0];
		var testimonial_id = split[1];
	
		// Ajax request
		$.ajax({
			type: "POST", 
			url: "/testimonials/rate/" + testimonial_id + "/" + rating,
			success: function(data) { 
				var dom_element = "#ratings_count_" + testimonial_id;
	
				if (data > 0) {
					// change flag
					if (rating_incremented == false) { 
						rating_incremented = true;
	
						$(dom_element).text(
							function(i, text) { 
								return ++[+text][0];  
							}
						);
					}
				} else {
					$(dom_element).text(
						function(i, text) { 
							var subtracted_dom = --[-text][0]; 
	
							if (subtracted_dom < 0) {
								return '0';
							} else {
								return subtracted_dom;  
							}  // if
						}
					); // text
				} // else
			} //function(data)
		}); //ajax
	  } // callback
	 }); //.rating()
	}); // $(function) 
	////////////////
	// END RATING //
	////////////////
	
	//////////////////
	// SELECTED TAB //
	//////////////////
	$(function() {
		var tab = window.location.pathname;
		tab = tab.replace('.html', '');
		switch(tab)
		{	
			case '/home':
			case '/':
				tab = "home";
				break;
			case '/find-cdpe-results':
				tab = "find_cdpe";
				break;
			case '/what-is-a-cdpe':
				tab = "what_is_cdpe";
				break;
			case '/become-a-cdpe-now':
				tab = "become_cdpe";
				break;
			case '/forum/':
				tab = "forum";
				break;
			case '/press-releases':
				tab = "press_releases";
				break;
			case '/blog/':
				tab = "blog";
				break;
			case '/contact-us':
				tab = "contact_us";
				break;
			case '/schedule-now':
				tab = "schedule_now";
				break;
			case '/resources':
				tab = "resources";
				break;
		}

		tab = document.getElementById(tab);
		
		if(tab)
		{
			tab.setAttribute('class', 'active')
		}
	});
	//////////////////////
	// END SELECTED TAB //
	//////////////////////
	
	//////////////////
	// AUTOCOMPLETE //
	//////////////////
	$('#autocomplete-email').autocomplete({
		source: '/ajax/email_autocomplete',
		minLength: 3
	});
	
	$('#autocomplete-username').autocomplete({
		source: '/ajax/username',
		minLength: 3
	});
	//////////////////////
	// END AUTOCOMPLETE //
	//////////////////////
	
	/////////////
	// DIALOGS //
	/////////////
	$('a[rel*=facebox]').facebox();
	//Opaque Facebox
	$.facebox.settings.opacity = .331;
	
	/////////////////
	// END DIALOGS //
	/////////////////
	
//Testimonials and headlines	
  $('#testimonials').innerfade({
  animationtype: 'slide',
  speed: 750,
  timeout: 15000,
  type: 'random',
  containerheight: '40px'
  });

  $('.headlines_right').cycle({
    fx: 'fade',
    speedIn: 400,
    sync: 0,
    speedOut: 400,
    timeout: 6000
  });
  
	//tabs on resources 
	// $("#resource_tabs").tabs({ cookie: { expires: 30 } });
	$("#datepicker").datepicker();
	$("#datepicker2").datepicker();
 
 	/////////////////////////////////////////////
	// MULTISELECT RESOURCE CATEGORY DROP DOWN //
	/////////////////////////////////////////////
    $("#category").multiSelect();
    ////////////////////////////////////////////////
	// END MULTISELECT RESOURCE CATEGORY DROP DOWN//
	////////////////////////////////////////////////
    
  	$('#hide-me').css('display','none');
    $('a.close_toggle').click(function() {   
	$("#find_icon").toggleClass("member_on").next().slideToggle("slow");
	return false;	  
	});
	 
	
	//Dont hate.
	
	 $("#Form1").submit(function() {
	 	
	 	var zip = $("#zip").val();
	 	var street = $('#street').val();
	 	var radzip = $('#radius_zip').val();
	 	var city = $('#city').val();
	 	var radius = $('#radius').val();
	 	var state = $('#state').val();
	 	
	 	//Regex Matching
	 	var other_zip = /^[0-9]+$/.exec(radzip);
	 	var match_zip = /^[0-9]+$/.exec(zip);

	 	
      if ($("#first_name").val() == "First Name" && $("#last_name").val() == "Last Name") {
        $(".name_error").fadeIn();
        return false;
      } else if (
      	zip == "Enter Exact Zip Code" && 
		street == 'Street Address' &&
		radzip == 'ZIP Code' &&
		city == 'City' &&
		radius == '' &&
		state == ''
	  	) {
  	$(".all_error").fadeIn();
        return false;
    }
      else if (
    	      	zip == "Enter Exact Zip Code" && 
    			street == 'Street Address' &&
    			radzip == 'ZIP Code' &&
    			city == 'City' &&
    			state == ''
    		  	) 
      {
    	  	$(".street_error").fadeIn();
    	        return false;
      } 
    		else if ( 
				(zip == "Enter Exact Zip Code") && 
				(city == 'City') && (state == '') &&  
				(!other_zip) &&
				(radzip.length == 5) && 
				(radzip != 'ZIP Code')
				
			 ) 
	{
		 
			$(".radius_error").fadeIn();
	        return false; 			
		 
	}
    else   
    if ( (zip != "Enter Exact Zip Code") ) 
    {
    	
    	if (zip.length != 5)
    	{ 
	    	$(".zip_error").fadeIn();
	        return false;
    	}
    	else if (!match_zip)
    	{
	    	$(".zip_error").fadeIn();
	        return false;    		
    	}
	}

	else if ( 
				(zip == "Enter Exact Zip Code") && 
				(city == 'City') && (state == '') && 
				(street == 'Street Address') && 
				(radzip.length == 5) && 
				(radzip != 'ZIP Code')  
				
			 ) 
	{
		if (!other_zip) { 
			$(".radius_error").fadeIn();
	        return false; 			
		}	
		else 
		{
		$(".street_error").fadeIn();
        	return false; 
		}
	}

	else if ( 	(zip == "Enter Exact Zip Code") &&
				(city == 'City') &&
				(state == '') &&  
				(radzip != 'ZIP Code') && 
				(radzip.length != 5)  
			 ) 
	{
		$(".radius_error").fadeIn();
        return false; 
	} 
	else if ( 
				(zip == "Enter Exact Zip Code") && 
				(city == 'City') && (state == '') && 
				(street != 'Street Address') && 
				(radzip == 'ZIP Code')
				
			 ) 
	{
		$(".street_error").fadeIn();
        	return false; 
	} 
    

	else if ( 
				(zip == "Enter Exact Zip Code") && 
				(city == 'City') && (state == '') && 
				(street =! 'Street Address') && 
				(!other_zip)  
			 ) 
	{
 
			$(".radius_error").fadeIn();
	        return false;	 
 
	} 		 

	else if ( 
				(zip == "Enter Exact Zip Code") && 
				(radzip == 'ZIP Code') && 
				(street == 'Street Address') && 
				(city == 'City') && 
				(state != '')  ) 
	{
		$(".city_error").fadeIn();
       		return false; 
	}
	
		else if ( 
				(zip == "Enter Exact Zip Code") && 
				(radzip == 'ZIP Code') && 
				(street == 'Street Address') && 
				(city != 'City') && 
				(state == '')  ) 
	{
		$(".city_error").fadeIn();
 	       return false; 
	} 
}); 


 
}); //End Document Ready


var cdpe_jump = true; 
function cdpe_member(some_id) {  
		//Removed Delay
		//var date = new Date();
		//var curDate = null;
		//do { curDate = new Date(); }
		//while(curDate-date < 250);
	  cdpe_jump = false;
      google.maps.Event.trigger(markers[some_id], 'click');  
}
