/**
 * @author shanebarron
 */

$(document).ready(function(){
    
  $("#book_class").validate();
	
	var searchBox2Default = "Therapies or Classes...";
	
	$("#courselist tr:odd").addClass("odd");
    $("#courselist tr:not(.odd)").hide();
    $("#courselist tr:first-child").show("slow");
    
    $("#courselist tr.odd").click(function(){
    	$(this).find(".plus").toggleClass("minus");
        $(this).next("tr").toggle("slow");
    });
    
   
    //submit the newsletter
    $(".button").click(function() { 
    	 var name = $("input#index-name").val();  
         if (name == "") {  
        	alert("Please add your name.");
        	$("input#index-name").focus();  
        	return false;  
         }
         var email = $("input#index-email").val();
         if (isValidEmailAddress(email) == false || email == "") {  
            alert("Please enter a valid email address."); 
         	$("input#index-email").focus();  
         	return false;  
          }
         
         var dataString = 'newsletter_name='+ name + '&newsletter_email='+ email; 
         $.ajax({  
           type: "POST",  
           url: "http://www.dublinholisticcentre.com/ajax/newsletter_signup.php",  
           data: dataString,  
           success: function() {  
        	 $('#newsletter_signup').html("<div id='message_success'></div>");  
             $('#message_success').html("<h2>Thank You for subscribing to our Newsletter.</h2>")   
             .show();
           },
           error:function (xhr, ajaxOptions, thrownError){
        	   alert(xhr.statusText);
               alert(thrownError);
           } 
         });  
         return false;
    }); 
    
    $("#inputString").focus(function(){  
        if($(this).attr("value") == searchBox2Default) $(this).attr("value", "");
        $(this).attr("class", "search_active");
    });  
    
    $("#inputString").blur(function(){  
        if($(this).attr("value") == "") $(this).attr("value", searchBox2Default);  
    });
    
    $(window).scroll(function()
    {
      $('#message_box').animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 350});
    });
    
    //when the close button at right corner of the message box is clicked
    $('#close_message').click(function()
    {
      //the messagebox gets scrool down with top property and gets hidden with zero opacity
      $('#message_box').animate({ top:"+=15px",opacity:0 }, "slow");
    });
     
});

//search box 
function lookup(inputString) {
	if(inputString.length == 0) {
		// Hide the suggestion box.
		$('#suggestions').hide();
	} else {
		$.post("http://www.dublinholisticcentre.com/ajax/search.php", {queryString: ""+inputString+""}, function(data){
			if(data.length >0) {
				$('#suggestions').show();
				$('#autoSuggestionsList').html(data);
			}
		});
	}
} // lookup

function fill(thisValue, thisURL) {
	$('#inputString').val(thisValue);
	$("#urlString").attr("href", thisURL);

	setTimeout("$('#suggestions').hide();", 200);
}

$(function() {
		$( "#accordion" ).accordion();
});

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};



