

var Levitation = {} || Levitation;

Levitation.global = function() {
	this.logo = $('#logo');
	this.moreBtn = $('#more');
	this.sidebarMore = $('#dynamic_sidebar #aside');
	this.sidebar = $('#right_sticky');
	this.posts = $('.post-content');
	this.showPosts = $('.show_post');
	this.search = $('#search');
	this.init();
	
	
	// $('#gallery').cycle({
	// 	prev: $('#left'),
	// 	next: $('#right'),
	// 	fx: 'scrollHorz',
	// 	easing:'easeOutCubic'
	// });
	new Levitation.slider();
};

Levitation.global.prototype = {
	init : function() {
		this.moreHandler();
		this.logoEvent();
		this.postHandler();
		this.searchEvent();
	},
	logoEvent : function(){
		this.logo.click(function() {
			document.location = '/';
		});
	},
	postHandler : function() {
		
		
		var winHeight = $(window).height();
		
		
		var allImages = this.posts.find('img');

		allImages.each(function(){
			var height = $(this).height();
			if ( height < winHeight) {
				return;
			}
			if ( height > winHeight) {
				$(this).css({height : winHeight -30 });
			}
		});

		
		if($('body').hasClass('single')){
			this.posts.each(function(){
				var _this = $(this);
				_this.find('.figure, p, footer').fadeIn();
			});
			return;
		}
		
		$('.show_post').css({ display : 'block' });
		
		this.posts.each(function(){
			var _this = $(this);
			_this.find('.figure:first-child').fadeIn();
		});
		
		this.showPosts.toggle(function(){
			var _this = $(this);
			
			_this.addClass('hide_post');
			
			var container = _this.parent();
			container.find('.figure, p, footer').fadeIn().addClass('highlight');
			
			var id = _this.attr('data-id');
			
			$('body,html').animate({
				scrollTop: $('#'+id).offset().top
			}, 400);

		}, function(){
			var _this = $(this);
			
			_this.removeClass('hide_post');
			
			var container = _this.parent();
			
			container.find('.figure, p, footer').not('.figure:first').fadeOut().addClass('highlight');
			
			var id = _this.attr('data-id');
			
			$('body,html').animate({
				scrollTop: $('#'+id).offset().top
			}, 400);
			
		});
	},
	searchEvent : function() {
		var self = this;
		var input = $('#search_form');

		this.search.bind('click',function(event){
			var _this = $(this);
			
			self.sidebarMore.removeClass('active');
			
			self.search.find('.search_icon').addClass('icon_active');
			input.addClass('search_active');
			input.find('input').focus();
			
			$(document).bind('click', function(e) {
				self.search.find('.search_icon').removeClass('icon_active');
				input.removeClass('search_active');
			   	$(this).unbind(e);
				_this.unbind(e);
			});

			return false;
		});
		
	},
	moreHandler : function() {
		var self = this;
		this.moreBtn.bind('click',function(event){

			var _this = $(this);
			self.sidebarMore.addClass('active');
			$('#search_form').removeClass();
			
			$(document).bind('click', function(e) {
				var target = e.target;
				if (!$(target).is('#dynamic_sidebar *')){
				    self.sidebarMore.removeClass('active');
					$(this).unbind(e);
					_this.unbind(e);
				}
			});

			return false;
		});
	}
};

Levitation.slider = function() {
	this.wrapper = $('#gallery');
	this.index = 0;
	this.slideCount = $('#gallery').children().length;
	
	this.paginationWrap = $('#pagination');
	this.timeInterval = null;
	this.ARROW = $('.arrow');
	this.WIDTH = 960;
	this.init();
	
};

Levitation.slider.prototype = {
	init : function() {
		this.wrapper.children().eq(this.index).addClass('animate');
		this.pagination();
		this.events();
	},
	pagination : function() {
		
		var slides = this.slideCount;
		var html = [];
		var link = '<a href="#" class="pag"></a>';
		var pagWidth = 18 * slides;

		while(slides--) {
			html.push(link);
		}
		this.paginationWrap.append(html.join(''));
		this.paginationWrap.css({ width : pagWidth+'px' });
		this.updatePagination();
	},
	updatePagination : function() {
		var pag = this.paginationWrap.children();
		pag.removeClass('active');
		pag.eq(this.index).addClass('active');
	},
	events : function(e) {
		this.arrows();
		this.paginationEvent();
		//this.timer();
	},
	paginationEvent : function() {
		var self = this;
		var pag = this.paginationWrap.children();
		var direction;
		
		pag.click(function(event){
			var currentIndex = self.index;
			if(currentIndex > $(this).index()) {
				direction = 'left';
			} else {
				direction = 'right';
			}
			self.slide($(this).index(), direction, event.target);
			return false;
		});
	},
	arrows : function() {
		var self = this;
		this.ARROW.hover(function(){
			$(this).stop().animate({ opacity : '1' });
		}, function(){
			$(this).stop().animate({ opacity : '0.6' });
		});
		
		this.ARROW.click(function(){
			var direction = $(this).attr('id');
			var index = self.nextSlide(direction);
			self.slide(index, direction);
			
			//self.timer();
			
			return false;
		});
	},
	nextSlide : function(dir) {	
		
		var imageLength = this.slideCount;
		
		if( dir == "right" ) {
			
			var index = (this.index + 1) % imageLength;
			return index;
		}
		if( dir == "left" ){
			
			var index = (this.index - 1 < 0) ? imageLength + (this.index - 1) : (this.index - 1) % imageLength;
			return index;
		}
	},
	updateIndex : function(dir, index) {
		var imageLength = this.slideCount;
		if( dir == "right" ) {
			this.index = (this.index + 1) % imageLength;
		}
		if( dir == "left" ){
			this.index = (this.index - 1 < 0) ? imageLength + (this.index - 1) : (this.index - 1) % imageLength;
		}
		if (dir == "index") {
			this.index = index;
		}
	},
	slide : function(index, dir, event) {
		
		var self = this;
		var amount = (dir == 'right') ? 960 : -960;
		var newStart = (dir == 'right') ? -960 : 960;
		
		var newSlide = this.wrapper.children().eq(index);
		
		newSlide.addClass('setToCurrent_'+dir+'');
			debug(newSlide);
		self.wrapper.children().eq(this.index).addClass('setToOld_'+dir+'').removeClass('animate');
		newSlide.removeClass('setToOld_right setToOld_left');
		setTimeout(function(){
			self.wrapper.children().eq(self.index).removeClass('setToOld_'+dir+'');
			newSlide.removeClass('setToCurrent_right setToCurrent_left');
			newSlide.addClass('animate');
		}, 30);
		
		
		if($(event).hasClass('pag')) {
			dir = 'index';
			self.updateIndex(dir,index);
			self.updatePagination();
		} else {
			self.updateIndex(dir);
			self.updatePagination();
		}
	},
	timer : function(){
		
		clearInterval(this.timeInterval);
		
		this.timeInterval = setInterval(function(){
			$('#right').trigger('click');
		},12000);
	}
};

new Levitation.global();

function debug(el) { if (window.console != undefined) console.debug(el); }




















