$(document).ready(function() {
   //Post our form values to the JSP that searches for available domains
   $(':text:first').focus();
   $(".sprite-btnFindIt, .sprite-btn_search_again, .search-again").click(function () {
	  if($('.domain-search').val() ==''){
		 $('.domain-search').addClass("error");
		 $(':text:first').focus(function () {
		 $('.domain-search').removeClass("error")});
	  }else{ 
		 //hide these parts from past searches
		 $('.step-2, .step-2-additions, .step-2-no-premium, .sprite-btnFindIt, .sprite-btn_search_again,  #get-this-now, .domain-status-wrapper, .alternatives, .validationErrorMessage').hide();
		 $('#loading').show();
		 
		 //remove wide class on three boxes
		 $('.alternate-names, .alternate-extensions, .premium-domains').removeClass('wide');
		 
		 //reset the state of the three boxes
		 $('.alternate-names, .alternate-extensions, .premium-domains').show();
		 
		 //Define both search string and extension for POST data 
		 var numRand = Math.floor(Math.random()*100000);//cachebuster
		 var domain = $('.domain-search').val(); 
		 var tld = $('.styled').val();
		 
		 $.post("/promo/onepagecheckout/ajax.rcmx?searchString="+domain+"&selectedTld="+tld+"&action=SEARCHDOMAIN&ts="+numRand+"", $("#domain-search-form").serialize(),
		 function(data){
			var status = jQuery.trim(data);
			
			//hide the alternative domains on new search
			$('.step-2, .step-2-additions').hide();
			
			if(status == 'Available') { 
				$('#loading, .sprite-domain-not-available, .sprite-btnFindIt').hide();
				$('.domain-status-wrapper, .sprite-domain-available, #get-this-now, .step-2-additions').show();
				$('#features').text('Now add a feature to ' +domain+tld);
				$('#email').html('Get yourname@'+domain+tld+'<br />Present a professional business image with every email you send.');
				//set the value of the input so we can pass it off to the form processor
				$('#domainToPurchaseAddEmail, #domainToPurchaseDim').val(domain+tld);
				
				//set the Get this domain now button with the chosen domain name
				$(".sprite-BTN-GetThisNow ").attr("href", "/domain/availableDomainToCart.rcmx?domainName="+domain+tld+"&domainTerm=36");

			}
			else if(status == 'Not Available') { 
				$('#loading, .sprite-domain-available, .sprite-btnFindIt').hide();
				$('.domain-status-wrapper, .sprite-domain-not-available, .alternatives, .sprite-btn_search_again').show();	
				
				$("#toggle").click(function () {
					$(".alternatives").fadeOut();
					$(".step-2").slideDown();
			});	
		
		/***AJAX POSTING FUNCTIONS BEGINS***/
		
		//Write the preloader into each box for each new AJAX request
		var preload = '<div class="alternate-loader"></div>'
		
		//Alternate Extensions
		$('.alternate-names, .alternate-extensions, .premium-domains').html(preload);
		$('.step-2 .alternate-extensions').load('/promo/onepagecheckout/ajax.rcmx?searchString='+domain+'&selectedTld='+tld+'&action=SEARCHALTERNATIVES&ts='+numRand+'', function() {
		$('.alternate-extensions').hide();
		$('.alternate-extensions').fadeIn('slow');
		
		  //table striping
			$('.step-2 .wrapper li:nth-child(even), .step-2-no-premium .wrapper li:nth-child(even)').addClass('even');
			
		var altext = $('.alternate-extensions').html();
		if(altext == 'empty'){
			$('.alternate-extensions').hide();
			$('.alternate-names, .premium-domains').addClass('wide');
		}
		
		//Hide all but 5 rows of domains
		$(function() {
		  $("ul.more").each(function() {
			$("li:gt(4)", this).hide();
		  });
		});
		});
		
		
		//Alternate Names
		$('.alternate-names, .alternate-extensions, .premium-domains').html(preload);
		$('.step-2 .alternate-names').load('/promo/onepagecheckout/ajax.rcmx?searchString='+domain+'&selectedTld='+tld+'&action=SEARCHSPUN&ts='+numRand+'', function() {
		
		$('.alternate-names').hide();
		$('.alternate-names').fadeIn('slow');
		
		  //table striping
			$('.step-2 .wrapper li:nth-child(even), .step-2-no-premium .wrapper li:nth-child(even)').addClass('even');
		
		//Hide all but 5 rows of domains
		$(function() {
		  $("ul.more").each(function() {
			$("li:gt(4)", this).hide();
			//hide this until all results are returned
			$('#see-more').fadeIn();
		  });
		});
		});
		
		
		//Premium Domains
		$('.alternate-names, .alternate-extensions, .premium-domains').html(preload);
		$('.step-2 .premium-domains').load('/promo/onepagecheckout/ajax.rcmx?searchString='+domain+'&selectedTld='+tld+'&action=SEARCHPREMIUM&ts='+numRand+'', function() {
		
		$('.premium-domains').hide();
		$('.premium-domains').fadeIn('slow');
		$('.tooltip[title]').qtip({ 
			style: {name: 'light', tip: true}, 
			position: {adjust: { screen: true}}
		});
		
		  //table striping
			$('.step-2 .wrapper li:nth-child(even), .step-2-no-premium .wrapper li:nth-child(even)').addClass('even');
		
		var pdomains = $('.premium-domains').html();
			if(pdomains == 'empty'){
			$('.premium-domains').hide();
			$('.alternate-names, .alternate-extensions').addClass('wide');
		}
		
		//Hide all but 5 rows of domains
		$(function() {
		  $("ul.more").each(function() {
			$("li:gt(4)", this).hide();
		  });
		});
		});
		
		//On click, we show all the results
		$('#see-more').click(function() {
		  $('ul.more li').each(function() {
			$(this).show();
			$('#see-more').hide();
		  });
		});
		
		/***AJAX POSTING FUNCTIONS END***/
		
			}
			else if(status == '') { 
				//If status returns blank, throw generic error message
				$('#loading, .alternatives').hide();
				$('.validationErrorMessage, .sprite-btnFindIt').show();
				$('.validationErrorMessage span').text('The search encountered an error. Please try again');
			}else{
			 //catch for all search errors
			 $('#loading, .alternatives').hide();
			 $('.validationErrorMessage, .sprite-btnFindIt').show();
			 $('.validationErrorMessage span').html($('<div/>').html(status).text());
			}	
		}); 
	   }
	});
	
	//switches the styled select dropdown
	$("#domain-search-form select").mouseover(function(){
	  $('#single-page-checkout #selectselectedTld').removeClass("select");
      $('#single-page-checkout #selectselectedTld').addClass("select-hover");
    }).mouseout(function(){
      $('#single-page-checkout #selectselectedTld').removeClass("select-hover");
      $('#single-page-checkout #selectselectedTld').addClass("select");
    });
	
	//Binds enter key to the form
	$('#searchString').keypress(function(e) {
        if(e.which == 13) {
            jQuery(this).blur();
            jQuery('.sprite-btnFindIt').focus().click();
			return false;
        }
    });
});   
