var imageScroll, imageSlide, fadeLeft, fadeRight, fadeArrowL, fadeArrowL, myRequest, scrollOpacity = 1, albumCover, firstImage, currentImage, activity = false, sactivity = false, mactivity = false, morph, oldHash;
window.addEvent('domready', function(){ 
	var pos = $('measure').getPosition()
	if ($('right-cover'))
	{
		$('right-cover').setStyle('width', pos.x + "px");
		$('left-cover').setStyle('width', pos.x + "px");
		$('left-pad').setStyle('width', (pos.x) + "px");
		$('right-pad').setStyle('width', (pos.x + 54) + "px");
	}
	imageScroll = new Fx.Scroll($('wrapper'), {
		wait: false,
		duration: 2500,
		offset: {'x': (-pos.x), 'y': 0},
		transition: Fx.Transitions.Quad.easeInOut,
		onStart: function() {activity = true;},
		onChainComplete: function() {activity = false;}
	});
	imageSlide = new Fx.Tween('wrapper', {duration: 1000, onStart: function() {sactivity = true;}, onChainComplete: function() {sactivity = false;}});
	currentImage = "image-11";
	$('fashion').addEvent('click', function(){loadCategory('fashion');});
	$('fashion').addEvent('mouseenter', function(){bringSquareTo('fashion')});
	$('beauty').addEvent('click', function(){loadCategory('beauty');});
	$('beauty').addEvent('mouseenter', function(){bringSquareTo('beauty')});
	$('personal').addEvent('click', function(){loadCategory('personal');});
	$('personal').addEvent('mouseenter', function(){bringSquareTo('personal')});
	$('contact').addEvent('click', function(){loadCategory('contact');});
	$('contact').addEvent('mouseenter', function(){bringSquareTo('contact')});
	$('services').addEvent('click', function(){loadCategory('services');});
	$('services').addEvent('mouseenter', function(){bringSquareTo('services')});
	$('about').addEvent('click', function(){loadCategory('about');});
	$('about').addEvent('mouseenter', function(){bringSquareTo('about')});
	pos = $('fashion').getCoordinates()
	morph = new Fx.Morph('backfloat', {duration: 1000, onChainComplete: function() {mactivity = false}, onStart: function() {mactivity = true}});
	morph.set({
		height:  (pos.height + 5),
		width:   (pos.width),
		top:     (pos.top - 2.5),
		left:    (pos.left - 2.5),
		opacity: 1
	});
	if (window.location.hash)
		loadCategory(window.location.hash.substring(1));
	checkChangeHash.periodical(100);
});

var doTheFade = function(opacity)
{
	scrollOpacity = opacity;
	attachFunctions();
	if ($('left-cover'))
	{
		fadeLeft.start('opacity', opacity);
		fadeRight.start('opacity', opacity);
		fadeArrowL.start('opacity', (opacity == 1)?0:1);
		fadeArrowR.start('opacity', (opacity == 1)?0:1);
	}
}

var attachFunctions = function()
{
	fadeLeft   = new Fx.Tween('left-cover', {duration: 1000});
	fadeRight  = new Fx.Tween('right-cover', {duration: 1000});
	fadeArrowL = new Fx.Tween('left-arrow', {duration: 1000});
	fadeArrowR = new Fx.Tween('right-arrow', {duration: 1000});
	var pos = $('measure').getPosition()
	if ($('right-cover'))
	{
		$('right-cover').setStyle('width', pos.x + "px");
		$('left-cover').setStyle('width', pos.x + "px");
		$('left-pad').setStyle('width', (pos.x) + "px");
		$('right-pad').setStyle('width', (pos.x + 500) + "px");
	}
	milkbox.reloadGalleries();
}

