﻿$(document).ready(function() {

    //this is to compensate for the top bar.
    $('a[href^=#]').slideto({
    speed: 10
    });

    /*var scrollPosition = getCookie('autoScroll_scrollPosition');
    var rememberScroll = getCookie('autoScroll_rememberScroll');
    
    if (scrollPosition && rememberScroll) {
        $(window).scrollTop(scrollPosition);
        deleteCookie('autoScroll_scrollPosition');
    }*/

    var expandableListItems = $(".expandable li");

    if (expandableListItems.length > 0) {
        SetupExpandableListItems(expandableListItems);
    }

    var azFilter = $("ul#atoz li a");

    if (azFilter.length > 0) {
        SetupAtoZFilter(azFilter);
    }

    if ($("label.useLabel").length > 0) {
        SetupInputsWithLabelText("useLabel");
    }
    $("#ws").hide();
    $("#css").hide();
    if ($("#tabbedResults").length > 0) {
        SetUpTabs();
    }
    SetupTextResizer();

    printLink();
    if ($("#map").length > 0) {
        generateMap();
    }

    $('.jqAutoScroll').click(function() {
        setCookie('autoScroll_rememberScroll', true);
        return true;
    });
});

    function newWindow() {
        $("a.newWindow").click(function() {
            var url = $(this).attr("href");
            window.open(url, 'win', '');
            return false;
        });
    }


function printLink() {
    $("h1").append("<span class='print'>Print this page</span>");
    $("h1 span.print").click(function() {
        window.print();
    });
}

function SetUpTabs()
{
    $(".tabContent").hide(); //Hide all of the tabs content

    var websiteToDirectoryLink = '<p class="webDirSwitch webToDir">' +
                                    'You can also <a href="#">Look for Local Organisations ' +
                                    'associated with <strong>"[term]"</strong> in our Care Services Directory</a>' +
                                 '</p>';

    var directoryToWebsiteLink = '<p class="webDirSwitch dirToWeb">' +
                                    'You can also <a href="#">Look for all website pages associated with <strong>"[term]"</strong></a>' +
                                 '</p>';

    $(".tabContent:eq(0) .resultsHeader").append(websiteToDirectoryLink);
    $(".tabContent .resultsHeader p.webToDir > a").click(function()
    {
        setCookie('search-tab', 1);
        setClass(1);
        return false;
    });

    $(".tabContent:eq(1) .resultsHeader").append(directoryToWebsiteLink);
    $(".tabContent .resultsHeader p.dirToWeb > a").click(function()
    {
        setCookie('search-tab', 0);
        setClass(0);
        return false;
    });

    var searchTerm = $('.resultsHeader:first > h2 > strong').text();
    $('.tabContent .resultsHeader p.webDirSwitch > a > strong').text('"' + searchTerm + '"');

    var tab = parseInt(getCookie('search-tab'));

    if (tab)
    {
        setClass(tab);
    }
    else
    {
        setClass(0);
    }
}

function setClass(num)
{
    $(".tabContent").hide();
    $(".tabContent").eq(num).show(); // Show the correct content
}

function SetupExpandableListItems(expandableItems)
{
   var firstChild = expandableItems.children(":first-child");
   var secondChild = firstChild.next();
   var link = $('<a title="Select to expand answer" href="#"/>');
   secondChild.hide();
      
   firstChild.wrapInner(link).click(function(){ExpandListClick(this,"show");return false;});
   
}

function ExpandListClick(item,status) {
    $(item).unbind();
    if(status == "show")
    {
        $(item).next().slideDown("slow");
        $(item).children(":first-child").attr("title","Select to collapse answer");
        $(item).click(function(){ExpandListClick(item,"hide");return false;});
    }
    else {
        $(item).next().slideUp("slow");
        $(item).children(":first-child").attr("title","Select to expand answer");
        $(item).click(function(){ExpandListClick(item,"show");return false;});
    }

}

function SetupAtoZFilter(filter) {
    filter.click(function() { FilteredTermsStatusChanged(this); });

}

