
var scrollarea;

$(init);

function init () {
	
	if (!is_mobile()) scrollarea = new ScrollArea($("#main"));
}


/* delegate */
function delegate (this_object, method) {
	
	return function () { return method.apply(this_object, arguments); }
}

function delegateWithArgs (this_object, method, args) {
	
	return function () { return method.apply(this_object, args); }
}


/* scrollarea */
function ScrollArea (content) {
	//alert("ScrollArea(" + [content] + ")");
	
	var p = this;
	this.content = content;
	
	this.content.after($('<div id="scrollarea"><div class="bar" id="bar"></div><div class="bg"></div></div>'));
	this.area = $('#scrollarea');
	this.bar = $('.bar', this.area);
	this.bg = $('.bg', this.area);
	
	this.content_size;
	this.content_innersize;
	this.content_position = 0;
	this.content_position_max;
	this.content_bottom = 90 + 1;
	
	this.content_area_size;
	
	this.scroller_size;
	this.scroller_position = 0;
	this.scroller_position_offset = 60;
	this.scroller_position_max;
	
	this.scroller_area_top = $('#body').position().top + 1 + this.scroller_position_offset;
	this.scroller_area_bottom = 30 + this.content_bottom;
	this.scroller_area_size;
	
	this.start_position;
	this.target_position;
	
	$('html').css({overflowY:"hidden"});
	$('body').css({overflowY:"hidden"});
	
	this.bar.mousedown(delegate(this, this.barPress));
	this.bg.mousedown(delegate(this, this.bgPress));
	
	this.start();
	this.scrollTo();
}

ScrollArea.prototype.stop = function () {
	//alert("ScrollArea.stop()");
	
	$(document)
		.unbind("keydown")
		.unbind("mouseup")
		.unbind("mousewheel");
	
	$(window).unbind("resize");
	
	clearInterval(this.internval);
}

ScrollArea.prototype.start = function () {
	//alert("ScrollArea.start()");
	
	$(document)
		.keydown(delegate(this, this.onKeyDown))
		.mouseup(delegate(this, this.stopDrag))
		.mousewheel(delegate(this, this.onMouseWheel));
	
	$(window).resize(delegate(this, this.scrollTo));
	this.internval = setInterval(delegate(this, this.checkContentSize), 200);
}

ScrollArea.prototype.setSize = function (event) {
	//alert("ScrollArea.setSize(" + [event] + ")");
	
	this.content_area_size = $(window).height() - this.scroller_area_top;
	
	this.content_innersize = this.content.height();
	this.content_size = this.content_innersize + 20 + this.content_bottom;
	this.content_position_max = this.content_size - this.content_area_size;
	
	this.scroller_area_size = $(window).height() - this.scroller_area_top - this.scroller_area_bottom;
	this.scroller_area_size = Math.max(this.scroller_area_size, 100);
	
	this.scroller_size = Math.floor(this.content_area_size / this.content_size * this.scroller_area_size);
	this.scroller_size = Math.max(this.scroller_size, 50);
	
	this.scroller_position_max = this.scroller_area_size - this.scroller_size;
	
	var top = Math.floor((this.scroller_size  - 13) / 2)
	this.bar.css({backgroundPosition:"center " + top + "px"});
	
	//set
	this.bar.height(this.scroller_size);
	this.bg.height(this.scroller_area_size);
	
	if (this.content_size < this.content_area_size) this.area.css({visibility:"hidden"});
	else this.area.css({visibility:"visible"});
}

ScrollArea.prototype.scrollTo = function (value) {
	//alert("ScrollArea.scrollTo(" + [value] + ")");
	
	this.setSize();
	
	if ((value == undefined) || isNaN(value)) value = this.scroller_position;
	
	this.scroller_position = Math.max(Math.min(Math.round(value), this.scroller_position_max), 0);
	this.bar.css({top:this.scroller_position});
	
	this.content_position = Math.round(this.scroller_position / this.scroller_position_max * (this.content_area_size - this.content_size));
	this.content.css({marginTop:this.content_position});
}

ScrollArea.prototype.checkContentSize = function (event) {
	//alert("ScrollArea.checkContentSize(" + [event] + ")");
	
	if (this.content_innersize != this.content.height()) this.scrollTo();
}

ScrollArea.prototype.startDrag = function (event) {
	//alert("ScrollArea.startDrag()");
	
	this.start_position = event.pageY - this.scroller_area_top - this.scroller_position;
	
	$(document).unbind("mousemove");
	$(document).mousemove(delegate(this, this.onDrag));
	
	return false;
}
	
ScrollArea.prototype.stopDrag = function (event) {
	//alert("ScrollArea.stopDrag(" + [event] + ")");
	
	$(document).unbind("mousemove");
	
	return false;
}
	
ScrollArea.prototype.onDrag = function (event) {
		//alert("ScrollArea.onDrag(" + [event] + ")");
	
	if (!event) event = window.event; 
	
	this.scrollTo(event.pageY - this.scroller_area_top - this.start_position);
	
	return false;
}

ScrollArea.prototype.onKeyDown = function (event) {
	//alert("ScrollArea.onKeyDown(" + [event] + ")");
	
	if (event.keyCode == 38) this.scrollTo(this.scroller_position - 50);
	else if (event.keyCode == 40) this.scrollTo(this.scroller_position + 50);
}

ScrollArea.prototype.onMouseWheel = function (event, delta) {
	//alert("ScrollArea.onMouseWheel(" + [event, delta] + ")");
	
	this.scrollTo(this.scroller_position - (delta * 8));
	
	if (event.preventDefault) event.preventDefault();
	event.returnValue = false;
}


ScrollArea.prototype.barPress = function (event) {
	//alert("ScrollArea.barPress(" + [event] + ")");
	
	this.startDrag(event);
	
	return false;
}

ScrollArea.prototype.bgPress = function (event) {
	//alert("ScrollArea.areaPress(" + [event] + ")");
	
	var y = event.pageY - this.scroller_area_top > this.scroller_position ? this.scroller_size : -this.scroller_size;
	this.scrollTo(this.scroller_position + y);
	
	return false;
}


// Thanks to BraveNewCode's WPtouch iPhone Theme for the UA list.
// (http://wordpress.org/extend/plugins/wptouch/)
function is_mobile () {
  var useragents = [
    'iPhone',         // Apple iPhone
    'iPad',         // Apple iPad
    'iPod',           // Apple iPod touch
    'Android',        // 1.5+ Android
    'dream',          // Pre 1.5 Android
    'CUPCAKE',        // 1.5+ Android
    'blackberry9500', // Storm
    'blackberry9530', // Storm
    'blackberry9520', // Storm v2
    'blackberry9550', // Storm v2
    'blackberry9800', // Torch
    'webOS',          // Palm Pre Experimental
    'incognito',      // Other iPhone browser
    'webmate'         // Other iPhone browser
  ];
  var pattern = new RegExp(useragents.join('|'), 'i');
  return pattern.test(navigator.userAgent);
}




