(function($){
  $.fn.extend({ 
    
    slideShow: function(options) {
      var options = $.extend({  
        delay: 5,
        speed: 1000,
        opacity: 0.8,
        click: null,
	    type: null
      }, options);
      
      return this.each(function() {
        var timer = null;
        var aindex = -1;
        var obj = $(this);
        var items = [];
        var display = [null, null];
        var links = null;
        var inAnimation = false;
        
        loadItems();
        buildDisplay();
//        buildLinks();
        showItem(0);
        
        function loadItems() {
          items = [];
          obj.find("li").each(function() {
            items.push({
              order: items.length,
              obj: $(this),
              path: $(this).find("img").attr("src") || null,
              link: $(this).find("a").attr("href") || null,
              rel: $(this).find("a").attr("rel") || null,
              desc: $(this).find("a").attr("title") || null,
              title: $(this).find("a").attr("id") || null
            });
          });
        }

        function buildDisplay() {
          display = [$('<div class="img-cont"><img class="display a1" style="position:absolute"><div class="desc"><h2></h2><span></span></div></div>'), $('<div class="img-cont"><img class="display a2" style="position:absolute"><div class="desc"><h2></h2><span></span></div></div>')];
          
          var clickFn;
		  clickFn = function() {
		    if(items[aindex].link)
		      pageTracker._trackEvent(options.type ,' Big '+(parseInt(aindex, 10)+1), items[aindex].rel);
			  if(items[aindex].link.indexOf('orto.si') > -1 || items[aindex].link.indexOf('toptalk.si') > -1){
				  window.open(items[aindex].link);
				  return false;
			  } else {
				  location = items[aindex].link;
			  }
		  };

          display[0].click(clickFn).hide();
          display[1].click(clickFn).hide();
          obj.append(display[0]);
          obj.append(display[1]);
        }
        
        function buildLinks() {
          links = obj.find("ul");
		  $(".slideShow .links li").show();
          links.addClass("links").css('z-index', '3');
          links.fadeTo(0, options.opacity);

          for(i in items) {
            items[i].obj.html(parseInt(i)+1);
            setEvents(items[i]);
          }
          
          function setEvents(item) {
            item.obj.click(function() {
              showItem(item.order);
            });
          }
        }
        
        function showItem(index) {
          if(inAnimation || index == aindex)
            return;
          inAnimation = true;

          var nindex = index+1 >= items.length ? 0 : index+1;
          if(timer)
            clearTimeout(timer);

          display[0].find('img').attr("src", items[index].path);
		  if(items[index].desc != null && items[index].desc != "" && items[index].desc != 'null'){
			  display[0].find('.desc h2').html(items[index].title);
			  display[0].find('.desc span').html(items[index].desc);
			  display[0].find('.desc').show();
			  display[0].find('.desc').fadeTo(0, options.opacity);
		  } else {
			  display[0].find('.desc').hide();
			  display[0].find('.desc h2').html('');
			  display[0].find('.desc span').html('');
		  }
          display[0].css('z-index', '2');
          display[1].css('z-index', '1');
          if(items[index].link || options.click) {
            display[0].addClass("active");
          } else {
            display[0].removeClass("active");
          }
          display[0].fadeIn(options.speed, function() {
            display[1].hide();
            if(aindex != -1) {
              items[aindex].obj.removeClass("selected");
            }
            items[index].obj.addClass("selected");
            aindex = index;

            if(options.delay > 0)
              timer = setTimeout(function(){
                showItem(nindex);
              }, options.delay*1000+options.speed);
            
            display = display.reverse();
            inAnimation = false;
          });
        }
        
      });
    }
  });
})(jQuery);

