(function($){
    $.fn.ChooseTrailWizard = function(map_trails)
    {
        var wizard = new wizardMaker(this);

        wizard.addSlide('trail_choose');
        wizard.setInitialSlide('trail_choose');
        wizard.setSlideOptions('trail_choose', {
            'action' : 'slides/trail_choose',
            'width' : 950,
            'height' : 620,
            'onLoad' : trailChooseLoad
        });
        
        wizard.setClose(onChooseClose);

/*********************************LOADS*******************************************************/

       function trailChooseLoad()
       {
            var map = new GMap2($('#wizard_trail_choose .map').get(0));
            map.setCenter(new GLatLng(39.8093938794331, -121.058808928102));
            map.setZoom(9);
            map.setUIToDefault();

	//this sets max/min zoom levels
			var minMapScale = 9; 
			var maxMapScale = 16; 
			// get array of map types 
			var mapTypes = map.getMapTypes(); 
			// overwrite the getMinimumResolution() and getMaximumResolution() methods for each map type 
			for (var i=0; i<mapTypes.length; i++) { 
			mapTypes[i].getMinimumResolution = function() {return minMapScale;} 
			mapTypes[i].getMaximumResolution = function() {return maxMapScale;} 
			}
	//end max/min zoom levels
	
            map_trails.each(function(index, trail){
                trail.addToMap(map);
            });

            $.post(absoluteFilepath('actions/GetMyTrails'),
                {},
                function(results){
                   $.each(results, function(key, item){
                       map_trails.getTrail(item['id']).setOn();
                   });
                },
                'json');

           $('#wizard_trail_choose .next').click(function(){

                   //Post which trails user selected
               var selected_trails = [];
               var deselected_trails = [];

               $('#wizard_trail_choose .trail_list .selected .trail').each(function(){
                   selected_trails.push(parseInt($(this).attr('trail_id')));
               });

               $('#wizard_trail_choose .trail_list .deselected .trail').each(function(){
                   deselected_trails.push(parseInt($(this).attr('trail_id')));
               });

               $.post(absoluteFilepath('actions/addSurveyTrails'),
                   {'selected_trails' : $.toJSON(selected_trails),
                    'deselected_trails' : $.toJSON(deselected_trails)
                   },
                   function(){
                       $.post(absoluteFilepath('user_trails/trails_subview'),
                       {},
                       function(results){
                           $('#user_trails .trail_list').html(results);
                           add_trail_listeners();
                       });
                   }       
               );

               $('#fancy_close').click();
           });
       }

       function onChooseClose()
       {
            map_trails.each(function(index, map_trail)
            {
                map_trail.setOff();
            });
       }
      
/*********************************FUNCTIONS*******************************************************/

    }
})(jQuery);



