jQuery.extend({

	/* -------------------------------------------------------------------------
	 * DARKBOX
	 */
	darkbox: function( url ) {
			
		// insert darkbox elements
			
		jQuery("body").append('<div id="darkbox-dimmer"></div>');
		jQuery("body").append('<div id="darkbox">' +
								'<div class="content">' +
									'<div class="close"></div>' +
									'<div class="scrollpane"></div>' +
								'</div>' +
							   '</div>' );
		
		// inline html or ajax load
		
		var returnval = null;
		
		if( url != '' ) {
			jQuery.ajax({
				url: url,
				dataType: 'text',
				type: 'GET',
				error: function( jqXHR, textStatus, errorThrown ) {
				},
				success: function( data, textStatus, jqXHR ) {
					window.location.hash = '#' + url.replace( /\.html$/, '' );
					jQuery("#darkbox .content .scrollpane").html( data );

					jQuery("#darkbox .scrollpane").jScrollPane();
					var jsp = jQuery("#darkbox .scrollpane").data('jsp');
					
					// load dynamic content?
					if( url == 'photos.html' ) {
						jQuery(".flickr-photos .scrollable .items").flickrFeed({
							tags: 'promote',
							scrollable: true,
							complete: function() {
								jsp.reinitialise();
							}
						});
					}
					else if( url == 'press.html' ) {
						jQuery(".flickr-photos").flickrFeed({
							tags: 'press',
							complete: function() {
								jsp.reinitialise();
							}
						});
					}
				}
			});
		}
		else {
			returnval =	jQuery("#darkbox .content .scrollpane");
		}

		// show darkbox + bind close			
			
		jQuery("#darkbox").fadeIn().click(function(e) {
			if( $(e.target).attr("id") == "darkbox") {
				jQuery.closeDarkbox();
				return false;
			}
			return true;
		});
		jQuery("#darkbox-dimmer").fadeIn("slow").click(function() {
			jQuery.closeDarkbox();
			return false;
		});
		jQuery("#darkbox .close").click(function() {
			jQuery.closeDarkbox();
			return false;
		});
		
		return returnval;
	},
	
	closeDarkbox: function() {
		jQuery("#darkbox").fadeOut("fast", function() {
			jQuery("#darkbox").remove();
		});
		jQuery("#darkbox-dimmer").fadeOut("fast", function() {
			jQuery("#darkbox-dimmer").remove();
		});
		window.location.hash = '';
	}


});

jQuery.fn.extend({

	/* -------------------------------------------------------------------------
	 * DARKBOX
	 */
	darkboxLink: function() {
		jQuery(this).click( function() {
			var url = jQuery(this).attr("href");
			if( url == '#' ) {
				return false;
			}
			url = url.replace( /^[#]/, '' );
			
			jQuery.darkbox( url + '.html' );
			
			return false;
		});
	},
		
	darkboxTwitpic: function() {
		jQuery(this).click(function() {
			var box = jQuery.darkbox('');
			jQuery("#darkbox .scrollpane").css("background", "url('img/ajax-loader-c.gif') no-repeat center center");
			box.html('<div style="height:100%;text-align:center"><img src="'+jQuery(this).attr("href")+'" alt="" height="100%"/></div>');
			return false;
		});
	}	
	
});

/* -------------------------------------------------------------------------------
 * INITIALIZE
 */

jQuery(document).ready(function() {

	// display last tweet
	jQuery("#last-tweet div").tweet({
		username: "tvoff"
	});

	// display twitpics

	jQuery("#twitpics .items img").each(function(i) {
		if( i < 8 ) {
			jQuery(this).delay(i*30).animate({
				top: '-=80'
			}, 500, function() {
				
				if( i == 7 ) {
					// animation complete
				
					jQuery("#twitpics .browse").css('opacity','1.0');

					// load twitpics
				
					jQuery("#twitpics .items").photostream({
						username: 'tvoff',
						tumblr: 'tvoff.tumblr.com',
						complete: function() {
							// darkbox images
							jQuery("#twitpics .items a.twitpic").each(function() {
								jQuery(this).darkboxTwitpic();
							});
							// init scrollable
							jQuery("#twitpics .scrollable").scrollable();
						}
					});
				}

			});
		}
		else {
			jQuery(this).css('top', '0px');
		}
	});
		
	// display newsflash if present
	if( jQuery(".newsflash").length ) {
		jQuery(".newsflash").fadeIn( function() {
			jQuery(".newsflash .close").click(function() {
				jQuery(".newsflash").fadeOut("fast");
			});
		});
	}
	
	// init menus
	jQuery("#nav .menu a").each(function() {
		jQuery(this).darkboxLink();
	});
	
	// hash? walk the menu, launch darkbox if hashtag matches
	if( window.location.hash.match( /^[#].+$/ )) {
		var url = window.location.hash;
		jQuery( "#nav .menu a" ).each(function() {
			if( jQuery(this).attr("href") == url ) {
				jQuery(this).trigger( "click" );
				return false;
			}
		});
	}

});


