var MNJohn = {};

MNJohn.ready = function()
{
	MNJohn.window = $(window);
	MNJohn.noOpacity = $.browser.msie;
	MNJohn.subContainer = $('#sub-container');
	MNJohn.subContainedLink = MNJohn.subContainer.children('a');
	MNJohn.subContainedImage = MNJohn.subContainedLink.children('.current-large-image').addClass('z-front');
	MNJohn.window.resize(MNJohn.change).scroll(MNJohn.change);
	MNJohn.foregroundAccents = $('.accent-color-foreground');
	MNJohn.backgroundAccents = $('.accent-color-background');
	MNJohn.inverse = {top: 'bottom', bottom: 'top'};
	MNJohn.aspirium = $('#aspirium');
	MNJohn.copyrightArtist = $('#copyright-artist');
	MNJohn.about = $('#about');
	MNJohn.aboutTimeout = false;
	MNJohn.title = $('#mn-john').mouseenter(MNJohn.controlAbout).mouseleave(MNJohn.controlAbout);
	MNJohn.description = $('#description').mouseenter(MNJohn.onDescriptionMouse).mouseleave(MNJohn.onDescriptionMouse);
	MNJohn.pageDescription = MNJohn.description.children('#page-description');
	MNJohn.itemIndex = 0;
	MNJohn.aboutAllowed = true;
	MNJohn.items[0].href = MNJohn.subContainedLink.attr('href');
	MNJohn.items[0].image = MNJohn.subContainedImage;
	MNJohn.items[0].actionButton = MNJohn.pageDescription.children('.details-button');
	if(!MNJohn.noOpacity) MNJohn.items[MNJohn.itemIndex].actionButton.css({opacity: 0.6});
	MNJohn.adjustLayoutValues();
	MNJohn.change();
	MNJohn.transitionTimeout = setTimeout(MNJohn.getNextItem, 8000);
}

MNJohn.adjustLayoutValues = function()
{
	MNJohn.titleOn = MNJohn.items[MNJohn.itemIndex].titleOn;
	MNJohn.descriptionOn = MNJohn.items[MNJohn.itemIndex].descriptionOn;
	MNJohn.about.addClass('title-on-'+MNJohn.titleOn).removeClass('title-on-'+MNJohn.inverse[MNJohn.titleOn]);
}

MNJohn.getNextItem = function()
{
	MNJohn.transitionTimeout = false;
	MNJohn.itemIndex ++;
	if(MNJohn.itemIndex >= MNJohn.items.length) MNJohn.itemIndex = 0;
	if(MNJohn.items[MNJohn.itemIndex].image)
	{
		MNJohn.transitionImage();
	}
	else
	{
		MNJohn.items[MNJohn.itemIndex].image = $(document.createElement('img'));
		MNJohn.items[MNJohn.itemIndex].image.load(MNJohn.transitionImage).addClass('large-image');
		MNJohn.items[MNJohn.itemIndex].image[0].src = '/media/' + MNJohn.items[MNJohn.itemIndex].id + '.jpg';
		MNJohn.subContainedLink.append(MNJohn.items[MNJohn.itemIndex].image);
	}
}

MNJohn.transitionImage = function()
{
	//$('title').text('trans start');
	MNJohn.previousLargeImage = MNJohn.subContainedImage.removeClass('z-front');
	MNJohn.subContainedImage = MNJohn.items[MNJohn.itemIndex].image.addClass('z-front').addClass('current-large-image').css({opacity: 0}).animate({opacity: 1}, {duration: 1000, complete: MNJohn.completeTransition});
	if(MNJohn.noOpacity)
	{
		if(MNJohn.items[MNJohn.itemIndex].titleOn === false) MNJohn.title.css({display: 'none'});
	}
	else
	{
		if(MNJohn.titleOn != MNJohn.items[MNJohn.itemIndex].titleOn)
		{
			MNJohn.title.stop(true, false).animate({opacity: 0}, {duration: 450, queue: false});
		}
		MNJohn.description.stop(true, false).animate({opacity: 0}, {duration: 450, queue: false});
	}
	MNJohn.change();
	MNJohn.transitionTimeout = setTimeout(MNJohn.getNextItem, 8000);
	setTimeout(MNJohn.transitionObjects, 500);
}

MNJohn.transitionObjects = function()
{
	if(MNJohn.noOpacity)
	{
		MNJohn.title.css({display: (MNJohn.items[MNJohn.itemIndex].titleOn !== false) ? 'block' : 'none'});
		MNJohn.description.css({display: 'block'})
	}
	else
	{
		if((MNJohn.items[MNJohn.itemIndex].titleOn !== false) && (MNJohn.titleOn != MNJohn.items[MNJohn.itemIndex].titleOn)) MNJohn.title.stop(true, false).animate({opacity: 1}, {duration: 650, queue: false});
		MNJohn.description.stop(true, false).animate({opacity: 1}, {duration: 650, queue: false});
	}
	MNJohn.description.css({width: MNJohn.items[MNJohn.itemIndex].descriptionWidth})
	MNJohn.subContainedLink.attr('href', MNJohn.items[MNJohn.itemIndex].href);
	MNJohn.pageDescription.attr('href', MNJohn.items[MNJohn.itemIndex].href);
	MNJohn.pageDescription.children('.details-button').detach();
	MNJohn.pageDescription.html(MNJohn.items[MNJohn.itemIndex].text);
	if(MNJohn.items[MNJohn.itemIndex].actionButton)
	{
		MNJohn.pageDescription.append(MNJohn.items[MNJohn.itemIndex].actionButton);
	}
	else
	{
		MNJohn.items[MNJohn.itemIndex].actionButton = $(document.createElement('span'));
		MNJohn.pageDescription.append(MNJohn.items[MNJohn.itemIndex].actionButton);
		MNJohn.items[MNJohn.itemIndex].actionButton.addClass('details-button').css({backgroundColor: MNJohn.items[MNJohn.itemIndex].accentColor}).html(MNJohn.items[MNJohn.itemIndex].action + '&nbsp;<span class="ext-chr-safe">&raquo;</span>');
		if(!MNJohn.noOpacity) MNJohn.items[MNJohn.itemIndex].actionButton.css({opacity: 0.6});
		MNJohn.backgroundAccents = MNJohn.backgroundAccents.add(MNJohn.items[MNJohn.itemIndex].actionButton);
	}
	MNJohn.descriptionOn = MNJohn.items[MNJohn.itemIndex].descriptionOn;
	MNJohn.foregroundAccents.css({color: MNJohn.items[MNJohn.itemIndex].accentColor});
	MNJohn.backgroundAccents.css({backgroundColor: MNJohn.items[MNJohn.itemIndex].accentColor});
	MNJohn.adjustLayoutValues();
	MNJohn.change();
}

