$.jdvars = {}
$.jdvars.thumbnailOffset = 0;

$(document).ready(function(){
    $('#menu a.category').click(function(){
        $('.categoryPages').hide('selected');
        $(this).parent().find('.categoryPages').toggle('fast');
        $(this).parent().addClass('selected');
        sizeWindow();
        return false;
    });

    if (window.location.hash) {
        var new_img_id = window.location.hash.slice(1)
        initializePhotos(new_img_id);
    } else {
        var new_img_id = $("#pagePhotos li:first .thumb").next('.theImage').attr('id');
        if (new_img_id) {
            initializePhotos(new_img_id);
        };
    }

    sizeWindow();

    $(window).resize(function() {
      sizeWindow();
    });

    $("#pagePhotos li .thumb").click(function() {
        var new_img_id = $(this).next('.theImage').attr('id');
        fadeAndSwapPhoto(new_img_id);
    });

    $('#prevImage').click(function() {
        prevPhoto();
        return false;
    });

    $('#nextImage').click(function() {
        nextPhoto();
        return false;
    });

    $('.noDragClick').bind("contextmenu",function(e){
        return false;
    });
    $('.noDragClick').bind("mousedown",function(e){
        return false;
    });

    $('#thumbnailToggle').click(function() {
        $('#pagePhotos ul').slideToggle('fast');
        
        if ($.jdvars.thumbnailOffset==0) {
            $.jdvars.thumbnailOffset = 40;
        } else {
            $.jdvars.thumbnailOffset = 0;
        };
        sizeWindow();
        
        $('#thumbnailPreviews').jcarousel({
            scroll: 1,
            initCallback: thumbnails_initCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null
        });
        return false;
    });

    $(document).bind("keydown", function(e) {
        if (e.keyCode==37) {
            prevPhoto();
            return false;
        } else if (e.keyCode==39) {
            nextPhoto();
            return false;
        };
    });

});


var prevPhoto = function() {
    var new_img_id = $('#pagePhotos li img.active').parent().prev().find('.theImage').attr('id');
    if (new_img_id) {
        fadeAndSwapPhoto(new_img_id);
    } else {
        var new_img_id = $('#pagePhotos li:last').find('.theImage').attr('id');
        fadeAndSwapPhoto(new_img_id);
    }
    return false;
}


var nextPhoto = function() {
    var new_img_id = $('#pagePhotos li img.active').parent().next().find('.theImage').attr('id');
    if (new_img_id) {
        fadeAndSwapPhoto(new_img_id);
    } else {
        var new_img_id = $('#pagePhotos li:first').find('.theImage').attr('id');
        fadeAndSwapPhoto(new_img_id);
    }
    return false;
}


function thumbnails_initCallback(carousel) {
    $('#thumbnailPreviews li img.thumb').click(function() {
        carousel.scroll($.jcarousel.intval($(this).attr('img_num')));
        return false;
    });

    $('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = $.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    $('#nextImage').click(function() {
        carousel.next();
        return false;
    });

    $('#activeImage').click(function() {
        carousel.next();
        return false;
    });

    $('#prevImage').click(function() {
        carousel.prev();
        return false;
    });

    $(document).bind("keydown", function(e) {
        if (e.keyCode==37) {
            carousel.prev();
            return false;
        } else if (e.keyCode==39) {
            carousel.next();
            return false;
        };
    });
};



var fadeAndSwapPhoto = function(new_img_id, fadeTime) {
    if (fadeTime=='undefined') {
        var fadeTime = 200;
    }
    $('#activeImage').fadeOut(200, function() {
        swapPhoto(new_img_id);
    });
    $('#activeImage').fadeIn(200);
    $('#imageInfos').fadeOut(200, function() {
        swapCaption(new_img_id);
    });
    $('#imageInfos').fadeIn(200);

    window.location.hash = '#'+new_img_id;
}

