/**
Eye Object
Creates an eye that will follow the users cursor.
*/
function Eye(x,y,size,append){
	var self = this;
	var i = 0;
	this.x = x;
	this.y = y;
	this.size = size;
	
	//Create the Eye Dom Node useing canvas.
	this.create = function create(x,y,size,append){
		//Create dom node
		var eye = document.createElement('canvas');
		eye.width=size;
		eye.height=size;
		eye.style.position = 'relative';
		eye.style.top = y+'px';
		eye.style.left = x+'px';
		document.getElementById(append).appendChild(eye);
		//Get canvas
		canvas = eye.getContext("2d")
		//draw eye
		canvas.beginPath();
		radius = size/2;
		canvas.arc(radius, radius, radius, 0, Math.PI*2, true); 
		canvas.closePath();
		canvas.fillStyle = "rgb(255,255,255)";
		canvas.fill();
		//draw pupil
		canvas.beginPath();
		canvas.arc(radius, radius/2, radius/2, 0, Math.PI*2, true); 
		canvas.closePath();
		canvas.fillStyle = "rgb(0,0,0)";
		canvas.fill();
		
		return eye;
	}
	//Rotate the Dom node to a given angle.
	this.rotate = function(x){
		this.node.style.MozTransform="rotate("+x+"deg)";
		this.node.style.WebkitTransform="rotate("+x+"deg)";
		this.node.style.OTransform="rotate("+x+"deg)";
		this.node.style.msTransform="rotate("+x+"deg)";
		this.node.style.Transform="rotate("+x+"deg)";

	}
	//Update every 100 miliseconds
	setInterval(function(){
		//Math!
		angleFromEye = Math.atan2((cursorLocation.y-self.my_y), cursorLocation.x-self.my_x)*(180/Math.PI)+90;
		//Rotate
		self.rotate(angleFromEye);
		//Refresh own position every 25th time (in case screen is resized)
		i++;if(i>25){self.locateSelf();i=0;}
	
	},100);
	
	this.locateSelf = function(){
		this.my_x = this.node.offsetLeft + (this.size/2);
		this.my_y = this.node.offsetTop + (this.size/2);
		//If it has offsetParent, add em up to get the objects full position.
		if (this.node.offsetParent) {
			temp = this.node;
			while(temp = temp.offsetParent){
				this.my_x +=temp.offsetLeft;
				this.my_y +=temp.offsetTop;
			}
		}
	}
	
	//Call the node create function when the AlienEye Object is created.
	this.node = this.create(x,y,size,append);
	this.locateSelf();
	//Now the node has been added to the page, lets figure out exact where
	//it is relative to the documents top.
	
	//Get the basic position
	
	
}
//Create Object to store and provide access to current cusor position
var cursorLocation = new function(){
	this.x = 0;
	this.y = 0;
	//This function is called onmousemove to update the stored position
	this.update = function(e){
		var w = window, b = document.body;
		this.x =  e.clientX + (w.scrollX || b.scrollLeft || b.parentNode.scrollLeft || 0);
		this.y = e.clientY + (w.scrollY || b.scrollTop || b.parentNode.scrollTop || 0);
	}
}
//Hook onmousemove up to the above update function.
document.onmousemove=function(e){ cursorLocation.update(e); };


//Image hover//

(function ($) {
        $.fn.cross = function (options) {
            return this.each(function (i) { 
                // cache the copy of jQuery(this) - the start image
                var $$ = $(this);
                
                // get the target from the backgroundImage + regexp
                var target = $$.css('backgroundImage').replace(/^url|[\(\)'"]/g, '');

                // nice long chain: wrap img element in span
                $$.wrap('<span style="position: relative;"></span>')
                    // change selector to parent - i.e. newly created span
                    .parent()
                    // prepend a new image inside the span
                    .prepend('<img>')
                    // change the selector to the newly created image
                    .find(':first-child')
                    // set the image to the target
                    .attr('src', target);

                // the CSS styling of the start image needs to be handled
                // differently for different browsers
                if ($.browser.msie || $.browser.mozilla) {
                    $$.css({
                        'position' : 'absolute', 
                        'left' : 0,
                        'background' : '',
                        'top' : this.offsetTop
                    });
                } else if ($.browser.opera && $.browser.version < 9.5) {
                    // Browser sniffing is bad - however opera < 9.5 has a render bug 
                    // so this is required to get around it we can't apply the 'top' : 0 
                    // separately because Mozilla strips the style set originally somehow...                    
                    $$.css({
                        'position' : 'absolute', 
                        'left' : 0,
                        'background' : '',
                        'top' : "0"
                    });
                } else { // Safari
                    $$.css({
                        'position' : 'absolute', 
                        'left' : 0,
                        'background' : ''
                    });
                }

                // similar effect as single image technique, except using .animate 
                // which will handle the fading up from the right opacity for us
                $$.hover(function () {
                    $$.stop().animate({
                        opacity: 0
                    }, 250);
                }, function () {
                    $$.stop().animate({
                        opacity: 1
                    }, 250);
                });
            });
        };
        
    })(jQuery);
    
    // note that this uses the .bind('load') on the window object, rather than $(document).ready() 
    // because .ready() fires before the images have loaded, but we need to fire *after* because
    // our code relies on the dimensions of the images already in place.
    $(window).bind('load', function () {
        $('img.fade').cross();
    });