MNJohn.completeTransition = function()
{
	//$('title').text('trans complete');
	MNJohn.previousLargeImage.removeClass('current-large-image');
}
MNJohn.controlAbout = function(evt)
{
	if(evt.type=='mouseenter')
	{
		if(MNJohn.aboutTimeout !== false) { clearTimeout(MNJohn.aboutTimeout); MNJohn.aboutTimeout = false; }
		if(MNJohn.aboutAllowed)
		{
			if(MNJohn.transitionTimeout !== false) { clearTimeout(MNJohn.transitionTimeout); MNJohn.transitionTimeout = false; }
			MNJohn.aboutAllowed = false;
			MNJohn.about.slideDown(500, MNJohn.allowAbout);
		}
	}
	else
	{
		if(MNJohn.aboutTimeout === false) { MNJohn.aboutTimeout = setTimeout(MNJohn.closeAbout, 1000); }
	}
}

MNJohn.allowAbout = function() { MNJohn.aboutAllowed = true; }

MNJohn.closeAbout = function()
{
	MNJohn.aboutAllowed = false;
	MNJohn.about.slideUp(500, MNJohn.allowAbout);
	if(MNJohn.transitionTimeout === false) { MNJohn.transitionTimeout = setTimeout(MNJohn.getNextItem, 4000); }
}

MNJohn.change = function()
{
	var titleCss, descriptionCss, containerCss;
	MNJohn.wWidth = MNJohn.window.width();
	MNJohn.wHeight = MNJohn.window.height();
	MNJohn.aspect = MNJohn.wWidth / MNJohn.wHeight;
	MNJohn.generalSize = (MNJohn.wWidth + MNJohn.wHeight) / 2;
	MNJohn.minSize = Math.min(MNJohn.wHeight, MNJohn.wWidth);
	MNJohn.subContainer.addClass('w-horizontal').removeClass('w-vertical').css({width: MNJohn.minSize + 'px', height: MNJohn.minSize + 'px'});
	MNJohn.sWidth = MNJohn.subContainedImage.width();
	MNJohn.sHeight = MNJohn.subContainedImage.height();
	if((MNJohn.sHeight == 0) || (MNJohn.sWidth == 0))
	{
		MNJohn.subContainedImage.load(MNJohn.change);
	}
	else
	{
		containerCss = {left:(MNJohn.wWidth / 2) - (MNJohn.sWidth / 2), top: (MNJohn.wHeight / 2) - (MNJohn.sHeight / 2)}
		MNJohn.subContainer.css(containerCss);
	}
	if(MNJohn.items[MNJohn.itemIndex].titleOn)
	{
		titleCss = {fontSize: MNJohn.generalSize / 8, lineHeight: (MNJohn.generalSize / 7) + 'px', right: MNJohn.generalSize / 25};
		titleCss[MNJohn.titleOn] = MNJohn.generalSize / 30;
		titleCss[MNJohn.inverse[MNJohn.titleOn]] = 'auto';
		MNJohn.title.css(titleCss);
	}
	descriptionCss = {fontSize: MNJohn.generalSize / 25, lineHeight: (MNJohn.generalSize / 20) + 'px', left: MNJohn.generalSize / 20}
	descriptionCss[MNJohn.descriptionOn] = MNJohn.generalSize / 20;
	descriptionCss[MNJohn.inverse[MNJohn.descriptionOn]] = 'auto';
	MNJohn.description.css(descriptionCss);
	MNJohn.aspirium.css({display: (MNJohn.title.width() > 230) ? 'block' : 'none'});
	MNJohn.copyrightArtist.css({display: (MNJohn.title.width() > 310) ? 'inline' : 'none'});
}

MNJohn.onDescriptionMouse = function(evt)
{
	if(evt.type=='mouseenter')
	{
		if(MNJohn.transitionTimeout !== false) { clearTimeout(MNJohn.transitionTimeout); MNJohn.transitionTimeout = false; }
		if(!MNJohn.noOpacity) MNJohn.items[MNJohn.itemIndex].actionButton.stop(true, false).animate({opacity: 1}, {duration: 400});
	}
	else
	{
		if(MNJohn.transitionTimeout === false) { MNJohn.transitionTimeout = setTimeout(MNJohn.getNextItem, 4000); }
		if(!MNJohn.noOpacity) MNJohn.items[MNJohn.itemIndex].actionButton.stop(true, false).animate({opacity: 0.6}, {duration: 400});
	}
}

$(MNJohn.ready);
