(function($) {
$.fn.jPromo = function(o) {
    return this.each(function() {
        var self = $(this);
        var links = new Array();
        var actual = 0;
        var max = 0;
        var timer = null;

        $('a', self).each (function(i) {
        	links[i] = $(this);

        	if (i > 0)
        		links[i].css({display: 'none'});

        	links[i].bind('click', function(e) {
        		e.preventDefault();

        		var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));

				if (!xmlhttp)
					return false;

				//xmlhttp.open('GET', '/index.php?ma=ajax&sid=promo&p=' + ($('img', links[i]).attr('title')));
				xmlhttp.open('GET', '/index.php?ma=ajax&sid=promo&p=' + (links[i].attr('title')));
				xmlhttp.send(content);

				xmlhttp.onreadystatechange = function() {
					presun(xmlhttp, links[i].attr('href'));
				};
        	});

        	max++;
        });


        if (max > 1) {
	        timer = setInterval(function() {
	        	links[actual].css({display: 'none'});

				if (actual >= max - 1)
					actual = 0;
				else
					actual++;

				//links[actual].css({display: 'block'});
				links[actual].fadeIn();
			}, 8000);
        }

        function presun(xmlhttp, url) {
        	if (xmlhttp.readyState == 4) {
				window.location = url;
			}
        }
    });
};
})(jQuery);
