// Search box
jQuery(function($) {

	$('fieldset.search a').bind('click', function(){
	    fireSearch();
	});

	$('fieldset.search input').bind('keyup', function(e) {
	    if(e.keyCode==13){
		fireSearch();
	    }
	});

	function fireSearch(btn, event, URL) {
	    var searchTerm = $('.searchInput').attr("value");

	    searchTerm = "/pages/home/search.aspx?s=" + searchTerm;
	    // navigate the url with the value as query string
	    top.location=searchTerm;
	}
	
	$(".sendToAFriendForm .closeBtn a").bind("click", function(event){
	        $(stoaf).slideUp(600);
		event.preventDefault();
	});
	$('input:text').hint();
    	$('textarea').hint();
}); 

var stoaf = ".sendToAFriendForm";

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};

// SEND TO A FRIEND ----------------------------------------------------------------------------------------------------------------
function stoafSlide () {
    // on click of either the stoaf button or the close button slideToggle the panel
    //$("#accessibilityPanel .controls a.stoaf, .sendToAFriendForm .closeBtn a").unbind("click");
    //$("#accessibilityPanel .controls a.stoaf, .sendToAFriendForm .closeBtn a").bind("click", function(event){
        $(stoaf).slideToggle(600);
        //event.preventDefault();
    //});
}

// display, change and hide stoaf form
function stoafMsgSent() {
    // after two seconds change to message sent, swap image, swap text, change color
    $(stoaf).delay(2000,function(){
        $(stoaf + ' .stoafSent img').attr("src","/images/shd/loaded.gif");
        $(stoaf + ' .stoafSent p').css("color","#A5C718");
        $(stoaf + ' .stoafSent p').text("Message sent");
        
        // after 3 seconds hide the panel    
        $(stoaf).delay(3000,function(){
            $(stoaf).slideToggle(600);   
            
            // once the panel is hidden change the message back to loading
            $(stoaf).delay(700,function(){
                $(stoaf + ' .stoafSent img').attr("src","/images/shd/loading.gif");
                $(stoaf + ' .stoafSent p').css("color","#666666");
                $(stoaf + ' .stoafSent p').text("Sending message...");
                $('#pnlMain').attr("style","display:block;");
                $('#pnlThanks').attr("style","display:none;");
                ReApplyAfterUpdate();
            });
        });
    });
}

function validateForm(element) {
    var bOK 
    bOK = true;
    $(element).each(
        function() {
            
            var textBox = this;
            var boxValue = $(this).val();
            
            if (boxValue == "" || boxValue.indexOf("Your ") == 0) {
                $(this).css("background-color","#EDFFA4");
                bOK = false;
            }
        }
    );
    return bOK;
}

function ReApplyAfterUpdate()
{
    $(".sendToAFriendForm .closeBtn a").bind("click", function(event){
        $(stoaf).slideUp(600);
        event.preventDefault();
    });
    $('input:text').hint();
    $('textarea').hint();
}

