var uri;
var pos = 0;
var i;
var w = 1280;
var container;
var myFx;
var intervalID;
var loadIndex;
var loadItems;
var contentLinks;


window.onload = function () {
	
	var page = pageID();
	
	// insertSocialMediaLinks();

	container = $('container');
	
	if (container) {
		
		container.fade('hide');
		container.fade('in');
		
		$('text').fade('hide');
		$('text').fade('in');
		$('nav-prev').fade('hide');
		$('nav-next').fade('hide');
		
		$('nav-prev').onclick = movePrev;
		$('nav-next').onclick = moveNext;

		$('back-to-start').onclick = backToStart;
		
		myFx = new Fx.Tween(container, {property: 'margin-left', link:'cancel', transition: 'quad:out'});
		myFx.addEvent('chainComplete', function() {
				if (pos == 0) $('text').fade('in');
			});
		
		if (!(Browser.Platform.ios || Browser.Platform.android)) {
		
			setSize();
			
			var timer;
			window.addEvent('resize', function(){
	  			$clear(timer);
				timer = (function(){
					setSize();
				}).delay(1000);
			}); 
		
		}
		
		loadContent();
		
	}

}


// general


function debug(d) { $('debug').innerHTML = d; }

function pageID () { return $$('body').get('id') }

function getDimensions() {
	var myWidth = 0;
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	var dimensions = { w: myWidth, h: myHeight }
	return dimensions;
}

function insertSocialMediaLinks () {

	var socialLinks = $('social-links').dispose();
	socialLinks.inject($$('#navigationBottom .content-navigation')[0], 'before');

}

function setSize () {
	
	if (getDimensions().h < 840) {
		w = 960;
		$('HDcontent').addClass('size-M');
	} else {
		w = 1280;
		$('HDcontent').removeClass('size-M');	
	}
	
	slideTo(pos);

}


// home page




// content loading


function loadContent () {

	loadIndex = 0;
	
	contentLinks = $$('#content-links a');
	loadItems = contentLinks.length;
	loadItem(contentLinks[0].get('class'), contentLinks[0].get('href'));
	setStoryNav();

}

function loadItem (type, src) {
	
	function injectItem (item) {
		item.inject($('story-nav'), 'before');
		item.fade('hide');
		item.fade('in');
		updateNav();
		loadIndex += 1;
		if (loadIndex < (loadItems)) {
			loadItem(contentLinks[loadIndex].get('class'), contentLinks[loadIndex].get('href'));
		} else {
			$$('h2.title')[0].style.backgroundImage = 'none';
		}
	}

	switch (type) {
	
		case 'photo':
		
			Asset.image(src, {
			    onLoad: function() {
		 			var newItem = new Element ('div', {'class': 'item' });
	 				this.set('width', '100%');
	 				this.set('height', '100%');
	 				this.inject($(newItem));
		 			injectItem(newItem);
					}
			});
			break;
		
		case 'video':
		
		 	var newItem = new Element ('div', {'class': 'item' });
	 		var newIframe =  new Element ('iframe', {'src': src, 'width': '100%', 'height': '100%', 'frameborder': '0' });
		 	newIframe.inject(newItem);
		 	injectItem(newItem);
			break;
		
	}

}

function setStoryNav () {

	var nextStory = $$('a.journal-entry-navigation-next');

	if (nextStory.length > 0) {
		var nextStoryUrl = nextStory.getProperty('href');
		var nextStoryTitle = String.from(nextStory.getProperty('html'));
		nextStoryTitle = nextStoryTitle.substr(0, nextStoryTitle.length-2)
		$('next-story-link').setProperties ({
			'href': nextStoryUrl,
			'html': nextStoryTitle,
		});
		$('next-story').style.display = 'block';
		
	}

	var prevStory = $$('a.journal-entry-navigation-prev');

	if (prevStory.length > 0) {
		var prevStoryUrl = prevStory.getProperty('href');
		var prevStoryTitle = String.from(prevStory.getProperty('html'));
		prevStoryTitle = prevStoryTitle.substr(2)
		$('prev-story-link').setProperties ({
			'href': prevStoryUrl,
			'html': prevStoryTitle,
		});
		$('prev-story').style.display = 'block';
		
	}

	$('story-nav').fade('hide');

}

// navigation

function updateNav () {

	i = $$('#container .item').length;
	var screenWidth = getDimensions().w;
	
	if (((i-pos) * w) + 80 > getDimensions().w) {
		$('nav-next').fade('in');
		$('story-nav').fade('out');
	} else {
		$('nav-next').fade('out');
		if (i == loadItems) $('story-nav').fade('in');
	}
	
	if (pos == 0) {
		$('text').fade('in');
		$('nav-prev').fade('out');
	} else {
		$('text').fade('out');
		$('nav-prev').fade('in');
	}


}

function movePrev () { if (pos >= 1) slide(-1); }

function moveNext () { if (pos < i-1) slide(1); }

function backToStart () { slideTo(0); }

function slide (d) {
	
	var startPos = -pos * (w);
	if (pos > 0) startPos += 80;
	if (pos > 0) startPos += 1;
	pos += d;
	var endPos = -pos * (w) + 80;
	if (endPos > 0) endPos = 0;

	myFx.start(startPos, endPos);

	updateNav();
	
}

function slideTo (p) {

	var startPos = -pos * (w);
	if (pos > 0) startPos += 80;
	var endPos = -p * (w) + 80;
	if (endPos > 0) endPos = 0;

	pos = p;
	
	myFx.start(startPos, endPos);
	
	updateNav();


}

