$(window).bind('keydown', function(e){
    if(e.keyCode==32) 
        return false; 
});
   
$(window).bind('resize', function(){
    $('.bg img').fixImageSize(); 
    $.scrollTo('#' + activeSite);
    
    var ww = $(window).width();
    $('h1').each(function(){
    	$(this).css('left', (ww - $(this).width())/2); 
	});
    
    if($(window).height() >= 710) {
    	$('.footer').show();
    } else {
    	$('.footer').hide();
    }
});


$.fn.fadeToggle = function(time) {
    time = time || 300;
    return this.each(function(){
        var t = $(this);
        if(t.is(':visible'))
            t.fadeOut(time);
        else
            t.fadeIn(time);
        
    });
}

$.fn.fixImageSize = function() {
    var bw = $(window).width(), bh = $(window).height();
	
    return this.each(function() {
        var iw = $(this).width(), ih = $(this).height();
        var ratio = $(this).data('ratio') || iw/ih;
        $(this).data('ratio', ratio);
        
        var ih, iw;
        if ((bw/bh) < ratio){
        	ih = bh;
            iw = bh * ratio;
        } else {
            iw = bw;
            ih = bw / ratio;
        }
        
        var css = {
        	'height': ih,
        	'width': iw,
            'left': (bw - iw)/2,
            'top': (bh - ih)/2
        };
        
        if($(this).hasClass('anchor_top'))
        	css.top = '0';
        
        $(this).css(css);
    });
};

var activeSite = 'home';
var activeShow = null;

var gotoSite = function(site) {
    var $s = $('#' + site);
    
    $('.site').trigger('deactivate').css('z-index', '-1').find('.contents').fadeOut(300, function(){ $(this).empty(); });

    $s.trigger('activate').css('z-index', '2');
    
    $s.css('visibility', 'visible');
    $.scrollTo($s, 800, { onAfter: function() { $('.site').not($s).css('visibility', 'hidden'); } });
    activeSite = site;
    
    if(activeShow != null)
        activeShow.stop();
    
    setTimeout(function(){ $s.find('.nav .index a').click(); }, 1300);
}

$().ready(function(){
    
    $('.bg img').fixImageSize(); 
    
    var ww = $(window).width();
    $('h1').each(function(){
    	$(this).css('left', (ww - $(this).width())/2); 
	});
    
    $.scrollTo('#home');
    
    $('.go-home').click(function(){ gotoSite('home'); return false; });
    $('#go-up').click(function(){ gotoSite('up'); return false; });
    $('#go-down').click(function(){ gotoSite('down'); return false; });
    
    
    $('.footer').hide();
    if($(window).height() >= 710) {
    	$('.footer').show();
    }
    
    
    $('.nav a').click(function(){
    	var a = $(this);
    	var s = a.closest('.site');
    	var t = s.find('.contents');
    	t.fadeOut(400, function(){
    		a.addClass('active').parent().siblings().children().removeClass('active');
    		t.empty().load(a.attr('href'), function(){ 
    			if($.browser.msie && parseInt($.browser.version) == 6) {
    				t.pngFix(); 
    			}
    			
    			t.fadeIn(400);
    			activeShow = t.find('.gallery .images img').bind('slideshow-pre-in', function(){ 
    				var ct = $(this).attr('data-title'), cc = t.find('.gallery .caption');
    				if(ct == '') { cc.fadeOut(100); } else { cc.text(ct).fadeIn(100); }
				}).slideshow();
    			
    			t.find('.inside.scrollable').jScrollPane();
			});
    	});
    	
    	return false;
    });
    
    
    $('#go-up').hover(function(){
        $('#home_up_on').css('z-index', 2).fadeIn();
        $('#up-tip').fadeIn();
    }, function(){
        $('#home_up_on').fadeOut(function(){ $(this).css('z-index', -1); });
        $('#up-tip').fadeOut();
    });
    
    $('#go-down').hover(function(){
        $('#home_down_on').css('z-index', 2).fadeIn();
        $('#down-tip').fadeIn();
    }, function(){
        $('#home_down_on').fadeOut(function(){ $(this).css('z-index', -1); });
        $('#down-tip').fadeOut();
    });
    
    
    $('.next').live('click', function(){ activeShow.stop().next(); return false; });
    $('.prev').live('click', function(){ activeShow.stop().prev(); return false; });
    
});