function FilteredTermsStatusChanged(termSelected) {
    var listId = termSelected.href.substring(termSelected.href.indexOf('#'));

    $('.expandable').parent().hide();
    $(listId).parent().show("slow");
}

function SetupInputsWithLabelText(className)
{
    var labelList = $("label." + className);
    for (i = 0; i < labelList.length; i++) {
        $(labelList[i]).addClass('hide');
        var inputId = $(labelList[i]).attr("for");
        var currentInput = $("#" + inputId);
        var labelVal = $(labelList[i]).text();
        currentInput.attr("value", labelVal)
        currentInput.data("valText", labelVal);
        currentInput.focus(function() {
            if ($(this).data("valText") == $(this).attr("value")) {
                $(this).val("");
            }
        });
        currentInput.blur(function() {
            if ($(this).attr("value") == "") {
                $(this).attr("value", $(this).data("valText"));
            }
        });
    }
       
}

function SetupTextResizer() {
    var trigger = $('#textResize li a');

    if (cookie == "medium" || cookie == "large") {
        $('#textResize .' + cookie).addClass('current');
    } else {
        $('#textResize a:first').addClass('current');
    }
    
    trigger.click(function(e) {
        e.preventDefault();
        $('#textResize a').removeClass('current');
        var size = $(this).find('img').attr('alt');
        setActiveStyleSheet(size);
        $(this).addClass('current');
    });
}

/*function SetupTextResizer() {
    $("#textResize li a").click(function() {
        $("#textResize a").removeClass("current");
        var request = location.href + $(this).attr("href");
        request = request.replace(".aspx", "/AjaxResizer.aspx");
        var params = request.match(/\?/g);
        if (params.length > 1) {
            request = request.replace("?size", "&size");
        }
        $.ajax({
            url: request,
            cache: false,
            success: function(html) {
                textResizing(html);
            }
        });
        $(this).addClass("current");
        return false;
    });
}*/

/*function textResizing(linkVal) {
    $("link[title='textResizer']").remove();
    $("head").append(linkVal);  
} */    

/*  Google Maps Stuff */
var map;
var directionsPanel;
var gdir;

google.load("maps", "2");
function generateMap() {
    // get lat and long variables from #map class attribute
    var locValues = $("#map").attr("className");
    var locArray = locValues.split(",");
    var fromLng = locArray[0];
    var fromLat = locArray[1];
    var toLng = locArray[2];
    var toLat = locArray[3];

    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.setCenter(new GLatLng(toLat, toLng), 13);
        map.addControl(new GMapTypeControl());
        map.addControl(new GOverviewMapControl());
        map.enableDoubleClickZoom();

        if (fromLat == "") {
            var toPoint = new GLatLng(toLat, toLng);
            map.addOverlay(new GMarker(toPoint));
        } else {
            directionsPanel = document.getElementById("directions");
            gdir = new GDirections(map, directionsPanel);
            gdir.load("from: "+fromLat+", "+ fromLng + " to: "+toLat+", "+ toLng);
        }

        window.unload = GUnload;
    }
}

/*  GUP  */
function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function getCookie(name)
{
    name = escape($.trim(name));

    var cookies = document.cookie.split(';');

    for (var i = 0; i < cookies.length; i++)
    {
        var cookieCrumbs = cookies[i].split('=');
        var cookieName = $.trim(cookieCrumbs[0]);
        var cookieValue = cookieCrumbs[1];

        if (cookieName == name)
        {
            return unescape(cookieValue);
        }
    }

    return false;
}

function setCookie(name, value)
{
    document.cookie = escape($.trim(name)) + '=' + escape(value) + ';path=/';
}

function deleteCookie(name)
{
    /*document.cookie = escape($.trim(name)) + '=;expires=Thu, 01-Jan-70 00:00:01 GMT;path=/';*/
    document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}

$(window).scroll(function()
{
    document.cookie = "autoScroll_scrollPosition=" + $(this).scrollTop();
});
