$(document).ready(function() {
	// The Stack
	var disclose = function(id, tab) {
		$('ul#the_stack li.slab ul li div').hide();
		$('ul#the_stack li.active').removeClass('active');
		$('ul#the_stack li#'+id).addClass('active');
		$('ul#the_stack li#'+id+' ul li').removeClass('active');
		$('ul#the_stack li#'+id+' ul').children('li#'+tab).addClass('active');
		$('ul#the_stack li.slab ul li.active div').fadeIn('slow');
		$('ul#the_stack li#'+id+'.active ul li h3').click(function() {
			$('ul#the_stack li#'+id+' ul li.active').removeClass('active');
			$(this).closest('li').addClass('active');
			$(this).closest('li').children('div').fadeIn('slow');
		});
	}
	$('ul#the_stack li.slab h2').click(function() {
		if(!$(this).closest('li').hasClass('no-open')) {
			disclose($(this).closest('li').attr('id'), 'tab_1');
		}
	});
	$('ul#the_stack li.slab h3').click(function() {
		id = $(this).closest('li.slab').attr('id');
		tab = $(this).closest('li').attr('id');
		disclose(id, tab);
	});
	pruneTabs = function() {
		$('li.tab').show();
		$('ul#the_stack').children('li.slab').each(function() {
			firstHidden = 0; foundHidden = false; limit = 0;
			$(this).children('ul').children('li.tab').each(function() {
				limit++;
				t = $(this); c = t.children('h3'); o = c.offset(); w = c.width();
				tid = t.attr('id'); tno = tid.replace('tab_', '');
				if(o.left+w > 950 || limit>4) {
					if(t.hasClass('active')) {
						t.removeClass('active');
						newTab = (parseInt(tno) > 1) ? tno-1 : 1;
						$(this).parents('li.slab').children('ul').children('#tab_1').addClass('active');
					}
					t.hide();
					t.siblings().each(function() {
						sid = $(this).attr('id');
						sno = sid.replace('tab_', '');
						if(parseInt(sno) > parseInt(tno)) { 
							$(this).hide();
						}
					});
					return;
				};
			});
		});
	}
	$(window).resize(function() {
		// pruneTabs();
	});

	// Scramble Artists
	$('li#slab_artists ul, li#slab_love ul').shuffle();
	
	// Pagination
	pageArray = new Array();
	pageIndex = 0;
	pageArray[pageIndex] = new Array();
	if($('#content.article-body').html()) {
		$('#content.article-body').children().each(function() {
			if($(this).is('hr')) {
				pageIndex ++;
				pageArray[pageIndex] = new Array();
			} else {
				pageArray[pageIndex].push($(this).html());
			}
		});
	}
	
	itemsPerPage = 5;
	var page = function(pageNo) {
		if(jqSlug == 'index') return;
		pageNo = pageNo-1;
		if($('#category_list').html()) {
			limit = pageNo*itemsPerPage+itemsPerPage;
			count = 1;
			$('#category_list li').hide();
			$('#category_list').children('li').each(function() {
				if(count > pageNo*itemsPerPage && count <= limit) {
					$(this).fadeIn('slow');
				}
				count ++;
			});
			redrawPager(pageNo+1);
		} else if($('#content.article-body').html()) {
			if(pageNo >= pageArray.length) {
				pageNo = pageArray.length-1;
			}
			$('#content.article-body').html('');
			$('#content.article-body').hide();
			for(p in pageArray[pageNo]) {
				$('#content.article-body').append('<p>'+pageArray[pageNo][p]+'</p>');
			}
			$('#content.article-body').fadeIn('slow');
		}
		redrawPager(pageNo+1);
		setPlayTags();
	}
	var redrawPager = function(pageNo) {
		count = 1;
		if($('#category_list').html()) {
			$('#category_list').children('li').each(function() {
				count ++;
			});
			if(count-1 > itemsPerPage) {
				pageCount = Math.ceil(count/itemsPerPage);
				$('#page-nav ul').html('');
				$('#page-nav').fadeIn('slow');
			}
		} else if($('#content.article-body')) {
			count = pageArray.length;
			if(count > 1) {
				pageCount = count;
				$('#page-nav ul').html('');
				$('#page-nav').fadeIn('slow');
			}
		}
		for(i=1;i<=pageCount;i++) {
			$('#page-nav ul').prepend('<li id="page_'+i+'"><div class="page-preview"><p>'+pageArray[i-1][0].substr(0, 100)+'...'+'</p></div></li>'); // Prepend because this div floats right.
		}
		$('#page_'+pageNo).addClass('on_page');
		$('div#page-nav ul li').click(function() {
			id = $(this).attr('id');
			no = id.replace('page_', '');
			page(no);
			return false;
		});
		$('div#page-nav ul li').mouseover(function() {
			$('div.page-preview').hide();
			$(this).children('div.page-preview').show();
		});
		$('div#page-nav ul').mouseout(function() {
			$('div.page-preview').hide();
		});
		$('a').click(function() {
			if($(this).attr('href').indexOf('#') > -1) {
				page($(this).attr('href').split('#')[1]);
			};
		});
	}
	
	// Markup Prep
	$('a.cat_link').each(function() {
		$(this).attr('href', $(this).parents('li.slab').attr('rel'));
		/*
		var replace; var original = $(this).parents('li.slab').children('h2').text();
		if (original == 'Us') {
			replace = 'See all about us'
		} else {
			replace = 'All '+$(this).parents('li.slab').children('h2').text().toLowerCase();
		}
		$(this).text(replace);
		*/
	});
	$('ul#the_stack').children('li.slab').each(function() {
		count = 1; $(this).children('ul').children('li').each(function() {
			$(this).attr('id', 'tab_'+count); // Unfortunately, we have to create duplicate id's to make this work...
			count ++;
		});
	});
	count = 1; $('#footer ul.love').children('li.loved').each(function() {
		if(count > 6) {
			$(this).hide();
		} else {
			count ++;
		}
	});
	if(jqPermalink) {
		slug = jqPermalink.split('/');
		if(
		slug[1] == 'projects' 	||
		slug[1] == 'concerts' 	||
		slug[1] == 'news' 		||
		slug[1] == 'records' 	||
		slug[1] == 'artists' 	||
		slug[1] == 'community' 	||
		slug[1] == 'us' 		||
		slug[1] == 'writing' 	||
		slug[1] == 'hilites' 	||
		slug[1] == 'love'		||
		slug[1] == 'comps'
		) {
			$('body').addClass(slug[1]+'-cat');
		} else {
			$('body.article h2').css('background', '#000');
		}
	}
	if($('div#article-nav').html() == '' || $('div#article-nav').html() == false) {
		$('div#article-nav').hide();
	}
	$('ul#the_stack li.slab ul li div').hide();
	$('div#canvas').css('overflow', 'hidden');
	$('body.article h2').css('background', '#000');
	
	// YouTube thumbnail capture. This is subject to change without notice.
	if($('div#video').html()) {
		$('div#video').children('br').hide();
		$('div#video').children('a').each(function() {
			url = $(this).attr('href');
			parseURL = url.match("[\\?&]v=([^&#]*)");
			vid = parseURL[1];
			$(this).html('<img src="/public/inc/lib/timthumb/timthumb.php?w=100&h=80&zc=1&src=http://img.youtube.com/vi/'+vid+'/2.jpg" />');
		});
	}
	
	$('.list-slab').each(function() {
		html = '<ul class="first">';
		$(this).children('ul').each(function() {
			i = 0; 
			$(this).children('li').each(function() {
				i ++;
				html += '<li>'+$(this).html()+'</li>';
				if(i == 8) {
					html += '</ul><ul>';
					i = 0;
				}
			});
		});
		html += '</ul>';
		$(this).children('ul').remove();
		$(this).append(html);
	});
	
	count = 1;
	$('#category_list').children('li').each(function() {
		$(this).attr('id', 'cat_item_'+count);
	});
	
	// $('#category-peek ul').hide(); $('#category-peek a').hide();
	$('#category-peek a.trigger').mouseover(function() {
		$('#category-peek ul').fadeIn();
	});
	$('#category-peek a.trigger').click(function() {
		return false;
	});
	$('#category-peek').mouseleave(function() {
		$(this).children('ul').fadeOut();
	});

	// Transitions
	$('div#package, div#media-area, #category-peek a').fadeIn(4000);

	// Modals
	$('a.modal-img').live('click', function() {
		imgSrc = $(this).attr('href');
		imgAlt = $(this).children('img').attr('alt');
		imgAlt = imgAlt.replace(/,,/g, '\'');
		$('div#modal>div div').html('<img src="/public/inc/lib/timthumb/timthumb.php?w=800&h=600&zc=1&src='+imgSrc+'" alt="'+imgAlt+'" />');
		$('div#modal>div p#caption').html(imgAlt);
		$('div#modal').fadeIn('slow');
		return false;
	});	
	$('div#pics a, a.modal-img').click(function() {
		imgSrc = $(this).attr('href');
		imgAlt = $(this).children('img').attr('alt');
		imgAlt = imgAlt.replace(/,,/g, '\'');
		$('div#modal>div div').html('<img src="/public/inc/lib/timthumb/timthumb.php?w=800&h=600&zc=1&src='+imgSrc+'" alt="'+imgAlt+'" />');
		$('div#modal>div p#caption').html(imgAlt);
		$('div#modal').fadeIn('slow');
		return false;
	});
	$('div#modal').click(function() {
		$(this).fadeOut('slow');
	});
	
	// Compatibility
	if(!$.browser.msie && !$.browser.version.substr(0,1) == '6') {
		$('div#footer').hide();
		$('div#fg').css({'padding':'10px','font-size':'18px','line-height':'30px','color':'#FFF'});
		$('div#fg').html('<p>Hey, no offense, but... dude, you\'re in IE 6.</p><p>...And on top of that, there\'s so much stuff we want to show you.</p><p>So go get your &raquo;<a href="http://www.google.com/search?q=download+ie&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a">upgrade</a>&laquo; on and come back.</p><p>We really want to see you here!</p>');
	}
	
	// Dog Star Archive Section
	$('body.dog-star-archive ul#the_stack li.slab').click(function() {
		if($(this).hasClass('no-open')) return;
		disclose($(this).attr('id'), 'tab_1');
		$('body.dog-star-archive ul#the_stack li.hint').fadeOut('slow');
	});
	
	$('body.dog-star-archive li.selector h2 a').click(function() {
		trigger = $(this).attr('id').replace('trigger-', '');
		if(trigger == 'home') return;
		$('body.dog-star-archive li.selector h2 a').removeClass('active');
		$('#trigger-'+trigger).addClass('active');
		
		$('body.dog-star-archive ul#the_stack li').hide();
		$('body.dog-star-archive ul#the_stack li.selector').show();
		$('body.dog-star-archive li.slab.active').removeClass('active');
		switch(trigger) {
			case 'about':
				$('body.dog-star-archive ul#the_stack li#about').fadeIn('slow');
				break;
			case 'archive':
				$('body.dog-star-archive ul#the_stack li').fadeIn('slow');
				
				$('body.dog-star-archive ul#the_stack li#about').hide();
				$('body.dog-star-archive ul#the_stack li#schedule').hide();
				$('body.dog-star-archive ul#the_stack li#pisaro').hide();
				break;
			case 'schedule':
				$('body.dog-star-archive ul#the_stack li#schedule').fadeIn('slow');
				break;
			case 'pisaro':
				$('body.dog-star-archive ul#the_stack li#pisaro').fadeIn('slow');
				break;
		}
		return false;
	});
	
	// jQuery PlayTag Player
	flashTPL = '<object style="vertical-align:bottom;margin-right:0.2em" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' +
	'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"' +
	'width="1" height="1" id="player" align="middle">' +
	'<param name="wmode" value="transparent" />' +
	'<param name="allowScriptAccess" value="sameDomain" />' +
	'<param name="flashVars" value="theLink=%url%&amp;fontColor=%css_color%" />' +
	'<param name="movie" value="http://'+jqDomain+'/public/inc/lib/playtag/player.swf" /><param name="quality" value="high" />' +
	'<embed style="vertical-align:bottom;margin-right:0.2em" src="http://'+jqDomain+'/public/inc/lib/playtag/player.swf" flashVars="theLink=%url%&amp;fontColor=%css_color%"'+
	'quality="high" wmode="transparent" width="1" height="1" name="player"' +
	'align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"' +
	' pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>'
	
	var players = []; var i = 0;
	blinkerCount = 1;
	function blinker() {
		if(blinkerCount < 2) {
			$('a.playing span.dots').append('.');
			blinkerCount ++;
		} else {
			$('a.playing span.dots').html('.');
			blinkerCount = 0;
		}
	}
	window.setInterval(function(){blinker()}, 500);
	setPlayTags = function() {
		$('a').each(function() {
			if($(this).attr('href').match(/\.mp3$/i)) {
				$(this).addClass('playtag');
				$(this).prepend('<em>[play]</em>&nbsp;&nbsp;&nbsp;');
				$(this).attr('id', 'player-'+i);
				players[i] = {
					playing : false,
					defaultHTML : $(this).html()
				};
				$(this).click(function() {
					myID = $(this).attr('id').replace('player-', '');
					if(players[myID].playing) {
						players[myID].playing = false;
						$(this).html(players[myID].defaultHTML);
						$(this).removeClass('playing');
					} else {
						for(p in players) {
							$('a#player-'+p).html(players[p].defaultHTML);
							$('a#player-'+p).removeClass('playing');
							$(this).children('strong').html('[play]');
							players[p].playing = false;
						}
						players[myID].playing = true;
						html = flashTPL;
						href = $(this).attr('href');
						html = html.replace(/%url%/g, $(this).attr('href'));
						html = html.replace(/%css_color%/g, $(this).css('color'));
						$(this).append(html);
						$(this).children('em').html('[stop<span class="dots">.</span>]');
						$(this).addClass('playing');					
					}
					return false;
				});
				i ++;
			}
		});
	}
	$('input').click(function () {
		// alert(1);
	});
	
	// Go
	disclose('slab_news', 'tab_1');
	// pruneTabs();
	setPlayTags();
	if(location.hash != '') {
		var pp = location.hash.replace('#', '');
		page(pp);
	} else {
		page(1);
	}
});