﻿var $$ = $.fn;

$$.extend({
    SplitID: function () {
        return this.attr('id').split('-').pop();
    },

    Slideshow: {
        Ready: function () {
            var tmpSlideshow = $("#tmpSlideshow");
            var rc = $("#tmpSlideshowControls>.rc");
            var _this=this;
            $.get(url, function (xml) {
                $(xml).find("images").each(function (i) {
                    var img = $(this).attr("img");
                    var link = $(this).attr("link") || "#";
                    var tmpSlide = document.createElement("div");
                    tmpSlide.id = "tmpSlide-" + (i + 1).toString();
                    tmpSlide.className = "tmpSlide";
                    tmpSlide.style.cssText = "display: none;";
                    tmpSlide.innerHTML = '<a href="' + link + '"><img src="' + img + '" border="0" ></a>';
                    tmpSlideshow.append(tmpSlide);
                    if (i == 0)
                        rc[0].innerHTML = '<div class="tmpSlideshowControl tmpSlideshowControlActive" id="tmpSlideshowControl-1"><span>1</span></div>';
                    else
                        rc[0].innerHTML += '<div class="tmpSlideshowControl" id="tmpSlideshowControl-' + (i + 1).toString() + '"><span>' + (i + 1).toString() + '</span></div>';
                });
                $('div.tmpSlideshowControl').hover(
                function () {
                    $(this).addClass('tmpSlideshowControlOn');
                },
                function () {
                    $(this).removeClass('tmpSlideshowControlOn');
                })
            .click(
                function () {
                    $$.Slideshow.Interrupted = true;
                    $('div.tmpSlide').fadeIn().hide();
                    $('div.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');
                    $('div#tmpSlide-' + $(this).SplitID()).show();
                    $(this).addClass('tmpSlideshowControlActive');
                });
                _this.Counter = 1;
            	_this.Interrupted = false;
            	_this.Transition();
            });

        },
        Transition: function () {
            if (this.Interrupted) {
                return;
            }
            this.Last = this.Counter - 1;

            if (this.Last < 1) {
                this.Last = max;
            }

            $('div#tmpSlide-' + this.Last).fadeOut('slow',function () {
            	$('div#tmpSlideshowControl-' + $$.Slideshow.Last).removeClass('tmpSlideshowControlActive');
           		$('div#tmpSlideshowControl-' + $$.Slideshow.Counter).addClass('tmpSlideshowControlActive');
            	$('div#tmpSlide-' + $$.Slideshow.Counter).fadeIn('slow');

            	$$.Slideshow.Counter++;

            	if ($$.Slideshow.Counter > max) {
                	$$.Slideshow.Counter = 1;
            	}
            	setTimeout('$$.Slideshow.Transition();', 5000);
        	});
        }
    }
});

$(document).ready(
  function () {
      $$.Slideshow.Ready();
  }
);
