var Index = {
	run: function() {
		this.accordion();
		this.tabs();
		this.slider();
		Util.remove_focus('.tabs-nav a');
	},
	
	accordion: function() {
		var show = $('#index-show');
		
		$('dt', show).click(function() {
			var dt = $(this);
			if (dt.hasClass('selected')) {
				return;
			}
			
			if (show.data('running')) {
				return;
			}
			show.data('running', true);
			
			
			var dd = dt.next();
			
			var sel_dt = $('dt.selected', show); 
			var sel_dd = $('dd.selected', show);
			
			var max_width = parseInt(sel_dd.width());
			
			dt.addClass('selected');
			dd.css('width', '0px').addClass('selected').show();
			
			sel_dt.removeClass('selected');
			sel_dd.removeClass('selected').show();	
			
			var handler = function() {
				var width = dd.width() + 40;
				width = width < max_width ? width : max_width;
				dd.css('width', width + 'px');
				sel_dd.css('width', (max_width - width) + 'px');

				if (width < max_width) {
					setTimeout(handler, 10);
				} else {
					show.removeData('running');
					sel_dd.hide();
				}
			};
			
			handler();
		});
	},
	
	tabs: function() {
		$('#index-show .tabs-nav li').live('click', function() {
			var li = $(this);
			var nav = li.parent();
			
			nav.find('li').removeClass('selected');
			li.addClass('selected');
			
			var body_lis = nav.parent().find('.tabs-body li');
			body_lis.hide().eq(li.index()).show();
			
			return false;
		});
	},
	
	do_slider: function(body, value) {
		var img = $('div :first-child', body);
		var height = img.height() - img.parent().height();
		var margin = (height / 100) * (100 - value);
		img.css('marginTop', -parseInt(margin) + 'px');
	},
	
	slider: function() {
		var self = this;
		var slider = $('#index-show .code-slider');
		slider.slider({ 
			orientation: 'vertical', 
			value: 100,
			max: 116,
			slide: function(event, ui) {
				if (ui.value <= 100) {
					var body = $(ui.handle).parent('.code-slider').parent().find('.code-body');
					self.do_slider(body, ui.value);
				} else {
					event.preventDefault();
				}
			}
		});
	}
};

