var loaded = [], loading = false;
var url = false;

function fireBackground(tmb, href) {
    $(tmb).parents('ul:first').find('.active').removeClass('active').find('img').css('opacity', 0.5);
    $(tmb).find('img').css('opacity', 1).parent().addClass('active');
    
    $('#slides .activeslide').removeClass('activeslide').addClass('prevslide');
    $('#slides .nextslide').hide().attr('src', href).removeClass('nextslide').addClass('activeslide').fadeIn(500);
    $('#slides .prevslide').removeClass('prevslide').addClass('nextslide');
}

function loadImage(tmb, href) {
    if (url != false) {
        var id = $(tmb).attr('id').split('_');
        id = id[1];
        $.get(url + id,
            function(data){
                $('#chg').html(data);
        });
    }
    
    if (jQuery.inArray(href, loaded) < 0) {
        loading = true;
        //$('#loading').show();
        $.cacheImage(href, {
            load : function (e) {
                loaded.push(href);
                fireBackground($(tmb), href);
                loading = false;
                //$('#loading').hide();
            }
        });
    } else {
        fireBackground($(tmb), href);
    }
}

function shiftImage(direction) {
    var act = $('#thumbs').find('a.active');
    
    if (!$(act).length) {
        tmb = direction < 0 ? $('#thumbs').find('li:last a') : $('#thumbs').find('li:first a');
    } else {
        if (direction < 0) {
            var tmb = $(act).parent().prev().length ? $(act).parent().prev().find('a') : $(act).parents('ul:first').find('li:last a');
        } else {
            var tmb = $(act).parent().next().length ? $(act).parent().next().find('a') : $(act).parents('ul:first').find('li:first a');
        }
    }
    loadImage($(tmb), $(tmb).attr('href'));
    
    return false;
}

function initGallery(iurl) {
    if (typeof iurl != 'undefined' && iurl != '') { url = iurl; }
    
    $('#thumbs a').not('.active').find('img').css('opacity', 0.5);
    $('#thumbs a').each(function(i, a) {
        $(a).hover(
            function() { $(this).find('img').css('opacity', 1); },
            function() { if (!$(this).hasClass('active')) { $(this).find('img').css('opacity', 0.5); } }
        );
        $(a).bind('click', function(e) {
            
            var href = $(this).attr('href');
            loadImage($(a), href);
            
            return false;
        });
    });
    
    $('#toggle_left').click(function() { shiftImage(-1); return false; });
    $('#toggle_right').click(function() { shiftImage(1); return false; });
}

jQuery().ready(function() {
    $('a').each(function() {
        $(this).bind('click', function() {
            if (this.blur) {this.blur();}
        });
    });
    
    $(window).load(function() {
        if ($('#team-wrap').length) {
            var ratio = 1.04;
            var h = $('#team-wrap .employee:first').height();
            var w = $('#team-wrap .employee:first').width();
            var pos_h = ((ratio * h) - h);
            var pos_w = ((ratio * w) - w);
            var cnt = $('#team-wrap .employee').length;
            
            
            $('#team-wrap').addClass('enabled').css('position', 'relative');
            $('#team-wrap .employee').each(function(i, o) {
                var left = (56 - 24) * i - 24;
                
                $(this).css({
                    'left': left,
                    'bottom': 0,
                    'position': 'absolute',
                    'float': 'none',
                    'display': 'block',
                    'margin': '0',
                    'zIndex': 2,
                    'opacity': 0.95
                });
                
                $(this).hover(
                    function() {
                        $(this).animate({
                            'height': ratio * h,
                            'bottom': - Math.round(pos_h / 1.2),
                            'left': Math.round(left - (pos_w * 2 / (i+1) / cnt * 14)),
                            'opacity': 1
                        }, 80, 'swing').css('overflow', 'visible');
                    },
                    function() {
                        $(this).stop(true, true).animate({
                            'height': h,
                            'bottom': 0,
                            'left': left,
                            'opacity': 0.95
                        }, 60, 'swing').css('overflow', 'visible');
                    }
                ).click(function(event) {
                    if (!$('#team-wrap').hasClass('locked')) {
                        $('#team-wrap').addClass('locked').find('.overlay:first')
                        .css({
                            'display': 'block',
                            'opacity': 0
                        }).animate({
                            'opacity': 0.35
                        }, 350);
                        
                        var person = $(this);
                        
                        var expose = $(this).clone().appendTo($(this).parent())
                        .css('zIndex', 100).animate({
                            'height': 600,
                            'bottom': -200,
                            'left': 200,
                            'width': 700
                        }, 180, 'swing', function() {
                            $(expose).css('overflow', 'visible').find('.name').css('display', 'block').hide().slideDown(150);
                            $(expose).find('dl:first').css('display', 'block').hide().fadeIn(200);
                        }).css('overflow', 'visible');
                        
                        $(expose).find('h3 a').unbind('click').click(function() {
                            $(expose).find('.name, dl').fadeOut(100, function() {
                                $(expose).animate({
                                    'height': h,
                                    'bottom': 0,
                                    'left': left,
                                    'width': 64
                                }, 150, 'swing', function() {    
                                    $(person).css('visibility', 'visible');
                                    $(this).remove();
                                    
                                    $('#team-wrap').removeClass('locked');
                                }).css('overflow', 'visible');
                                
                                $('#team-wrap').find('.overlay:first').animate({
                                    'opacity': 0
                                }, 300, 'swing', function() {
                                    $(this).css('display', 'none');
                                });
                            });

                            return false;
                        });
                        
                        $(this).css('visibility', 'hidden');
                    }
                });
            });
        }
    });
});
