function getaddress() {
	var xx = location.href.split('/');
	return xx[3];
}

function abrir() {
	
	$('.abre').bind('click',function() {
		$(this).unbind('click');
		$(this).bind('click', function() {
			$(this).parents('li').find('.piece').hide("blind", { direction: "vertical" }, 700).parents('li').removeClass('opened');
			$(this).unbind('click');
			abrir();
		});
		$a = $(this).closest('li');
		$margem = $(this).closest('.margem');
		$nav = $(this).parents('li').find('.navi');

		$a.addClass('opened').find('.piece').show("blind", { direction: "vertical" }, 300,
			function() { 
				if ($a[0] != $('.abre:first').parents('li')[0])
				$.scrollTo($(this).parents('li'), { axis:'y', duration:400 }); 
			}
		);
//		$.scrollTo($(this).parents('li'));
		//console.log($(this))

		
		$cyc = $a.find('.cycle tr td');
		
		if ($cyc.length > 1) 
		{
			$nav.html('');
			$nav.append('<a href="javascript:void(0)" class="prev"><img src="/imgs/ico_prev.png" width="12" height="12"></a>');
			
			$a.find('.cycle').css('left',0);
			$cyc.each(function(i) {
				$nav.append('<a href="javascript:void(0)" class="indice '+(i==0 ? 'sel' : '')+'" title="'+Math.round($(this).offset().left - $margem.offset().left)+'">'+(i+1) + '</a>');
			});
			
			$nav.append('<a href="javascript:void(0)" class="next"><img src="/imgs/ico_next.png" width="12" height="12"></a>');
			$nav.find('.prev').bind('click', function() {
				$este = $(this).parents('li');
				if (!$este.find('a.sel').prev('a.indice')[0])
					$este.find('a.indice:last').click();
				else
					$este.find('a.sel').prev('a.indice').click();
			});
			
			$nav.find('.next').bind('click', function() {
				$este = $(this).parents('li');
				if (!$este .find('a.sel').next('a.indice')[0])
					$este .find('a.indice:first').click();
				else
					$este .find('a.sel').next('a.indice').click();
			});		
			
			$('a.indice').bind('click', function() {
				//alert($(this).attr('title'));
				
				$(this).parents('.navi').find('a').removeClass('sel');
				$(this).addClass('sel');
				$(this).parents('li').find('.cycle').animate({ 
	        		left: - $(this).attr('title') + 88
	        	}, { queue:false, duration:800 });
			})
		} else {
			$nav.html('&nbsp;');
		}
	});
}






$(function(){
	//$("img").lazyload();
	$('.cycle img').lazyload({ placeholder: "/imgs/placeholder.gif" });
//	threshold : 200, 
	abrir();
/*
	$('#refina').change(function() {
		if ($(this).val())
			location.href='/type/'+$(this).val()+'/';
		else
			location.href='/';
	})	
*/

	$('#refina').change(function() {
		if ($(this).val()=='')
			$('.proj').show();
		else {
			$('.proj').hide();
			$('.cat_'+$(this).val()).show();
		}
	})	

	
	$('.close').click(function(e){
	
		if ($(this).parents('li').hasClass('opened')) {
		$(this).parents('.piece').hide("blind", { direction: "vertical" },300).parents('li').removeClass('opened');
		$('.abre').unbind('click');
		abrir();

//		$.scrollTo($('body'), { axis:'y', duration:700 });
		}
	})
	
	$('.closeall').click(function(e){
		$('#menu > li.opened .piece').hide("blind", { direction: "vertical" }, 300);//.hide();
		$('#menu > li').removeClass('opened');
		abrir();
		
		
	})	

	$('#hireme').css('left',$('#menu').offset().left + 800);

	$('.top').click(function(e){
		e.stopPropagation();
	})
	
	$('#xmenu > li').click(function(){
		$(this).addClass('opened').find('.piece').show().find('ul > li').css('left',Math.random()*300);
		$(this).find('ul li').each(function(i){
			$(this).prepend(i).click(function() { alert($(this).html())});
		})
	});
	
	
	var address = getaddress();
	
	if (address=='' || address == 'home') {
		$('.abre:first').click();
		//$('.proj:first').show().addClass('opened');
		
	}
	
	else {
		$('#'+address).click();
//		$.scrollTo($('#'+address), { axis:'y', duration:700 });
		
	//	$.scrollTo('#'+address);
	}
	
});