var cMenuTooltip = new Class.create({
	initialize: function(menu_id, duration){
		this.menu = menu_id;
		this.duration = duration || 0.5;
		
		this.start();
	},
	
	start: function(){
		var obj = this;
		$(this.menu).select("a").each(function(item, index){
			item.onmouseover = (function(){
				this.show.bind(this).defer(item, index);
			}).bind(obj);
			item.onmouseout = (function(){
				this.hide.bind(this).defer(item, index);
			}).bind(obj);
		});
		
		$(this.menu + "_more").select(".info").each(function(item){
			item.hide();
		});
	},
	
	show: function(item, index){
		// finds the position where we shall show the tooltip and move it there
		var pos = item.positionedOffset();
		var info = $(item.identify() + "i");
		var w = info.getWidth();
		var w2 = item.getWidth();
		
		var x = pos[0] - (w - w2) / 2;
		

		if (info["effects"]){
			info.effects.cancel();
		}
		
		info.setOpacity(0).show().setStyle({
			left: x - 200 + "px",
			visibility: 'visible',
			position: 'absolute'
		}).update(info.innerHTML.gsub(" ", "&nbsp;"));
		
		info.effects = new Effect.Morph(info, {
			style: 'left:' + x + 'px; opacity: 1',
			duration: this.duration
		});
	},
	
	hide: function(item, index){
		var item = $(item.identify() + "i");
		
		if (item["effects"]){
			item.effects.cancel();
		}
		
		item.effects = new Effect.Morph(item, {
			style: 'left: ' + (item.positionedOffset()[0] + 200) + 'px; opacity: 0', 
			duration: this.duration
		});	
	}
});

new cMenuTooltip("mm");

// create e-mail:
$$(".em").each(function(item){
	var mail = item.innerHTML.gsub(" [at] ", "@").gsub(" [em] ", "@");
	item.update("<a href='mailto:" + mail + "'>" + mail + "</a>");
});

// starts the pages
$("the_pages").select(".page").each(function(item, index){
	item.setStyle({position: 'absolute', top: "0px", left: (index * 770) + "px"});
});

// make sure we change the clicking for somethign cool
$$(".hmenu-item").each(function(item){
	if (item.href.indexOf("#") != -1){
		var page = item.href.split("#");
		item.href = "javascript: void(0)";
		page = page[1];
		item.addClassName("p-" + page);
		
		item.onclick = function(){
			showPage(page);
		};
	}
});

var _current_page;
function resizePage(time){
	if (!_current_page){ return }
	time = time || 1.0;
	new Effect.Morph($("the_pages"), {style: 'height: ' + $("p_" + _current_page).getHeight() + 'px;', duration: time});
}

function showPage(page_name){
	if (!page_name){ return }
	_current_page = page_name;
	var p = $("p_" + page_name);
	if (p){
		// changes the style for selected...
		$("mm").select("a.selected").invoke('removeClassName', "selected");
		$("mm").select("a.p-"+page_name).invoke('addClassName', "selected");
		resizePage();
		var ox = $("the_pages").scrollLeft;
		new Effect.Tween($("the_pages"), ox, p.offsetLeft, 'scrollLeft');
	}
}

// shows the current page
var page = location.hash.substr(1) || "index";
showPage.defer(page);

// creates the showcase
new ShowCase("showcase");

// changes lightbox images
var LightboxOptions = {
	fileLoadingImage: 'lib/images/loading.gif',
	fileBottomNavCloseImage: 'lib/images/closelabel.gif'
};

// portfolio
$$(".project-panel").invoke('hide');
$("nav").setStyle({display: 'block'}).hide().select("li > a").each(function(item){
	var filter = item.href.split("#")[1].substr(1);
	item.href = "javascript:void(0)";
	item.onclick = function(){
		var at = new Date()
		if (_oct && at - _oct < 400){ return }
		_oct = at;
		filterPortfolio(item.up(), filter);
	}
	item.onmouseover = function(){
		new Effect.Morph(item, {style: 'background-color: #bfc;', duration: 0.25});
	}
	item.onmouseout = function(){
		new Effect.Morph(item, {style: 'background-color: #f3f3f3;', duration: 0.25});
	}
});
$("nav").appear();

var _cur_filter = "";
var _oct;
function filterPortfolio(item, filter){
	if (filter == _cur_filter){ return }
	_cur_filter = filter;
	$$(".project-panel").each(function(it){
		if (it.identify() != "project_" + filter){
			if (!it['showing']){ return }
			it.showing = false;
			new Effect.Parallel([
				Effect.BlindUp(it, {sync: true}),
				new Effect.Fade(it, {sync: true})
			], {duration: 0.4, queue: {position: 'end', scope: it.identify()}});
		}
		else{
			Effect.Queues.get(it.identify()).each(function(e){ e.effects.each(function(e){ e.cancel() }); });
			it.showing = true;
			new Effect.Parallel([
				Effect.BlindDown(it, {sync: true, afterUpdate: function(){resizePage(0.01)}}),
				new Effect.Appear(it, {sync: true})
			], {duration: 0.4, queue: {position: 'front', scope: it.identify()}});
		}
	});
	$("nav").select("li").invoke('removeClassName', 'selected');
	item.addClassName('selected');
}

filterPortfolio($("mi_site"), "site");

// Contato
new Validation('frmContato', {immediate: true, onElementValidate: validaInput});

function validaInput(){
	resizePage.delay(0.5);
}