// JavaScript Document
var navid;
var out = 0;
$(document).ready(function(){
	$('a[rel=external]').click(function(e){
		window.open(this.getAttribute('href'), '_blank');
		e.preventDefault();
	});
	
	$('a, submit, button').focus(function(e){
		this.blur();
		e.preventDefault();
	});
	
	$('#carousel').jcarousel({wrap: 'circular', scroll: 1});
	
	/*$('#navigation a.out').mouseenter(function(){
		navid = $(this).attr('id');
		navid = navid.split('-');
		checkOut(navid[1]);
	});*/
	$('#navigation a.out').bind('mouseenter', mouseEnter);
	
	$('#navigation').mouseleave(function(){
		moveOut(navid[1]);
	});
	
	$('form').jqTransform({imgPath:'images/form/'});
});

function mouseEnter(){
	navid = $(this).attr('id');
	navid = navid.split('-');
	checkOut(navid[1]);	
}

function mouseLeave(){
	moveOut(navid[1]);	
}

function checkOut(id){
	if($('#navigation').css('bottom') == '-25px'){
		if(out != id){
			unbindNav();
			$('#sub-' + out).fadeOut(300, function(){fadeIn(id);});
			out = id;
		}
	}
	else{
		unbindNav();
		moveIn(id);	
	}
}

function moveIn(id){
	unbindNav();
	$('#navigation').animate({
		bottom: '-25'
	}, 300, 'linear', function(){fadeIn(id);});
	out = id;
}

function moveOut(id){
	unbindNav();
	$('#sub-' + id).fadeOut(300, function(){
		$('#navigation').animate({
			bottom: '-112'
		}, 300, 'linear', function(){$('#sub-1, #sub-2, #sub-3, #sub-4, #sub-5, #sub-6, #sub-7, #sub-8').css('display','none'); bindNav();});
	});
}

function fadeIn(id){
	unbindNav();
	if($('#sub-' + id).css('display') == 'none'){
		$('#sub-' + id).fadeIn(300, 'linear', bindNav);
	}
}

function bindNav(){
	$('#navigation a.out').bind('mouseenter', mouseEnter);	
}

function unbindNav(){
	$('#navigation a.out').unbind('mouseenter', mouseEnter);
}
