$(document).ready(function() { 
	prepareTreeMenu();
	prepareCarousel();
	showIntro();
});

function carouselComplete(carousel)
{
	setTimeout(function() {
		$(carousel.items.get(carousel.targetItem)).effect('shake', {duration: 300, times: 1, distance: 2});
	}, 50);
}

function prepareTreeMenu()
{
	$(".menu").treeview({unique:true, collapsed:true, animated:400}); 
}

function prepareCarousel()
{
	
	$('#carousel-move-right').click(function(event) {
		$('#carousel-images').carousel('moveNext');
	});
	
	$('#carousel-move-left').click(function(event) {
		$('#carousel-images').carousel('movePrevious');
	});
	
	$('#carousel-images').click(function(event){
//		event.stopImmediatePropagation();
	});
	
	if (0 < $("#intro").length) { 
		$("#carousel-wrapper").css({opacity: 0, visibility: 'hidden'});
		$("#separator-top *").css({visibility: 'hidden'});
	}
	
	$('#carousel-wrapper .carousel-images').carousel('pause');
}

function showIntro()
{
	if (0 < $("#intro").length) { 
		var imageObj = new Image();
		$(imageObj).attr('src', $('#intro img').attr('src')).load(function() {
			setTimeout(function() {
				hideIntro();
			}, 2250);
		});
		setTimeout(function() {
			if ('none' != $('#intro').css('display')) {
				hideIntro();
			}
		}, 8000);
	} else {
		showCarousel();
	}
}

function hideIntro() {
	$('#intro').fadeTo(1000, 0, function() { // How long it takes to fade
		showCarousel();
		$('#carousel-wrapper').fadeTo(200, 1);
		$("#separator-top *").css({visibility: 'visible'});
		$(this).hide();
	});
}

function showCarousel() {
	$("#carousel .carousel-images").carousel({
		height: 300,
		width: 200,
		animateByMouse: false,
		animate: 0,
		direction: 'right',
		radius: 200,
		handle: false,
		speed: 0.015,
		adjustmentY: 40,
		adjustmentX: -45
	});
	
	if ('none' == $("#carousel .wrapper").css('display')) {
		$("#carousel .wrapper").css({display: 'block'});										  
	}
	
	$('#carousel-move-left').show();
	$('#carousel-move-right').show();
											  
	$("#carousel-wrapper").css({visibility: 'visible'});
	$($('#carousel-wrapper').parents().get(0)).height($("#carousel-wrapper").height());
}

if (jQuery.validator) {
	jQuery.validator.addMethod("multipart", function(value, element, params) {
		var multivalid = true;

		for (var i = 0; i < params.length && true == multivalid; i++)
		{
			if ('' == $.trim($(params[i]).val()))
			{
				multivalid = false;
			}
		}
	
		return ('' != $.trim(value)) &&  multivalid;
	}, jQuery.format("This field is required."));
}
