var offsetOjoX = 127,
	offsetOjoY = 34,
	margenOjoX = 57,
	offsetOjitoX = 136,
	offsetOjitoY = 43,
	margenOjitoX = 57,
	anchoOjo = 23,
	altoOjo = 24,
	anchoOjito = 9,
	altoOjito = 9,
	anchoLogo = 49,
	altoLogo = 49,
	ojoFinalX = 0,
	ojoFinalY = 0,
	ojitoFinalX = 0,
	ojitoFinalY = 0;
var relativeOjoX = 0;
var relativeOjoY = 0;
var tetha = 0;
var maxTetha1 = 0;
var maxTetha2 = 0;
var maxTetha3 = 0;
var maxTetha4 = 0;
var hypotenuse = 0;
var maxHypotenuse = 0;
var opposite = 0;
var adjacent = 0;
var radius = 13;
var radiusRatio = 0;
var directionX = 0;
var directionY = 0;
	
function update(){
	//imgPos1 = jQuery("#logo").offset();
	jQuery("#ojo").css("left", ojoFinalX + "px");
	jQuery("#ojo").css("top", ojoFinalY + "px");
	jQuery("#ojito").css("left", ojitoFinalX + "px");
	jQuery("#ojito").css("top", ojitoFinalY + "px");
	//console.log( "(" + ojoFinalX + " - " + ojoFinalY + ")");
}

