ronchetti = {
	setPosition: function(empty){
		
		var boxes = $('.box_home');
		
		if(boxes.length < 9){
			for(var z = 0; z < empty.length; z++){
				if(empty[z] == 0){
					$('.box_home').eq(empty[z]).before('<div class="box_home"></div>');
				}else{
					$('.box_home').eq(empty[z]).after('<div class="box_home"></div>');
				}
				
			}
		
			boxes = $('.box_home');
		}
		
		var left = 270;
		var top = 100;
		var pad  = 20;
		var bHeight = 0;
		var space = false, spaceTop = false;
		var x = 0;
		var y = 0;
		var riga = 0;
		boxes.each(function(i, box){
			
			
			if(bHeight < $(box).height()){
				bHeight = $(box).height();
				bHeight = 140;
			}
			
			if(i != 0 && i % 3 == 0){
				spaceTop = true;
				space = false;
				i = 0;
				x = i;
				y++;
				riga = y;
			}
			
			for(var j = 0; j < empty.length; j++){
				if(empty[j] == i){
					space = false;
					//x = j + 1;
				}
			}
			
			if(space){
				var l = ((i + x) * 130) + ((i + x) * pad);
			}else{
				var l = (x * 130) + (x * pad);
				x++;
			}
			
			$(box).css({
				left: left + l
			});
			
			if(spaceTop){
				
				var t = (y * bHeight) + (y * pad);
				
				$(box).css({
					top: top +  t
				});
			}
			//console.info(y);
			
		});
		
		//console.info(boxes[2]);
	
	},
	
	openPopup: function(url){
		window.open(url,'score','toolbar=no,scrollbars=yes,width=550,height=750,resizable=yes,statusbar=no');
	},
	
	sweetPages: function(opts){
	
		// If no options were passed, create an empty opts object
		if(!opts) opts = {};
		
		var resultsPerPage = opts.perPage || 3;
		
		// The plugin works best for unordered lists, althugh ols would do just as well:
		var ul = opts.holder;
		var li = ul.find('li');
		
		li.each(function(){
			// Calculating the height of each li element, and storing it with the data method:
			var el = $(this);
			el.data('height',el.outerHeight(true));
		});
		
		// Calculating the total number of pages:
		var pagesNumber = Math.ceil(li.length/resultsPerPage);
		
		// If the pages are less than two, do nothing:
		if(pagesNumber<2) return this;
	
		// Creating the controls div:
		var swControls = $('<div class="swControls">');
		
		for(var i=0;i<pagesNumber;i++)
		{
			// Slice a portion of the lis, and wrap it in a swPage div:
			li.slice(i*resultsPerPage,(i+1)*resultsPerPage).wrapAll('<div class="swPage" />');
			
			// Adding a link to the swControls div:
			swControls.append('<a href="" class="swShowPage">'+(i+1)+'</a>');
		}
	
		ul.parent().append(swControls);
		
		var maxHeight = 0;
		var totalWidth = 0;
		
		var swPage = ul.find('.swPage');
		swPage.each(function(){
			
			// Looping through all the newly created pages:
			
			var elem = $(this);
	
			var tmpHeight = 0;
			elem.find('li').each(function(){tmpHeight+=$(this).data('height');});
	
			if(tmpHeight>maxHeight)
				maxHeight = tmpHeight;
	
			totalWidth+=elem.outerWidth();
			
			elem.css('float','left').width(ul.width());
		});
		
		swPage.wrapAll('<div class="swSlider" />');
		
		// Setting the height of the ul to the height of the tallest page:
		ul.height(maxHeight);
		
		var swSlider = ul.find('.swSlider');
		swSlider.append('<div class="clearfix" />').width(totalWidth);
	
		var hyperLinks = ul.parent().find('a.swShowPage');
		
		hyperLinks.click(function(e){
			
			// If one of the control links is clicked, slide the swSlider div 
			// (which contains all the pages) and mark it as active:
	
			$(this).addClass('active').siblings().removeClass('active');
			
			swSlider.stop().animate({'margin-left':-(parseInt($(this).text())-1)*ul.width()},'slow');
			try{
				e.preventDefault();
			}catch(e){
				return false;
			}
			
		});
		
		// Mark the first link as active the first time this code runs:
		hyperLinks.eq(0).addClass('active');
		
		// Center the control div:
		swControls.css({
			'left':'50%',
			'margin-left':-swControls.width()/2
		});
		
		return this;
	}
}

$(document).ready(function(){
	if($('#submenu').length){
		if($('#left_box').length){
			$('#left_box').height(340);
		}
	}
	
	if($('.bbox_home').length){
		$('.bbox_home a').click(function(){
			
			$('#overlay').fadeTo(300, 0.25);
			var h = $(this).attr('href');
			var n = h.split('#');
			$(h).css('background', 'url(img/home/bg_b'+n[1]+'_1.jpg) no-repeat top left #666').show(800);
			
			return false;
		});
		
		$('.bbox_close').click(function(){
			$('.bbox').hide(600);
			$('#overlay').fadeOut(300);
			$('.bbox_text').hide();
		})
		
		$('.bbox a').click(function(){
			var option = $(this).attr('rel');
			var ref  = $(this).attr('href');
			var a = [];
			option = option.split(';');
			for(var i = 0; i < option.length; i++){
				var p = option[i].split(':')
				a.push(p[1]);
			}

			if(a[1] != 'none'){
				a[1] = 'url(img/home/bg_b'+a[1]+'.jpg)';
			}
						
			$(this).parent().css('background', a[1] + ' ' + a[0] + ' no-repeat top left');
			
			$('.bbox_text').hide();
			if($(ref).is(':visible')){
				$(ref).hide();
			} else {
				$(ref).show();
			}
			
			
		});
		
	}
	
	if($('.scores').length){
		$('.scores a').click(function(){
			var h = $(this).attr('href');
			ronchetti.openPopup(h);
			
			return false;
			
		});
	}
	
	if($('#projects').length){
	ronchetti.sweetPages({perPage:24, holder: $('#holder')});
	
	$('#projects a').hover(function(){
		var title = $(this).attr('rel');
		var i;
		var t = '';
		title = title.replace('=', '<br />');
		
		$('#project_title').html(title);
	}, function(){
		$('#project_title').empty();	
	});
	
	}
	
});
