$(function() {
	var browserType = $.browser;

	var $window, $header, $page, $footer, windowWidth, windowHeight, headerHeight, sectionWidth;
	$window = $(window);
	$header = $('#header');
	$page = $('#page');
	$main = $('#main');
	$aside = $('#aside');
	$footer = $('#footer');

	function getDimensions() {
		windowWidth = $window.width();
		windowHeight = $window.height();
		headerHeight = $header.outerHeight();
		sectionWidth = $page.find('.section').eq(0).outerWidth();
	}

	if ( !(parseInt(browserType.version) == 7 && browserType.msie) && !navigator.userAgent.match(/iPhone/i) && !navigator.userAgent.match(/iPod/i) && !navigator.userAgent.match(/iPad/i) ) {

		$(window).ready(getDimensions()).resize(getDimensions());

		$('.pagination .pages .listing').before('<span>Pages</span>');

/***** UNIVERSAL *****/
$('body').addClass('has-js');

// Dashboard
if(!$('#dashboard .container:has(.avatar)').length) $('#dashboard').hide();

$('#header #navigation .toggle').click();

// Navigation
$('#header #navigation li:has(ul)').addClass('sub');

// Breadcrumbs
if($('#main.full, .main.full').length > 0) {
	$('body').addClass('full');
}

/***** HOME *****/
if($('body').hasClass('home')) {

	function setDimensions() {
		$page.css({
			height		: windowHeight
		});
	}

	$(window).ready(setDimensions).resize(setDimensions);

	$('.section.recent.media').removeClass('section').appendTo('#main').css({
		top		: headerHeight + 50
	});

	$('.section.products.featured').attr('id', 'store');

	// Media Fan
	$('.recent.media').css({
		paddingLeft		: 420,
		height			: $('#news').outerHeight()
	}).find('.thumbs a').each(function() {
		$(this).css({
			marginBottom		: -$(this).height() * 0.8
		})
	}).end().hover(function() {
		$(this).stop().animate({
			paddingLeft		: 500
		}, 'fast').find('.thumbs a').filter(':not(:last-child)').each(function() {
			$(this).stop().animate({
				marginBottom		: -$(this).height() * 0.3
				}, 'fast');
			});
		}, function() {
			$(this).stop().animate({
				paddingLeft		: 420
				}, 'fast');
				$(this).find('.thumbs a').filter(':not(:last-child)').each(function() {
					$(this).stop().animate({
						marginBottom		: -$(this).height() * 0.8
						}, 'fast');
					});
				}).find('.thumbs a').css({
					top				: 0,
					'z-index'		: 0
				});

				// Content Swipe
				$('#navigation .nav ul').children('.news').data('slide', '#news').end().children('.events').data('slide', '#tour').end().children('.media').data('slide', '#video').end().children('.store').data('slide', '#store').end().children('.users').data('slide', '#users').end().children('.bio').data('slide', '#bio').end().children('.discography').data('slide', '#releases').end().children('.causes').data('slide', '#causes');

				i = 0;
				$('#main .section').each(function() {
					$(this).data('eq', i);
					i++;
				}).slides({
					speed				: 400,
					defaultState		: {
						position		: 'absolute',
						top				: headerHeight + 50
					},
					currentState		: {
						left			: 0
					},
					nonCurrentState		: {
						left			: windowWidth * 2
					},
					currentTransition	: {
						left			: 0
					},
					nonCurrentTransition: {
						left			: -windowWidth * 2
					}
				}, function() {
					$('.recent.media').hide();
				}, function() {
					$('.recent.media').fadeIn();
				});


				// Aside Mods
				$aside.wrapInner('<div class="container" />');
				$aside.prepend('<div class="trigger"><a href="#">Expand</a></div>');

				asideOpenBottom = $footer.outerHeight();
				asideClosedBottom = $footer.outerHeight() - $aside.outerHeight();
				$aside.css('bottom',asideClosedBottom);
				$aside.find('.trigger a').toggle(function() {
					$aside.animate({
						bottom			: asideOpenBottom
					});
					$(this).text('collapse');
				}, function() {
					$aside.animate({
						bottom			: asideClosedBottom
					});
					$(this).text('expand');
				});

				// Login / Register Popup
				/*
				$('#navigation .controls .login a').attr('href', '#login').fancybox({
				autoDimensions		: false,
				width				: 370,
				height				: 270
				});
				$('#navigation .controls .mailing_list a').attr('href', '#subscribe').fancybox({
				autoDimensions		: false,
				width				: 370,
				height				: 170
				});
				$('#subscribe input[type="submit"]').click($.fancybox.close);
				*/
			}

/***** NEWS *****/
if($('body').hasClass('news') && $('.section.news.all').length > 0) {

	$('body').addClass('all');

	// News Slider
	$section = $('#main .section.news.all');
	$listing = $('#main .section.news.all > .listing');		
	$items = $listing.find('.item');
	diff = ($section.width() - $items.eq(0).outerWidth(true)) / 2;
	$section.css({
		marginLeft	: diff,
		marginRight	: diff
	});
	$section.children('.header').css({
		marginLeft	: -diff,
		marginRight	: -diff
	});
	$listing.css('width', $items.eq(0).outerWidth(true) * $items.length);
	$items.equalHeights();
	$items.css('opacity', 0.25);
	$items.filter(':first-child').addClass('current').css('opacity', 1);

	$items.each(function() {
		$(this).data('offset', $(this).position());
	});

	$items.click(function() {
		var offset = $(this).data('offset');

		$items.filter('.current').stop().animate({
			opacity		: 0.25
			}).removeClass('current');
			$(this).addClass('current').stop().animate({
				opacity		: 1
			});
			$listing.stop().animate({
				marginLeft	: -offset.left
			});
		});

		$('#aside .section').equalHeights();

		pagePadding = $page.outerHeight() - $page.height();

		$page.css({
			height		: $(document).height() - pagePadding,
			overflow	: 'hidden'
		});
	}
}

if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) {
	window.scrollTo(0,1);
	$('#dashboard').hide();
	$('#navigation .nav> ul > li > span').toggle(function() {
		$(this).siblings('ul').css('left',0);
	}, function() {
		$(this).siblings('ul').css('left',-9999);
	})
}

});
