﻿
// declare web namespace
WEB = {};

// Check that the page has loaded
$(document).ready(function() {

    setSearch();

});

function pageLoad() {

    setForm('.TVI-form');

}

function setSearch() {

    //get search object
    var s = $('#search');

    //check for return key and redirect to search page
    s.keydown(function(e) {

        if (returnKeyPressed(e)) {

            var k = s.val();

            if (k.length > 0) {

                //replace any spaces with hyphens and redirect to search page
                k = k.replace(/ /g, "-");

                var url = "/browse/" + k + ".aspx";

                window.location = url;

            }
        
        }

    });

    //check for key presses and
    s.keyup(function(e) {

        var k = s.val();

        if (k.length > 0) {

            search(k);

        }
        else {

            clearSearch();

        }

    });

    //check for key presses and
    s.focus(function() {

        var k = s.val();

        if (k.length > 0) {

            search(k);

        }

    });

    s.blur(function() {

        clearSearch();

    });

    $('#searchTabs a').click(function() {
        $('#searchTabs a').removeClass('selected');
        $(this).addClass('selected');
        $('.searchIndex').hide();
        var selectedTab = $('#searchTabs .selected').attr('class').replace(' selected', '');
        $('#' + selectedTab + 'Browse').show();
        return false;
    });

}

function search(k) {

    //replace ' with `
    k = k.replace(/'/g, "`");
    
    //see which tab is selected
    var selectedTab = $('#searchTabs .selected').attr('class').replace(' selected', '');

    //get search suggestions
    $.ajax({
        type: "POST",
        url: '/handlers/search.aspx/suggestions',
        data: "{'mode' : '" + selectedTab + "', 'keywords' : '" + k + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

            $('.searchDropDown').html(data.d);
            
        }
    });
    
}


function clearSearch() {

    window.setTimeout(function() {

        $('.searchDropDown').html('');

    }, 500);

}

function setForm(formname) {

    //clear events
    $(formname).unbind();

    $(formname).keypress(function(e) {

        //check return key is pressed
        if (returnKeyPressed(e)) {

            //check input is not textarea
            if (e.target.type != 'textarea') {

                //get postback arguments from button
                var postback = $(this).find(".btn").attr("href");
                if (postback != undefined && postback.indexOf('doPostBack') > -1) {

                    postback = postback.replace("javascript:__doPostBack('", "");
                    postback = postback.replace("','')", "");

                    //call postback
                    __doPostBack(postback, '');

                }

            }

        }

    });

}

function returnKeyPressed(e) {

    //check if event has been passed in, otherwise set it to the window event
    if (!e) e = event;

    //check return key has been pressed
    if ((e.which == 13) || (e.keyCode == 13)) {

        //check input is not textarea
        if (e.target.type != 'textarea') {

            //stop postback
            e.returnValue = false;
            if (!$.browser.mozilla) {
                event.returnValue = false;
            }

        }

        return true;

    } else {
        return false;
    }

}

function setMap() {

//    // Check to see if the map exists and create it if it doesn't.
//    if (WEB.map === undefined) {

//        // Create a function to run once the google map script has loaded
//        WEB.loadMap = function() {

//            if (GBrowserIsCompatible()) {
//                WEB.map = new GMap2(document.getElementById("map"));
//                WEB.map.setCenter(new GLatLng(51.82138, -0.83825), 14);
//                WEB.map.addControl(new GSmallMapControl());

//                //Add marker
//                marker = new GMarker(new GLatLng(51.82063, -0.83809));
//                WEB.map.addOverlay(marker);

//            }

//            // Add the unload funciton to the body tag
//            $(window).unload(function() { GUnload(); });

//        };

//        // Load the google maps scripts and call the callback function (loadMap)
//        var script = document.createElement("script");
//        script.setAttribute("src", "http://maps.google.com/maps?file=api&v=2.x&key=ABQIAAAAuua8_0ah1Kq0N6FUYJSTsRRHHtzABalzoV7pfhNG42SvV8m6SBSvOC1YDgX73-5ijP220A3C_nqiRA&async=2&callback=WEB.loadMap");
//        script.setAttribute("type", "text/javascript");
//        document.documentElement.firstChild.appendChild(script);

//    }

}


var loginStatus = function () {

    /* Shows a login message on button click */

    var loginform = $('#loginform');
    var loginformStatus = $('#loginformStatus');

    loginform.hide();
    loginformStatus.html('Logging in...');
    loginformStatus.show();

    setTimeout(function () {

        $('#loginformStatus').html('Login failed. <a href="/account/password.aspx">Forgotten Password?</a>');
        setTimeout(function () {
            $('#loginformStatus').html('Logging in...');
            loginformStatus.hide(); 

            loginform.show();

        }, 3000);

    }, 2000);

};
