// JavaScript Document

function image_slideshow_inc( file, obj_name, fn, timeout ){
	eval( 'var testObj = (typeof '+obj_name+'==\'undefined\');' );
	if ( testObj ){
		var e = document.createElement( 'script' );
		e.type = 'text/javascript'; e.src = file;
		document.getElementsByTagName('head')[0].appendChild(e);
		var tout = setTimeout(function(){
			// Timeout for 5 seconds, display error when script is not loaded
			clearTimeout(t);
			if ( timeout ) timeout();
		}, 20000); // ten seconds timeout
		var t = setInterval( function(){
			eval( 'var testObj = (typeof '+obj_name+'!=\'undefined\');' );
			if ( testObj ){
				clearTimeout( t );
				clearTimeout( tout );
				if ( fn ){ fn(); }
			} }, 100 );
	} else { if ( fn ) fn(); }
}

function image_slideshow_start_slideshow(){
	$('.cms-image-slideshow-cycle').each(function(i){
		eval('var opts = '+$(this).attr('options')+';');
		if ( !opts ) opts = null;
		$(this).cycle(opts);
	});
}

// Dynamically load jQuery
setTimeout( function(){
	image_slideshow_inc( _image_slideshow_core[0], 'jQuery', function(){
		image_slideshow_inc( _image_slideshow_core[1], 'jQuery().cycle', function(){
			// Start slideshow
			image_slideshow_start_slideshow();
		});
	});
}, 10 );