jQuery(document).ready(function() {
	var imgPos1 = jQuery("#logo").offset();
	var imgPos2 = jQuery("#ojo").offset();
	var imgPos3 = jQuery("#ojito").offset();
	relativeOjoX = imgPos1.left + offsetOjoX + parseInt(anchoLogo/2) - parseInt(anchoOjo/2);
	relativeOjoY = imgPos1.top + offsetOjoY + parseInt(altoLogo/2) - parseInt(anchoOjo/2);
	//ojoFinalX = imgPos1.left + offsetOjoX + anchoLogo - anchoOjo;//L+135+49-23
	//ojoFinalY = imgPos1.top + offsetOjoY + altoLogo - altoOjo;//L+34+49-24
	ojoFinalX = relativeOjoX;
	ojoFinalY = relativeOjoY;
	ojitoFinalX = ojoFinalX + anchoOjito * 1.5;
	ojitoFinalY = ojoFinalY + altoOjito * 1.5;//- altoOjito * 2;
	update();
	jQuery("#ojo").css("visibility","visible");
	jQuery("#ojito").css("visibility","visible");
	jQuery(window).resize(function(e){
		imgPos1 = jQuery("#logo").offset();
		relativeOjoX = imgPos1.left + offsetOjoX + parseInt(anchoLogo/2) - parseInt(anchoOjo/2);
		relativeOjoY = imgPos1.top + offsetOjoY + parseInt(altoLogo/2) - parseInt(anchoOjo/2);
		
		//maximum angles for each quadrant
		maxTetha1 = Math.atan2(relativeOjoY, jQuery(window).width() - relativeOjoX);
		maxTetha2 = Math.atan2(relativeOjoY, relativeOjoX);
		maxTetha3 = Math.atan2(jQuery(window).height() - relativeOjoY, relativeOjoX);
		maxTetha4 = Math.atan2(jQuery(window).height() - relativeOjoY, jQuery(window).width() - relativeOjoX);
		
		if( e.clientY < relativeOjoY ){
			opposite = relativeOjoY - e.clientY;
			directionY = -1;
			if(e.clientX < relativeOjoX){
				//segundo cuadrante
				adjacent = relativeOjoX - e.clientX;
				directionX = -1;
				tetha = Math.atan2(opposite, adjacent);
				hypotenuse = opposite/Math.sin(tetha);
				if(tetha > maxTetha2)
					maxHypotenuse = hypotenuse + e.clientY/Math.sin(tetha);
				else if(tetha < maxTetha2)
					maxHypotenuse = hypotenuse + e.clientX/Math.cos(tetha);
				else
					maxHypotenuse = hypotenuse + relativeOjoX/Math.cos(tetha);					
			}else if(e.clientX > relativeOjoX){
				//primer cuadrante
				adjacent = e.clientX - relativeOjoX;		
				directionX = 1;
				tetha = Math.atan2(opposite, adjacent);
				hypotenuse = opposite/Math.sin(tetha);
				if(tetha > maxTetha1)
					maxHypotenuse = hypotenuse + e.clientY/Math.sin(tetha);
				else if(tetha < maxTetha1)
					maxHypotenuse = hypotenuse + (jQuery(window).width() - e.clientX)/Math.cos(tetha);
				else
					maxHypotenuse = hypotenuse + e.clientY/Math.sin(tetha);
			}
		}else if(e.clientY > relativeOjoY){
			opposite = e.clientY - relativeOjoY;
			directionY = 1;
			if(e.clientX < relativeOjoX){
				//tercer cuadrante
				adjacent = relativeOjoX - e.clientX;
				directionX = -1;
				tetha = Math.atan2(opposite, adjacent);
				hypotenuse = opposite/Math.sin(tetha);
				if(tetha > maxTetha3)
					maxHypotenuse = hypotenuse + (jQuery(window).height() - e.clientY)/Math.sin(tetha);
				else if(tetha < maxTetha3)
					maxHypotenuse = hypotenuse + e.clientX/Math.cos(tetha);
				else
					maxHypotenuse = hypotenuse + (e.clientY - relativeOjoY)/Math.sin(tetha);
			}else if(e.clientX > relativeOjoX){
				//cuarto cuadrante
				adjacent = e.clientX - relativeOjoX;		
				directionX = 1;	
				tetha = Math.atan2(opposite, adjacent);
				hypotenuse = opposite/Math.sin(tetha);
				if(tetha > maxTetha4)
					maxHypotenuse = hypotenuse + (jQuery(window).height() - e.clientY)/Math.sin(tetha);
				else if(tetha < maxTetha4)
					maxHypotenuse = hypotenuse + (jQuery(window).width() - e.clientX)/Math.cos(tetha);
				else
					maxHypotenuse = hypotenuse + (e.clientY - relativeOjoY)/Math.sin(tetha);
			}
		}
		//hypotenuse = opposite/Math.sin(tetha);
		radiusRatio = hypotenuse/maxHypotenuse;
		ojoFinalX = relativeOjoX + (directionX * Math.ceil((Math.cos(tetha) * radius * radiusRatio)));
		ojoFinalY = relativeOjoY + (directionY * Math.ceil((Math.sin(tetha) * radius * radiusRatio)));
		ojitoFinalX = ojoFinalX + anchoOjito + (directionX * Math.ceil((Math.cos(tetha) * 5 * radiusRatio)));
		ojitoFinalY = ojoFinalY + altoOjito + (directionY * Math.ceil((Math.sin(tetha) * 5 * radiusRatio)));
		update();
	});
  
	jQuery(document).mousemove(function(e){
		imgPos1 = jQuery("#logo").offset();
		relativeOjoX = imgPos1.left + offsetOjoX + parseInt(anchoLogo/2) - parseInt(anchoOjo/2);
		relativeOjoY = imgPos1.top + offsetOjoY + parseInt(altoLogo/2) - parseInt(anchoOjo/2);
		
		//maximum angles for each quadrant
		maxTetha1 = Math.atan2(relativeOjoY, jQuery(window).width() - relativeOjoX);
		maxTetha2 = Math.atan2(relativeOjoY, relativeOjoX);
		maxTetha3 = Math.atan2(jQuery(window).height() - relativeOjoY, relativeOjoX);
		maxTetha4 = Math.atan2(jQuery(window).height() - relativeOjoY, jQuery(window).width() - relativeOjoX);
		
		if( e.clientY < relativeOjoY ){
			opposite = relativeOjoY - e.clientY;
			directionY = -1;
			if(e.clientX < relativeOjoX){
				//segundo cuadrante
				adjacent = relativeOjoX - e.clientX;
				directionX = -1;
				tetha = Math.atan2(opposite, adjacent);
				hypotenuse = opposite/Math.sin(tetha);
				if(tetha > maxTetha2)
					maxHypotenuse = hypotenuse + e.clientY/Math.sin(tetha);
				else if(tetha < maxTetha2)
					maxHypotenuse = hypotenuse + e.clientX/Math.cos(tetha);
				else
					maxHypotenuse = hypotenuse + relativeOjoX/Math.cos(tetha);					
			}else if(e.clientX > relativeOjoX){
				//primer cuadrante
				adjacent = e.clientX - relativeOjoX;		
				directionX = 1;
				tetha = Math.atan2(opposite, adjacent);
				hypotenuse = opposite/Math.sin(tetha);
				if(tetha > maxTetha1)
					maxHypotenuse = hypotenuse + e.clientY/Math.sin(tetha);
				else if(tetha < maxTetha1)
					maxHypotenuse = hypotenuse + (jQuery(window).width() - e.clientX)/Math.cos(tetha);
				else
					maxHypotenuse = hypotenuse + e.clientY/Math.sin(tetha);
			}
		}else if(e.clientY > relativeOjoY){
			opposite = e.clientY - relativeOjoY;
			directionY = 1;
			if(e.clientX < relativeOjoX){
				//tercer cuadrante
				adjacent = relativeOjoX - e.clientX;
				directionX = -1;
				tetha = Math.atan2(opposite, adjacent);
				hypotenuse = opposite/Math.sin(tetha);
				if(tetha > maxTetha3)
					maxHypotenuse = hypotenuse + (jQuery(window).height() - e.clientY)/Math.sin(tetha);
				else if(tetha < maxTetha3)
					maxHypotenuse = hypotenuse + e.clientX/Math.cos(tetha);
				else
					maxHypotenuse = hypotenuse + (e.clientY - relativeOjoY)/Math.sin(tetha);
			}else if(e.clientX > relativeOjoX){
				//cuarto cuadrante
				adjacent = e.clientX - relativeOjoX;		
				directionX = 1;	
				tetha = Math.atan2(opposite, adjacent);
				hypotenuse = opposite/Math.sin(tetha);
				if(tetha > maxTetha4)
					maxHypotenuse = hypotenuse + (jQuery(window).height() - e.clientY)/Math.sin(tetha);
				else if(tetha < maxTetha4)
					maxHypotenuse = hypotenuse + (jQuery(window).width() - e.clientX)/Math.cos(tetha);
				else
					maxHypotenuse = hypotenuse + (e.clientY - relativeOjoY)/Math.sin(tetha);
			}
		}
		//hypotenuse = opposite/Math.sin(tetha);
		radiusRatio = hypotenuse/maxHypotenuse;
		ojoFinalX = relativeOjoX + (directionX * Math.ceil((Math.cos(tetha) * radius * radiusRatio)));
		ojoFinalY = relativeOjoY + (directionY * Math.ceil((Math.sin(tetha) * radius * radiusRatio)));
		ojitoFinalX = ojoFinalX + anchoOjito + (directionX * Math.ceil((Math.cos(tetha) * 5 * radiusRatio)));
		ojitoFinalY = ojoFinalY + altoOjito + (directionY * Math.ceil((Math.sin(tetha) * 5 * radiusRatio)));
		update();
	});
	
});
