//Run the code when document ready
$(function() {
   //Auto-fill the subdomain based on Business name  
   $('#user_business_attributes_name').change(function(){
       propsed_value = $(this).val().replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
       $('#user_business_attributes_subdomain').val(propsed_value);
      }
   );

	// Expand Panel
	$("#open").click(function(){
		$("div#login_wrap").slideDown("slow");	
	});	
	
	// Collapse Panel
	$("#close").click(function(){
		$("div#login_wrap").slideUp("slow");	
	});		
	
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});
	
 
	$('#login_page').submit(function(){
    if($('#login_page_url').val() != "")
      location.href = "https://" + $('#login_page_url').val() + ".curdbee.com/login";
    return false;
 });


  //check for subdomain availability
  $('#user_business_attributes_subdomain').blur(function(){
     $.getJSON("/subdomain_checker?subdomain=" + $(this).val(), function(data){
        if(data[0]){ 
          $("#subdomain_availability").html("This ID is available.")
                                      .attr("class", "availability")
                                      .show();
          $("#create_account_bt").attr("disabled", "")
        }
        else {
          $("#subdomain_availability").html("This ID is not available!")
                                      .attr("class", "not_availability")
                                      .show();
          $("#create_account_bt").attr("disabled", "disabled")
        }
     }) 
  });


  //toggle help box section
  $("div#help_box dd").hide();
	$('#help_box dt')
		.hover(
			function(){ $(this).css('cursor','pointer'); },
			function(){ $(this).css('cursor','default'); }
		)
		.click(function(){
			var b = true;
			$(this).toggleClass('close'); 
			$(this).nextAll().each(function(i,el){
				if (el.nodeName=='DT') b = false;
				if (b) jQuery(el).toggle();
			});
		});

  //open links in facebox
  $('a[rel*=facebox]').facebox();

  $("div.highligh a").hover(
    function(){
      $(this).children("img.normal").hide();
      $(this).children("img.hover").show()
    },
    function(){
      $(this).children("img.normal").show();
      $(this).children("img.hover").hide();
    }
  );

})

