$().ready(function(){
	$().Accordion({
		first : '#block1',
	 	other : ['#block2', '#block3'],
	 	distance : '766px',
	 	speed : 'slow'
	});	
});

jQuery.fn.Accordion = function(uOption) {
	var options = {	};
	var where_block;
	
	$.extend(options, uOption);
	
	$(options.other[1]).click(function(){
		var id = options.other[1];
		var prev_id = options.other[0];
		if ( id != get_active() ) {
			
			var pos = $(id).position();
			var pos_prev = $(prev_id).position();
			
			if ( pos.left > 400 ) {
				if ( pos_prev.left > 600 ) {
					$(prev_id).animate({"left": "-="+options.distance}, options.speed);
					cnange_arrow(prev_id);
				}
				$(id).animate({"left": "-="+options.distance}, options.speed);
			}
			
			cnange_arrow(id);
			set_active(id);
		}
	});
	
	$(options.other[0]).click(function(){
		var id = options.other[0];
		var next_id = options.other[1];
		if ( id != get_active() ) {
			
			var pos = $(id).position();
			var pos_next = $(next_id).position();
			
			if ( pos.left > 400 ) {
				$(id).animate({"left": "-="+options.distance}, options.speed);
				cnange_arrow(id);
			} else {
				if ( pos_next.left < 100 ) {
					$(next_id).animate({"left": "+="+options.distance}, options.speed);
					cnange_arrow(next_id);
				}
			}
			
			set_active(id);
		}
	});
	
	$(options.first).click(function(){
		if ( options.first != get_active() ) {
			$.each(options.other, function(i, val) {
				var pos = $(val).position();
				if ( pos.left < 400 ) {
					$(val).animate({"left": "+="+options.distance}, options.speed);
					cnange_arrow(val);
				}
			});
			set_active(options.first);
		}
	});
	
	function cnange_arrow(val) {
		var pos = $(val).position();		
		if ( pos.left > 400 ) {
			$(val + ' div.arrow-class').removeClass('acc-arrow-l');
			$(val + ' div.arrow-class').addClass('acc-arrow-r');
		} else {
			$(val + ' div.arrow-class').addClass('acc-arrow-l');
			$(val + ' div.arrow-class').removeClass('acc-arrow-r');
		}
	}
	
	function other_slide(val) {
		
	}
	
	function get_active() {
		return $('input[name=active_acc]').val();
	}
	
	function set_active(val) {
		$('input[name=active_acc]').val(val);
	}
	
}
