﻿function wireInlineSignupForm() {
    $("#tbl-signup input[title]").each(
        function() {
            var input = $(this);
            if (input.val() == '')
                input.val(input.attr('title'));

            input.focus(function() {
                if (input.val() == input.attr('title'))
                    input.val('');
            });

            input.blur(function() {
                if (input.val() === '') {
                    input.val(input.attr('title'));
                }
            });
        }
    )
}

(function ($) {
    $.fn.storyFlipper = function (options) {
        var rootObj = this;
        var opts = $.extend({}, $.fn.storyFlipper.defaults, options);
        var timerCookie = null;
        var current = 0;

        var buttons = $("#featured-box-nav > li > a", rootObj);

        //--check that we have images and buttons
        if (buttons.length == 0) {
            alert('No flipper buttons found');
            return;
        }

        $("#featured-box-nav a:last").addClass("last");
        
        //--show first image
        if (buttons.length > 0)
            changeStory($(buttons[current]), false);

        buttons.each(
            function() {
                var link = $(this);
                link.click(function() {
                    changeStory(link, true);
                    return false;
                }
                );
            }
        );

        function wireTimer() {
            //--start timer
            if (timerCookie != null)
                clearTimeout(timerCookie);

            timerCookie = setTimeout(nextStory, opts.wait);
        }

        function nextStory() {
            if (current < buttons.length - 1)
                current++;
            else
                current = 0;

            changeStory($(buttons[current]), false);
        }

        function changeStory(selectedLink, stopTimer) {
            var id = selectedLink.context.id.replace("flink-", "");
            $("#featured-box-nav a.current").toggleClass("current", false);
            selectedLink.addClass("current", true);

            $("#featured-box div:visible").hide("slow");
            
            $("#fpanel-" + id).show("slow");
            $("#fpanel-" + id + " div").show("slow"); /* Needed for extra div inside #fpanel-[id] */

            current = parseInt(id) - 1;

            if( !stopTimer )
                wireTimer();
            else
                clearTimeout(timerCookie);
        }
    };
    // private function for debugging
    function debug(line) {
        if (window.console && window.console.log)
            window.console.log(line);
    };
    $.fn.storyFlipper.defaults = {
        wait: 2000
    };
})(jQuery);

function wireFeaturedStories() {
    //--show first story
    var initialStory = $("#featured-box-nav a:first");
    $("#featured-box-nav a:last").addClass("last");

    if (initialStory.length > 0)
        changeStory($(initialStory[0]));

    $("#featured-box-nav a").each(
        function() {
            var link = $(this);
            link.click(function() {
                changeStory(link);
                return false;
            }
            );
        }
    );
}

function changeStory(selectedLink) {
    var id = selectedLink.context.id.replace("flink-", "");
    $("#featured-box-nav a.current").toggleClass("current", false);
    selectedLink.addClass("current", true);

    $("#featured-box div:visible").hide("fast");
    $("#fpanel-" + id).show("fast");
}

function wireFontSizer() {
    $("#lnkFontSmall").click(function() { changeFontSize(false); return false; });
    $("#lnkFontLarge").click(function() { changeFontSize(true); return false; });
    var currentSize = getFontSizeCookieValue();
    if( currentSize != null )
        $("body").css("font-size", currentSize + "%");
}

function changeFontSize(increase) {
    var size = $("body").css("font-size").replace("px", "");

    if (size.indexOf("%") >= 0)
        size = parseInt((size.replace("%", "") * 16) / 100);

    if (increase) {
        if (size < 16) {
            size++;
            var perc = parseFloat(size / 16) * 100;
            $("body").css("font-size", perc + "%");
            setFontSizeCookie(perc);
        }
        else
            setFontSizeCookie(null);
    }
    else {
        if (size > 12) {
            size--;
            var perc = parseFloat(size / 16) * 100;
            $("body").css("font-size", perc + "%");
            setFontSizeCookie(perc);
        }
        else
            setFontSizeCookie(null);
    }
}

function setFontSizeCookie(perc) {
    if (perc != null)
        $.cookie('font-size', perc, { path: '/', expires: 1 });
    else
        $.cookie('font-size', null);
}

function getFontSizeCookieValue() {
    var size = $.cookie('font-size');

    if (size != null && size.indexOf("%") >= 0)
        size = parseInt((size.replace("%", "") * 16) / 100);

    return size;
}

function gotoState(mapId) {
    var val = $('#' + mapId).val();
    if (val.length > 0)
        window.location = '/School-Choice/State/' + val + '.aspx';
}

function gotoStateEvent(page, mapId) {
    var val = $('#' + mapId).val();
    if (val.length > 0)
        window.location = page + '?state=' + val;
        //window.location = '/' + val + '.aspx';
}

function submitMailingForm() {
    var fname = $('#signup-firstname').val();
    var lname = $('#signup-lastname').val();
    var zip = $('#signup-zip').val();
    var email = $('#signup-email').val();

    if ($('#signup-firstname').attr('title') == fname)
        fname = '';

    if ($('#signup-lastname').attr('title') == lname)
        lname = '';

    if ($('#signup-zip').attr('title') == zip)
        zip = '';

    if ($('#signup-email').attr('title') == email)
        email = '';

    $.cookie('signup-firstname', fname, { path: '/', expires: 1 });
    $.cookie('signup-lastname', lname, { path: '/', expires: 1 });
    $.cookie('signup-zip', zip, { path: '/', expires: 1 });
    $.cookie('signup-email', email, { path: '/', expires: 1 });
    window.location = '/About-Us/Sign-Up-For-Updates.aspx';
}

function loadMailingFormDefaults() {
    var fname = $.cookie('signup-firstname');
    var lname = $.cookie('signup-lastname');
    var zip = $.cookie('signup-zip');
    var email = $.cookie('signup-email');

    if( fname == null )
        fname = ''

    if (lname == null)
        lname = ''

    if (zip == null)
        zip = ''

    if (email == null)
        email = ''
        
    $("*[id$='FirstName']").val(fname);
    $("*[id$='LastName']").val(lname);
    $("*[id$='Zip']").val(zip);
    $("*[id$='EmailInput']").val(email);
}

