var rot = false;
var last_active = 0;
var position = 6;

function powieksz(img,width)
{
	img.style.width = Math.round(width)+'px';
			//img.galleryObject.photoLayer.object.style.width = '700px';
			
	img.galleryObject.photoLayer.center();
}
var params = {
	galery:
	{
 		galleryBackgroundId :'layer-background',
 		galleryLayerId:'layer-image',
 		galleryBackgroundClose:true,
 		galleryClass: 'gallery',
	 	galleryHorizontalMargin: 90,
	 	galleryCaptionHtml:true,
	 	galleryDontScale:true, 
 		galleryOnPhotoError:function()
 		{
    		return true;
   		}
	},
		slider:{
		slideShowClass: 'slideshow',
		slideShowTransTime: 250,
		slideShowWaitTime: 5000,
		slideShowTransType: 'opacity',
		slideShowCurrent: 0,
		slideShowLoop: true,
		slideShowOnShowSlide : function() 
  		{
  
  			var id = parseInt(this.currentSlide);
  			var rotator = $('list_rotator');
  			var active = FSite2.getElementsByClassName('num active',rotator);
			var num_count = parseInt(this.getCount());
			FSite2.unsetElementClass(active[0],'active');
			FSite2.setElementClass($('num_'+id),'active');
			
			if(last_active != id)
			{
				if(!rot)
				{
					if(id - last_active == -1 || id - last_active == 1 || id - last_active == -(num_count-1))
					{
						if(id - position == 0  && last_active < parseInt(id) && parseInt(id) > 0)
						{
							//console.log("ID:"+id+" POSITION:"+position+" dodalem 31");
							rotator.scrollLeft+=31;
							position = id+1;						
						}
						else if(last_active > parseInt(id) && position - parseInt(id) > 6 && parseInt(id) > 0)
						{
							//console.log("ID:"+id+" LAST_ACTIVE:"+last_active+" POSITION:"+position+" odjalem 31");
							rotator.scrollLeft-=31;
							position = position - 1; 
							
						}
						else if(parseInt(id) == 0)
						{
							rotator.scrollLeft=0;	
							position = 6;
						}
						else
						{
							//console.log("ID:"+id+" LAST_ACTIVE:"+last_active+" POSITION:"+position+" bez zmian");
						}
					}
					else
					{
						rotator.scrollLeft = num_count*31;
						position = num_count;
						//console.log("ID:"+id+" LAST_ACTIVE:"+last_active+" POSITION:"+position+" wstecz");
					}	
				}
				else
				{
					rot = false;
					//console.log("ID:"+id+" LAST_ACTIVE:"+last_active+" POSITION:"+position+" klik");
				}	
			}
			else
			{
				if(rot)
					rot =false;
				
			}
			
			var active = $('op_'+parseInt(last_active));
			var active2 = $('op_'+parseInt(id));

			last_active = parseInt(id);
			active.style.display = 'none';
			active2.style.display = ''; 				
  		}
	 }
};

function extendForm() 
{
	FSite2.extendForm(document,params.slider);
	FSite2.extendForm(document,params.galery);
}

window.onload = function(){

	setTimeout(function()
	{
		extendForm();
	
	if($('polska') != undefined && $('map_polska') != undefined) 
		SIEC_SPRZEDAZY.init();
		
	if($('google_map') != undefined)
		MAP_GOOGLE.init();
				
		SLIDESHOW.init();
		
			
	if($('photos_small') != undefined)
	{
		var images_small = FSite2.getElementsByClassName('small_images',$('photos_small'));

		for(var j=0;j<images_small.length;j++)
		{	
			images_small[j].onclick = function()
			{
				var id = this.getAttribute("id");
				id = id.split("_");
				id = id[1];
				var active = FSite2.getElementsByClassName('active',$('photo_item'));
				var id_active = active[0].getAttribute("id");
				id_active = id_active.split("_");
				id_active = id_active[1];
				
				if(id != id_active)
				{
					FSite2.unsetElementClass(active[0],'active');
					FSite2.setElementClass($('mid_'+id),'active');	
				}
				
			}
		}
	}


		
	},200);
}
