var allLoaded = function(i) {
	if ($.dblrbl.loading && ++$.dblrbl.loading == i) $.dblrbl.loading = false;
};
var loadPage = function(which) {
	if ($.dblrbl.loading) return false;
	else $.dblrbl.loading = 1;
	pageTracker._trackPageview('/' + which);
	var $is_c = $('#is_circle');
	if (which != 'contact' && $is_c.is(':visible')) $is_c.animate({ top: '-=30px', opacity: 'hide' }, 300, 'easeOutQuart');
	if (which == 'welcome') $('#btnholder img').each( function() { $(this).attr('src', $(this).data('origsrc') + '.png'); });
	else $('#btnholder img').each( function() {
		var $this = $(this);
		$this.attr('src', $this.data('origsrc') + (($this.attr('id').split('_')[1] == which) ? 's' : '') + '.png');
	});
	$('#about_c').fadeOut(500, function() { $(this).remove(); });
	$.get(which + '_0.html?cache', function(data) {
		var $el = $('#header .content');
		$el.find('.main_content, .bienvenido').animate({ opacity: 0, height: 'hide' }, '500', function() {	$(this).siblings('script').andSelf().remove(); });
		$(data).css({ display: 'none' }).appendTo($el).pause(550).animate({ height: 'show' }, 500, function() { allLoaded(4); }); //.slideDown(500);
	});
	$.get(which + '_1.html?cache', function(data) {
		var $el = $('#welcomebox .content');
		$el.find('.sub_content').fadeOut('500', function() { $(this).siblings('script').andSelf().remove(); });
		$(data).css({ display: 'none' }).appendTo($el).pause(550).slideDown(500, function() { allLoaded(4); });
	});
	if (which =='about') $.get(which + '_2.html?cache', function(data) {
			$(data).css({ display: 'none' }).find('#about_2').addClass('short').end()
				.insertAfter('#welcomebox').pause(550).slideDown(500, function() { allLoaded(4); });
	});
	else allLoaded(4);
	return false;
};

var slidePage = function() {
	$.dblrbl.loaded = false;
	$.get('/whois.php', function(data) {
		$els = $(data).css({ display: 'none' }).insertAfter('#sub_btns');
		if ($.dblrbl.loaded) $els.slideDown(800, function() {
			$('#footer_text').animate({ width: $('body').width() - 339 }, 100);
			if (!$.support.opacity) $('#whois_wrap').css({ marginBottom: 0 });
		});
	});
	var $wrapper = $('#wrapper'), css, els = [ 'btnholder' ], i = els.length - 1, alpha = ($.support.opacity) ? 0 : 'hide';
	$.dblrbl.oldWrapMarginL = css = $wrapper.offset().left; //store this old margin
	$('#header, #welcomebox, #about_c, #space_1').fadeOut(1400, function() { $(this).remove(); });
	$wrapper.css({ marginLeft: css }).animate({ marginLeft: -600 }, 1000, 'easeInOutQuart', function() {
		$('#whois_head').css({ top: -90, display: 'block' }).animate({ top: 0 }, 500, 'easeOutCubic');
		$('#btn_whois').animate({ marginTop: -80, opacity: alpha }, 500, 'easeOutCubic', function() { $(this).css({ display: 'none' }); });
		$('#btn_www').css({ display: 'block' }).animate({ opacity: ($.support.opacity) ? 1 : 'show' }, 500, function() { $('#sub_btns').animate({ right: '+=40px' }, 500); });
		$('#crate').magnifierReset().magnifierDestroy().addClass('shifted').magnifier($.dblrbl.magnifyOptions);
		$('#whois_wrap').slideDown(800, function() {
			$('#footer_text').animate({ width: $('body').width() - 339 }, 100);
			if (!$.support.opacity) $('#whois_wrap').css({ marginBottom: 0 });
		});
		$.dblrbl.loaded = true;
	});
	$('#footer_text').animate({ width: "-=339px" }, 800, 'easeOutExpo');
	$('#lr_corner').animate({ width: 339 }, 800, 'easeOutExpo', function() { $('#byline').animate({ marginRight: -330 }, 800, 'easeOutExpo'); });
};

var returnPage = function() {
	$.dblrbl.loaded = false;
	$.get('/index.php?fixie', function(data) {
		$els = $(data).css({ display: 'none' }).insertAfter('#whois_wrap');
		if ($.dblrbl.loaded) { $els.fadeIn(1000); navInit(); }
	});
	var els = [ 'btnholder' ], i = els.length - 1, alpha = ($.support.opacity) ? 1 : 'show';
	$('#byline').animate({ marginRight: 0 }, 800, 'easeOutExpo', function() {
		$('#lr_corner').animate({ width: 0 }, 800, 'easeOutExpo');
		$('#footer_text').animate({ width: "+=339px" }, 800, 'easeOutExpo');
	});
	$('#whois_head').animate({ top: -90 }, 500, 'easeOutCubic');
	$('#sub_btns').animate({ right: '-=40px' }, 500, function() {
		($.support.opacity && $('#btn_whois').css({ display: 'block' }));
		$('#btn_whois').animate({ marginTop: 0, opacity: alpha }, 500, 'easeOutCubic');
		$('#btn_www').animate({ opacity: ($.support.opacity) ? 0 : 'hide' }, 500);
		($.support.opacity && $('#header, #welcomebox, #about_c, #space_1').fadeIn(1000)) || $('#header, #welcomebox, #about_c, #space_1').css({ display: 'block' });
		navInit();
		$.dblrbl.loaded = true;
		$('#whois_wrap').animate({ height: 'hide', opacity: 'hide' }, 800, function() {
			$('#footer_text').animate({ width: '100%' }, 100);
			$('#wrapper').animate({ marginLeft: $.dblrbl.oldWrapMarginL || ($('body').width() / 2) - (960/2) }, 1000, 'easeInOutQuart', function() {
				$(this).css({ marginLeft: 'auto' });
				$('#crate').magnifierReset().magnifierDestroy().removeClass('shifted').magnifier($.dblrbl.magnifyOptions);
			});
			$(this).remove();
		});
	});
};


	/*
	** don't forget to make a vector shaker for bar ad
	**
	** throw up a block with (3d?) loading dialog
	** first start grabbing the ajax data
	**
	** slidePage()
	** 
	** fill with data on ajax callback
	**
	** enlarge the comment counter icon
	*/

