var items = new Array();
function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
 	onerror = function () { return true; };
    var idx = carousel.index(i, items.length);
    carousel.add(i, items[idx - 1].content);
	$('#ProjectTitle').html(items[idx-1].title);
	if (items.length < 2) { carousel.lock(); }
}

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
}

function mycarousel_Init (carousel, state) {
    var hideDelayTimer = null;
	if (state == 'init') {
		jQuery('#CarouselForward').bind('click', function() {
		    carousel.next();
		    return false;
		});
		jQuery('#CarouselBack').bind('click', function() {
		    carousel.prev();
		    return false;
		});
		jQuery('#CarouselPlay').bind('click', function() {
		    carousel.startAuto(3);
		    jQuery('#CarouselPlay').hide();
		    jQuery('#CarouselPause').show();
		    return false;
		});
		jQuery('#CarouselPause').bind('click', function() {
		    carousel.stopAuto();
		    carousel.options.auto = 0;
		   	jQuery('#CarouselPause').hide();
		   	jQuery('#CarouselPlay').show();
		    return false;
		});
		
 		jQuery('#ProjectCarousel').mouseout(function () {
		  	if (hideDelayTimer) clearTimeout(hideDelayTimer);
          	hideDelayTimer = setTimeout(function () {
          		hideDelayTimer = null;
		 		jQuery('#CarouselNavigation').fadeOut(1000);
            }, 2000);
            return false;
        });
        jQuery('#ProjectCarousel').mouseover(function () {
		  	if (hideDelayTimer) clearTimeout(hideDelayTimer);
		  	if (items.length > 1) {
	 			jQuery('#CarouselNavigation').fadeIn(1000);
	 		}
            return false;
        });
        jQuery('#CarouselNavigation').mouseover(function () {
		  	if (hideDelayTimer) clearTimeout(hideDelayTimer);
            return false;
        });
        jQuery('#CarouselNavigation').mouseout(function () {
		  	if (hideDelayTimer) clearTimeout(hideDelayTimer);
          	hideDelayTimer = setTimeout(function () {
          		hideDelayTimer = null;
		 		jQuery('#CarouselNavigation').fadeOut(1000);
            }, 2000);
            return false;
        });
        
	}
}

$(document).ready(function() {
	$.getJSON(document.URL, function(data) {
		items = data;
	    jQuery('#ProjectCarousel').jcarousel({
        	wrap: 'circular',
			scroll: 1,
			initCallback : mycarousel_Init,
        	itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        	itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
		});
		if (items.length > 1) {
			jQuery('#CarouselNavigation').fadeIn(1000);
		}
    });
});