var sizeWindow = function() {
    var win_height = $(window).height();
    var win_width = $(window).width();

    if (win_height>440) {
        if (win_height>720) {
            var container_ht = 680;
        } else {
            var container_ht = win_height - 40;
        }
    } else {
        var container_ht = 400;
    }
    var container_ht_index = win_height - 40;

    if (win_width>600) {
        if (win_width>1600) { 
            var content_wt = 1400;
        } else {
            var content_wt = win_width - 220;
        }
    } else {
        var content_wt = 400;
    }

    if ($('#activeImage img').width()>content_wt) {
        content_wt = $('#activeImage img').width();
    }

    var content_ht = container_ht - 40 - $.jdvars.thumbnailOffset;
    $('#container').css('height', container_ht+'px');
    $('#container #pageContent').css('height', content_ht+'px');
    $('#container #pageContent').css('width', content_wt+'px');

    if ($('#activeImage .infoPageContent').length == 0) {
        $('#container #pageContent #imageNavigation').css('top', content_ht+7+'px');
    
        var num_thumbs = $('#thumbnailPreviews li').size();
        var thumbs_width = 42*num_thumbs;
        if (thumbs_width>content_wt) {
            thumbs_width = content_wt;
        };
        $('#container #pageContent #pagePhotos').css('top', content_ht+30+'px');
        $('#container #pageContent #pagePhotos').css('width', thumbs_width+'px');
        $('#container #pageContent #pagePhotos').css('margin-left', '-'+thumbs_width/2+'px');
    
        var imgHeight = $('#activeImage img').height()/2;
        var imgWidth = $('#activeImage img').width()/2;
        var divHeight = content_ht/2;
        var divWidth = content_wt/2;
        
        $('#activeImage img').css(
            {
                'top': divHeight - imgHeight,
                'left': divWidth - imgWidth
            }
        );
    
        // move caption to be 20px above menu
        var top_menu = $('#navigation #menu').height() + 50;
        $('#navigation #imageInfos').css('bottom', top_menu);

    } else {
        $('#activeImage .infoPageContent').css('max-height', content_ht+'px');
        $('#activeImage .infoPageContent').css('max-width', content_wt+'px');

        if (content_wt>800) {
            content_wt = 800;
        }
        $('#activeImage').show();
        // adjust info page size
        
        $('#activeImage').css('width', content_wt-100+'px');
        $('#activeImage').css('margin-right', '0px');
        var txt_wt = $('#activeImage .infoPageContent').textWidth();
        var txt_ht = $('#activeImage .infoPageContent').height();
        if (txt_wt<content_wt) {
            $('#activeImage').css('width', txt_wt+'px');
        }
        $('#activeImage').css('height', txt_ht+'px');
        $('#activeImage').css('left', '50%');
        $('#activeImage').css('top', '50%');
        $('#activeImage').css('margin-left', '-'+txt_wt/2+'px');
        $('#activeImage').css('margin-top', '-'+txt_ht/2+'px');
    };
};

var initializePhotos = function(new_img_id) {
    swapPhoto(new_img_id);
    swapCaption(new_img_id);
    $('#activeImage img').load(function() {
        $('#activeImage').fadeIn(500);
        sizeWindow();
    });
    $('#prevImage').removeClass('prevImageActive');
};


var swapPhoto = function(new_img_id) {
    var new_img = $('#'+new_img_id);

    var img_id = $('#thumbnailPreviews li').index(new_img.closest('li')) + 1;
    $('#currentImageNumber span').html(img_id);
    $('#id_current_image').val(img_id);

    $('#activeImage').html(new_img.html());
    $('#pagePhotos li img').removeClass('active');
    $('#pagePhotos li img.'+new_img_id).addClass('active');

    if (new_img.parent().prev().length == 0) {
        $('#prevImage').removeClass('prevImageActive');
    } else {
        $('#prevImage').addClass('prevImageActive');
    };

    if (new_img.parent().next().length == 0) {
        $('#nextImage').removeClass('nextImageActive');
    } else {
        $('#nextImage').addClass('nextImageActive');
    };
    sizeWindow();

    $('#activeImage img').mouseover(function() {
        $('#menu').animate({
            opacity: 0.25
          }, 200);
    });

    $('#activeImage img').mouseout(function() {
        $('#menu').animate({
            opacity: 1
          }, 200);
    });
    $('#activeImage img').bind("contextmenu", function(e) {
        return false;
    });
    $('#activeImage img').bind("mousedown", function(e) {
        nextPhoto();
        return false;
    });
};

var swapCaption = function(new_img_id) {
    var new_img = $('#'+new_img_id);
    $('#extraText').html(new_img.find('.imageCaption').html());
};

/**
 * jQuery.fullBg
 * Version 1.0
 * Copyright (c) 2010 c.bavota - http://bavotasan.com
 * Dual licensed under MIT and GPL.
 * Date: 02/23/2010
**/
(function($) {
    $.fn.fullBg = function(){
        var bgImg = $(this);        
 
        function resizeImg() {
        var imgwidth = bgImg.width();
        var imgheight = bgImg.height();
        
        var winwidth = $(window).width();
        var winheight = $(window).height();
        
        var widthratio = winwidth / imgwidth;
        var heightratio = winheight / imgheight;
        
        var widthdiff = heightratio * imgwidth;
        var heightdiff = widthratio * imgheight;
        
        if(heightdiff > winheight) {
            bgImg.css({
                width: winwidth+'px',
                height: heightdiff+'px'
            });
        } else {
            bgImg.css({
                width: widthdiff+'px',
                height: winheight+'px'
            });        
        };

    } 
    resizeImg();
    $(window).resize(function() {
        resizeImg();
    }); 
  };
})(jQuery)


$.fn.textWidth = function(){
    var sensor = $('<div />').css({margin: 0, padding: 0});
    $(this).append(sensor);
    var width = sensor.width();
    sensor.remove();
    return width;
};
