/* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 * 
 * Requires: 1.2.2+
 */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);

$(document).ready(function () {
	var move_x1, move_x2, timeout;
	
	$('#content_big').mousedown(function (event) {
		$(this)
			.data('down', true)
			.data('x', event.clientX)
			.data('scrollLeft', this.scrollLeft);
		if(($('#move').width() + 300) > $(window).width())
			$(this).css({'cursor' : '-moz-grab'});
		move_x1 = $(this).data('x');
		return false;
	}).mouseup(function (event) {
		$(this)
			.data('x', event.clientX)
			.css({'cursor' : ''});
		move_x2 = $(this).data('x');
		
		/*
		for(var i=5000;i>=1;i--){
			this.scrollLeft += i*0.00025;
			}
		*/
		
		var dx_move = $("#move").offset().left;
		if(move_x1 - move_x2 > 0){
			// to the left
			dx_move > 0 ? dx = Math.abs(dx_move) % 300 : dx = Math.abs(300 + (dx_move % 300));
			$(this).animate({scrollLeft: (this.scrollLeft + dx)}, 'normal');
			}
		else{
			// to the right
			dx_move > 0 ? dx = Math.abs(300 - (dx_move % 300)) : dx = Math.abs(dx_move) % 300;
			$(this).animate({scrollLeft: (this.scrollLeft - dx)}, 'normal');
			}
		
		//$(this).unbind('mousemove');
		$(this).data('down', false);
	}).mousemove(function (event) {
		if ($(this).data('down') == true) {
			this.scrollLeft = $(this).data('scrollLeft') + $(this).data('x') - event.clientX;
		}
	}).css({
		'overflow' : 'hidden'
	});
	
	// deny link, when moving
	$("#move a").click(function() {
		if(move_x1 != move_x2)
			return false;
		});
	
	// active input
	$("#move input").click(function() {
		if(move_x1 != move_x2)
			return false;
		else{
			$(this).select();
			}
		});
	
	$(".scroll_box .inner_box").mousewheel(function (event, delta) {
		this.scrollTop -= (delta * 30);
	});
});


$(window).mouseout(function (event) {
	if ($('#content_big').data('down')) {
		try {
			if (event.originalTarget.nodeName == 'BODY' || event.originalTarget.nodeName == 'HTML') {
				$('#content_big').data('down', false);
			}                
		} catch (e) {}
	}
});
