/*
 * Pollinate Mobile Framework
 * @author: Nathan Searles
 * @copyright: ©2010 All Rights Reserved. You may not copy, 
 * distribute or modifiy this code with out authorization by Pollinate.
 * @published: 11/09/2008
 * @updated: 29/09/2008
 */
if(typeof jQuery != 'undefined') {
	jQuery(function($) {
		$.fn.extend({
			mobile: function(options) {
				var settings = $.extend({}, $.fn.mobile.defaults, options);
				return this.each(
					function() {
						if($.fn.jquery < '1.4') {return;}
						var $t = $(this);
						var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings;
						var parent,clicked,next,scrollposition;
						var active = false;
						var firstload = true;
						var history = [];
							
						$t.find(o.current).css({
							display: 'block'
						});
						
						window.onload = function() {
						  setTimeout(function(){window.scrollTo(0, 1);}, 100);
						}
						
						$(o.navigation + ' li a').live('click',function(e){
							e.preventDefault();
							action('next',this);
						});
						
						$(o.prev).live('click',function(e){
							e.preventDefault(e);
							action('prev');
						});
						
						$(o.back).live('click',function(e){
							e.preventDefault();
							action('prev');
						});
						
						location.hash = '/' + $(o.current).attr('id');
						
						$(window).bind("hashchange", function() {
							if (o.history && firstload) {
								$('a.back').css({opacity:1.0,left:10});
								history.push(o.history);
								location.hash = o.start;
								firstload = false;
								return;
							}
							if (history.length < 1) {
								location.hash = '#/home';
								return;
							}
							action('prev');
						});
						function action(dir,obj,effect) {
							if (!active) {
								active = true;
								parent = $(obj).parents('div:eq(0)').attr("id");
								clicked = $(obj).attr('href');
								switch(dir) {
									case 'next':
									scrollposition = window.pageYOffset;
									history.push(window.location.hash);
									if (clicked[0] === '#') {
										location.hash = '/' + $(obj).attr('href').replace('#','');
										$('#' + parent).removeClass(o.current.replace('.',''));
										next = clicked;			
									} else {
										external = clicked;
										clicked = clicked.split('.');
										directory = clicked[0].split('/');
										location.hash = '/' + directory[directory.length - 1];
										if (!$('#' + directory[directory.length - 1]).length) {
											$("<div/>", {
											    id: directory[directory.length - 1],
											    html: function() {
											    	$.get(external,function(data){
														$('#' +directory[directory.length - 1]).append(data); 
													});
											    }
											}).appendTo($t);
										}
										next = '#' + directory[directory.length - 1];
									}
									break;
									case 'prev':
										parent = $(o.current).attr('id');
										next = history[history.length-1].replace('/','');
										location.hash = '/' + next.replace('#','');
										history.splice(history.length-1);
									break;
								}
								
								(history.length > o.historyOffset) ? $('a.back').animate({opacity:1.0,left:10},250) : $('a.back').animate({opacity:0,left:70},250);
								
								if (scrollposition < 50) {
									scrollposition = 1;
								}
								
								$t.children().removeClass(o.current.replace('.',''));
								
								$(next).css({
									position: 'absolute',
									left: (dir === 'next') ? $(window).width() : -$(window).width(),
									display:'block'
								}).addClass(o.current.replace('.',''));
								
								$t.animate({
									left: (dir === 'next') ? -$(window).width() : $(window).width()
								}, o.speed,
								function(){
									$(next).trigger("dynamic");
									$t.css({ left:0 });
									$(next).css({ left:0 });
									$('#' + parent).css({display:'none'});
									(dir === 'next') ? window.scrollTo(0, 1) : window.scrollTo(0, scrollposition);
									active = false;
								});
							}
						}
					}
				);
			}
		});
		$.fn.mobile.defaults = {
			current: '.current',
			navigation: '.active',
			next: '.next',
			prev: '.back',
			speed: 250,
			history: 0,
			start: 0,
			historyOffset: 0
		};
	});
}
