﻿var bob = {
	initial_run			: true,
	_onready_call_backs : [],
	_onload_call_backs 	: [],
	
	add_onready_function : function ( func ) {
		if ( typeof func == 'function' ) {
			bob._onready_call_backs.push( func );
		}
	},
	
	add_onload_function : function ( func ) {
		if ( typeof func == 'function' ) {
			bob._onload_call_backs.push( func );
		}
	},
	
	on_load : function () {
		var _function;
		
		for ( var i = 0; i < bob._onload_call_backs.length; i++ ) {
			_function = bob._onload_call_backs[i];
			if ( typeof _function == 'function' ) {
				_function();
			}
		}
		
		$j('.select').selectBox();
	},
	
	on_dom_ready : function () {
		var _function;
		
		for ( var i = 0; i < bob._onready_call_backs.length; i++ ) {
			_function = bob._onready_call_backs[i];
			if ( typeof _function == 'function' ) {
				_function();
			}
		}
	},
	
	log : function (str) {
		if ( typeof console !== 'undefined' && typeof console.log !== 'undefined' ) {
			console.log(str);
		}
	}
};

// dropdown
var dropdown = {
	
	dropdowns 	: [],
	links		: false,
	
	initialised : false,
	
	hover : function ( container ) {
		container.children('a').addClass('active');
		container.children('div').show();
	},
	
	unhover : function ( container ) {
		container.children('a').removeClass('active');
		container.children('div').hide();
	},
	
	initialise : function () {
		if ( dropdown.initialised == false ) {
			dropdown.links = $j('.header-nav-link-container');
			dropdown.links.hover( function () { dropdown.hover( $j(this) ); }, function () { dropdown.unhover( $j(this) ); } );
		}
		dropdown.initialised = true;
	}
}

var twitter = {
	
	viewport_top : false,
	viewport_bot : false,
	
	element_top : false,
	element_bot : false,
	
	initiated : false,
	
	// calculate the string for relative time since tweeted...
	calc_time : function ( date ) {
		// time now, and when tweeted...
		var created = new Date( twitter.parse_date(date) ).getTime();
		var now		= new Date().getTime();
		
		// difference...
		var timeago = Math.floor( ( now - created ) / 1000 );
		
		// logic calc...
		if 			( timeago < 60) return 'less than a minute ago';
		else if 	( timeago < 120 ) return 'about a minute ago';
		else if 	( timeago < ( 45 * 60 ) ) return ( parseInt( timeago / 60 ) ).toString() + ' minutes ago';
		else if 	( timeago < ( 90 * 60 ) ) return 'about an hour ago';
		else if 	( timeago < ( 24 * 60 * 60 ) ) return 'about ' + ( parseInt( timeago / 3600 ) ).toString() + ' hours ago';
		else if 	( timeago < ( 48 * 60 * 60 ) ) return '1 day ago';
		else return ( parseInt  ( timeago / 86400 ) ).toString() + ' days ago';
	},
	
	// this function is needed to ensure cross browser data compatibilty (ie, lol)
	parse_date : function ( date ) {
		var v = date.split(' ');
		return Date.parse(v[1]+" "+v[2]+", "+v[5]+" "+v[3]+" GMT");
	},
	
	// parse links in the twitter feed and create a tags...
	parse_links : function ( text ) {
		var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
		return text.replace(exp,"<a href=\"$1\">$1</a>");
	},
	
	load : function () {
		
		// if not loaded prev'ly, load the latest tweet
		if ( twitter.initiated == false ) {
			if ( dom_loaded = true ) {
				// fetch tweet...
				$j.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=wwballoonrides&count=1&callback=?&include_rts=1", function(data) {
					
					setTimeout( function () {
						// load data...
						$j.each(data, function( i, tweet ) {
							$j('.twitter-time').text( twitter.calc_time( tweet.created_at  ) );
							$j('.twitter-msg').html( twitter.parse_links( tweet.text ) );
						});
						
						// fade out loader, and fade in tweet
						$j('.footer-twitter .loader').fadeOut('slow', function () {
							$j('.footer-twitter .container').fadeIn('slow');
						});
					}, 1000 );
				});
				
				// is now loaded...
				twitter.initiated = true;
			}
		}
	},
	
	// function to test is element has come into view...
	is_in_view : function () {
		// view port dimensions
		twitter.viewport_top = $j(window).scrollTop();
		twitter.viewport_bot = twitter.viewport_top + $j(window).height();
		
		// element dimensions
		twitter.element_top = $j('.footer-twitter').offset().top;
		twitter.element_bot = ( $j('.footer-twitter').height() + twitter.element_top );
		
		// return true or false
		return ( ( twitter.element_bot >= twitter.viewport_top ) && ( twitter.element_top <= twitter.viewport_bot) );
	},
	
	check : function () {
		if ( twitter.initiated == false ) {
			if ( twitter.is_in_view() ) {
				twitter.load();
				clearInterval( twitter.i );
			}
		} else {
			clearInterval( twitter.i );
		}
	},
	
	initialise : function () {
		
		// if already in view, then load, else wait for scroll into view...
		if ( twitter.is_in_view() ) {
			twitter.load();
		} else {
			// detect scroll
			$j(window).scroll(function () {
				// if not already initiated
				if ( twitter.initiated == false ) {
					// check if in view...
					if ( twitter.is_in_view() ) {
						// load if came into view
						twitter.load();
						
						// do not need this event handler any more...
						$j(window).unbind('scroll');
					}
				}
			});
		}
		
		twitter.i = setInterval( twitter.check, 200 );
	}
};

imagify = {
	
	images : null,
	
	process : function ( img ) {
		var parent = img.parent();
		var align  = img.attr('lang');
		var width  = img.width();
		
		parent
			.addClass('image-container')
			.append('<span class="top"><span></span></span>')
			.append('<span class="mid"><span></span></span>')
			.append('<span class="bot"><span></span></span>');
		
		img.show();
		
		parent.find('span.mid span').append(img);
		
		if ( align == 'left' ) {
			parent.addClass('image-container-left');
			parent.css( 'width', width + 40 );
			parent.children('span').css( 'width', width + 20 );
		} else if ( align == 'right' ) {
			parent.addClass('image-container-right');
			parent.css( 'width', width + 40 );
			parent.children('span').css( 'width', width + 20 );
		} else {
			parent.addClass('image-container-center');
			parent.css( 'width', width + 20 );
			parent.children('span').css( 'width', width + 20 );
		}
		
		parent.fadeIn('fast');
	},
	
	initialise : function () {
		imagify.images = $j('.std p img');
		
		imagify.images.each(function () {
			imagify.process( $j(this) );
		});
	}
};

bob.add_onload_function( imagify.initialise );
bob.add_onready_function( twitter.initialise );
bob.add_onready_function( dropdown.initialise );
