// jQuery function that fires on document ready
$(function() {
    var firstPage = true;
    
    // set onlick event for links
    // livequery used to work fine with loaded content
    $('a:not(a.singlePhoto, a.galleryPhoto)').livequery('click', function(event) {
        var link = $(this).attr('href');
        var suffix = link.substring(link.length - 3);
        if (link.indexOf('http://') != 0 && link.indexOf('mailto:') != 0 && jQuery.inArray(suffix, ['doc', 'pdf', 'tif', 'mp3']) < 0) {
            event.preventDefault();
            $('.hideOnReload').fadeOut(500);
            $.history.load(link);
        }
    });
    
    // set onsubmit event for search
    $('form#search').livequery('submit', function(event) {
        event.preventDefault();
        var link = $(this).attr("action") + '?mod=search&search=' + $('#searchInput').val();
        $('.hideOnReload').fadeOut(500);
        $.history.load(link);
    });
    
    // set onsubmit event for mailing list susbscription
    $('form#mailinglistSubscribeForm').livequery('submit', function(event) {
        event.preventDefault();
        var link = $(this).attr("action") + '&subscribe=1&email=' + $('#mailinglistSubscribeEmailInput').val();
        $('.hideOnReload').fadeOut(500);
        $.history.load(link);
    });
    
    // initialize history plugin
    $.history.init(function(link) {
        if (!firstPage || link != "") {
            scroll(0, 0);
            $.ajax({
                method: "get", url: "page.php", data: "link="+link,
                beforeSend: function() { $('body').append('<div id="lightbox-loading"><a href="#" id="lightbox-loading-link"><img src="./images/lightbox-ico-loading.gif"></a></div>'); },
                complete: function() { $('#lightbox-loading').remove(); },
                success: function(html) { //so, if data is retrieved, store it in html
                    $('#page').html(html); //show the html inside .content div
                    $('.hideOnReload').hide().fadeIn(500); //animation
                }
            });
        }
        firstPage = false;
    });

});

