Parallax = function(src, x,y,z, start_x, start_y){ //z is how far forward it is at start, 0 is background
	//if move is 0, put fix
	//console.log(jQuery.makeArray(arguments))
    var is_string = typeof src == "string"
    this.is_string = is_string;
    this.x = x;
	this.start_x =start_x|| x / 25;
	this.x_multiple = this.x - this.start_x;
		
	this.y = y;
	this.start_y = start_y || y / 25;
	this.y_multiple = this.y - this.start_y;
	this.z = z;
	this.z2 =z * z*z;
	this.img = is_string ? $(new Image()) : src;
    
    this.img.css('position',"fixed").css("zIndex",100+(this.is_string ?  0 : 100-z))//.css('top','0px').css('left','0px');
	

	
	var self = this;
    if(is_string){
        this.img.attr('src', src).css('opacity',0)
        $(document.body).append(this.img)
        //this.img.css("zIndex",100);
        this.img.load(function(){ self.set_height_and_width()})
    }else{
        Parallax.images.push(this);
        this.set_height_and_width();
    }
}
Parallax.prototype.set_height_and_width = function(){
    this.h = this.img.height();
	this.w = this.img.width();
	this.start_h = this.h / 50;
	this.start_w = this.w / 50;
	this.h_multiple = this.h - this.start_h;
	this.w_multiple = this.w - this.start_w;
    
    
    
    
    if(this.is_string){
        var self= this;
        self.current_z = 0;
        self.animate(null, null, 0, true)
        this.next()
    }else{
        this.img.find("*")
        this.animate();
    }
}
Parallax.prototype.next = function(){
    var self = this;
    this.current_z = this.current_z+1;
    this.animate(null, null, this.current_z, null, function(){
        self.img.remove();
    })

}
Parallax.prototype.animate = function(x, y , z, position, callback){
	//console.log(position ?"position":"animate")
    x = x ||  $().width() /2;
	y = y || horizon;
	z = z || 0 ;
	//if(this.z == 0) return;
	if(z > this.z+1 || (z < this.z-6 && z!=0) ){
		this.img.hide();return;
	}else{
		this.img.show()
	}
	if(z  < this.z){
		this.img.find('table').hide()
	}else{
		this.img.find('table').show()
	}
    var td = 5 - (this.z - z )
	var d = td*td*td / (25*5);
    var fuzz = 0.05
	d = d < 1+fuzz && d > 1-fuzz ? 1 : d;	
	var h = (this.h_multiple*d)+this.start_h;
	var w = (this.w_multiple*d)+this.start_w;
	var xp = (this.x_multiple*d)+this.start_x;
	var yp = (this.y_multiple*d)+this.start_y;
	var alpha =  (z > this.z ?  0  : d );
	//console.log('position ',d, w, h,x+(this.x*d) );
	
    if(!position){
        //console.log(x + xp -(w/2), y-(yp + h),h, w, alpha )
        //console.log(y, )
        
        if(!callback && alpha == 1){
            var self = this;
            callback = function(){
                self.img.css("opacity","")
            }
        }
		if(!callback && z == this.z+1){
            var self = this;
            callback = function(){
                self.img.hide()
            }
        }
        this.img.animate( 
		{
			"left": x + xp -(w/2)+ 'px',
			top: y-(yp + h/2)+'px',
			height: h+'px',
			width: (w)+'px',
			opacity: alpha ,
			"fontSize" : (d*95)+"%"
		}, "slow", null, callback);
    }else{
        
        this.img.css('top', y-(yp + h)+'px' )
			.css('left',x + xp + 'px')
			.height(h+'px')
			.width((w)+'px').css("opacity", alpha == 1 ? "": alpha).css("fontSize" , (d*100)+"%");
    }
    
	
	
}
Parallax.images = []; // this is going to have to be anything
$(window).load(function(){
	var b = $(document.body)
	b.height("3600px");
	//new Parallax("background.png", 0,0,0 );
	//new Parallax("jmvc.png", -170, -400, 8 );
	//new Parallax("sign.png", 100, 100, 6 ); //should travel horizontally
	//new Parallax("sign.png", 100, 100, 4 );
	var position = 0;
    $('.parallax').each(function(){
        var cur = $(this);
        
        var pz = cur.attr('pz');
        position = pz == "previous"  ? position : pz == "next" ?  position+2 : position+1;
        cur.attr("z", position);
        new Parallax($(this), parseInt(cur.attr('px') || 0, 10), parseInt(cur.attr('py') || 0,10), position);
        
    })
    /*
    
    new Parallax($("#jupiter"), -170, 100, 2 );
    
	new Parallax($("#welcome"), -170, 100, 3 );
	new Parallax($("#summary"), 100, 100, 4);
	new Parallax($("#jmvc"), 100, 100, 5);
	new Parallax($("#star1"), -400, 0, 6, -200,0);*/
	
	
	$().scroll(scroller)
	$('#next').click(function(){
		$().scrollTop(  $().scrollTop()+step_size);
	});
	$('#previous').click(function(){
		$().scrollTop(  $().scrollTop()-step_size);
	});

    
    setTimeout(create_star, Math.random()*1000)
    scroller();
    
    
    $('a.goto').click(function(){
        $().scrollTo( parseInt($("#"+ $(this).attr("href")).attr("z"),10)*step_size);
    })
    
})
horizon = 325;
step_size = 60
create_star = function(){
    var randx = Math.random() * 1000 - 500;
    var randy = Math.random() * 1000- 500;
    new Parallax("star.png", randx, randy, 1, Math.random()* 200 - 100,Math.random()* 200 - 100);
    setTimeout(create_star, Math.random()*2000)
}
scroller = function(){
        var w = $().width()
		//move each image
		var st = $().scrollTop()

		var m = st % step_size;
		var z = m > step_size/2 ? st + (step_size-m) : st-m;
		//console.log(st, m, z, z/step_size)
        $('#page').text(z/step_size)
		for(var i =0 ; i < Parallax.images.length; i++)
			Parallax.images[i].animate(w/2, horizon, z/step_size);
	}
	
$().keypress(function(e){
	if(e.keyCode == 34){
		e.preventDefault();
		$().scrollTop(  $().scrollTop()+step_size);
	}else if(e.keyCode == 33){
		e.preventDefault();
		$().scrollTop(  $().scrollTop()-step_size);
	}
})

