/*
 * jEye jQuery plugin
 *
 * Copyright (c) 2009 Giovanni Casassa (senamion.com - senamion.it)
 *
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://www.senamion.com
 *
 */

jQuery.fn.jEye = function(o) {

	o = jQuery.extend({
		xEye: 39,
		yEye: 34,
		wEye: 28,
		hEye: 28,
		wPupil: 18,
		hPupil: 18,
		eyes: 1
	}, o);

	return this.each(function(i) {
		var el = $(this);
    var xPupil = Math.floor((o.wEye - o.wPupil) / 2);
		var yPupil = Math.floor((o.hEye - o.hPupil) / 2);
		var radius = Math.floor((o.wEye - o.wPupil) / 2) - 1;
		var	position = el.position();
		var xEl = position.left;
		var yEl = position.top;
		var offset = $('#sardine').offset();
		var xYeux =  offset.left;
		var yYeux =  offset.top;
		//el.append("<div style='background: transparent url(themes/bestof/images/tete_sardine.png) center center no-repeat;width:69px;height:91px;position:absolute;top:70px;left:38%;'><div id='occhio" + i + "' style='position: relative; background: transparent url(themes/bestof/images/blanc_sardine.png) center center no-repeat; top:39px; left:34px; width: " + o.wEye + "px; height: " + o.hEye + "px;' >" +
		//	"<img id='pupilla" + i + "' src='themes/bestof/images/oeil_sardine.png' style='position: relative; top:" + yPupil + "px; left:" + xPupil + "px; width: " + o.wPupil + "px; height:" + o.hPupil + "px' />" +
			//"</div></div>");

		el.mousemove(function(kmouse){
			var r = 3;
			var ym = kmouse.pageY - yEl;
			var xm = kmouse.pageX - xEl;
      var xo = xYeux + 19;
			var yo = yYeux + 19;
			ang = Math.atan((yo - ym) / (xm - xo));
			if (xo > xm)
				ang += Math.PI;
			$('#pupilla').css("top", (radius - Math.floor(Math.sin(ang) * r))+"px").css("left", (Math.floor(Math.cos(ang) * r) + radius) + "px");
      /*$('#logme').html($('#logme').html() + ang + '<br>');  */
		});
	});
};
