/*!
 * MBP - Moible boilerplate helper functions: 
 */

 
var MBP = MBP || {}; 
 
// Hide URL Bar for iOS
// http://remysharp.com/2010/08/05/doing-it-right-skipping-the-iphone-url-bar/

MBP.hideUrlBar = function () {
	/mobile/i.test(navigator.userAgent) && !pageYOffset && !location.hash && setTimeout(function () {
	window.scrollTo(0, 1);
	}, 1000);
}


// Fast Buttons
// http://code.google.com/mobile/articles/fast_buttons.html

MBP.fastButton = function (element, handler) {
    this.element = element;
    this.handler = handler;
	element.addEventListener('touchstart', this, false);
	element.addEventListener('click', this, false);
};

MBP.fastButton.prototype.handleEvent = function(event) {
    switch (event.type) {
		case 'touchstart': this.onTouchStart(event); break;
        case 'touchmove': this.onTouchMove(event); break;
        case 'touchend': this.onClick(event); break;
        case 'click': this.onClick(event); break;
    }
};

MBP.fastButton.prototype.onTouchStart = function(event) {
    event.stopPropagation();
    this.element.addEventListener('touchend', this, false);
	document.body.addEventListener('touchmove', this, false);
    this.startX = event.touches[0].clientX;
    this.startY = event.touches[0].clientY;
    this.element.style.backgroundColor = "rgba(0,0,0,.7)";
};

MBP.fastButton.prototype.onTouchMove = function(event) {
    if(Math.abs(event.touches[0].clientX - this.startX) > 10 || Math.abs(event.touches[0].clientY - this.startY) > 10) {
		this.reset();
    }
};
MBP.fastButton.prototype.onClick = function(event) {
    event.stopPropagation();
    this.reset();
    this.handler(event);
    if(event.type == 'touchend') {
		preventGhostClick(this.startX, this.startY);
    }
    this.element.style.backgroundColor = "";
};
MBP.fastButton.prototype.reset = function() {
    this.element.removeEventListener('touchend', this, false);
    document.body.removeEventListener('touchmove', this, false);
    this.element.style.backgroundColor = "";
};
function preventGhostClick(x, y) {
    coordinates.push(x, y);
    window.setTimeout(gpop, 2500);
};
function gpop() {
	coordinates.splice(0, 2);
};
function gonClick(event) {
	for(var i = 0; i < coordinates.length; i += 2) {
		var x = coordinates[i];
        var y = coordinates[i + 1];
        if(Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) {
			event.stopPropagation();
            event.preventDefault();
        }
    }
};
document.addEventListener('click', gonClick, true);
var coordinates = [];


// iOS Startup Image
// https://github.com/shichuan/mobile-html5-boilerplate/issues#issue/2

MBP.splash = function () {
  var filename = navigator.platform === 'iPad' ? 'h/' : 'l/';
  document.write('<link rel="apple-touch-startup-image" href="/img/' + filename + 'splash.png" />' );
}


// Autogrow
// http://googlecode.blogspot.com/2009/07/gmail-for-mobile-html5-series.html

MBP.autogrow = function (element, lh) {
	var setLineHeight = (lh) ? 11 : 12;
	var textLineHeight = element.style.lineHeight;
	if (textLineHeight.indexOf("px") == -1) {
		textLineHeight = setLineHeight;
	} else {
		textLineHeight = parseInt(textLineHeight, 10);
	}
	element.style.overflow = "hidden";
	element.onkeyup = function(e){
		var newHeight = this.scrollHeight;
		var currentHeight = this.clientHeight;
		if (newHeight > currentHeight) {
			this.style.height = newHeight + 3 * textLineHeight + "px";
		}
	}
}




@@ -1,11 +1,10 @@
 /*
-!
 %0A * MBP 
@@ -11,10 +11,10 @@
 - Mo
-i
 b
+i
 le b
@@ -44,18 +44,16 @@
 ions
-: 
 %0A */%0A%0A %0A
 var 
@@ -52,18 +52,28 @@
 %0A%0A %0A
-var MBP = 
+window.MBP = window.
 MBP 
@@ -1672,16 +1672,20 @@
 d') %7B%0A%09%09
+MBP.
 preventG
@@ -1997,25 +1997,20 @@
  %22%22;%0A%7D;%0A
-function 
+MBP.
 preventG
@@ -2018,16 +2018,28 @@
 ostClick
+ = function 
 (x, y) %7B
@@ -2043,26 +2043,25 @@
 ) %7B%0A    
+MBP.
 coord
-inate
 s.push(x
@@ -2092,24 +2092,8 @@
 out(
-gpop, 2500);%0A%7D;%0A
 func
@@ -2101,28 +2101,26 @@
 ion 
-gpop
 ()
- 
 %7B%0A
-%09coordinate
+    %09MBP.coord
 s.sp
@@ -2131,19 +2131,59 @@
 (0, 2);%0A
-%7D;%0A
+    %7D, 2500);%0A%7D;%0A;%0AMBP.ghostClickHandler = 
 function
@@ -2183,24 +2183,16 @@
 unction 
-gonClick
 (event) 
@@ -2211,24 +2211,25 @@
  = 0
-; i %3C coordinate
+, len = MBP.coord
 s.le
@@ -2233,16 +2233,25 @@
 .length;
+ i %3C len;
  i += 2)
@@ -2263,26 +2263,25 @@
 var x = 
+MBP.
 coord
-inate
 s%5Bi%5D;%0A  
@@ -2294,26 +2294,25 @@
 var y = 
+MBP.
 coord
-inate
 s%5Bi + 1%5D
@@ -2518,16 +2518,29 @@
 k', 
-gonClick
+MBP.ghostClickHandler
 , tr
@@ -2548,22 +2548,17 @@
 e);%0A
-var 
+MBP.
 coord
-inate
 s = 

throw some more of the fastButtons code under the MBP namespace.