$(document).ready(function() {

$('#content').hide();

//jquery address (patched not to remove the last '/')
$.address.state('/');
$.address.init(function(event) {

if (window.location.hash == "") {
    $.address.value(window.location.pathname);
}

// Initializes plugin support for links
$('a:not([href^=mailto]):not([href^=http])').address();
            }).change(function(event) {
                // Identifies the page selection
                var page = event.parameters.page ? '/?page=' + event.parameters.page : event.path;
                $('#content').hide();
                var handler = function(data) {
                    $('#content').html($('#content', data).html());
                    $.address.title(/>([^<]*)<\/title/.exec(data)[1]);
                    $('body').attr('class', 'home');
                    if(page.indexOf('projects') >= 0) {
                        $('body').attr('class', 'projects');
                        $(window).trigger('resize');
                    }
                    return false;
                };

                // Loads the page content and inserts it into the content area
                $.ajax({
                    url: page,
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        handler(XMLHttpRequest.responseText);
                    },
                    success: function(data, textStatus, XMLHttpRequest) {
                        handler(data.replace(/<script(.|\s)*?\/script>/gi, ''));
                        runstuff();
                        $('#content').fadeIn('slow');
                    }
                });
                
   });
   
   function runstuff() {
//run stuff when jquery address finsish...
$.ajax({
  url: '/gallery-html',
  success: function(data) {
    $('#hello').html(data);

    $(window).trigger('resize');
    $('#content').fadeIn('slow');
  }
});

$('#loading').hide();

$("#loading").ajaxStart(function(){
   $(this).show();
 });

 $("#loading").ajaxStop(function(){
    $(this).hide();
  });

if($('.slideshow_item').length > 1) {
    $('#slideshow').cycle({ 
        fx:     'scrollHorz', 
        speed: 520, 
        prev:   '#prev', 
        next:   '#next',
        after: onAfter,
        timeout: 0
    }).touchwipe({
        wipeLeft: function() {
            $("#slideshow").cycle("next");
        },
        wipeRight: function() {
            $("#slideshow").cycle("prev");
        }
    });
} else {
    onAfter(null, null, {currSlide: 0, slideCount: 1});
    $('#content .nav').addClass('disabled');
}

$('.box img').lazyload({
             placeholder : '/i/grey.gif',
             effect      : 'fadeIn'
});

}

function resize() {
    var aspect_ratio = 4/3,
        max_height = $(window).height() - height_offset,
        max_width = $(window).width() - width_offset,
        height = max_height,
        width;

        if(height > 960) {
            height = 960;
        }

        width = height * aspect_ratio;

        if(width > max_width) {
            width = max_width;
            height = width / aspect_ratio;
        }


        var per_row = parseInt((max_width + 15) / 259, 10);
        $("#mnav").width(function() {
            if($("body").hasClass("projects")) {
                return width - parseInt($(this).css('left'), 10);
            }
            return per_row * 259 - 115;
        });
        
        $("#slideshow").height(height);
        $(".slideshow_item").height(height).width(width);
        
        
        if ($('body').hasClass('home')) {
            $(".pgwrap").width('100%');
        }
        else {
            $(".pgwrap").width(width);
        }
        
                
/*
        $('.pgbody .gallery').removeClass('end-row').removeClass('first-row');
        var per_row = parseInt((width + 15) / 259, 10);
        $('.pgbody .gallery:nth-child('+per_row+'n)').addClass('end-row');
        $('.pgbody .gallery:nth-child('+per_row+'n+1)').addClass('first-row');
*/
}


function hideHelp() {
    if ($('.pgwrap').width() < 750) {
        $('#help').hide();
    } 
    else {
        $('#help').show();
    }
};


    
function onAfter(curr, next, opts) {
    var index = opts.currSlide,
        caption = '' + (opts.currSlide + 1) + ' / ' + opts.slideCount;
    $('#caption').html(caption);
    if($('body').hasClass('projects')) {
        resize();
    }
}

function navigateGallery(direction) {
    $('.gallery_item > div').each(function(index) {
        if($('.slideshow_item').length > 1)
            $('#prev,#next', this)[direction]().click();
    });
}
// Keyboard control for Cycle (left and right keys)
$(window).keyup(function(event) {
    var direction = null;
    if (event.keyCode == 37) {
        direction = 'prev';
    } else if (event.keyCode == 39) {
        direction = 'next';
    } else if(event.keyCode == 27) {
        $('.slideshow_item a').first().click();
    }
    if(direction) {
        navigateGallery(direction);
    }
    return false;
});

resize();
$(window).resize(resize);
$('body').bind('turn', resize);

});

