var bigData;
var limit;
var cur=0;
var act=new Array(3)
for (i=0; i <3; i++)
act[i]=new Array(2);
var exe;
var czywieszze_dir = '/retail/blue/images/home/'; 

function sendGoogle(vPath) {
	try {
		var pageTracker = _gat._getTracker("UA-1972630-8");
		pageTracker._initData();
		pageTracker._trackPageview(vPath);
	} catch(err) { }
}

function zmieniarka(operation,current){
	cur=current;
	clearTimeout(exe);
	$('#links a').removeClass('active');		
			$('#container').stop().html('').fadeTo(500,0,function(){
			$('<a href="'+bigData[current].href+'"><img src="'+bigData[current].img+'" alt="" /></a>')
			.data('baner', bigData[current])
			.click(function(){
				var item = $(this).data('baner');
				if ( item.ga.click ) {
					sendGoogle(item.ga.click);
				}
				if (item.gemius.click) {
						gemius_hit_int(item.gemius.click,0,0);
					}
				if ( item.newWindow ) {
					window.open( item.href );		
				} else {
						window.location.href = item.href;		
                }
				return false;
			})
			.appendTo('#container');
			current++;
			$('#links .'+current).addClass('active');
			$('#container').fadeTo(500,1);
			if (current<limit) exe=setTimeout('zmieniarka("run",'+(current)+');',4000);
			else exe=setTimeout('zmieniarka("run",0)',4000);
		});
}

$(document).ready(function(){
				$('#kolorowe_d').html('<div id="container"></div><div class="absolute"><div id="background"></div><div id="links"></div></div>');
				bigData=kolorowe.duze;
				limit=bigData.length;
				var links='';
				links+='<a href="#" id="prev">poprzedni</a><div id="nums">';
				for(var i=0;i<bigData.length;i++) {
					$('#container').html('<img src="'+bigData[i].img+'" alt="." />').css('visibility','hidden');
					links+='<a href="'+bigData[i].url+'" class="'+(parseInt(i)+1)+'">'+(parseInt(i)+1)+'</a>';
				}
				links+='</div><a href="#" id="next">następny</a>';
				$('#links').html(links);	
				$('#container').css('visibility','visible');	
				zmieniarka("run",0); 
		
				$("#nums a").live("click", function() {		
					if ( !$(this).hasClass('active') ) zmieniarka("stop",parseInt($(this).attr('class'))-1);
					return false;
				});
				$("#prev").live("click", function() {
					if (cur-1>=0) zmieniarka("stop",cur-1);
					else zmieniarka("stop",limit-1);
					return false;
				});
				$("#next").live("click", function() {
					if (cur+1!=limit) zmieniarka("stop",cur+1);
					else zmieniarka("stop",0);
					return false;
				});
				
				/*małe banerki*/
				smallData=kolorowe.male;
				totalWeight=0;
				for (i=0; i<smallData.length; i++) {
					totalWeight += smallData[i].weight;
				}
				for (var i=0;i<3;i++) {	
					do {
					free=true;
					curSmall=Math.floor(((smallData.length-1)-(0-1))*Math.random());
					for (var j=0;j<act.length;j++) {
						randomWeight=Math.floor(Math.random() * totalWeight);
						if ( act[j][0]==curSmall || act[j][1]==smallData[curSmall].kategoria || randomWeight > smallData[curSmall].weight) free=false;
					}			
					} while (!free);
					act[i][0]=curSmall;
					act[i][1]=smallData[curSmall].kategoria;
					$('<div class="smallBanner"></div>')	
					.html('<div class="bannerArrow"></div><a href="'+smallData[curSmall].href+'"><img src="'+ smallData[curSmall].img[1] +'" alt="" /></a><div class="smallBannerTop"><a href="'+smallData[curSmall].href+'"><img src="'+ smallData[curSmall].img[0] +'" /></a></div>') 
					 .data('baner', smallData[curSmall])
					 .click(function(){
						var obj = $(this).data('baner');
					    if ( obj.ga.click ) {
							sendGoogle(obj.ga.click);
						}
						if ( obj.newWindow ) {
							window.open( obj.href );
						} else {
							window.location.href = obj.href;
						}
						return false;
                    })
					.appendTo('#kolorowe_m');
				}
				$('.smallBanner').mouseenter(function(){
					$(this).find('.bannerArrow').stop().animate({
							'marginLeft': '206px'
					},function() {
						$(this).animate({
							'height':'81px',
							'top':'-3px'
						},function(){
						$(this).parent().find('.smallBannerTop').stop().animate({
						'marginLeft': '0px'
						},{
						step: function(now, fx){
							$(this).parent().find('.bannerArrow').html('');
							$(this).parent().find('.bannerArrow').css('margin-left',now-16);
						}
						});
						});
						});
				});
				$('.smallBanner').mouseleave(function(){
					$(this).find('.smallBannerTop').stop().animate({
						'marginLeft': '236px'
					},{
					step: function(now,fx) {
						$(this).parent().find('.bannerArrow').html('');
						$(this).parent().find('.bannerArrow').css('margin-left',now-30);
					},
					complete: function(){
						$(this).parent().find('.bannerArrow').stop().animate({	
						'height': '15px',
						'top': '47px'
						},function(){
							$(this).animate({
								'marginLeft':'0px'
							},function(){
								$(this).css('overflow','visible');
								$(this).html('<span>Sprawdź</span>');
								});
						});
					}
					});
				});
				$('.smallBannerTop').stop().animate({
						'marginLeft': '236px'
					},{
					step: function(now,fx) {
						$(this).parent().find('.bannerArrow').html('');
						$(this).parent().find('.bannerArrow').css('margin-left',now-30);
					},
					complete: function(){
						$(this).parent().find('.bannerArrow').stop().animate({	
						'height': '15px',
						'top': '47px'
						},function(){
							$(this).animate({
								'marginLeft':'0px'
							},function(){
								$(this).css('overflow','visible');
								$(this).html('<span>Sprawdź</span>');
								});
						});
					}
				}
				);
				/*czy wiesz że*/
				czyWieszZeData=kolorowe.czywieszze;
				curCzyWieszZe=Math.floor(((czyWieszZeData.length-1)-(0-1))*Math.random());
				$('#czyWieszZeContent')					
				.html('<p><a href="'+czyWieszZeData[curCzyWieszZe].href+'"><img src="'+czywieszze_dir+czyWieszZeData[curCzyWieszZe].img+'" alt="'+czyWieszZeData[curCzyWieszZe].alt+'" class="right"/></a><a href="'+czyWieszZeData[curCzyWieszZe].href+'"><b>'+czyWieszZeData[curCzyWieszZe].boldText+'</b></a></p><p><a href="'+czyWieszZeData[curCzyWieszZe].href+'">'+czyWieszZeData[curCzyWieszZe].text+'</a></p><div class="clear">&nbsp;</div><p><a href="'+czyWieszZeData[curCzyWieszZe].href+'" class="more">więcej »</a></p>')
				.click(function(){
					sendGoogle('/czy_wiesz_ze/'+czyWieszZeData[curCzyWieszZe].gID+'/click/');
				});
				sendGoogle('/czy_wiesz_ze/'+czyWieszZeData[curCzyWieszZe].gID+'/view/');

});
