﻿jQuery.fn.showcase = function(settings) {
    var settings = jQuery.extend({
        speed: 'slow'
    }, settings);
    return this.each(function(i) {
        aShowCase.init(settings, this);
    });
};

var aShowCase = {
	current: 0,
	interval: [],
	elements: [],
    init: function(s, e) {
	  current = 0;
	  elements = $(e).children('.element');
	  aShowCase.fill(e);
      $('.infoZone', e).slideDown('slow');
	  $('a.prev').click(function() {
		aShowCase.prev(e);
		aShowCase.pause(e);
	  });
	  $('a.next').click(function() {
		aShowCase.next(e);
		aShowCase.pause(e);
	  });

	  //$('a.trig').toggle(aShowCase.pause, aShowCase.play);
	  	  
	  aShowCase.play(e);
    },
	prev: function(e) {
	   $('.infoZone', e).slideUp('fast');
	   $(elements[current--]).fadeOut(1, function(){
		if(current < 0) {
			current = elements.length - 1;
		}
		aShowCase.fill(e);	
	   $(elements[current]).fadeIn(1, function(){
		 $('.infoZone', e).slideDown('slow');
	   });	   
	   
	   });
	   return false;
	},
	next: function(e) {
	   $('.infoZone', e).slideUp('fast');
	   $(elements[current++]).fadeOut(1, function(){
		   if(current > (elements.length - 1)) {
			 current = 0;
		   }
		   aShowCase.fill(e);
		   $(elements[current], e).fadeIn(1, function(){
 			 $('.infoZone').slideDown('slow'); 
		   });	   

	   });
	   return false;
	},
	fill: function(e) {
	  $('.infoZone h4', e).text($(elements[current]).attr('category'));
	  $('.infoZone p', e).text($(elements[current]).attr('caption')).append('<br />'+$(elements[current]).attr('time'));
	  $('.infoZone p', e).append(' <a style="color: #FFF; font-size: 12px; text-decoration: none;" href="'+$(elements[current]).attr('node')+'">《全文》</a>');	
	},
	play: function(e) {
	   interval = window.setInterval(aShowCase.next, 6000);		
	},
	pause: function(e) {
	  clearInterval(interval);
	}
};