var jlib = {

	// queues one or more functions to be run on load / document ready
	// if first argument is false, functions are added to the front of the queue
	queue: function () {
		var args = $.makeArray(arguments), isJump = !args[0];
		if (!jlib._queue) {
			jlib._queue = [];
		}
		if (isJump) {
			args.shift();
			Array.prototype.unshift.apply(jlib._queue, args);
		} else {
			Array.prototype.push.apply(jlib._queue, args);
		}
		if (jlib._queueStarted && jlib._queueStopped) {
			jlib._queueStopped = false;
			jlib.runQueue();
		}
	},

	// starts calling functions passed to jlib.queue()
	runQueue: function (context) {
		context = (context || window);
		jlib._queueStarted = true;
		setTimeout(function () {
				var q = jlib._queue;
				if (!q || q.length == 0) {
					jlib._queueStopped = true;
					return;
				}
				(q.shift()).call(context);
				setTimeout(arguments.callee, 10);
			}, 10);
	},

	// detaches the given element from the dom, returns a function that can be called to reattach the element
	detach: function (el) {
		var par, pos, hold;
		el = $(el);
		par = el.parent();
		pos = par.children().index(el);
		hold = $('<div></div>').append(el);
		return function () {
				par.children().eq(pos).before(el);
				el = par = hold = null;
			};
	}
};

/************************************************************
 *  open link in new window
 */
jlib.openNewWindow = function () {
	var rel, 
		href;
	$("a").click(function (e) {
		rel = $(this).attr('rel');
		href = $(this).attr('href');
		if (rel.indexOf('external') > -1) {
			e.preventDefault();
			window.open(href);
		}
	});
}

/************************************************************
 *  tabs
 */
jlib.tabs = function () {
	var curTab;
	$('ul.tabs a').each(function () {
		this.img = $(this).find('img')[0];
		this.div = $(this.hash);
	})
	.click(function (e) {
		e.preventDefault();
		if (curTab == this) {
			return;
		}
		if (curTab) {
			curTab.img.src = curTab.img.src.replace(/_(?:on)\./, '_off.');
			curTab.div.addClass('hidden');
		}
		this.img.src = this.img.src.replace(/_(?:off)\./, '_on.');
		this.div.removeClass('hidden');
		curTab = this;
	})
	.eq(0).click();
};



/************************************************************
 * homepage images 
 */
jlib.home = function () {
	var homeImages = [
			{
				src: '/images/global/hero_visual/hero_visual_home_01.jpg',
				alt: 'Greenpeace China Image 1'
			},
			{
				src: '/images/global/hero_visual/hero_visual_home_02.jpg',
				alt: 'Greenpeace China Image 2'
			},
			{
				src: '/images/global/hero_visual/hero_visual_home_03.jpg',
				alt: 'Greenpeace China Image 3'
			}
		],
		curImg = 0,
		imgCount = 0,
		PI = Math.PI,
		duration = 1000,
		delay = 3000,
		imgs, curTab;

	if (!$('body').hasClass('homepage')) {
		return;
	}

	// images

	// from http://www.robertpenner.com/easing/
	// t - current time (from 0)
	// b - start value (begin)
	// c - end value - start value (change)
	// d - duration
	function easeInOutSine(t, b, c, d) {
		return -c/2 * (Math.cos(PI * t / d) - 1) + b;
	}

	function fadeIn() {
		var cur = imgs[curImg], start = (new Date()).getTime(), d = duration;
		cur.css({
			opacity: '0',
			zIndex: '1',
			visibility: 'visible'
		});
		(function () {
			var t = (new Date()).getTime() - start;
			if (t >= d) {
				initFadeOut();
				return;
			}
			cur.css('opacity', easeInOutSine(t, 0, 1, d));
			setTimeout(arguments.callee, 0);
		})();
	}

	function initFadeOut() {
		var cur = imgs[curImg], next = imgs[(curImg + 1) % imgCount];
		cur.css({
			visibility: 'visible',
			zIndex: '1',
			opacity: '1'
		});
		next.css({
			visibility: 'visible',
			zIndex: '',
			opacity: '1'
		});
		// "fix" white dots
		if ($.browser.msie) {
			cur[0].style.removeAttribute('filter');
			next[0].style.removeAttribute('filter');
		}
		setTimeout(fadeOut, delay);
	}

	function fadeOut() {
		var cur = imgs[curImg], start = (new Date()).getTime(), d = duration;
		(function () {
			var t = (new Date()).getTime() - start, nextImg;
			if (t >= d) {
				nextImg = (curImg + 1) % imgCount;
				imgs[nextImg].css('z-index', '1');
				cur.css({
					visibility: 'hidden',
					zIndex: '',
					opacity: '1'
				});
				curImg = nextImg;
				initFadeOut();
				return;
			}
			cur.css('opacity', easeInOutSine(t, 1, -1, d));
			setTimeout(arguments.callee, 0);
		})();
	}

	imgs = $.map(homeImages, function (o) {
		return $('<img />')
			.load(function () {
				$(this).unbind('load', arguments.callee);
				imgCount++;
				if (imgCount == homeImages.length) {
					fadeIn();
				}
			})
			.css('visibility', 'hidden')
			.attr(o);
	});
	$('div.fadeimgs').append($.map(imgs, function (o) { return o[0]; }));

};
/************************************************************
 * onload, onunload
 */
$(function () {
	jlib.home();
	jlib.openNewWindow();
	jlib.tabs();
	
	if (jlib.formInit) {
		jlib.formInit();
	}

	
});