/* Version 1.1 */

$(function(){
	

	_loadContent('home');
	
	$(window).resize(function(){
		_resizeWindow();
	});
	
	$('ol.menu a').click(function(e){
		e.preventDefault();
		$('ol.menu a').each(function(){
			$(this).removeClass('active');
		});
		if($(this).attr('href')!="#"){
			_loadContent($(this).attr('href'));
		}
		$(this).addClass('active');
	});
	
	$('a.toggle').click(function(){
		_toggleText();
	});
	
	$(document).bind('keyup', 'i', function(evt){
		console.log(evt);
		{ _toggleText(); }
		return false; 
	});
	
	$(document).bind('keyup', 'e', function(evt){
		window.location = "mailto:me@andy-myers.co.uk";
		return false; 
	});
	
	$(document).bind('keyup', 't', function(evt){
		window.location = "http://twitter.com/andyrmyers";
		return false; 
	});
	
	/* quick hard coding job that allows you to add key functionality */
	
	$(document).bind('keyup', '1', function(evt){
		_loadContent('spectrum');
		$('ol.menu a').removeClass('active');
		$('ol.menu a[href=spectrum]').addClass('active');
		return false; 
	});
	
	$(document).bind('keyup', '2', function(evt){
		_loadContent('bruntwood');
		$('ol.menu a').removeClass('active');
		$('ol.menu a[href=bruntwood]').addClass('active');
		return false; 
	});
	
	$(document).bind('keyup', '3', function(evt){
		_loadContent('12hiltonstreet');
		$('ol.menu a').removeClass('active');
		$('ol.menu a[href=12hiltonstreet]').addClass('active');
		return false; 
	});
	
	$(document).bind('keyup', '4', function(evt){
		_loadContent('greeninthecity');
		$('ol.menu a').removeClass('active');
		$('ol.menu a[href=greeninthecity]').addClass('active');
		return false; 
	});
	
		$(document).bind('keyup', '5', function(evt){
		_loadContent('avid');
		$('ol.menu a').removeClass('active');
		$('ol.menu a[href=avid]').addClass('active');
		return false; 
	});
		
		$(document).bind('keyup', '6', function(evt){
		_loadContent('asi');
		$('ol.menu a').removeClass('active');
		$('ol.menu a[href=asi]').addClass('active');
		return false; 
	}); 
	
	$(document).bind('keyup', '7', function(evt){
		_loadContent('still');
		$('ol.menu a').removeClass('active');
		$('ol.menu a[href=still]').addClass('active');
		return false; 
	});
	
	$(document).bind('keyup', 'a', function(evt){
		_loadContent('home');
		$('ol.menu a').removeClass('active');
		return false; 
	});
	
	
});

var currSlide = 0;
var rate = 500;
var initDelay = 500; /* delay after images are loaded in */
var textOpen;
var kp;

function _resizeWindow() { 
	$('div.items div').width($(window).width()).height($(window).height());
	$('div.items').css('left',-$(window).width()*currSlide);
	$('div.items img').each(function(){
		
		$(this).css('margin-top',_imgDiff($(this))); 
	});
};

function _imgDiff(img){
	/* Replaced this with hard-coded size, will need to work loading out otherwise */
	// iH = img.height();
	iH = 600;
	wH = $(window).height();
	c = wH - iH;
	return Math.floor(c/2);
}

function _updateMarker(a,b){
	$('ul.dir li.marker span').text(a+' / '+b);
}

function _loadContent(content){
	
	_gaq.push(['_trackPageview', '/'+content]); /* Track which portfolio item in Analytics */
	
	$('div.items').fadeOut(rate,function(){
		$('ul.dir,div.navi').fadeOut(rate);
		$('body').prepend('<div class="ajax-loader"></div>');
		$('div.ajax-loader').hide().fadeIn(rate/2);
		$('div.items').load('content/slides/'+content+'.html',function(){
			if($('div.items').children().length>1){
				
				var api = $("div.scrollable").scrollable();
				
				$('#scrollable').scrollable({
					keyboard: true,
					onSeek: function(){
						currSlide = this.getIndex();
						_updateMarker(currSlide+1,this.getSize());
					}
				});

				var api = $("#scrollable").data("scrollable");
				api.begin();
				//_moveNavi();
				$('ul.dir,div.navi').fadeIn(rate);
				
			
				
			}
			$('div.ajax-loader').delay(initDelay).fadeOut(rate/2,function(){
				$('div.items').show();
				_resizeWindow();
				$('div.items div:first').hide();
				$('div.items img:first,div.items div:first').fadeIn(rate,function(){
					$('div.ajax-loader').remove();
				});
			});
		});
		
		
		
		$('div.content').delay(1000).animate({
			'margin-left' : -24
		},rate/2,function(){
			$('div.content_loader').load('content/text/'+content+'.html',function(){
				$('a.toggle').addClass('active').text('i');
				$('div.content_loader').slideDown(rate/2);
				$('div.content').animate({
					'margin-left' : -300
				},rate/2);
				textOpen = true;
			});
		});
		
		/*
		$('div.content_loader').slideUp(rate/2,function(){
			$('div.content_loader').load('content/text/'+content+'.html',function(){
				$(this).delay(initDelay).slideDown(rate/2);
				$('a.toggle').addClass('active').text('i');
				textOpen = true;
			});
		});
		*/
		
	});
	
}

function _toggleText(){
	if(textOpen==true){
		$('a.toggle').removeClass('active').text('i');
		$('div.content_loader').slideUp(rate/2);
		$('div.content').animate({
			'margin-left' : -24
		},rate/2);
		textOpen = false;
	}else{
		$('a.toggle').addClass('active').text('i');
		$('div.content_loader').slideDown(rate/2);
		$('div.content').animate({
			'margin-left' : -300
		},rate/2);
		textOpen = true;
	}
}

