var mayumi = (function(){

	var page,
		left,
		right,
		did,
		w,
		h,
		dropWidth,
		dropHeight,
		el,
		currentDrop,
		currentId,
		t,
		l,
		lastT,
		lastL,
		storyPage;
	return{
		init: function(p){
			self = this;
			currentDrop = $('#str');
			
			el   = $('#backdrops');
			self.size();
			
			if(p != "")
				self.animate(p);
			
			
			$(window).bind('resize', function(){
				self.size();
			});

		},
		size: function(){
			w = $(document).width();
			dropWidth = w * 3;
						
			h = $(document).height();
			dropHeight = h * 3;
			
			$('#backdrops').css({width:dropWidth,height:dropHeight});
			currentDrop.css({width:dropWidth,height:dropHeight});
			$('.backdrop').css({width:w,height:h});
			self.setXY(did);
			el.css({'left':l,'top':t});
		},
		_loadPage: function(id,ad){
			$.ajax({
  				url: 'pages/'+id+'.html',
  				success:function(d,ts,jqXHR){
  					$('#'+ad).html('');
  					$('#'+ad).html(d);
  					$('#'+ad).addClass('loaded');
  					$("#slider").easySlider({
						continuous: true 
					});
					
					if(id=='photos')
						self._setupPhotoSlide();
					if(id=='videos')
						self._setupVideo();
					if(id="story")
						self._setupStory();
					if(id='timeline')
						self._setupTimeline();
  				}	
			});
		},
		_setupTimeline: function(){
			$().timelinr({containerDiv:'#tmline'});
		},
		_setupPhotoSlide: function(){
			
			$('.photo-link').click(function(){
				var id= $(this).attr('id');
				
				$.ajax({
  					url: 'pages/photocontroller.php',
  					data: 'set='+id,
  					success:function(d,ts,jqXHR){
  						$('.photo-col-b').fadeOut('fast',function(){
  							$(this).html(d);
  							$(this).fadeIn('fast');
  							$("#slider").easySlider({
								continuous: true 
							});
  					
  						});
  					}
				});
			
			});
			
			$('.video-link').click(function(){
					$('.redbox').addClass('hidden');
					var rel = $(this).attr('id');
					$('div[rel="'+rel+'"]').removeClass('hidden');
			});
		},
		_setupVideo: function(){
			$('.video-link').click(function(){
				var id= $(this).attr('id');
				
				$.ajax({
  					url: 'pages/videocontroller.php',
  					data: 'set='+id,
  					success:function(d,ts,jqXHR){
  						$('#video-col-b').fadeOut('fast',function(){
  							$(this).html(d);
  							$(this).fadeIn('fast');
  						});
  					}
				});
			
			});
			
			$('.video-link').click(function(){
					$('.redbox').addClass('hidden');
					var rel = $(this).attr('id');
					$('div[rel="'+rel+'"]').removeClass('hidden');
			});
		},
		_setupStory: function(){
			self = this;
			
			$('.story-nav').click(function(){
				var page = $('#story-text').attr('rel');
				var control = $(this).attr('id');
				
				$('#story-'+page).fadeOut('fast', function(){
					if(control == "story-arrow-left"){
					page --;
					if(page == 1){
						$("#story-arrow-left").fadeOut('fast');
					}
					if(page == ($('.story-text').size()-1)){
						$("#story-arrow-right").fadeIn('fast');
					}
					if(page <= 0){
						page = 1;
					}
				}
				
				if(control == "story-arrow-right"){
					page ++;
					if(page == $('.story-text').size()){
						$("#story-arrow-right").fadeOut('fast');
					}
					if(page == 2){
						$("#story-arrow-left").fadeIn('fast');
					}
					if(page >= $('.story-text').size()){
						page = $('.story-text').size();
					}
				}
				
				 $('#story-text').attr('rel',page);
				 $('#story-'+page).fadeIn('fast');
					
				
				});	
			});
			
			
		},
		_decidePath: function(){
			var moveVert=false;
			var moveHoriz=false;
			if(lastT != t)moveVert=true;
			if(lastL != l)moveHoriz=true;
			
			if(moveVert && moveHoriz)
				return 'animateXY';
			if(moveVert && !moveHoriz)
				return 'animateY';
			if(!moveVert && moveHoriz)
				return 'animateX';
			
			return 'noanimation';
		
		},
		_getTwitterStatus : function(){
			var user = 'Karenolivo';
      
   			 // using jquery built in get json method with twitter api, return only one result
    		$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' + user + '&count=1&include_rts=1&callback=?', function(data)      {
          
       		 	// result returned
        		var tweet = data[0].text;
        		var created =data[0].created_at;
      			if(tweet.length >75)
      				tweet = tweet.substr(0,75) + " ...";
        		// process links and reply
       			 tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) {
           		 	return '<a href="'+url+'">'+url+'</a>';
       			 }).replace(/B@([_a-z0-9]+)/ig, function(reply) {
           		 	return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
       			 });
      			
       		 	// output the result
       			 $("#twitter-body p a").html(tweet);
       			 $('#twitter-time').html(self._formatTime(created));
    		}); 
		
		},
		_formatTime : function(dateString) {
        	var rightNow = new Date();
        	var then = new Date(dateString);
         
       		 if ($.browser.msie) {
           		 // IE can't parse these crazy Ruby dates
           		 then = Date.parse(dateString.replace(/( \+)/, ' UTC$1'));
        	 }
 
        	var diff = rightNow - then;
 
        	var second = 1000,
        	minute = second * 60,
        	hour = minute * 60,
        	day = hour * 24,
       		 week = day * 7;
 
        	if (isNaN(diff) || diff < 0) {
            	return ""; // return blank string if unknown
       		 }
 
       		 if (diff < second * 2) {
           		 // within 2 seconds
            		return "created right now";
       		 }
 
        	if (diff < minute) {
            	return "created " + Math.floor(diff / second) + " seconds ago";
        	}
 
        	if (diff < minute * 2) {
            	return "created about 1 minute ago";
        	}
 
        	if (diff < hour) {
           	    return "created " + Math.floor(diff / minute) + " minutes ago";
        	}
 
        	if (diff < hour * 2) {
            	return "created about 1 hour ago";
       		 }
 
        	if (diff < day) {
            	return  "created " + Math.floor(diff / hour) + " hours ago";
       		 }
 
        	if (diff > day && diff < day * 2) {
            	return "created yesterday";
        	}
 
        	if (diff < day * 365) {
            	return "created " + Math.floor(diff / day) + " days ago";
       		 }
 
        	else {
           		 return "created over a year ago";
       		 }
   		 },
		setXY : function(id){
			switch(id){
		
				case 'timeline':
					t = 0; l =-w*2;
					currentDrop = $('#tline');
					break;
				case 'videos':
					t = -h; l = 0;
					currentDrop = $('#mus');
					break;
				case 'photos':
					t = -h; l = -w;
					currentDrop = $('#phot');
					break;
				case 'projects':
					t = -h; l = -w*2;
					currentDrop = $('#proj');
					break;
				case 'contact':
					t = -h*2; l = 0;
					currentDrop = $('#cont');
					break;
				case 'story':
					t = 0; l = -w;
					currentDrop = $('#str');
					id="story";
					break;	
				default:
					t = 0; l = 0;
					currentDrop = $('#home');
					id="home";
					$('#page-nav').css({'display':'none'});
					$('#page-nav').addClass('hidden');
					if($('.backdrop#home #index-wrap, #twitter').hasClass('gone')){
						$('.backdrop#home #index-wrap, #twitter').animate({opacity:200},function(){$(this).removeClass('gone')});
					}
					break;			
			
			}
		
		
		},
		navSelector : function(id){
			$('.page-nav-link a').removeClass('nav-selected');
			$('.page-nav-link#'+id+' a').addClass('nav-selected');
		},
		shouldTurnOffLowerNav : function(id){
			if(id != 'home'){
				if($('#page-nav').hasClass('hidden')){
					$('#page-nav').css({'display':'block'});
					$('#page-nav').removeClass('hidden');
				}
				if(!$('.backdrop#home #index-wrap, #twitter').hasClass('gone')){
					$('.backdrop#home #index-wrap, #twitter').animate({opacity: 0},function(){$(this).addClass('gone')});
				}
			}else{
				
			
			}
		},
		animate: function(id){
		
			
			//$('.backdrop').animate({'opacity':.5},'fast','easeInOutCubic');
			if(!$('#'+id).hasClass('active'))
				$('.drop-content').animate({'opacity':0},400,'easeInOutCubic');
			
			
			did = id;
			//$('#'+id).animate({'opacity':1},700,'easeInOutCubic');
			self.setXY(did);
			
			currentId = currentDrop.attr('id');
			if(!currentDrop.hasClass('loaded'))
				self._loadPage(id, currentId);
				
			var path = self._decidePath();
			switch(path){
				case 'animateXY':
					el.animate({left:l},2000,'easeInOutCubic',function(){
						$(this).animate({top:t},2000,'easeInOutCubic',function(){
							currentDrop.animate({'opacity':1},'fast','easeInOutCubic');
						   $('div#'+currentId+" > " +'div.drop-content').animate({'opacity':1},375,'jswing',function(){self.shouldTurnOffLowerNav(id);});
						});
					});
					break;
				case 'animateY':
					el.animate({top:t},2000,'easeInOutCubic',function(){
						currentDrop.animate({'opacity':1},'fast','easeInOutCubic');
					   	$('div#'+currentId+" > " +'div.drop-content').animate({'opacity':1},375,'jswing',function(){self.shouldTurnOffLowerNav(id);});
					});
					break;
				case 'animateX':
					el.animate({left:l},2000,'easeInOutCubic',function(){
						currentDrop.animate({'opacity':1},'fast','easeInOutCubic');
					   	$('div#'+currentId+" > " +'div.drop-content').animate({'opacity':1},375,'jswing',function(){self.shouldTurnOffLowerNav(id);});
					});
					break;
				default:
					currentDrop.animate({'opacity':1},'fast','easeInOutCubic');
					$('div#'+currentId+" > " +'div.drop-content').animate({'opacity':1},375,'jswing',function(){self.shouldTurnOffLowerNav(id);});
					break;
			
			}
			
			
			lastT = t;
			lastL = l;
		}
		
	
	};

})();
$(document).ready(function(){
	var pathArray = window.location.pathname.split( '/' );
	mayumi.init(pathArray[1]);
	
	 $.address.init(function() {
 
        // Initializes the plugin
       $('.nav').address();                
      }).change(function(event) {
              var pa = event.path.split( '/' );
              mayumi.animate(pa[1]);
              mayumi.navSelector(pa[1]);
       });
       mayumi._getTwitterStatus();
       
      
});
