/* Initialize */

var maxId = 0;

Event.observe(window, 'load', function() {

	new Ajax.Request('index.php?id=max', { onComplete: function(transport) { 

		maxId = parseInt(transport.responseText); 
	
		if (screen.width < 1200) {
		
			window.location = 'http://m.anselia.net';

		}

		$('centerView').style.right = (window.outerWidth / 2) + 'px';

		$('centerView').show();
		$('rightView').show();
		$('quotesView').show();

	}});


});

/* Highlight & Downlight */

function highlightEntry(id) { new Effect.Opacity($(id), { from: 0.7, to: 1.0, duration: 0.2 }); }

function downlightEntry(id) { new Effect.Opacity($(id), { from: 1.0, to: 0.7, duration: 0.2 }); }


/* Switching Views */

var animated = 4;

function previousView(element) {
		
	if (element.up().id == 'centerView') {
		showEntry(element.src);
		return;
	}
	
	if (animated != 4) {
		return;
	}
	
	animated = 0;

	Effect.Fade('quotesView', { duration: 0.5, afterFinish: function() {
		
		$('farLeftView').style.right = '-820px';
				
		new Effect.Morph('leftView', { style: 'right: ' + (window.outerWidth + 960) + 'px;', duration: 0.8, afterFinish: function() { animated++; } });
		
		new Effect.Morph('centerView', { style: 'right: ' + (window.outerWidth + 300) + 'px;', duration: 0.8, afterFinish: function() { animated++; } });		

		new Effect.Morph('rightView', { style: 'right: ' +  (window.outerWidth / 2) + 'px;', duration: 0.8, afterFinish: function() { 
			
			animated++; 
			
			bottomId = $('centerView').down(0).src.replace('http://anselia.net/images/post.php?id=', '');
			topId = $('centerView').down(1).src.replace('http://anselia.net/images/post.php?id=', '');

			new Ajax.Request('http://anselia.net/images/quote.php?eid=' + bottomId, { onComplete: function(transport) { 

				new Ajax.Request('http://anselia.net/images/quote.php?eid=' + topId, { onComplete: function(transport) { 

					$('quoteTop').src = 'images/quote.php?eid=' + topId;
					$('quoteBottom').src = 'images/quote.php?eid=' + bottomId;

					Effect.Appear('quotesView', { duration: 0.5 });


				}});

			}});
			
		} });
		
		new Effect.Morph('farRightView', { style: 'right: -300px;', duration: 0.8, afterFinish: function() { animated++; } });
		
		temp = $('farRightView');
		
		$('farLeftView').id = 'farRightView';
		$('leftView').id = 'farLeftView';
		$('centerView').id = 'leftView';
		$('rightView').id = 'centerView';
		
		temp.id = 'rightView';
		
		leftId = parseInt( $('rightView').down(0).src.replace('http://anselia.net/images/post.php?id=', '')) - 2;
		rightId = parseInt( $('rightView').down(1).src.replace('http://anselia.net/images/post.php?id=', '')) - 2;
										
			new Ajax.Request('http://anselia.net/images/post.php?id=' + leftId, { onComplete: function(transport) { 
		
				new Ajax.Request('http://anselia.net/images/post.php?id=' + rightId, { onComplete: function(transport) { 

					if (leftId <= 0) { $('farRightView').down(0).hide(); } else { $('farRightView').down(0).show(); }
					if (rightId <= 0) { $('farRightView').down(1).hide(); } else { $('farRightView').down(1).show(); }

					$('farRightView').down(0).src = 'http://anselia.net/images/post.php?id=' + leftId;
					$('farRightView').down(1).src = 'http://anselia.net/images/post.php?id=' + rightId;
				
				}});	
			
		}});

						
	} });
	
}


