/**
 * Javascript enhancements for project
 *
 * @date 2009-01-01
 * @author maw
 *
 * TOC
 * - utility functions
 * - utility functions for page parameters
 * - tween
 */

/**
 * @section utility functions
 */
$.fn.noSpam = function() {
	return this.each(function(){
		var email = String("mailto:" + $(this).html()).replace(/ at /, "@");
		var title = String($(this).attr('title')).replace(/ at /, "@");
		var anchor = $(document.createElement('a')).attr('href', email).text(title);

		if($(this).attr("id")) {
			anchor.attr("id", $(this).attr("id"));
		}

		if($(this).attr("class")) {
			anchor.attr("class", $(this).attr("class"));
		}

		$(this).replaceWith(anchor);
	});
};
function updateAnchor(){
	$('#content a:not(:has(img))').filter(function() {
		return this.hostname && (this.hostname).split(":")[0] !== (location.hostname).split(":")[0];
  	}).addClass('external').click(function(){
		window.open(this.href);
		return false;
	});
}
function swapCheckboxes(el){
	$(el).each(function(){
		$(this).show();
		$label = $('label[for="'+$(this).attr('id')+'"]');
		if($(this).is(':checked')) {
			$label.addClass('checked');
			$label.parent().addClass('selected');
		}
		$label.addClass('item').addClass($(this).attr('type'));
		$label.bind('click',function(){
			$('input#' + $(this).attr('for')).triggerHandler('click');

			if($('input#' + $(this).attr('for')).is(':checkbox')){
				$(this).toggleClass('checked');
				$('input#' + $(this).attr('for')).checked = true;
			} else {
				$toCheck = $('input#' + $(this).attr('for'));
				$('input[name="'+$toCheck.attr('name')+'"]').each(function(){
					$('label[for="' + $(this).attr('id')+'"]').removeClass('checked');
					$('label[for="' + $(this).attr('id')+'"]').parent().removeClass('selected');
				});
				$(this).addClass('checked');
				$(this).parent().addClass('selected');
				$toCheck.checked = true;
			};
		});
	});
}
function cleanSplit(str, separator){
	if(!str)return new Array();
	var a=str.split(separator),i=a.length;
	while(i>=0){if(a[i]=='')a.splice(i,1);i--;}
	return a;
}

/**
 * @section utility functions for page parameters
 */
function hasPageParams(){
	return _pageParams.length > 0;
}
function getPageParams(){
	return _pageParams.length ? _pageParams : null;
}
function getFirstPageParam(){
	return _pageParams.length ? _pageParams[0] : null;
}
function getLastPageParam(){
	return _pageParams.length ? _pageParams[_pageParams.length-1] : null;
}

/**
 * @section tween
 */
Tween = function (el, prop){
	this.el = el;
	this.prop = prop;
	this.interval = null;
	this.onComplete = function(){};
	this.obj = "TweenInstance_" + (++ Tween.instance);
	eval(this.obj + "=this");
}
Tween.instance = 0;
Tween.prototype.start = function (current, end, duration, ease) {
	this.stop();
	if (current) this.current = current;
	else this.current = parseInt(this.el.style[this.prop]) || 0;
	this.end = end;
	this.duration = duration;
	this.ease = (ease && typeof(TweenEase[ease]) == 'function') ? ease : 'linear';
	this.elapsed = 0;
	this.difference = this.end - this.current;
	this.doTween();
	this.interval = setInterval(this.obj + ".doTween()", 50);
}
Tween.prototype.doTween = function(){
	if (this.elapsed ++ < this.duration) {
		var p = TweenEase[this.ease](this.elapsed, this.current, this.difference, this.duration);
		this.el.style[this.prop] = p + 'px';
	} else {
		this.el.style[this.prop] = this.end + 'px';
		this.stop();
		this.onComplete();
	}
}
Tween.prototype.stop = function(){
	clearInterval(this.interval);
	this.interval = null;
}
TweenEase = {
	linear : function (t, b, c, d) {
		return c*t/d+b;
	},
	easein : function (t, b, c, d) {
		return c*(t/=d)*t+b;
	},
	easeout : function (t, b, c, d) {
		return -c*(t/=d)*(t-2)+b;
	}
}

$(document).ready(function() {
	$(".email").noSpam();

	// bind event to external calls
	updateAnchor();

	// form element focus
	$("input, textarea").focus(function(){
		$(this).addClass('focus');
	}).blur(function(){
		$(this).removeClass('focus');
	});

	// pretty.dropdown.js
	$("form select").PrettyDropdown();
	
	// autoresize.js
	$('form textarea').autoResize({
	    extraSpace : 0,
        limit: 5000
	});
});
