/**
 * POKE.gird namespace
 * Grid aricle pages, handles all functionality for grid style overview pages
 * Auth: Chris Reeves, POKE
 */
POKE.grid = {}
POKE.grid.loadMoreWait = 500;
POKE.grid.loading = false;
POKE.grid.autoLoadPages = Array(
  'magazine', 'news'
);

/**
 * Init method
 * Setup events and auto scale content
 */
POKE.grid.init = function() {
	POKE.grid.bindEvents();
	POKE.grid.scale();
	POKE.grid.moveLoaderToBottom();
}

/**
 * Event Handlers
 */
POKE.grid.bindEvents = function() {
	$(window).bind('resize', POKE.grid.scale);
	$(window).bind('scroll', POKE.grid.scroll);
	$('.video a').live('click', POKE.video.play);
  $(".box").live('mouseenter', function () {
    $(this).addClass('hover');
  });
  $(".box").live('mouseout', function () {
    $(this).removeClass('hover');
  });
}

POKE.grid.moveLoaderToBottom = function() {
  var model = $('#container').attr('class');
  if(POKE.grid.isAutoLoadPage(model)) {
    var loader = $('.loading');
    $('#main-content').append(loader);
    if($('.box').length >= 9) {
      loader.show();
    }
  } else {
    $('.loading').remove();
  }
}

/**
 * Scaler
 * Scale the grid, maintain a 16:9 ratio
 */
POKE.grid.scale = function() {
	// scale the main article feature
	if($('body').attr('id') == 'magazine') {
  	POKE.scaling.scaler({
  		callback: function(){POKE.scaling.homePosterwall()}
  	});
  }
	// scale the grid
	POKE.scaling.scaler({
		elmn: '.grid-wrapper',
		height: false,
		callback: function() {
			POKE.grid.scaleBox();
		}
	});
}

/**
 * Box Scaler
 * Scales the small grid boxes to maintain a 16:9 ratio
 */
POKE.grid.scaleBox = function() {
	var ratio = 9 / 16;
	var width = $('.grid-wrapper').find('.box:first').width();
	var height = width * ratio;
	$('.grid-wrapper').find('.box').height(height);
	$('.gridposterwall').posterwall({vertical_center:1, ratio:[16,9], min_size:[0, 0], container:$('.grid-wrapper').find('.box')});
}

/**
 * Scroller
 * Captures scroll event and loads more content if the scrollbar hits the bottom of the page
 */
POKE.grid.scroll = function() {
  if($(window).scrollTop() == $(document).height() - $(window).height()) {
    var model = $('#container').attr('class');
    var obj = eval('POKE.urlmaps.'+model+'LoadMore');
    if(POKE.grid.isAutoLoadPage(model)) {
      setTimeout(function() {
        if(!POKE.grid.loading) {
          POKE.grid.loading = true;
          if($('.box').length >= 9) {
            $('.loading').show();
          }
          var args = Array();
          if(model == 'magazine') {
            args.push(($('body').find('.box').length+1).toString());
          } else {
            args.push(($('body').find('.box').length).toString());
          }
          args.push((parseInt(args[0])+9).toString());
          var url = POKE.url(obj, args);
          $.get(url, {}, function(response) {
            if(response != '0') {
              $('.loading').slideUp(500, function() {
                POKE.grid.moveLoaderToBottom();
              });
              $('.grid-wrapper').append(response);
              POKE.scaling.scaler({
              	elmn: '.grid-wrapper',
              	height: false,
              	callback: function() {
              		POKE.grid.scaleBox();
									Cufon.set('fontFamily', 'Facebuster').replace('.grid-row h1 strong');
              	}
              });
            } 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.grid.loading = false;
          }, 'html');
        }
      }, POKE.grid.loadMoreWait);
    }
  }
}

/**
 * Is auto load page
 * Detects if a model is to auto load content, only for news and magazine, not car, partners or team poges, these will be loaded in one go
 * @param string model
 */
POKE.grid.isAutoLoadPage = function(model) {
  var isAutoLoad = false;
  for(i in POKE.grid.autoLoadPages) {
    if(POKE.grid.autoLoadPages[i] == model) {
      isAutoLoad = true;
    }
  }
  return isAutoLoad;
}