var paneCount = 0;
var easing_method = 'easeOutExpo';
var easing_speed = 800;

function mycarousel_initCallback(carousel, state)
{
    // Lock until all items are loaded. That prevents jCarousel from
    // setup correctly and we have to do that in the ajax callback
    // function with carousel.setup().
    // We're doing that because we don't know the exact height of each
    // items until they are added to the list.
    carousel.lock();

    jQuery.get(
        'special_textscroller.php',
        {
            'feed': 'http://www.milstudio.com/blog/rss.xml'
        },
        function(xml) {
            mycarousel_itemAddCallback(carousel, xml);
        },
        'xml'
    );
};

function mycarousel_itemAddCallback(carousel, xml)
{
    var $items = jQuery('item', xml);

    $items.each(function(i) {
        carousel.add(i + 1, mycarousel_getItemHTML(this, i+1));
		paneCount++;
    });

    carousel.size($items.size());

    // Unlock and setup.
    carousel.unlock();
    carousel.setup();
	scrollSetup();
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item, paneID)
{
    //return '<p>'+mycarousel_truncate($('description', item).text(), 90)+'</p>';
	var date = $('pubDate', item).text();
	date = date.substr(0,16);
	return '<h3><a href="#" onclick="$(\'#pane' + paneID + '\').stop().scrollTo( 0, ' + easing_speed + ', {easing:\'' + easing_method + '\'} ); return false;">' + date + '</a></h3><div class="pane" id="pane' + paneID + '">' + $('description', item).text() + '</div><a href="#" id="scrollPane' + paneID + '" class="readMore" onclick="$(\'#pane' + paneID + '\').stop().scrollTo( \'+=435px\', ' + easing_speed + ', {easing: \'' + easing_method + '\'} ); return false;" style="display: none; margin-top: 23px;">YOU SHOULD READ MORE</a>';
};

jQuery(document).ready(function() {
    /**
     * We show a simple loading indicator
     * using the jQuery ajax events
     */
    
	/*
	jQuery().ajaxStart(function() {
        jQuery(".jcarousel-clip-horizontal").addClass('loading');
    });

    jQuery().ajaxStop(function() {
        jQuery(".jcarousel-clip-horizontal").removeClass('loading');
    });
	

    jQuery('#mycarousel').jcarousel({
        size: 0,
		visible: 3,
		easing: easing_method,
		animation: easing_speed,
        initCallback: mycarousel_initCallback
    });
	*/
});

function scrollSetup()
{
	for( var i = 0; i < paneCount; i++ )
	{
		$('#pane' + (i+1)).scrollTo( 0 );						// Reset scroll on page refreshes
		if( $('#pane' + (i+1)).height() > 405 )					// If content is too long, hide overflow and display read more link
		{
			$('#pane' + (i+1)).css("height" , "405px");
			$('#pane' + (i+1)).css("overflow" , "hidden");
			$('#scrollPane' + (i+1)).css("display" , "block");
		}
	}
};
