/* Breadcrumbs  */

function bcFamilyMouseOver() {
	if (!window.breadCrumbOpen) {
		$('header > nav').animate({right: '-100%'}, 400, 'swing');
	}

	$(this).find('nav').animate({width: 'show'}, 230);
	$(this).parent().unbind('mouseover');
	window.breadCrumbOpen = true;
	if (window.breadCrumbTimeout >= 0) {
		window.clearTimeout(window.breadCrumbTimeout);
	}
}

function bcFamilyMouseOut() {
	window.breadCrumbOpen = false;
	window.breadCrumbTimeout = window.setTimeout(bcClose, 1000);
}

function bcClose()
{
	if(!window.breadCrumbOpen) {
		ulWidth = $('header > nav').width();
		animationLength = ulWidth * 2 / 3;
		
		$('.breadcrumbs > li.family').find('nav').animate({width: 'hide'}, 200);
		
		$('header > nav').css('right', '-' + ulWidth + 'px');
		$('header > nav').animate({right: '0px'}, animationLength, 'swing');
		window.breadCrumbTimeout = -1;
	}
}





/* Misc */

$(document).ready(function() {
	
	/* Breadcrumbs */
	
	$('.breadcrumbs > li.family')
	.bind('mouseover', bcFamilyMouseOver)
	.bind('mouseleave', bcFamilyMouseOut);
	
	
	/* Switchers */
	
	$('.switcher > *').click(function() {
		// Verify we are truly switching pages.
		if($(this).parent().find('.active').index() == $(this).index())
			return false
		
		switchSlider($(this).parent().parent(), $(this).index());
			
		return false;
	});
	
	
	/* Cycling switchers */
	
	$('.switcher.cycle').data('cycling_skip', 0);
	$('.switcher.cycle').data('cycling_allowed', true);
	
	// Cycle interval.
	setInterval(function() {
		$('.switcher.cycle').each(function() {
			if($(this).data('cycling_skip') > 0)
				$(this).data('cycling_skip', $(this).data('cycling_skip') - 1);
			
			if($(this).data('cycling_skip') == 0 && $(this).data('cycling_allowed')) {
				// Fetch the active index.
				var index = $(this).find('.active').index();
				
				// Move one index forward, start from 0 if exceeded the total of switchers.
				index ++;
				if(index >= $(this).find('div').length)
					index = 0;
				
				switchSlider($(this).parent(), index);
			}
		});
	}, 5000);
	
	// Skip cycling while hovering the switcher.
	$('.switcher.cycle > div').hover(function() {
		$(this).parent().data('cycling_allowed', false);
	}, function() {
		$(this).parent().data('cycling_allowed', true);
	});
	
	// Skip cycling while reading.
	$('.switcher.cycle > div').click(function() {
		$(this).parent().data('cycling_skip', 2);
	});
	
	
	
	
	/* Header navigation buttons */
	
	$('header > nav > a').click(function() {
		$('header > nav > a').removeClass('active');
		$(this).addClass('active');
	});
	
	$('header > ul.breadcrumbs > li.family > nav > a').click(function() {
		$(this).addClass('active');
	});
});


function switchSlider(obj, index) {
	// Make the current item active.
	$(obj).find('.switcher > .active').removeClass('active');
	$(obj).find('.switcher > div').eq(index).addClass('active');
	
	// Make the current page active.
	$(obj).find('.pages > div:visible').fadeOut(200);
	$(obj).find('.pages > div').eq(index).fadeIn(200);
}


/* Forms */

function transmitionStatus(state, form) {
	if (!form) form = '';
	if (form.length > 0) form = '#' + form + ' ';
	if (state == true) {
		$(form + '.btn.sel').addClass('pressed');
		$(form + '.submit.fader').fadeOut(50);
		$(form + '.circleLoader').addClass('show');
	} else {
		$(form + '.btn.sel').removeClass('pressed');
		$(form + '.submit.fader').fadeIn(50);
		$(form + '.circleLoader').removeClass('show');
	}
}

function doSubmit() {
	transmitionStatus(true);
	$('form').submit();
}
