﻿//File contains all code run onready of the master page

$(document).ready(function() {

    //Resize code based on http://www.shopdev.co.uk/blog/text-resizing-with-jquery/
    var originalFontSize = $('html').css('font-size');

    //Make text resize options visable as JS enabled

    // Set Font Size Cookie (Session Cookie destroyed when browser closed)
    var cookie_name = "textsize";
    if ($.cookie(cookie_name)) {
        var $getSize = $.cookie(cookie_name);
        $("html").css({ fontSize: $getSize + "px" });
    }

    // Reset Font Size
    $("#ResetFont").click(function(event) {
        event.preventDefault();
        $('html').css({ fontSize: originalFontSize });
        $.cookie(cookie_name, null, { path: '/' });
    });

    // Increase Font Size
    $("#IncreaseFont").click(function(event) {
        event.preventDefault();
        var currentFontSize = $('html').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum * 1.2;
        if (newFontSize < 30) {
            $('html').css({ fontSize: newFontSize });
            $.cookie(cookie_name, newFontSize, { path: '/' });
        }
        return false;
    });

    // Decrease Font Size
    $("#DecreaseFont").click(function(event) {
        event.preventDefault();
        var currentFontSize = $('html').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum * 0.8;
        if (newFontSize > 10) {
            $('html').css({ fontSize: newFontSize });
            $.cookie(cookie_name, newFontSize, { path: '/' });
        }
        return false;
    });

    //Add Hint Text to all text boxes
    $(function() {
        // find all the input elements with title attributes
        $('input[title!=""]').hint();
    });

    //Convert RadiusToSearch DropDown List to Nice looking 
    //ones from http://www.jankoatwarpspeed.com/post/2009/07/28/reinventing-drop-down-with-css-jquery.aspx

    $(function() {
        var source = $("#RadiusToSearch");
        var selected = source.find("option[selected]");  // get selected <option>
        var options = $("option", source);  // get all <option> elements

        // create <dl> and <dt> with selected value inside it
        $('#RadiusToSearch').after('<dl id="target" class="dropdown"></dl>')
        $("#target").append('<dt><a href="#">' + selected.text() +
        '<span class="value">' + selected.val() +
        '</span></a></dt>')
        $("#target").append('<dd><ul></ul></dd>')

        // iterate through all the <option> elements and create UL
        options.each(function() {
            $("#target dd ul").append('<li><a href="#">' +
            $(this).text() + '<span class="value">' +
            $(this).val() + '</span></a></li>');
        });
        source.hide();

    });

    $(".dropdown dt a").click(function() {
        $(".dropdown dd ul").toggle();
    });

    $(".dropdown dd ul li a").click(function() {
        var text = $(this).html();
        $(".dropdown dt a span").html(text);
        $(".dropdown dd ul").hide();
    });

    $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (!$clicked.parents().hasClass("dropdown"))
            $(".dropdown dd ul").hide();
    });

    $(".dropdown dd ul li a").click(function() {
        var text = $(this).html();
        $(".dropdown dt a").html(text);
        $(".dropdown dd ul").hide();
        var source = $("#RadiusToSearch");
        source.val($(this).find("span.value").html())
    });

});

