
$().ready(function() {

	$('#slideshow ul').jcarousel({
		scroll: 6,
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
        buttonPrevHTML: null,
		easing: 'easeOutQuad'
	});

	$('#contactForm').ajaxForm({
		beforeSubmit: function(a,f,o) {
			$('#contact a.send').hide();
			$('#contact p.loader').show();
		},
		success: function(result) {
			processFormResult(result);
		}
	});
	
	$('#contactForm a.send').click(function(e){
		e.preventDefault();
		$('#contactForm').submit();
	});

	$('div#portfolio-list .filter a').click(function(e){
		e.preventDefault();
		if(!$(this).hasClass('active')){
			$('div#portfolio-list ul.list li').hide();
			$('div#portfolio-list .preloader').show();
			$.get(
				"/gallery/getlist",
				{ type: $(this).attr('rel'), url: $('#url').val() },
				function(result){
					$('div#portfolio-list ul.list').html(result);
					$('div#portfolio-list ul.list li').each(function(e){
						$(this).hide();
						$('div#portfolio-list .preloader').fadeOut(250);
						$(this).delay((e+1)*200).fadeIn(800);
					});
				}
			);
			$('div#portfolio-list .filter a').removeClass('active');
			$(this).addClass('active');
		}
	});

	if($('#portfolio .slideshow img').length > 1){
		$('#portfolio .slideshow').cycle({
			//fx: 'scrollLeft',
			timeout: 0,
			next: '#next',
			prev: '#prev'
		});
	
		$('#portfolio .images a').click(function(e){
			e.preventDefault();
			$('#portfolio .slideshow').cycle(parseInt($(this).attr('rel')));
		});
	}


	if($('#home .promo ul.images li').length > 1){
		start = true;
		$('#home .promo ul.images').cycle({
			timeout: 5000,
			speed: 800,
			easing: 'easeOutQuad',
			pager: '#home .promo .pagination',
			before: function(curr, next, opts){
				if(!start){
					$('.info'+opts.currSlide).animate({opacity: '0', left: '+=300'}, '600');
					$('.info'+opts.nextSlide).animate({left: '400', opacity: 0}, '0');
					$('.info'+opts.nextSlide).delay(200).animate({left: '100', opacity: 1}, '600');
				}
				start = false;
			}
		});
	}
	
	$('a.top').click(function(e){
		e.preventDefault();
		$('body,html').animate({ scrollTop: 0 }, 800);
	});

});

function processFormResult(result){
	result = jQuery.parseJSON(result);
	$('.error').removeClass('active');
	for (error in result.errors){
		$('#'+error+'Error').show();
	}
	$('#contact p.loader').hide();
	if(result.status == 'success'){
		$('#contact p.success').fadeIn('50');
	}else{
		$('#contact a.send').show();
	}
	
}

function mycarousel_initCallback(carousel) {
    $('#slideshow a.next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#slideshow a.prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