function nextView(element) {

	if (element.up().id == 'centerView') {
		showEntry(element.src);
		return;
	}
	
	if (animated != 4) {
		return;
	}
	
	animated = 0;

	Effect.Fade('quotesView', { duration: 0.5, afterFinish: function() {
		
		$('farRightView').style.right =  (window.outerWidth + 960) + 'px';
				
		new Effect.Morph('rightView', { style: 'right: -600px;', duration: 0.8, afterFinish: function() { animated++; } });
		
		new Effect.Morph('centerView', { style: 'right: -300px;', duration: 0.8, afterFinish: function() { animated++; } });		

		new Effect.Morph('leftView', { style: 'right: ' +  (window.outerWidth / 2) + 'px;', duration: 0.8, afterFinish: function() {
			
			animated++;
			
			bottomId = $('centerView').down(0).src.replace('http://anselia.net/images/post.php?id=', '');
			topId = $('centerView').down(1).src.replace('http://anselia.net/images/post.php?id=', '');

			new Ajax.Request('http://anselia.net/images/quote.php?eid=' + bottomId, { onComplete: function(transport) { 

				new Ajax.Request('http://anselia.net/images/quote.php?eid=' + topId, { onComplete: function(transport) { 

					$('quoteTop').src = 'images/quote.php?eid=' + topId;
					$('quoteBottom').src = 'images/quote.php?eid=' + bottomId;

					Effect.Appear('quotesView', { duration: 0.5 });


				}});

			}});
			
		} });
		
		new Effect.Morph('farLeftView', { style: 'right: ' + (window.outerWidth + 300) + 'px;', duration: 0.8, afterFinish: function() { animated++; } });
		
		temp = $('farRightView');
		
		$('rightView').id = 'farRightView';
		$('centerView').id = 'rightView';
		$('leftView').id = 'centerView';
		$('farLeftView').id = 'leftView';
		
		temp.id = 'farLeftView';
		
		leftId = parseInt( $('leftView').down(0).src.replace('http://anselia.net/images/post.php?id=', '')) + 2;
		rightId = parseInt( $('leftView').down(1).src.replace('http://anselia.net/images/post.php?id=', '')) + 2;
		
		new Ajax.Request('http://anselia.net/images/post.php?id=' + leftId, { onComplete: function(transport) { 
		
			new Ajax.Request('http://anselia.net/images/post.php?id=' + rightId, { onComplete: function(transport) { 

				if (leftId > maxId) { $('farLeftView').down(0).hide(); } else { $('farLeftView').down(0).show(); }
				if (rightId > maxId) { $('farLeftView').down(1).hide(); } else { $('farLeftView').down(1).show(); }

				$('farLeftView').down(0).src = 'http://anselia.net/images/post.php?id=' + leftId;
				$('farLeftView').down(1).src = 'http://anselia.net/images/post.php?id=' + rightId;
				
			}});	
			
		}});
		
		
	} });
}

/ * Opening & Closing Entries */

function showEntry(id) {

	eid = id.replace('http://anselia.net/images/post.php?id=', '');
		
	new Ajax.Request('images/title.php?id=.php?eid=' + eid, { onComplete: function(transport) { 
		
		$('postTitle').src = 'images/title.php?id=' + eid;
	
		new Ajax.Request('index.php?id=' + eid, { onComplete: function(transport) { 
			
			$('postContent').innerHTML = transport.responseText;

			Effect.Appear('postView', { duration: 0.5, from: 0, to: 0.8, afterFinish: function() {

				generateScroller();

			} });

			Effect.Appear('contentArea', { duration: 0.5, from: 0, to: 1.0 });
		
		}}); 
	
	}}); 
}

function closeEntry() {
	
	Effect.Fade('postView', { duration: 0.5, from: 0.8, to: 0 });
	Effect.Fade('contentArea', { duration: 0.5, from: 0.8, to: 0 });
	
}

function generateScroller() {
	
	$('contentView').style.overflow = 'hidden';
    	$('scrollerTrack').style.display = 'block';
    
	var slider = new Control.Slider('scrollerHandle', 'scrollerTrack', {
		axis: 'vertical',
		range: $R(0, 10),
		values: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
		onSlide: function (v) {
			scrollVertical(v, $('contentView'), slider);
		},
		onChange: function (v) {
			scrollVertical(v, $('contentView'), slider);
		}
	});

	if ($('contentView').scrollHeight <= $('contentView').offsetHeight) {
		$('scrollerHandle').style.height = '0%';
	} else {
		$('scrollerHandle').style.height = '10%';
	}

	function scrollVertical(value, element, slider) {
		element.scrollTop = Math.round(value / slider.maximum * (element.scrollHeight - element.offsetHeight));
	}


	function handle(delta) {
		slider.setValueBy(-delta);
	}

	function wheel(event) {

		var delta = 0;

		if (!event) event = window.event;

		if (event.wheelDelta) {

			delta = event.wheelDelta / 120;

			if (window.opera) delta = -delta;

		} else if (event.detail) {

			delta = -event.detail / 3;

		}

		if (delta) {
			handle(delta);
		}

		if (event.preventDefault) {
			event.preventDefault();
		}

		event.returnValue = false;
	}

	Event.observe('contentView', 'DOMMouseScroll', wheel);
	Event.observe('contentView', 'mousewheel', wheel);

	Event.observe('scrollerTrack', 'DOMMouseScroll', wheel);
	Event.observe('scrollerTrack', 'mousewheel', wheel);

}

