(function($){
    $.fn.TeaserWizard = 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.addSlide('register_slide');
        wizard.setSlideOptions('register_slide', {
           'action' : 'slides/register_page1',
           'width' : 500,
           'height' : 300,
           'onLoad' : registerSlideLoad
        });
        
        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 = [];

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

               wizard.setData('selected_trails', selected_trails);

               wizard.activate({
                  'sendTo' : 'register_slide'
               });
           });
       }

       function registerSlideLoad()
       {
            $('#wizard_register_page1 .next').click(function(){

              $('#wizard_register_page1 .error').html("");

              $.post(absoluteFilepath('actions/checkRegistration'),
              {
                  'email' : $('#wizard_register_page1 .email').val(),
                  'pass1' : $('#wizard_register_page1 .pass1').val(),
                  'pass2' : $('#wizard_register_page1 .pass2').val(),
                  'login' : true
              },
              function(results){
                 if(results == "success")
                 {
                     $.post(absoluteFilepath('actions/addSurveyTrails'),
                        {'selected_trails' : $.toJSON(wizard.getData('selected_trails')),
                         'deselected_trails' : $.toJSON([])
                        },
                        function(){
                            window.location = absoluteFilepath('user_trails');
                        }
                     );
                 }
                 else
                 {
                     $('#wizard_register_page1 .error').html(results);
                 }
              });
           });  
       }

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

    }
})(jQuery);



