'use strict';

var $               = require('./main')
  , getViewportRect = require('./get-viewport-rect')
  , getElementRect  = require('./get-element-rect');

module.exports = $.showInViewport = function (el/*, options*/) {
	var vpRect = getViewportRect()
	  , elRect = getElementRect(el)
	  , options = Object(arguments[1])
	  , padding = isNaN(options.padding) ? 0 : Number(options.padding)
	  , elTopLeft = { x: elRect.left, y: elRect.top }
	  , elBottomRight = { x: elRect.left + elRect.width, y: elRect.top + elRect.height }
	  , vpTopLeft = { x: vpRect.left + padding, y: vpRect.top + padding }
	  , vpBottomRight = { x: vpRect.left + vpRect.width - padding,
			y: vpRect.top + vpRect.height - padding }
	  , diffPoint = { x: 0, y: 0 }, diff;

	if (elBottomRight.x > vpBottomRight.x) {
		// right beyond
		diff = elBottomRight.x - vpBottomRight.x;
		elTopLeft.x -= diff;
		elBottomRight.x -= diff;
		diffPoint.x -= diff;
	}

	if (elBottomRight.y > vpBottomRight.y) 


	if (elTopLeft.x < vpTopLeft.x) {
		// left beyond
		diff = vpTopLeft.x - elTopLeft.x;
		elTopLeft.x += diff;
		elBottomRight.x += diff;
		diffPoint.x += diff;
	}

	if (elTopLeft.y < vpTopLeft.y) {
		// top beyond
		diff = vpTopLeft.y - elTopLeft.y;
		elTopLeft.y += diff;
		elBottomRight.y += diff;
		diffPoint.y += diff;
	}
	if (diffPoint.x) el.style.left = (el.offsetLeft + diffPoint.x) + "px";
	if (diffPoint.y) el.style.top = (el.offsetTop + diffPoint.y) + "px";
};

{
		// bottom beyond
		diff = elBottomRight.y - vpBottomRight.y;
		elTopLeft.y -= diff;
		elBottomRight.y -= diff;
		diffPoint.y -= diff;
	}