/*
 * jQuery Slideshow Plugin v1.0
 * by TechNinja - 12/2010
 *
 * Centrihost
 *
 *
 *Options:
 *
 * time: Auto cycle time in ms
 * width/height: the height/width of the intended slideshow
 * pause: weather to start paused
 * desc_container: jquery selector identifying where the title for the slide
 *   should be placed, defaults to none.
 * readmore_text: if set, will add a link with that anchor text of the slide's
 *   href under the description text in the desc container
 *
 */

jQuery.fn.slideshow = function(settings) {
	settings = jQuery.extend({
		time: 3000,
    width: '400px',
    height: '400px',
    pause: false,
    desc_container: null,
    readmore_text: null
	}, settings);
	return this.each(function(){
		var list = $('ul', this);
    var thumbs = $('div.static', this);
    var timer_id = 0;
    var descriptions = [];
    var max = 0;

		// Setup Container CSS
		list.css({
      width: settings.width,
      height: settings.height,
      position: 'relative',
      overflow: 'hidden',
      padding: 0,
      margin: 0
    });

    // Setup item CSS
		list.find('li').css({
      left: 0, top: 0,
      position: 'absolute'
    });

    // Setup Controller wrapper
    list.before('<div class="slide-control"></div>');
    var controls = list.siblings('.slide-control');

    // Setup items
    $('li', list).each(function(i){
      $(this).data('index', i);
      $(this).addClass('slide_'+i);
      controls.append('<span class="select slide_'+i+'">' + (i + 1) + '</span>')

      // Default frame timeout to settings, otherwise pull it from p tag
      $(this).data('timeout', (parseInt($('p', this).text()) > 0) ? parseInt($('p', this).text())*1000 : settings.time);
      $('p', this).remove();

      // Inititalize and create subelements for description container
      if ($(settings.desc_container).length){
        var readmore = '';
        if (settings.readmore_text){
          readmore = '<a class="readmore" href="' + $('a', this).attr('href') + '">' + settings.readmore_text + '</a>';
        }
        $(settings.desc_container).append('<div style="display:'+(i==0 ? 'block' : 'none')+'">' + $('img', this).attr('title') + readmore + '</div>')
      }

      if (i==0){
        $(this).addClass('selected');
        $(thumbs[i]).addClass('selected');
        $('.slide_0', controls).addClass('selected');
      }else{
        $(this).hide();
        $(thumbs[i]).hide();
      }
      max = i; // Eventually becomes the id of the last slide
    });

    // If subelements for description were created, fill a handy variable with em
    if ($('div', settings.desc_container).length){
      descriptions = $('div', settings.desc_container);
    }

    // Add the Pause Button & bind clicks
    controls.append('<span class="pause">II</span>');
    $('span', controls).click(function(){
      if ($(this).is('.select')){
        // Select button
        slide_change(0, $(this).text()-1);
      }else{
        // Play / Pause button
        settings.pause = !settings.pause;

        if (settings.pause) {
          clearTimeout(timer_id);
        }else{
          slide_start();
        }

      }
    }).css('cursor', 'pointer');



    // Setup slide change
    slide_start();

    function slide_change(offset, id){
      var current = $('li.selected', list);
      var current_id = current.data('index');
      var current_thumb = $('div.static.selected', list.parent());

      // If no id given, move to next
      if (id == null || id > max){
        id = current_id + 1;
        if (id > max) id = 0;
      }else{
        // Automatically pause if one is selected
        slide_stop();
      }

      // Hide old pic
      current.fadeOut('slow').removeClass('selected');
      current_thumb.fadeOut('slow').removeClass('selected');
      $(descriptions[current_id]).fadeOut('slow');

      // Show new pic
      $('li.slide_' + id, list).fadeIn('slow').addClass('selected');
      $(thumbs[id]).fadeIn('slow').addClass('selected');
      $(descriptions[id]).fadeIn('slow');

      // Select control
      $('.select', controls).removeClass('selected');
      $('.slide_'+id, controls).addClass('selected');

      if (!settings.pause){
        slide_start();
      }
    }

    function slide_stop(){
      clearTimeout(timer_id);
      settings.pause = true;
    }

    function slide_start(){
      timer_id = setTimeout(slide_change, $('li.selected', list).data('timeout'));
    }

	});
};