/*
** on whois/code hover and hold, show arrow on left
** slide down and fade in list of latest updates
** incorporate code blog entries in code
** incorporate music entries into dnb
** etc.
** 
**
*/

var showVinyl = function(ui) {
	var $self = $(ui.current).data('disabled', true), oldtop = $self.css('top'), id = 'vinyl' + Math.floor( Math.random() * 10000 );
	$('<div id="' + id + '" class="vinylCap">').text(
		$self.stop(true).animate(
			{ top: '-=100px' },
			500,
			'easeOutQuint',
			function() {
				var $this = $('#' + id).css($self.offset());
				$this.css({ opacity: 0 }).css({ display: 'block' })
					.animate({ marginLeft: ($('#crate').hasClass('shifted')) ? 140 : -$this.width(), opacity: 1 }, 500, 'easeOutQuint')
					.pause(2000).fadeOut(300, function() { $(this).remove(); });
			}
		).attr('title')
	).appendTo('body').click( function() { window.open($self.attr('longdesc')); return false; });
	$self.pause(2500).animate({ top: oldtop }, 500, 'easeOutQuint', function() { $(this).data('disabled', false); });
};

var navInit = function() {
	$('#dblrbl_logo').mousedown( function() { $(this).stop(true).animate({ opacity: 1 }, 50); })
		.mouseup( function() { $(this).animate({ opacity: 0 }, 200); })
		.click( function() { return loadPage('welcome'); })
		.hover( function() { $(this).stop(true).animate({ opacity: 0.47 }, 200); },  function() { $(this).stop(true).animate({ opacity: 0 }, 200); })
		.find('img').fixPNG('scale',false,true).parent()
		.add('#bglayer, #btnholder img, #crate, #crate img, #whois_head img, #footer_text, #lr_corner').fixPNG();
	(!$.support.opacity && $('#dblrbl_logo').animate({ opacity: 0 }, 1));
	$('#btn_about').hover( function() {
		$(this).stop(true).animate({ marginRight: 70}, 500, 'easeOutQuint');
	  }, function() {
		$(this).pause(300).stop(true).animate({ marginRight: 0}, 500, 'easeOutQuart');
	}).click( function() { return loadPage('about'); });
	$('#btn_contact').hover( function() {
		$(this).stop(true).animate({ marginRight: 50}, 500, 'easeOutQuint');
		$('#btn_about').stop(true).animate({ marginRight: 100}, 500, 'easeOutQuint');
	  }, function() {
		$(this).add('#btn_about').pause(300).stop(true).animate({ marginRight: 0}, 500, 'easeOutQuart');
	}).click( function() { return loadPage('contact'); });
	$('#btn_portfolio').click( function() { return loadPage('portfolio'); });
	$('#btnholder img').each(  function() { $(this).data('origsrc', $(this).attr('src').split('.')[0]); })
		.mousedown( function() { $(this).animate({ marginRight: '-=10px' }, 50, 'easeOutQuart'); })
		.mouseup( function() { $(this).animate({ marginRight: '+=10px' }, 50, 'easeOutQuart'); });
	
	var $is_c = $('#is_circle');
	if ($is_c.is(':hidden')) $is_c.animate({ top: '-=30px' }, 150);
	
	var imgarray = [ 'btn_about-v2s.png','btn_contact-v2s.png','btn_portfolio-v2s.png', 'dblrbl_v2_head-hover.png' ]; //preload button states!
	for (var x in imgarray){ (new Image()).src = 'images/' + imgarray[x]; }
};

$(function(){
	$.dblrbl = {
		magnifyOptions: {
			axis: 'y',
			baseline: -0.01,
			magnification: 1.4,
			distance: 30,
			offset: { y: 75, x: 12 },
			overlap: true,
			items: '>*:not(#front)',
			mousedown: function(e, ui) { if (!$(ui.current).data('disabled')) showVinyl(ui); }
		},
		loading: false,
		loaded: false
	};
	//create a button plugin for all these damned button animations
	navInit();
	
	$('#sub_btns a').hover( function() { $(this).find('img').stop(true).animate({ top: -3, left: -2 }, 25); }, function() { $(this).find('img').stop(true).animate({ top: 0, left: 0 }, 25); })
		.mousedown(function() { $(this).find('img').stop(true).animate({ top: 0, left: 0 }, 25); })
		.find('img').fixPNG('scale',false,true);
	$('#btn_whois').click( function() { slidePage(); return false; });
	$('#btn_www').click( function() { returnPage(); return false; });
	(!$.support.opacity && $('#btn_www').animate({ opacity: 'hide' }, 1));
	$('#whois_head').click( function() { returnPage(); });
	
	$('#crate').magnifier($.dblrbl.magnifyOptions);
});