/*
* 
* Moreno Di Domenico
* www.moretech.it
*
*/



Moretech = function(){
	$this = this;
	
	
	/* balls */
	$this.balls = function(){
		
		var method = this;
		
		/* declare variables */
		bigBall = $("#big-ball");
		bigBallHeight = 599;
		littleBall = $(".ball");
		littleBallHeight = 142;
		
		
		/* big ball show function */
		method.bigBallShow = function()
		{
		 // bigBallTop();
			bigBall.animate({left : 0}, 1000, 'easeOutBounce', function(){
				method.littleBallsShow();
			});
		}
		
		
		/* big ball set top height */
		method.bigBallTop = function()
		{
			bigBall.css({
				top : parseInt(($(window).height()-bigBallHeight)/2)
			});
		}
		
		
		method.littleBallsShow = function()
		{
			littleBallTop = 250;
			littleBall.each(function(){
				$(this).animate({top : littleBallTop}, randomBetween(500, 800), 'easeOutBounce');
			});
		}
		
		method.littleBallsTop = function()
		{
			littleBall.css({
				top : parseInt(($(window).height()-littleBallHeight)/2)
			});
		}
		
		
		method.littleBallsClick = function()
		{
			littleBall.find("a").bind("click", function()
			{
				$.scrollTo($(this).attr("href"), 500);
				return false;
			});
		}
		
		
		/* init */
		method._init = function(){
			method.bigBallShow();
			method.littleBallsClick();
		}
		
		method._init();
	}
	




	/* menu */
	$this.menu = function(){
		
		var method = this;
		
		bigBall = $("#big-ball");
		littleBall = $(".ball");
		
		method.navigation = function(){
			$(".header a").bind("click", function(){
				target = $(this).attr("href");
				if(target == "#intro"){
					
					bigBall.animate({top : -320}, 300,  function(){
						bigBall.animate({top : 60}, 600, 'easeOutBounce');
					});
					
					littleBall.each(function(){
						$(this).animate({top : 20}, function(){
							$(this).animate({top : 250},  randomBetween(500, 800), 'easeOutBounce');
						});
					});
						
				}
				
				$.scrollTo(target, 500);
				return false;
			});
		}
		
		method._init = function(){
			method.navigation();	
		}
		
		method._init();
	}


	
	
	
	
	/* works */
	$this.works = function(){
		
		var method = this;
		
		var work = $("#work");
	
		method.alignWorks = function(){
			i=1;
			$(".works li").each(function(e){
				if(i == 4){
					$(".works li").eq(e).addClass("last");
					i = 0;	
				}
				i++;
			});
		}
		
		
		method.openWork = function(){
			
			$("#works ul li a").live("click", function(){
				
				work.find(".site").hide();
				work.find(".images").remove();
				
				father = $(this).parent("li");
				
				folder = father.find(".folder").text();
				type = father.find(".type").text();
				agency = father.find(".agency").text();
				year = father.find(".year").text();
				site = father.find(".site").text();
				ext = father.find(".ext").text();
				
				images  = '<ul class="images">';
				images += '<li><img src="/media/works/'+folder+'/1.'+ext+'" /></li>';
				images += '<li><img src="/media/works/'+folder+'/2.'+ext+'" /></li>';		
				images += '</ul>';
				
				work.append(images);
				
				work.find(".type").html('<span>type: </span>'+type);
				work.find(".agency").html('<span>agency: </span>'+agency);
				work.find(".year").html('<span>year: </span>'+year);
				
				if(site){
					work.find(".site").show();
					work.find(".site a").attr("href", site);	
				}
				
				work.slideDown(200, function(){
					$.scrollTo("#work", 500);
				});
	
	
	
				$('ul.images').cycle({ 
					speed:  'slow', 
					timeout: 0, 
					next:   '.btn_next', 
					prev:   '.btn_prev'
				});
				
				
				return false;
			});
		}
		
		
		method.closeWork = function(){
			work.find(".btn_close").bind("click", function(){
				work.slideUp();
					$.scrollTo("#works", 500);
				return false;
			});
	
		}
		
		method._init = function(){
			method.openWork();	
			method.closeWork();
			method.alignWorks();
		}
		
		method._init();
	}


	randomNumber = function(X) {
			return Math.floor(X * (Math.random() % 1));
	}


	randomBetween = function(MinV, MaxV) {
		  return MinV + randomNumber(MaxV - MinV + 1);
	}
	
	

	/* init */
	$this._init = function(){
		$this.balls();
		$this.menu();
		$this.works();
	}
 
	/* trigger init */
	$(window).load(function(){
		$this._init();
		$.scrollTo("#intro", 500);
	});
	
}

Moretech();
