/* jCarousel Callback Functions */
function jcarousel_initCallback(carousel) {
	jQuery('#promo .jcarousel-control a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('name')));
		return false;
	});
};

function jcarousel_itemVisibleInCallback(carousel, li, index, action) {
	$('#promo' + index).hide().fadeIn(1000); /* Slide fading appearance */
	
	$('#promo .jcarousel-control a').each(function(){
		if ($(this).attr('name') == '' + index) {
			$(this).addClass('current');
			return false;
		};
	});
};

function jcarousel_itemVisibleOutCallback(carousel, li, index, action) {
	$('#promo .jcarousel-control a').each(function(){
		if ($(this).attr('name') == '' + index) {
			$(this).removeClass('current');
			return false;
		};
	});
};

/* DOM Ready */
$(function() {

	/* jCarousel */
	$('#promo > ul').jcarousel({
		animation: 0,
		auto: 5,
		initCallback: jcarousel_initCallback,
		itemVisibleInCallback: jcarousel_itemVisibleInCallback,
		itemVisibleOutCallback: jcarousel_itemVisibleOutCallback,
		scroll: 1,
		wrap: 'both'
	});
	
	$('#portfolio.slider > ul.items').jcarousel({
		animation: 500,
		buttonPrevHTML: '<img src="/images/blank.gif" class="arrow prev" alt="&larr;">',
		buttonNextHTML: '<img src="/images/blank.gif" class="arrow next" alt="&rarr;">',
		scroll: 2,
		wrap: 'circular'
	});
	
	/* Popup */
	$('#showContacts').click(function() {
    $('#popupContacts').dialog('open').dialog({
    	close: function() {
  			$('#showContacts').removeClass('active');
    	} 
    }).position({
			my: 'center top',
			at: 'center bottom',
			of: '#showContacts',
            offset: '0 20', /* 'left top' */
			collision: 'flip'
		});
		$('#showContacts').addClass('active');
		return false;
	});
	
	$('#popupContacts > .close').live('click', function() {
		$('#popupContacts').dialog('close');
	});
	
	/* Header */
	$(window).scroll(function() {
		var headerFixed = $('header.fixed');
		var headerOffset = headerFixed.offset();

		$('header').addClass('fixed');
		$('#push').addClass('push');

        if(headerOffset && headerOffset.top == 0) {
            $('header').removeClass('fixed');
            $('#push').removeClass('push');
        }
	});
	
	/* Hovering */
	$('#portfolio .items li a').hover(
		function() {
			$(this).children('.preview380').children('.hover').fadeIn(250);
		}, function() {
			$(this).children('.preview380').children('.hover').fadeOut(250);
		}
	);
	
	/* Scrolling */
	function scrollTop(speed) {
		$('html, body').animate({scrollTop:0}, speed);
	}
	
	function checkHeight() {
		if ($(document).height() > $(window).height()) {
			$('#scrollTop').show();
		} else {
			$('#scrollTop').hide();
		}
	};
	
	checkHeight();
	
	$(document).resize(function() {
		checkHeight();
	});
	
	$('#scrollTop').click(function() {
		scrollTop(500);
		return false;
	});
	
});
