var explore_panel = function(window, controller, settings, cache) {
    /* Private properties */
    var _root_selector   = '#explore_panel_wrapper';
    var _root_el         = null;

    var _templates       = {};
    var _parser          = TrimPath.parseDOMTemplate;
    
    var _messenger       = window;
    var _settings        = settings;
    var _controller      = controller;
    var _cache           = cache;
    
    var _sid             = '';
    
    var _saved_state     = '';
    
    /* Private Methods */


    /* View methods */
    function _show_home(e, d) {
        _saved_state = '';
        
        var html = _templates['explore_list_box'].process({
            total      : 0,
            data       : [],
            pager      : '',
			hasCFilter : false
        });
        $(_root_el).html(html);

        return false;
    }

    function _show_bof_search(e, d) {
        _saved_state = '';
        
        var html = _templates['explore_list_box'].process({
            total      : 0,
            data       : [],
            pager      : '',
            hasCFilter : false
        });
        $(_root_el).html(html);

        return false;
    }

    function _show_loc_search(e, d) {
        _saved_state = '';
        
        var html = _templates['explore_list_box'].process({
            total      : 0,
            data       : [],
            pager      : '',
            hasCFilter : false
        });
        $(_root_el).html(html);

        return false;
    }

    function _show_fac_search(e, d) {
        _saved_state = '';
        
        var html = _templates['explore_list_box'].process({
            total      : 0,
            data       : [],
            pager      : '',
            hasCFilter : false
        });
        $(_root_el).html(html);

        return false;
    }

    function _show_fish_search(e, d) {
        _saved_state = '';
        
        var html = _templates['explore_list_box'].process({
            total      : 0,
            data       : [],
            pager      : '',
            hasCFilter : false
        });
        $(_root_el).html(html);

        return false;
    }

    function _show_results(e, d, q) {
        _saved_state = '';
        
        var html = _templates['explore_list_box'].process({
            total      : 0,
            data       : [],
            pager      : '',
            hasCFilter : false
        });
        $(_root_el).html(html);

        return false;
    }

    function _show_explore(e, d) {
        var pager  = _templates['explore_pager_box'].process({
            pager      : d.pager,
            page       : d.page.num
        });

        var html = _templates['explore_list_box'].process({
            total      : d.total,
            data       : d.page.data,
            pager      : pager,
            base_url   : _settings['base_url'],
            hasCFilter : controller.hasCampingFilter()
        });
        $(_root_el).html(html);

        return false;
    }


    /* Public Functions and Variables */
    return {
        /**
        * Init function
        *
        * @method init
        * @public
        */
        init: function() {
            _controller.setExplorePanel(this);
            
            /* Parse _templates */
            _templates['explore_list_box']     = _parser('explore_list_box_tpl');
            _templates['explore_pager_box']    = _parser('explore_pager_box_tpl');


            /* Initialize local vars */
            _sid      = settings.curr_state;
            _root_el  = $(_root_selector)[0];


            /* Register message responders */
            $(_messenger).bind('view::home',    _show_home);
            $(_messenger).bind('view::bof',     _show_bof_search);
            $(_messenger).bind('view::loc',     _show_loc_search);
            $(_messenger).bind('view::fac',     _show_fac_search);
            $(_messenger).bind('view::fish',    _show_fish_search);

            $(_messenger).bind('sp::search::received', _show_results);
            $(_messenger).bind('sp::explore::received', _show_explore);


            /* Local DOM responders */
            $('a.action', _root_el).
            livequery('click', function() {
                var q = $(this).attr('href').replace(/^\?/, '');

                _controller.dispatch(q);

                return false;
            });
            
            $('a.explore_pager_link', _root_el).
            livequery('click', function() {
                var page = $(this).attr('rel');

                $(_messenger).trigger('ep::explore::parameters::changed', [page]);

                return false;
            });
            
            $('a.external', _root_el).
            livequery('click', function() {
                var url = $(this).attr('href');

                window.open(url, 'external');

                return false;
            });
            
            $('a.explore_item_button', _root_el).
            livequery('click', function() {
                var url = $(this).attr('rel');
                url = url.split('/');

                var item = _cache.get(url[1]);

                return false;
            });
        },
        /**
        * 
        *
        * @method getState
        * @public
        */
        getState: function() {
            return _saved_state;
        }
    };
}(window, controller, settings, cache);

$(function() {
    explore_panel.init();
});

  