var nextImage = function()
{
	//fadeArrowL.cancel();
	fadeArrowR.cancel();
	//attachFunctions();
	for(i = 0; i < images.length; i++)
	{
		if (images[i] == currentImage)
		{
			if (i == (images.length - 1))
			{
				if (i == (images.length - 1))
					fadeArrowR.start('opacity', 0);
				else
				{
					fadeArrowR.start('opacity', .85);
					fadeArrowL.start('opacity', .85);
				}
				return false;
			} else {
				b = (i+1);
				while (!images[b] && b < images.length)
					b++;
				if (b == (images.length - 1))
					fadeArrowR.start('opacity', 0);
				else
				{
					fadeArrowR.start('opacity', .85);
					fadeArrowL.start('opacity', .85);
				}
				//attachFunctions()
				imageScroll.toElement(images[b]);
				currentImage = images[b];
				break;
			}
		}
		//alert('this should never happen.')
	}
}

var previousImage = function()
{
	//attachFunctions();
	fadeArrowL.cancel();
	//fadeArrowR.cancel();
	for(i = 0; i < images.length; i++)
	{
		if (images[i] == currentImage)
		{
			if (i == 0)
			{
				if (i-1 <= 0)
					fadeArrowL.start('opacity', 0);
				else
				{
					fadeArrowR.start('opacity', .85);
					fadeArrowL.start('opacity', .85);
				}
				return false;
			} else {
				if (i-1 == 0)
					fadeArrowL.start('opacity', 0);
				else
				{
					fadeArrowR.start('opacity', .85);
					fadeArrowL.start('opacity', .85);
				}
				//attachFunctions();
				b = (i-1);
				while (!images[b] && b > 0)
					b--;
				imageScroll.toElement(images[b]);
				currentImage = images[b];
				break;
			}
		}
	}
}

var loadCategory = function(category)
{
	if (activity)
		return false;
	if (!category || category.contains('usermessage'))
		return false;
	morph.cancel();
	morph.set({
		opacity: 0
	});
	imageSlide.start('height', 0);
	if (scrollOpacity < 1)
		doTheFade(1);
	myRequest = new Request(
		{
			url: '/ajax/' + category + '/',
			method: 'get',
			evalScripts: true,
			onSuccess: 
				function(responseText) {
					/*images = $$('.gallery-a-image');
					for(i = 0; i < images.length; i++)
						images[i].dispose();*/
					$('wrapper').innerHTML = responseText;
					//imageScroll.set(0, 10);
					attachFunctions();
					imageScroll.set(0,0);
					imageSlide.start('height', 400);
					if ($(albumCover))
						setTimeout("doTheFade(.85);imageScroll.toElement(albumCover);currentImage = albumCover;setTimeout('previousImage();', 500);", 1000);
					else
						setTimeout("doTheFade(.85);setTimeout('currentImage=images[0];previousImage();', 500);", 1000);
				}
		}
	);
	oldHash = window.location.hash = "#" + category;
	highlight(category);
	setTimeout("myRequest.get();", 1000);
}

var highlight = function(category)
{
	$$('.links').each(
		function(el)
		{
			background = el.getStyle('background-image');
			el.setStyle('background-image', background.replace('-selected', ''));
		}
	);
	background = $(category).getStyle('background-image');
	$(category).setStyle('background-image', background.replace('.png', '-selected.png'));
	
}

var bringSquareTo = function(id)
{
	if (morph && !activity)
		morph.cancel();
	if (sactivity)
		return false;
	// #d3d3d3
	pos = $(id).getCoordinates()
	morph = new Fx.Morph('backfloat', {duration: 1000, onChainComplete: function() {mactivity = false}, onStart: function() {mactivity = true}});
	morph.start({
		height:  (pos.height + 5),
		width:   (pos.width),
		top:     (pos.top - 2.5),
		left:    (pos.left - 2.5),
		opacity: 1
	});
}

var checkChangeHash = function()
{
	if (window.location.hash.indexOf("cf_field") != -1)
	{
		window.location.hash = oldHash;
		imageScroll.set(0,0);
	}
	if (oldHash != window.location.hash && window.location.hash != "#" && window.location.hash != '' && window.location.hash != "usermessagea")
	{
		//activity = mactivity = sactivity = false;
		oldHash = window.location.hash;
		loadCategory(window.location.hash.substring(1));
	}
}