(function(jQuery) {    
  var IE = jQuery.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent)	
  
  
  jQuery.fn.newsRoter = function(options) {	
  
	var opts = jQuery.extend({}, jQuery.fn.newsRoter.defaults, options);
	var slideTimeInterval=setInterval(slideToNext, opts.timeout);	
	items = jQuery('.boxPhoto', this); 
	var zIndex = opts.zIndex; 
	//indeks dla dymków i pagera
	var topZIndex = zIndex + items.length;	
	var fume = jQuery('<div class="boxFume" style="display:none; z-index:'+ (topZIndex + 1) +';"></div>');
	/*if(IE) { function(){
		var image = fume.css('backgroundImage');	
		fume.css({'backgroundImage': 'none',
				  'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"});
	}}*/
	
	jQuery('#boxesPhoto').append(fume);
	
	function slideToNext()
	{
		var _a = jQuery('#boxPag a.activeLink').next(); 		
		if(_a.length==0) _a = jQuery('#boxPag a:first');
		//_a.trigger('click');
		var position = jQuery('#boxPag a').index(_a[0]);
		var _current = items.eq(position);
		slideTo(_a, _current);
	}
	
	function slideTo(_a,_current)
	{
		var onStartZindex = _current.css('z-index');
		//window.console.log("current.css: " + onStartZindex + " topZIndex: " +topZIndex);
		jQuery('#boxPag a').removeClass('activeLink');
		jQuery(_a).addClass('activeLink');
		jQuery(items).animate({'opacity':0},{duration:500, queue: false});
		jQuery(_current).animate({'opacity':1},500);	
		jQuery(items).css('z-index', zIndex);
		jQuery(_current).css('z-index', topZIndex);
		
	}
	
	jQuery.each(items, function(index){
		var current = jQuery(this);	
		current.css('z-index',zIndex-index);
		
		
		var a = jQuery('<a href="#">'+(index+1)+'</a>');
		index > 0 ? current.css('opacity',0):a.attr('class','activeLink');
		a.bind('click', {obj:current}, function(event){	
			clearInterval(slideTimeInterval);
			slideTo(jQuery(this),event.data.obj);			
			//setTimeout('slideTimeInterval=setInterval(slideToNext, opts.timeout)', opts.renew);
			return false;
		});
		
		a.hover(function(){				
			var left = jQuery(this).position().left;
			var _current = current;
			var _newposition = left-40;
			var _zIndex= zIndex; 
			
			
			function showTip(){				
				jQuery(".boxFume").css({left: _newposition, display: 'block', zIndex:topZIndex+2});
				jQuery(".boxFume").animate({'opacity':0.8}, 200);
				jQuery(".boxFume").html('<h2><span>'+_current.find('.boxDesc h2 a').text()+'</span></h2><span class="programName">'+_current.find('.boxDesc .programName').text()+'</span>');
			};
			
			t=setTimeout(showTip, 100);									
			return false;
			
		}, function(){
			clearTimeout(t)
			jQuery(".boxFume").animate({'opacity':0}, 100);
			jQuery(".boxFume").css('z-index',zIndex);
			return false;
		});
		
		jQuery('#boxPag').append(a);		
	});	
	
	jQuery('#boxPag', this).css('z-index', topZIndex+2)
  };  
  
  function debug(jQueryobj) {
	if (window.console && window.console.log)
	window.console.log('count: ' + jQueryobj.size());
  };
  
  jQuery.fn.newsRoter.defaults = {
	zIndex: 10,
	timeout: 7000,
	renew: 1000
  };
})(jQuery);
