$(document).ready(function (){
    applyCufon();
    applyOtherCufon();
    
    $('input:text').hint();
    
    initialiseSearch();
    
    $('#bodyId').show();
    bindRegInput();

});


function bindRegInput(){
    $("#getInvolved input").bind("keyup", function(e){
        if(e.keyCode == "13"){
          window.location = $("#getInvolved .submit").attr('href');
        }
        else{
          $("#getInvolved .submit").attr('href',('/pages/account/register.aspx?e=' + $(this).val()))
        }
    });
  }

function initialiseSearch() {
    $('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 = $('.searchBox').attr("value");

    searchTerm = "/pages/home/search.aspx?s=" + searchTerm;
    // navigate the url with the value as query string
    top.location=searchTerm;
}


// INITIALISE CUFON --------------------------------------------------------------------------------------------------------------------

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function replacecarriagereturn(element,replaceWith) {   
    // alert(element);
    // for each element found matching this selector
    $(element).each(function(){       
        // this particular selector instance's content
        var elementHTML = $(this).text() 
        
        // escape the characeters to show any carriage returns as %A0
        elementHTML = escape(elementHTML);  
        
        // if any carriage returns are found then remove them
        if(elementHTML.indexOf("%0D%0A") > -1)  
        {   
            elementHTML=elementHTML.replace("%0D%0A",replaceWith);  
        }  
        else if(elementHTML.indexOf("%0A") > -1)  
        {   
            elementHTML=elementHTML.replace("%0A",replaceWith);  
        }  
        else if(elementHTML.indexOf("%0D") > -1)  
        {   
            elementHTML=elementHTML.replace("%0D",replaceWith);  
        }  
        
        // unescape the characeters
        elementHTML = unescape(elementHTML); 
        
        //alert("before trim: " + elementHTML);
        elementHTML = trim(elementHTML);
        //alert("after trim: " + elementHTML);
        
        // put the filtered text back into this element
        $(this).text(elementHTML);
    });
    
    // cufon replace 
    if ($(element).length != 0) {
        Cufon.replace(element);
    }
}  

function applyCufon() {
    
    var cufonElements = new Array();
        
    cufonElements.push('#leftCol h1');
    cufonElements.push('#content .followOnLink');
    cufonElements.push('li.submit a');
    cufonElements.push('h1.pageTitle');
    cufonElements.push('#newsStoryDisplay h2');
    cufonElements.push('h2.cufonTitle');
    cufonElements.push('.home h2');
    cufonElements.push('.home h3');
    cufonElements.push('.home .intro');
    cufonElements.push('#middleCol h2');
    cufonElements.push('#middleCol h3');
    cufonElements.push('#middleCol h4');
    cufonElements.push('#middleCol h5');

    for (var x = 0; x <= cufonElements.length - 1; x++)
    {
        replacecarriagereturn(cufonElements[x],"");
    }
}

function applyOtherCufon() {
    Cufon.replace('span.day')('span.month');
}