/// <reference path="jquery-intellisense.js" />

prevThumb = new Image(120, 80);
prevThumb.src = "/Content/images/error-message-box-small.jpg";

var currentJson;
var currentPageNum = 1;
var maxPages = 1;
var currentTool = 'None'
var directionString = '';
var animationSpeed = 250;
function doSearch() {
    if ($('#location').val() != blankLocationText) {        
        if ($('#location').val() == blankLocationText) $('#location').val('');
        var varUrl = '/search/jsonpeople.html';//?location=' + $('#location').val() + '&superCategory=' + $('#superCategory').val() + '&new=true';
        showError('Searching, Please Wait.');
        //Don't animate initially
        directionString = '';
        try {
            $.getJSON(varUrl, { location: $('#location').val(), superCategory: $('#superCategory').val(), New: "true" }, createHtml);
        } catch (e) {
            alert(e);
        }        
    } else {
        showError('Please enter something to search for.');
    }
}

function createHtml(json) {
    try {
        currentJson = json;
        if (currentJson.results.length > 0) {
            showError('');
            currentPageNum = 1;
            maxPages = currentJson.results.length;
            $('#pageCount').text(maxPages);            
            showResult();            
            showResultsTab();            
        }
        else {
            showError('No results returned. Please try another search.');
        }
    } catch (e) {
        alert(e);
    }
}

function showError(message) {
    if (message.length > 0) {
        $('#errorMessage').text(message);
        $('#errorMessageBox').css("display", "block");
    }
    else {
        $('#errorMessageBox').css("display", "none");
    }
}

function showInfo(thisControl) {
    showError(thisControl.title);
}

function showResults() {    
    if ($('#youSearchedFor').html().length > 0) {
        showResultsTab();
    }
    else {
        doSearch();
    }
}

function pause(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while (curDate - date < millis);
}

function moveResult(number) {
    if ((currentPageNum + number) >= 1 && (currentPageNum + number) <= maxPages) {
        currentPageNum += number;
        showResultsTab();
        if (number > 0) {
            directionString = 'right';
            $('div.content-search-result').hide('slide', { direction: 'left' }, animationSpeed, showResult);
        }
        else {
            directionString = 'left';
            $('div.content-search-result').hide('slide', { direction: 'right' }, animationSpeed, showResult);
        }
    }
}

function formatActivities(activityList) {

    var tempList = activityList.toString().split(',');

    activityList = "";
    
    for (var i in tempList) {

        if (tempList[i].length > 0) {
        
        var myAct = tempList[i];

        var spaceList = myAct.toString().split(' ');
        myAct = "";
        for (var s in spaceList)
            {
                myAct = myAct.toString() + spaceList[s].substring(0,1).toUpperCase() + spaceList[s].substring(1) + " ";                
            }
            activityList = activityList + myAct.substring(0, myAct.length -1) + ", ";
        }
    }
    
    activityList = activityList.toString().substring(0, activityList.toString().length - 2);    
    
    return activityList;
}

function showResult() {
    $('#currentPage').text(currentPageNum);
    $('#youSearchedFor').html($('#location').val());
    $('#resultLink').attr("href", "http://www.activescotland.org.uk/search/groups.html?location=" + $('#location').val() + "&superCategory=" + $('#superCategory').val() + "&query=&resultsPerPage=5&new=true");
    $('#resultLink').attr("title", currentJson.results[currentPageNum - 1].title);
    $('#resultLink').html(currentJson.results[currentPageNum - 1].title);
    $('#findOutMore').attr("href", "http://www.activescotland.org.uk/search/groups.html?location=" + $('#location').val() + "&superCategory=" + $('#superCategory').val() + "&query=&resultsPerPage=5&new=true");

    $('#activities').html(formatActivities(currentJson.results[currentPageNum - 1].activities));
    $('#shortDescription').html(currentJson.results[currentPageNum - 1].shortDescription);
        
    showHideNav();
    if (directionString.length > 0) {
        $('div.content-search-result').show('slide', { direction: directionString }, animationSpeed);
    }
}

function showHideNav() {
    if (currentPageNum <= 1) {
        $('#lnkPrev').addClass("disabled");
    }
    else {
        $('#lnkPrev').removeClass("disabled");
    }
    if (currentPageNum >= maxPages) {
        $('#lnkNext').addClass("disabled");
    }
    else {
        $('#lnkNext').removeClass("disabled");
    }
}

function showResultsTab() {    
    if ($('#location').val() == blankLocationText) {
        showError('Please enter something to search for.');
        return;
    }
    $('#searchPanel').css("display", "none");
    $('#resultsPanel').css("display", "block");    
}

function showSearchTab() {
    $('#searchPanel').css("display", "block");
    $('#resultsPanel').css("display", "none");    
}

function setTool(thisControl) {
    currentTool = thisControl.id;
    $('#GetActive').attr("class", "");
    $('#EntertainMe').attr("class", "");
    $('#CreativeAndCultured').attr("class", "");
    $('#GetInvolved').attr("class", "");
    $('#Volunteer').attr("class", "");
    $('#LearningZone').attr("class", "");
    $('#None').attr("class", "");
    thisControl.className = 'selected';
    if (thisControl.id == 'None') {
        $('#searchResult h2').text("Search for an activity near you");
    }
    else {
        $('#searchResult h2').text("'" + $('#' + thisControl.id + ' img').attr('title') + "' Activities");
    }
}
