﻿
//This class manages aspects of user search interaction
function BreadCrumbManager(isSearching, section, searchToken)
{
    var __self = this;
    var _sections = {Advocates:0,Ballots:1,Users:2}; 
    var _meta = {
        Section: null,
        LastSeenUrl: null,
        TargetUrlPart: null
    };

    function GetCookie()
    {
        eval("var _c_ = " + $.cookie('bread_manager'));
        if (_c_ != null) {
            _meta = _c_;
        } return _c_;
    }
    function SetCookie()//to meta
    {
        $.cookie('bread_manager', null);
        $.cookie('bread_manager', $.toJSON(_meta), { path: '/' });
    }
    function MetaChanged()
    {
        //write to cookie...
        SetCookie();

    }

    //ctor:
    if (GetCookie() == null)
    {
        SetCookie();
    }
    $(document).ready(function() {
        //when the page loads bind to the click of every 'breadCrumbButton'
        //a check that will record the current url of the page when clicked
    $('.breadCrumbButton').click(function() {
            var href = $(this).attr('href');
            if (searchToken != null) {
                _meta.LastSeenUrl = window.location.pathname + "?searchToken=" + searchToken;
            } else {
                _meta.LastSeenUrl = window.location.toString()
            }
            _meta.TargetUrlPart = href.split('advocates/')[href.split('advocates/').length - 1].replace('/', '');
            MetaChanged();
        });
        //if the last seen url was defined - they clicked on a bread crumb button at somepoint.
        if (_meta.LastSeenUrl != null) {
            //change the breadcrumb history button to point to where they just were...
            //unless they have navigated away from that page target (url part)
            if (window.location.toString().indexOf(_meta.TargetUrlPart) > -1) {
                //same item
                $('.breadCrumbBackButton').attr('href', _meta.LastSeenUrl);
            } else {
                _meta = {};
                MetaChanged();
            }
        }
    });

}

