// ===================
// = js utilities    =
// = v.0.2 by a2co   =
// ===================

// html5 tags enabler on ie8

document.createElement('header');
document.createElement('hgroup');
document.createElement('nav');
document.createElement('menu');
document.createElement('section');
document.createElement('article');
document.createElement('aside');
document.createElement('footer');

var Utilities = function(){ };

// prevent the target="blank" attribute
Utilities.prototype.targetBlank = function(){

	$$("a[href^='http']").addEvent("click", function(e){
		e.stop();
		window.open(this.get("href"), "_blank");
	});
};

// pull the window scroll to the top during initialization
Utilities.prototype.initToTheTop = function(){

	var winInitScroll = new Fx.Scroll(window,{ duration: 0 });

	window.addEvent('domready',function(){
		winInitScroll.toTop();
	});

	if(Browser.safari || Browser.chrome){
		window.addEvent('load',function(){
			winInitScroll.toTop();
		});
	};
};

// prevent any action of the user on everything and everywhere
Utilities.prototype.setPreventUser = function(){

	var prvUsrStr = new Element('div', { id: 'preventUserStore' }).inject(document.body, 'bottom');

	$('preventUserStore').store({
		'htmlLastOverflow': document.html.getStyle('overflow-y'),
		'bodyLastOverflow': document.body.getStyle('overflow-y'),
		'bodyLastHeight': document.body.getHeight()
	});

	window.addEvents({
		'mousewheel': triggerPreventUser,
		'scroll': triggerPreventUserScrollbar,
		'click': triggerPreventUser
	});
}

var triggerPreventUser = function(e){
	e.stop();
}

var triggerPreventUserScrollbar = function(){
	document.html.setStyle('overflow-y','scroll');
	document.body.setStyles({'overflow-y':'hidden','height':window.getHeight()});
}

Utilities.prototype.destroyPreventUser = function(){

	window.removeEvents({
		'mousewheel': triggerPreventUser,
		'scroll': triggerPreventUserScrollbar,
		'click': triggerPreventUser
	});

	document.html.setStyle( 'overflow', $('preventUserStore').retrieve('htmlLastOverflow') );
	document.body.setStyles({ 'overflow': $('preventUserStore').retrieve('bodyLastOverflow'), 'height': $('preventUserStore').retrieve('bodyLastHeight') });
	
	$('preventUserStore').destroy();
}

//create a nice spinner (require raphel.js and browser canvas)
Utilities.prototype.createSpinner = function(holderid, R1, R2, count, stroke_width, colour){

	var sectorsCount = count || 12,
	color = colour || "#fff",
	width = stroke_width || 15,
	r1 = Math.min(R1, R2) || 35,
	r2 = Math.max(R1, R2) || 60,
	cx = r2 + width,
	cy = r2 + width,
	r = Raphael(holderid, r2 * 2 + width * 2, r2 * 2 + width * 2),

	sectors = [],
	opacity = [],
	beta = 2 * Math.PI / sectorsCount,

	pathParams = {stroke: color, "stroke-width": width, "stroke-linecap": "round"};

	Raphael.getColor.reset();

	for (var i = 0; i < sectorsCount; i++) {
		var alpha = beta * i - Math.PI / 2,
		cos = Math.cos(alpha),
		sin = Math.sin(alpha);
		opacity[i] = 1 / sectorsCount * i;
		sectors[i] = r.path([["M", cx + r1 * cos, cy + r1 * sin], ["L", cx + r2 * cos, cy + r2 * sin]]).attr(pathParams);
		if (color == "rainbow") {
			sectors[i].attr("stroke", Raphael.getColor());
		}
	}

	var tick;

	(function ticker() {
		opacity.unshift(opacity.pop());
		for (var i = 0; i < sectorsCount; i++) {
			sectors[i].attr("opacity", opacity[i]);
		}
		r.safari();
		tick = setTimeout(ticker, 1000 / sectorsCount);
	})();

	return function () {
		clearTimeout(tick);
		r.remove();
	};
};

// scroll to element w/ dynamic timing (w/ iOS scroll event workaround)
Utilities.prototype.scrollToElement = function(theEl,extOffset){

	var scrollDoc;

	var elPosY = $$('#'+theEl)[0].getPosition().y;
	var maxScrollH = document.body.getStyle('height').toInt() - window.getHeight();
	var scrollDur = Math.round(Math.abs(document.body.getScroll().y - elPosY) * 1.5);
	var scrollTrans = Fx.Transitions.Quint.easeInOut;

	if( elPosY == 0 ){ var scrollOffs = 0; }
	else{ var scrollOffs = -extOffset }

	scrollDoc = new Fx.Scroll(window, {
		duration: scrollDur,
		transition: scrollTrans,
		wheelStops: false,
		onStart: iOSeventWorkaround,
		onComplete: iOSeventWorkaround,
		offset: {'y': scrollOffs }
	});

	if( elPosY > maxScrollH ){ scrollDoc.toBottom(); }
	else if( elPosY == 0 ){ scrollDoc.toTop(); }
	else{ scrollDoc.toElement(theEl); }

	function iOSeventWorkaround(){

		if(!Browser.Platform.ios)return;
		document.fireEvent('scroll'); window.fireEvent('scroll');
	}
}

