$(document).ready(function() {
    center_image();
    $('#username').val("username");
    $('#password').val("password");
    
    $('#username').focus(function() { if ($('#username').val() == "username")$('#username').val("") });
    $('#username').blur(function() { if ($('#username').val() == "") $('#username').val("username"); });
    
    $('#password').focus(function() { if ($('#password').val() == "password") $('#password').val("") });
    $('#password').blur(function() { if ($('#password').val() == "")$('#password').val("password"); });
    
    
    $('#header_image').cycle({
        fx: 'fade',
        speed: 2000,
        timeout: 3000,
        pause: 1 
    });
    
    
    $('.gallery_display .thumbs a img').fadeTo(0, 0.5)
    $('.gallery_display .thumbs a img').hover(
        function() {
            $(this).fadeTo(300, 1.0)
        },
        function() {
            $(this).fadeTo(300, 0.5)
        }
    );


    $('.gallery_display .thumbs a').each(
        function() {
            var new_img = $(this).attr('href')
            var new_img_full = new_img.replace('/cache', '').replace('_display.', '.');
            $(this).removeAttr('href');
            $(this).click(
                function() {
                    $('.gallery_display .photo img').attr('src', new_img);
                    $('.gallery_display .photo a').attr('href', new_img_full);
                    center_image();
                }
            )
        }
    );

    var pos = $('.gallery_display .photo').position();
    function fix_photo() {
        var win_top = $(window).scrollTop();
        if (win_top > pos.top && $(window).height() > 600) {
            $('.gallery_display .photo').css({'position':'fixed', 'top':'0px'});
        }
        else {
            $('.gallery_display .photo').css({'position':'relative'});
        }
    }
    //$(window).scroll(fix_photo);
    //$(window).load(fix_photo);
    
    function showItineraries() {
        var i = 0;
        var heading = 0;
        var itinerary = null;
        var show_hide = null;
        $('.content').children().each(function() {
            i++;
            if ($(this).is('h3')) {
                heading++;
                show_itinerary = false;
                $(this).nextUntil('h3').each(function() {
                    if ($(this).hasClass('itinerary')) {
                        show_itinerary = true;
                    }
                });
                
                if (show_itinerary) {
                    itinerary = $('<div id="itinerary_box_'+heading+'" class="itinerary_box"><h2>Itinerary</h2></div>');
                    $(this).after(itinerary);
                    show_hide = $('<div class="itinerary_show_hide">Show itinerary</div>');
                    show_hide.click(function() {
                        $('.itinerary_box').each(function() {
                            $(this).hide();
                        });
                        $(this).next().show();
                    });
                    $(this).after(show_hide);
                }
            }
            else {
                if (itinerary && $(this).is('p') && $(this).attr('class') == 'itinerary') {
                    itinerary.append($(this).html()+'<br />');
                }
            }
        });
    }
    
    
    showItineraries();
});

function center_image() {
    // Image centering
    var width_available = 600;
    var height_available = 450;

    var width_img = $('.gallery_display .photo img').width();
    var left_offset = ((width_available - width_img) / 2);
    $('.gallery_display .photo img').css('left', left_offset+'px');
    var height_img = $('.gallery_display .photo img').height();
    var top_offset = ((height_available - height_img) / 2);
    $('.gallery_display .photo img').css('top', top_offset+'px');

    $('.gallery_display .photo img').load(function () {
        var width_img = $('.gallery_display .photo img').width();
        var left_offset = ((width_available - width_img) / 2);
        $('.gallery_display .photo img').css('left', left_offset+'px');
        var height_img = $('.gallery_display .photo img').height();
        var top_offset = ((height_available - height_img) / 2);
        $('.gallery_display .photo img').css('top', top_offset+'px');
    });
}

