﻿var JLROTHER = (function () {

    var streamCookie = 'jlr_stream',
		showDelay = 1500; // ms delay before stream bar is shown

    // Quirksmode.com cookie functions http://www.quirksmode.org/js/cookies.html
    function createCookie(name, value, days) {
        var date = new Date(),
			expires;
        if (days) {
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toGMTString();
        } else {
            expires = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=",
			ca = document.cookie.split(';'),
			c,
			i;
        for (i = 0; i < ca.length; i++) {
            c = ca[i];
            while (c.charAt(0) === ' ') {
                c = c.substring(1, c.length);
            }
            if (c.indexOf(nameEQ) === 0) {
                return c.substring(nameEQ.length, c.length);
            }
        }
        return null;
    }

    function eraseCookie(name) {
        createCookie(name, "", -1);
    }
    // end of Quirksmode.com cookie functions



    function updateStreamBar(newStream) {
        // update current anchor in stream bar
        $('#stream-selector-bar p a').removeClass('current');
        $('#stream-selector-bar p a[rel=' + newStream + ']').addClass('current');
    }



    function showStreamBar() {
        var pattern = /\/Career-Opportunities\//,
            allowClickthrough = location.href.match(pattern);

        $('#stream-selector-bar').animate({ 'top': '0' }, 500);
        $('#stream-selector-bar a').click(function (e) {
            // only allow the clickthrough on /Career-Opportunities/ pages
            if (allowClickthrough === null) {
                e.preventDefault();
                updateStreamBar(e.target.rel);
                createCookie(streamCookie, e.target.rel, 3650);
            } else {
                e.preventDefault();
                updateStreamBar(e.target.rel);
                createCookie(streamCookie, e.target.rel, 3650);
                console.log(e.target.rel);
                switch (e.target.rel) {
                    case 'apprentices':
                        window.location.href = '/Career-Opportunities/Apprentices/Default.aspx';
                        break;
                    case 'graduates':
                        window.location.href = '/Career-Opportunities/Graduates/Default.aspx';
                        break;
                    case 'experienced':
                        window.location.href = '/Career-Opportunities/ExperiencedHires/Default.aspx';
                        break;
                    default:
                        window.location.href = '/Career-Opportunities/Default.aspx';
                        break;
                }
            }
        });
    }



    function init() {
        var userStream = readCookie(streamCookie);

        if (userStream === null) {
            userStream = 'default';
        }
        updateStreamBar(userStream);
        setTimeout(showStreamBar, showDelay);
    }


    return {
        init: init
    };

} ());









$(document).ready(function () {

    JLROTHER.init();

});

