//
// Site-wide Javascript
//
$(document).ready(

    function() {
    
        // No follow links
        //
        $('a.nolink').click( function() { return false } )
        
        // Search
        //
        $('a#searchButton').click(goToSearch);
        $('input#Search').keypress(searchKeyPressHandler);
            
        // Subscribe
        //
        $('input#subscribe').click( function(){ $(this).val('') } )
        $('a.subscribe').click( subscribe )

        // Quick nav
        //
        $('a#quickNav').click(
            function() {
                var val = $('select#navigation').val()
                if (val != 0) {
                    location.replace(val)
                }
                return false ;
            })

        // Email addresses
        //
        $('a.emailAddress').each(
            function() {
                retrieveEmail($(this).attr('id'))
            })

        // Mouse over links
        //
        $('.boxLinks li').mouseover(
            function() {
                if (!$(this).hasClass('nomouseover')) {
                    $(this).addClass('active')
                }
            })
        $('.boxLinks li').mouseout( function() { $(this).removeClass('active') } )
    }
)

// Search
//
function goToSearch() {

    var val
    val = $('input#Search').val()
    if (val == '' || val == 'Search epulse.eu') {
        alert('Please enter a search term')
        return false
    } else {
        $('a#searchButton').attr('href','/Search?term=' + val)
    }
}


function goToSearchViaKeyPress() {

    var val
    val = $('input#Search').val()
    if (val == '' || val == 'Search epulse.eu') {
        alert('Please enter a search term')
        return false
    } else {
        self.location.href = '/Search?term=' + val
    }
}


function searchKeyPressHandler(e)
{
  switch(e.keyCode)
  {
    case 38: // up
        e.preventDefault();
        break;
    case 40: // down
        e.preventDefault();
        break;
  case 9:  // tab
        break;
  case 13: // return
        e.preventDefault();
        goToSearchViaKeyPress();
        break;
  default:
        break;
  }
}


// Emails
//
function retrieveEmail(email) {

    // TBD
}

function subscribe() {

    var val = $('input#subscribe').val()
    if (!val || val == 'Your email address') {
        alert('Please enter your email address')
        return
    }
    var params = {}
    params['email'] = val
    params = $.toJSON(params)
    params = encodeURIComponent(params)

    $.ajax({
              type:        "POST",
              url:         "/AJAX/Subscribe",
              data:        params,
              dataType:    "json",
              contentType: "text/plain",
              processData: 0,
              timeout:     40000,
            error:
              function() {
                 return false;
              },
            success:
              function(data) {
                if (data.error) {
                    alert(data.message)
                    return false ;
                } else {
                    alert('Thank you for subscribing, we will be in touch shortly')
                    $('input#subscribe').val('Your email address')
                    tb_remove()
                }
            }
    })

}
