$(document).ready(function() {
	new Slide( {
		contentId : "slideContent"
	});
	
	$('.list li a').tooltip({
		showURL: false
	});
	slideSwitch();
    setInterval( "slideSwitch()", 3500 );
});

function addFavorites() {
	var tytul = document.title;
	var adres = document.location.href;
	if (window.sidebar) {
		window.sidebar.addPanel(tytul, adres, "");
	} else if (window.external) {
		window.external.AddFavorite(adres, tytul);
	} else if (window.opera && window.print) {
		var a = document.createElement('a');
		a.setAttribute('href', adres);
		a.setAttribute('title', tytul);
		a.setAttribute('rel', 'sidebar');
		a.click();
	}
}

/**
 * @version 0.1 Beta 2010-05-11
 */
var Slide = function(argSetup) {
	Setup = {};
	if (argSetup.fps != '') {Setup.fps = argSetup.fps;} else {Setup.fps = 10;}
	if (argSetup.contentId != '') {Setup.contentId = argSetup.contentId};
	
	var Slide = {
		_fps : 15,
		_contentId : Setup.contentId,
		_imagesPath : [],
		_imageCount : 0,
		
		_init : function() {
			setInterval( Slide._render, 1000 / Slide._fps );
			
			$("#" + Slide._contentId).css({
				position: 'relative',
				overflow: 'hidden'
			});
			
			$("#" + Slide._contentId).find("img").each(function ( i , v ) {
				Slide._imagesPath.push( $(v).attr("src") );
				$(v).remove();
			});
			
		},
		
		_render: function () {
			
			if (Slide._imageCount == Slide._imagesPath.length) {Slide._imageCount = 0}
			
			if ($("#" + Slide._contentId).find("img").size() > 0) {
				if ($("#" + Slide._contentId).find("img:last-child").position().left < $("#" + Slide._contentId).width() - $("#" + Slide._contentId).find("img:last-child").width() - 25) {
					$("#" + Slide._contentId).append('<img src="' + Slide._imagesPath[Slide._imageCount] + '" alt="' + Slide._imageCount++ + '" style="left:' + $("#" + Slide._contentId).width() + 'px; position: absolute;">');
				}
			} else {
				$("#" + Slide._contentId).append('<img src="' + Slide._imagesPath[Slide._imageCount] + '" alt="' + Slide._imageCount++ + '" style="left:' + $("#" + Slide._contentId).width() + 'px; position: absolute;">');
			}
			
			$("#" + Slide._contentId).find("img").each(function ( i , v ) {
				$(v).css({
					left: $(v).position().left - 1,
					top: ($("#" + Slide._contentId).height() / 2) - ($(v).height() / 2)
				});
				
				if ( $(v).position().left + $(v).width() < 0 ) {
					$(v).remove();
				}
				
			});
			
		}
	}

	Slide._init();
	return Slide;
}

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
