/**
 * Homepage Application
 * Scales the homepage acording to users window size maintaining aspect ratio and creates a "snappy" scrollbar.
 * Auth: Chris Reeves POKE, 2010-02-01
 */
POKE.home = {}

POKE.home.lastScroll = new Date();
POKE.home.snapScrollWait = 200;
POKE.home.snapScrollDuration = 200;
POKE.home.loadMoreWait = 500;
POKE.home.loading = false;
POKE.home.snapScrollSettings = {
  axis: 'y'
}

/**
 * init method
 */
POKE.home.init = function() {
	POKE.home.bindEvents();
	POKE.home.scale();
	POKE.home.moveLoaderToBottom();
}

/**
 * Bind Events
 */
POKE.home.bindEvents = function() {
	$(window).bind('resize', POKE.home.scale);
	$(window).bind("scroll", POKE.home.snapScroll);
	$(window).bind("scroll", POKE.home.loadMore);
	$('.video a').live('click', POKE.video.play);
}


POKE.home.moveLoaderToBottom = function() {
  var loader = $('.loading');
  $('#main-content').append(loader);
  loader.show();
}


/**
 * Scale
 * Calls main scaling method with callbacks to rescale the images and resnap the scroll bar
 */
POKE.home.scale = function() {
	POKE.scaling.scaler({
		callback: function(){
			POKE.scaling.homePosterwall();
			POKE.home.snapScroll();
		}
	});
}

/** 
 * Scrollbar Snap
 * Snaps the users scrollbar to a storty so a story is always at the top
 * of the browser window
 */
POKE.home.snapScroll = function() {
	var now = new Date();
	setTimeout(function() {
		var diff = now.getTime() - POKE.home.lastScroll.getTime();
		if(diff == 0 || diff == -1) {
			var pos = $(window).scrollTop();
			var i = 1;
			$('.wrapper').each(function() {
				var thisTopOffset = $(this).offset().top;
				if($(this).prev().length != 0) {
					var prevElmTopOffset = $(this).prev().offset().top;
				}
				if($(this).next().length != 0) {
						var nextElmTopOffset = $(this).next().offset().top;
            try {
  						if(pos > thisTopOffset && pos < nextElmTopOffset) {
  							  var thisSlideNextShiftPoint = $(this).offset().top + ($(this).height() / 3);
  							  var elmn;
    							if(pos > thisSlideNextShiftPoint) {
                    elmn = $(this).next();
    							} else {
    								elmn = $(this);
    							} 
  							$.scrollTo(elmn.offset().top, POKE.home.snapScrollDuration, POKE.home.snapScrollSettings);
  						}
					  } catch(e) {
					}
					i++;
				}
			});
		}
	}, POKE.home.snapScrollWait);
	POKE.home.lastScroll = new Date();
}

/**
 * Load More
 * Loads more content to the homepage, never ending page
 */
POKE.home.loadMore = function() {
  if($(window).scrollTop() == $(document).height() - $(window).height()) {
    setTimeout(function() {
      if(!POKE.home.loading) {
        POKE.home.loading = true;
        var args = Array();
        args.push(($('body').find('.wrapper').length).toString())
        args.push((parseInt(args[0])+5).toString());
        var url = POKE.url(POKE.urlmaps.homeLoadMore, args);
        $.get(url, {}, function(response) {
          if(response != '0') {
            $('#main-content').append(response);
            Cufon.set('fontFamily', 'Gotham Rounded').replace('#main-content h2, a.btn span.btn-text, .info .category, .info .date');            
            $('.loading').slideUp(500, function() {
              POKE.home.moveLoaderToBottom();
            });
            POKE.scaling.scaler({
              callback: function(){
                POKE.scaling.homePosterwall();
                Cufon.set('fontFamily', 'Facebuster');
                Cufon.refresh('h1 strong');
              }
            });
            POKE.home.snapScroll();
          } else {
            $('.loading').find('p').addClass('no-content').html('No more posts.');
            $('.loading').find('.snake').hide();
            Cufon.set('fontFamily', 'Gotham Rounded').replace('#main-content .loading .no-content');
          }
          POKE.home.loading = false;
        }, 'html');
      }
    }, POKE.home.loadMoreWait);
  }
}