$(document).ready( function() {
        	
});

function reformat_images( container_id ) {
    var counter = 1;

    $('.cycling_image_extras').hide();

    var return_text = "<div class='cycling_images' style='display:none;'>\n";
    var caption_bar = "<div class='cycling_caption_bar'>\n";

    cycling_images['container_' + container_id] = new Array();
    var number_of_images = $('#' + container_id + ' img').length;

    $('#' + container_id + ' .cycling_image_container img').each( function() {

        var next_image_class = "cycling_image_" + counter;

        var link = $(this).parent('a').attr('href');
        var nolines = $(this).hasClass('nolines') ? ' class="nolines"' : '';
        if (link) { return_text += "<a href='" + link + "'" + nolines + ">"; }
        return_text += "<img src='" + $(this).attr('src') + "' class='" + next_image_class + "'>\n";
        if (link) { return_text += "</a>"; }

		if ($(this).attr('link'))
		{
       		 caption_bar += "<div class='cycling_caption cycling_caption_" + counter + "' link='" + $(this).attr('link') + "'><span>" + $(this).attr('caption') + "</span></div>\n";
		}
		else
		{
       		 caption_bar += "<div class='cycling_caption cycling_caption_" + counter + "'><span>" + $(this).attr('caption') + "</span></div>\n";
		}
        counter++;

    } );

    caption_bar += "</div>\n";

    $('#' + container_id + " .cycling_image_container").html( return_text );
    $('#' + container_id + ' .cycling_images').show();

    $('#' + container_id).append( caption_bar );
    $('#' + container_id).append( "</div>\n" );

    var caption_width = Math.floor( $('#' + container_id).width() / number_of_images );
    var leftover = $('#' + container_id).width() - ( caption_width * number_of_images );




    $('#' + container_id + ' .cycling_caption').each( function() {
        $(this).css('width', caption_width);
        $(this).prepend("<div class='cycling_progress_bar'><div class='cycling_progress_bar_move'></div></div>");

        var current_caption = $(this).attr('class').match(/cycling_caption_\d+/).toString();
        $(this).click( function() { 
        	if ($(this).attr('link'))
        	{
        		document.location.href = $(this).attr('link');
        	}
        	else
        	{
       			rotate_images( container_id, current_caption.replace(/caption/, 'image'), false);
       		} 
        } );
    } );




    var last_width = ( (caption_width * 1) + (leftover * 1) );
    $('#' + container_id + ' .cycling_caption:last').css( 'width', last_width );
    if ($('#' + container_id + ' .cycling_image_1').parent('a').hasClass('nolines')) {$('#scan-lines').addClass('nolines');} else {$('#scan-lines').removeClass('nolines');}
    $('#' + container_id + ' .cycling_image_1').show();
    make_caption_active( container_id, 'cycling_caption_1', true );
    $('.' + container_id + '_cycling_image_extras_1').show();

    timer_ids[container_id] = setTimeout("rotate_images('" + container_id + "', 'next', true);", $('#' + container_id).attr('delay') * 1000);

    $('#main-carousel').hover(
        // Mouse over - kill the auto-move timer
        function() { 
            clearTimeout(timer_ids[container_id]); 
            timer_ids[container_id] = null; 

            var current_caption = $('#' + container_id + ' .cycling_caption_active').attr('class').match(/cycling_caption_\d+/);
            $('#' + container_id + ' .cycling_caption_active .cycling_progress_bar_move').stop().animate({ 'width': '0' }, 500);
 
 
 			// start timer again. Moved from mouse out
            timer_ids[container_id] = setTimeout("rotate_images('" + container_id + "', 'next', true);", $('#' + container_id).attr('delay') * 1000);

            var current_caption = $('#' + container_id + ' .cycling_caption_active').attr('class').match(/cycling_caption_\d+/);
            make_caption_active( container_id, current_caption, true );

 
 
 
        },

        // Mouse out - start the timer again
        function() {



        } 
     );


}

function rotate_images( container_id, direction_or_id, settimer ) {


    clearTimeout(timer_ids[container_id]);
    timer_ids[container_id] = null;

    var current_image = $('#' + container_id + ' img:visible').attr('class');
    var current_image_number = current_image.match(/\d+$/);

    var next_image = "";

    if ( direction_or_id == 'back' ) {

        if (current_image_number > 1) {
            next_image = 'cycling_image_' + ((current_image_number * 1) - 1);
        } else {
            next_image = $('#' + container_id + ' .cycling_images img:last').attr('class');
        }

    } else if ( direction_or_id == 'next' ) {

        next_image = 'cycling_image_' + ((current_image_number * 1) + 1);

    } else {

        next_image = direction_or_id;

    }



    var selector = '#' + container_id + ' .' + next_image;
    if ( ! $(selector).is(':hidden') ) { next_image = 'cycling_image_1'; }

    var next_image_number = next_image.match(/\d+$/);

	animating = true;
    $('#' + container_id + ' .' + current_image).fadeOut('50', function(){ animating = false }); // 500
    $('#' + container_id + ' .' + next_image).fadeIn('50', function(){ animating = false }); // 500
  
  
    
    $('.' + container_id + '_cycling_image_extras_' + next_image_number).parent().children('div').each( function() { $(this).fadeOut('50',function(){ // 500
/*
      if ($('#' + container_id + ' .' + next_image).parent('a').hasClass('nolines')) {$('#scan-lines').addClass('nolines');} else {$('#scan-lines').removeClass('nolines');}
*/
    }); } );
    $('.' + container_id + '_cycling_image_extras_' + next_image_number).fadeIn('50'); //500

    if (settimer == true) {
        timer_ids[container_id] = setTimeout("rotate_images('" + container_id + "', 'next', true);", $('#' + container_id).attr('delay') * 1000);
    }
    
    setTimeout("make_caption_active( '" + container_id + "', '" + next_image.replace(/image/, 'caption') +"', false );", 100);

}

function make_caption_active( container_id, caption_class, force_move ) {


    $('#' + container_id + ' .cycling_progress_bar').removeClass( 'cycling_progress_bar_active' );
    $('#' + container_id + ' div').removeClass( 'cycling_caption_active' );
    $('#' + container_id + ' .cycling_progress_bar_move').css('width', '0%').hide();
    setTimeout("$('#" + container_id + " .cycling_progress_bar_move').css('width', '0%').hide();", 50);

    $('#' + container_id + ' .' + caption_class + ' .cycling_progress_bar').addClass( 'cycling_progress_bar_active' );
    $('#' + container_id + ' .' + caption_class).addClass( 'cycling_caption_active' );


    if (timer_ids[container_id] || force_move == true) {
        $('.cycling_progress_bar_move').stop().hide();
        $('#' + container_id + ' .' + caption_class + ' .cycling_progress_bar_move').show().animate({ 'width': '100%' }, 
        $('#' + container_id).attr('delay') * 1000, 'linear');
    }

}
