$(document).ready(function(event){
    $('#suitcase_pricing').supersleight();
    


    if (selection_season != '' || isIE6 == false){
        $current_story          = $('#vacation_story dl ol div[season='+selection_season+'] a:first-child').attr('href').replace('?story=','');
    } else {
        $current_story          = $('#vacation_story dl ol div[class=current] a:first-child').attr('href').replace('?story=','');
        $('#vacation_story dl ol div[class=current] a:first-child').click();
    }
    $promo_code             = $('input[name=promo_url]').val();
    
    var $resoPhone = '';
    if ( $('#promo_0').length ) $resoPhone = $('#promo_0').attr('resoPhone');
    if ( $resoPhone == '' ) $resoPhone = '1-800-419-4615';

    $('#reso_phone_display').html($resoPhone);
    $('#lodging_error_phone').html($resoPhone);
    $('#lodging_info_phone').html($resoPhone);
    
    if($('#story_values img[story='+$current_story+']').attr('url') != ''){            
        var $video      = $('#story_values img[story='+$current_story+']').attr('url');
        var $image      = $('#story_values img[story='+$current_story+']').attr('src');
        var $video_id   = $('#story_values img[story='+$current_story+']').attr('id');
        
        swfobject.embedSWF('/media/video_player.swf?video_path='+$video+'&image_path='+$image,$video_id,'300','225','8.0.0','/media/expressInstall.swf','',{'wmode':'transparent'},{'video_path':$video,'styleclass':'current'});
        
        $('#vacation_story #story_titles div[season='+selection_season+'] h1:first-child').addClass('current');
    } else {
        $('#vacation_story #story_titles div[season='+selection_season+'] h1:first-child').addClass('current');
        $('#vacation_story #story_values div[season='+selection_season+'] img:first-child').addClass('current');
    }
    
    $('#vacation_story dl ol a').click(function(event){
        if(!$(this).hasClass('current')){        
            $('#story_promo').is(':visible') ? $('#story_promo').hide() : '';
        
            $('#story_titles h1.current').removeClass();
            $('#story_values img.current').removeClass();
            $('#story_values object.current').removeClass();
            $('#vacation_story dl ol a.current').removeClass('current');
        
            $current_story  = $(this).attr('href').replace('?story=','');
            pageTracker._trackPageview('/vacation_planner/stories/'+$current_story);
            
            $('#story_titles h1[story='+$current_story+']').addClass('current');
            $(this).addClass('current');
            
            if($('#story_values img[story='+$current_story+']').attr('url') != ''){            
                var $video      = $('#story_values img[story='+$current_story+']').attr('url');
                var $image      = $('#story_values img[story='+$current_story+']').attr('src');
                var $video_id   = $('#story_values img[story='+$current_story+']').attr('id');
                
                swfobject.embedSWF('/media/video_player.swf?video_path='+$video+'&image_path='+$image,$video_id,'300','225','8.0.0','/media/expressInstall.swf','',{'wmode':'transparent'},{'video_path':$video});
            }
            
            $('#story_values #'+$current_story).addClass('current');
        }
        return false;
    });
    
    $('#vacation_story dl a#story_toggle').click(function(event){ coerce_stories(); return false; });



    /*  STORY FUNCTIONS
        ----------------------------------------------------------------------------------------*/
    
    var $story_list         = $('#vacation_story dl ol');
    var $story_toggle       = $('#vacation_story dl a#story_toggle');
    var $stories            = $('#vacation_story dl ol div.current a').length;
    var $story_list_y       = $story_list.position().top;

    var $story_promo        = $('#promo_0').attr(selection_season+'Message');


    function coerce_stories(event){
        $stories            = $('#vacation_story dl ol div.current a').length;
    
        $story_list_min     = 200 - ($stories * 40);
        $story_list_max     = 0;
    
        if($story_list_y    > $story_list_min){
            $story_list_y  -= 40;
            $story_list.animate({top:$story_list_y},300,function(event){ check_coercion(); });
        } else if($story_list_y < $story_list_max){
            $story_list_y  += 40;
            $story_list.animate({top:$story_list_y},300,function(event){ check_coercion(); });
        }
    }
    
    function check_coercion(event){  
        $stories            = $('#vacation_story dl ol div.current a').length;
    
        $story_list_min     = 200 - ($stories * 40);
     
        if($story_list_y   <= $story_list_min){
            !$story_toggle.hasClass('toggled') ? $story_toggle.addClass('toggled') : '';
        } else {
            $story_toggle.hasClass('toggled') ? $story_toggle.removeClass('toggled') : '';
        }
    }
    
    if($story_promo){
        $('#story_promo').html($story_promo).attr('season',selection_season);
        
        var promoTitle  = $('#promo_0').attr('PromotionTitle');
        var promoStr    = '<a id="promo_added" href="?story=promo">'+promoTitle+'</a>';
        
        $('#vacation_story dl ol div[season='+selection_season+']').prepend(promoStr);
        $('a#promo_added').click(function(event){
            if(!$(this).hasClass('current')){
                $('#story_titles h1.current').removeClass();
                $('#story_values img.current').removeClass();
                $('#vacation_story dl ol a.current').removeClass('current');
                
                $current_story  = $(this).attr('href').replace('?story=','');
                $('#story_promo').show();
                $(this).addClass('current');
            }
            return false;
        });
        
        $('#vacation_story dl ol a#promo_added').click();
        $('#story_promo').fade_in();
        
        $stories < 6 ? $story_toggle.addClass('inactive') : $story_toggle.removeClass('inactive');
    } else {
        $stories < 6 ? $story_toggle.addClass('inactive') : $story_toggle.removeClass('inactive');
    }

        

    
    /*  PLANNER FUNCTIONS
        ----------------------------------------------------------------------------------------*/ 
        
    $('[step]').click(function(event){
    
        if(!$(this).hasClass('active')){ 

            // only do this if you are changing areas
            trackSession();
        }
        
    });
    
    function trackSession() {
        $.ajax({
            type: "POST",
            url: "includes/index.ajax.php?function=trackSession",
            data: '',
            success: function(msg){}
        });
    }
    
     /*  CALENDAR FUNCTIONS
        ----------------------------------------------------------------------------------------*/
    
    $('a.backward').click(function(event){ prevMonth(); return false; });
    $('a.forward').click(function(event){ nextMonth(); return false; });
    
    $('#selection-nights').change(function(event){ 
        resetDateHighlighting();
        
        var newNights           = parseInt($('#selection-nights').val());
        selection_end_julian    = parseInt(selection_start_julian) + newNights; 
        selection_end_nice      = julian2nice(selection_end_julian);
        selection_end           = julian2date(selection_end_julian);

        calendarIsScrolling = false;
        checkMonth();

        // highlightDates();
        
        resetCalendarLabel();        
        highlightDates();
        showLodging();
        refreshPricingDivs();
    });

    $('#selection-start').change(function(event){ 
        resetDateHighlighting();
        if ( $('#selection-start').val() != '' ) {
            selection_start = $('#selection-start').val();
            calendarIsScrolling = false;
            checkMonth();
        }
    });
    
    $('#selection-end').change(function(event) { 
        resetDateHighlighting();
        if ( $('#selection-end').val() != '' ) {
            selection_end = $('#selection-end').val();
            calendarIsScrolling = false;
            checkMonth();
        }
    });
    
    $('#adults').change(function(event){ updateGuests(); });
    $('#children').change(function(event){ updateGuests(); });
    $('#infants').change(function(event){ updateGuests(); });
    
    
    /*  LODGING FUNCTIONS
        ----------------------------------------------------------------------------------------*/ 
    
    $('#vacation_lodging ol a')
        .click(function(event){
            if($(this).hasClass('available')){
                $('#vacation_lodging ol a.selected').removeClass('selected');
                $('#vacation_lodging dl div.current').removeClass('current');
                $(this).addClass('selected');
                $('#vacation_lodging dl div[lodge='+$(this).attr('href').replace('?lodge=','')+']').addClass('current');
                selectRoom($(this).attr('id').replace('ROOM_',''));
                getPricing($(this).attr('id').replace('ROOM_',''));
            }
            
            return false;
        })
        .mouseover(function(event){ 
            $('#vacation_lodging ol a').removeClass('current');              
            $('#vacation_lodging dl div').hide();
            $('#vacation_lodging dl div[lodge='+$(this).attr('href').replace('?lodge=','')+']').show();
        
            $(this).addClass('current');
        });
    
    
    $('#vacation_lodging dl div a[href*=daily_rate_details]')
        .click(function(event){
            var $room = $(this).parent().attr('id').replace('ROOM_','');
            
            getPricing($room);
            $('#vacation_lodging_details').fade_in();
            $('a.close_details').fade_in();
            
            return false;
        });
    
    
    $('a.close_details').click(function(event){  
        $('#vacation_lodging_details').fade_out();
        $(this).fade_out();
    });
    
    
    
    /*  DETAILS FUNCTIONS
        ----------------------------------------------------------------------------------------*/ 
    
    var guest_edit_category = '';
    
    $('form#select_details a.save').click(function(event){ 
        var $guest_id       = $('form#select_details [name=guest]').val();
        var $first_name     = $('form#select_details [name=first_name]').val();
        var $last_name      = $('form#select_details [name=last_name]').val();
        var $birthday       = $('form#select_details [name=birthday]').val();
        var $slope_level    = $('form#select_details [name=slope_level]').val();
        var $slope_lesson   = $('form#select_details [name=slope_lesson]').val();
    
        var $current_guest  = $('#vacation_details ol a#'+$('form#select_details [name=guest]').val());
        $current_guest.children('b').text($first_name); // show the first name in the button
        
        // save the values in the link's attributes
        $current_guest.attr('first_name',   $first_name);
        $current_guest.attr('last_name',    $last_name);
        $current_guest.attr('birthday',     $birthday);
        $current_guest.attr('slope_level',  $slope_level);
        $current_guest.attr('slope_lesson', $slope_lesson);
        
        $current_guest.removeClass('current empty');
        $('form#select_details').fade_out();

        var $category = $current_guest.attr('category');
        
        saveGuestDetails($guest_id, $first_name, $last_name, $birthday, $slope_level, $slope_lesson, $category);
    });
    
    $('form#select_details [name=slope_level]').change(function(event){ 
        var $slope_level = parseInt($('form#select_details [name=slope_level]').val());
        var $category = $('form#select_details [name=category]').val();
        if ( $category == 'adult' ) {
            switch ($slope_level) {
                case 1:
                case 2:
                    $('#vacation_details #lesson_choice_area').show();
                    $('#vacation_details #lesson_choice_alt').hide();
                    break;
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    $('#vacation_details #lesson_choice_area').hide();
                    $('#vacation_details #lesson_choice_alt').show();
                    break;
                default:
                    $('#vacation_details #lesson_choice_area').hide();
                    $('#vacation_details #lesson_choice_alt').hide();
                    break;
                    
            }
        }
        if ( guest_edit_category == 'child' ) {
            alert('child');
            checkChildLesson();
        }
        if ( guest_edit_category == 'infant' ) {
            alert('infant');
        }
    });
    
    $('form#select_details [name=birthday]').change(function(event){ 
        var bday = $('form#select_details [name=birthday]').val();
        var julian = julianDateFromString(bday);
        if ( julian.toString() == 'NaN' ) {
            $('form#select_details [name=birthday]').val('');
        } else {
            if  ( julian > 14245 ) julian -= 36525;  // in this case, dates can be before 1970
            var nice = julian2nice(julian);
            $('form#select_details [name=birthday]').val(nice);
        }
        checkChildLesson();
    });
    
    $('#birthday_help_key').click(function(event){
        $('#birthday_help_value').show();
    });
    $('#birthday_help_value').mouseout(function(event){
        $('#birthday_help_value').hide();
    });
    
    function checkChildLesson() {
        var $slope_level = $('form#select_details [name=slope_level]').val();
        if ( $slope_level == 0 || selection_start_julian == '' ) {
            $('#vacation_details #lesson_choice_area').hide();
        } else {
            var bday = $('form#select_details [name=birthday]').val();
            var julian = julianDateFromString(bday);
            if ( julian.toString() == 'NaN' ) {
                $('#vacation_details #lesson_choice_area').hide();
            } else {
                var age = ( selection_start_julian - julian ) / 365.25;
                if  ( age > 2 && age < 18 ) {
                    $('#vacation_details #lesson_choice_area').show();
                } else {
                    $('#vacation_details #lesson_choice_area').hide();
                }
            }
        }
    }
    
    /*  INCLUSIONS FUNCTIONS
        ----------------------------------------------------------------------------------------*/ 
    
    $('#suitcase_pricing ol a[href*=vacation_lodging_details]').click(function(event){ $('#vacation_lodging_details').fade_in(); $('a.close_details').fade_in(); return false; });


    $('#suitcase_inclusions div a[href*=inclusion]').click(function(event){
        var $current_inclusion  = $('#suitcase_inclusions div p[inclusion='+$(this).attr('href').replace('?inclusion=','')+']');
        
        pageTracker._trackPageview('/vacation_planner/inclusions/'+$current_inclusion.attr('inclusion'));
        
        if($current_inclusion.is(':visible')){
            $(this).removeClass('current');
            $current_inclusion.slideUp(function(event){ $(this).removeClass('current'); });
        } else if($current_inclusion.text() != ''){ 
            $(this).addClass('current');
            $current_inclusion.slideDown(function(event){ $(this).addClass('current'); });
        }
        
        return false;
    });
    
    $('#your_vacation_details_btn').click(function(event){
        makeGuestDetails();
        $('#your_vacation_details').toggle();
        return false;
    });
    
    $('#your_vacation_details').click(function(event){
        $('#your_vacation_details').toggle();
        return false;
    });
    
    


    /*  BOOK_NOW FUNCTIONS
        ----------------------------------------------------------------------------------------*/ 
    $('a.save_20').click(function(event){
        window.open($(this).attr('href'),'save_20','location=1,status=1,toolbar=1,scrollbars=1,width=800,height=600');
        
        return false;
    });
    
    $('a.book_now').click(function(event){
        saveToSession('book_now');
        trackSession();
        document.location = 'confirmation.php';
        
        return false;
    });


    $('#smuggs_alert_area').click(function(event){
        $('#smuggs_alert_area').hide();
    });

});