﻿/**
 * @author Alexandre Magno
 * @desc Center a element with jQuery
 * @version 1.0
 * @example
 * $("element").center({
 *
 * 		vertical: true,
 *      horizontal: true
 *
 * });
 * @obs With no arguments, the default is above
 * @license free
 * @param bool vertical, bool horizontal
 * @contribution Paulo Radichi
 *
 */
jQuery.fn.random_position = function(params) {

		var options = {

			vertical: true,
			horizontal: true

		}
		op = jQuery.extend(options, params);

   return this.each(function(){

		//initializing variables
		var $self = jQuery(this);
		//get the dimensions using dimensions plugin
		var doc_width = $("body").width();
		var doc_height = $("body").height();
		var width = $self.width();
		var height = $self.outerHeight();
		//get the type of positioning
		var positionType = $self.parent().css("position");


		//get the paddings
		var paddingTop = parseInt($self.css("padding-top"));
		var paddingBottom = parseInt($self.css("padding-bottom"));
		//get the borders
		var borderTop = parseInt($self.css("border-top-width")) || 0;
		var borderBottom = parseInt($self.css("border-bottom-width")) || 0;
		//get the media of padding and borders
		var mediaBorder = (borderTop+borderBottom);
		var mediaPadding = (paddingTop+paddingBottom);
		// get the half minus of width and height
//		var halfWidth = (width/2)*(-1);
		var fullHeight = height+mediaPadding+mediaBorder;
		// initializing the css properties

		var random_left=Math.floor(Math.random()*(doc_width-width));
		var random_top=Math.floor(Math.random()*(doc_height-height));
		//$("#debug").text("Doc width: "+doc_width+" Doc height:"+doc_height+" elem width: "+width+" elem height:"+height+" random top: "+random_top+" random left:"+random_left)

		var cssProp = {
			'position': 'absolute',
			'margin': '0'
		};

			//cssProp.height = height;
			cssProp.top = random_top+'px';
			//cssProp.width = width;
			cssProp.left = random_left+'px';

		//check the current position
		if(positionType == 'static') {
			$self.parent().css("position","relative");
		}
		//aplying the css
		$self.css(cssProp);
//		$self.animate(cssProp, 250);

   });

};