var Overflow;
window.addEvent('domready', function(){
	adjustPagination();
	if (typeOf($$('div.sidebar_contact')[0]) == 'element') new Contact();
	if (typeOf($$('div.sidebar_category')[0]) == 'element') categoriesLinks();
	if (typeOf($$('div.other_projects')[0]) == 'element') new OtherProjects({sliderStep:3});
	if (typeOf($$('div.default_error')[0]) == 'element') errorPage();
	Overflow = new Overflow();
	new OverflowPhoto();
	new HomeHover();
});

function adjustPagination(){
	var pagination = $$('div.paginate')[0];
	if (typeOf(pagination) == 'element'){
		var width = 0;
		
		$$('div.paginate .selected, div.paginate .pager').each(function(element, index){
			width += parseInt(element.getStyle('margin-right'))+element.getCoordinates().width;
		});

		pagination.setStyles({'width' : width+5});
	}
}

function centerElement(elements){
	elements = $$(elements);
	
	elements.each(function(element){
		var width = 0;
		element.getChildren().each(function(child){
			console.log(parseInt(child.getStyle('width'))+" "+parseInt(child.getStyle('margin-left'))+" "+parseInt(child.getStyle('margin-right')))
			width += parseInt(child.getStyle('width'))+parseInt(child.getStyle('margin-left'))+parseInt(child.getStyle('margin-right'));
		});
		
		element.setStyles({'width' : width});
	});
}

function errorPage(){
	$$('div.freelance')[0].destroy();
	$$('div.snowman')[0].destroy();
}

function categoriesLinks(){
	var links = $$('ul.categories li a');
	links.each(function(link){
		link.store('fx', new Fx.Morph(link.getElement('span.sprite_arrow'), {duration:200, link:'cancel', fps:200}));
		
		link.addEvents({
			mouseenter: function(){
				link.retrieve('fx').start({'margin-left' : 0, 'margin-right' : 21});
			},
			
			mouseleave: function(){
				link.retrieve('fx').start({'margin-left' : 3, 'margin-right' : 18});
			}
		});
	});
}


