/**
* Nigel Riches Photography v4.0
* http://www.nigelriches.co.uk
*
* 2010 Designed & developed by Jim Hill at Wiseguy Digital
* http://www.wiseguydigital.com
*
* nigelriches.v4.js
*/

function resize() {
	
    $("#main-section").css({
        width:($(window).width()),
        height:($(window).height())
    });
	
    // Make sure that there are always 3 images off-screen
    // so first work out the current offset. This width needs
    // to be precise to stop gaps appearing to the right.
    var remainder = $(window).width() % 200;
    $("#scrollarea").css({
        width:(($(window).width() - remainder) + 600)
    });
}

function initScrollThumbs() {
    // Thumbnails
    $("#scrollarea").offset({ top: 0, left: 0 });
    $("#scrollarea").draggable()
    .bind( "drag", function(event, ui) {
        var containerWidth = $("#main-section").width();
        var containerHeight = $("#main-section").height();
        var width = $("#scrollarea").width();
        var height = $("#scrollarea").height();
        var top = ui.position.top;
        var left = ui.position.left;

        // When we are at the top of the screen
        if (top > 0) {
            $(this).data('draggable').position.top = 0;
        }
		
        // When we reach the bottom of the screen
        var bottomX = parseInt(0-(height - containerHeight + 40)); // 40 is the padding
        if (top < bottomX) {
            $(this).data('draggable').position.top = bottomX;
        }
		
        // When we are at the furthest left pos
        if (left > 0) {
            $(this).data('draggable').position.left = 0;
        }
		
        // When we are at the furthest right pos
        var rightX = parseInt(0-(width - containerWidth));
        if (left < rightX) {
            $(this).data('draggable').position.left = rightX;
        }
    });
}

/*function disableScrollThumbs() {
    $("#scrollarea").draggable('disable')
}*/

function initColorbox(galleryname) {
    $("a[rel='" + galleryname + "']").colorbox({
        transition:"elastic",
        slideshow:true,
        slideshowSpeed:5000,
        opacity:.95,
        photo:true
    });
}

function loadGallery(hash) {
    var url = 'http://www.nigelriches.co.uk/ajax-listing.php?album=' + hash;
		url = 'http://' + window.location.host + '/ajax-listing.php?album=' + hash;
    $('#thumbnails').empty();
    $('#main-section').css({
        'background': 'url(images/ajax-loader.gif) no-repeat center center'
    });
    //disableScrollThumbs();
    $.getJSON(url, function(data) {
        $.each(data.items, function(i,item) {
            var li = $('<li/>').appendTo("#thumbnails");
            var link = $('<a>').attr({
                'href': 	item.largeImg,
                'title': 	item.title,
                'rel': 		'gallery' + hash
            }).appendTo(li);
            $("<img/>").attr("src", item.smallImg).appendTo(link);
        });
        $('#main-section').css({
            'background-image': 'none'
        });
        // Thumbs scrolling
        initScrollThumbs();
        // Colorbox
        initColorbox('gallery' + hash);
    });
}

function pageload(hash) {
    // hash doesn't contain the first # character.
    if (!hash) hash = '/10';
    if(hash) {
        // Change the navigation style
        $('#gallery-nav li.active').removeClass('active');
        $("a[href='#" + hash + "']").parent().addClass('active');
        // Hide the about-me and contact-me box if open
        $('#about-me:visible').hide('fast');
        $('#contact-me:visible').hide('fast');
        // Load in the images
        hash = hash.replace('/', '');
        // Load in image data using AJAX
        loadGallery(hash);
    }
}


function initDeeplinking() {
    // The callback is called at once by present location.hash.
    jQuery.history.init(pageload);
    // set onlick event for buttons
    $("a[rel='history']").click(function(){
        var hash = this.href;
        hash = hash.replace(/^.*#/, '');
        // Hide welcome box if open still
        $('#welcome:visible').hide('fast');
        // moves to a new page.
        // pageload is called at once.
        jQuery.history.load(hash);
        return false;
    });
}

function initExtras() {
    $("a[href='#/about']").click(function(e){
        $('#about-me').toggle('fast');
        $('#contact-me:visible').hide('fast');
        e.preventDefault();
    });
    $("a[href='#/contact']").click(function(e){
        $('#contact-me').toggle('fast');
        $('#about-me:visible').hide('fast');
        e.preventDefault();
    });

    // Make rel external links link externally
    $("a[rel*=external]").attr("target", "_blank");

    // Add the email addresses
    $("a[href='mailto:email-nigel']").attr('href', 'ma'+'ilto:nige'+'l@nige'+'lriches.co.uk').html('nige'+'l@nige'+'lriches.co.uk');
    $("a[href='mailto:email-agency']").attr('href', 'ma'+'ilto:kat' + 'e@sohomanag' + 'ement.co.uk').html('kat' + 'e@sohomanag' + 'ement.co.uk');
}

function initWelcome() {
    $.colorbox({
        width:"560px",
        inline:true,
        href:"#welcome",
        onClosed:function(){ $('#welcome').hide(); }
    });
}

function initFooter() {
    $('#footer').mouseover(function(e){
        $('#wiseguy-text').fadeIn('slow');
    })
    .mouseout(function(e){
        $('#wiseguy-text').fadeOut('slow');
    });
}

$(document).ready(function(){
	
    // Initial resize
    resize();
    // Deep linking
    initDeeplinking();
    // Extras
    initExtras();
    // Welcome box
    initWelcome();
    // Footer box
    initFooter();
    // Resize handler
    $(window).resize(function() {
        resize();
    });
	
})
