//Tutaj sa drobne funkcje/klasy dla których szkoda tworzyć oddzielne pliki łąśó

function karuzelaTopObiekty(){
	var MO={};
	var interval=null;
	
	var init = function(){
		bindEvents();
		$('.karuzelaTopObiektySwitch').each(function(){ if (this.innerHTML==1){ $(this).css('color', '#2D5C88');}  });
	};
	
	var bindEvents = function(){
		$('.karuzelaTopObiektySwitch').click(function(){ 
			showPage(parseInt(this.innerHTML));
			if (interval){ clearInterval(interval); }
		});
		
		interval=setInterval(function(){
			changePage();
		}, 9000);
	};
	
	var showPage = function(nr){
		var visiblePage = $('#topObiekty .strona:visible').attr('id').substr(3);
		if (visiblePage==nr) { return; }
		$('.karuzelaTopObiektySwitch').each(function(){ 
			if (this.innerHTML==nr){ $(this).css('color', '#2D5C88');} 
			else { $(this).css('color', ''); } 
		});
		$('#topObiekty .strona:visible').fadeOut(function(){
			$('#topObiekty #str'+nr).fadeIn();		
		});
	};
	
	var changePage = function(){
		var visiblePage = $('#topObiekty .strona:visible').attr('id').substr(3);
		if (visiblePage) {
			showPage(visiblePage==1 ? 2 : 1);
		}
	};
	
	init();
}


//================ reklamy curl ==========================
function pobierzReklamyCurl(b){
	$.get('internet/inc/reklama_curl.php', function(odp){ 
		$('#baneryReklamowe #helper').html(odp);
		if(b){
			$('#helper').fadeIn(2000);
		}
	});
}


//================delegacja eventów======================
function classActionListener(){
	var events=[];

	var getEventTarget = function(e){
		e = e || window.event;
		var result = e.target || e.srcElement; 
		return result.nodeType == 3 ? result.parentNode : result;	//safari zwraca text node zamiast np span czy div o który nam chodzi
	};

	return {
		listen : function(e){
			var target = getEventTarget(e);
			for(var i=0; i<events.length; i++){
				if(typeof(events[i])!=='undefined' && events[i].condition(e, target)){
					if(events[i].f(e, target)===false){
						return false;
					}
				}
			}
			return true;
		},
		add : function(condition, f){
			return events.push({condition:condition, f:f})-1;
		},
		
		remove : function(id){
			if (id>events.length-1){
				throw "action listener: wrong id"
			} else{
				delete events[id];
			}
			
		}
	}; 
}



//========================dodawanie do bookamrks==============
function bookmarkSite(title,url){
	if (window.sidebar){ // firefox
		window.sidebar.addPanel(title, url, "");
	}	
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all){// ie
		window.external.AddFavorite(url, title);
	}
}
 



//================================mapka na stronie głównej==========================================
function MapaLinkWsp() { 
	var MO={};
	
	
	var inicjuj = function(){
		bindujEventy();
	};
	
	var bindujEventy = function(){	
			$('#mapaLink').click(function(e){
				var posx = 0;
				var posy = 0;
				if (!e) var e = window.event;
				if (e.pageX || e.pageY) 	{
					posx = e.pageX;
					posy = e.pageY;
				}	else 
				if (e.clientX || e.clientY) 	{
					posx = e.clientX + document.body.scrollLeft
						+ document.documentElement.scrollLeft;
					posy = e.clientY + document.body.scrollTop
						+ document.documentElement.scrollTop;
				}
				
				var korekta = Util.findPosition(document.getElementById('mapaLink'));
				
				//var x=13.637+(posx-korekta.x)/14.4;
				//var y=54.826-(posy-korekta.y)/24.11298;
				var x=13.637+(posx-korekta.x)/19.2;
				var y=54.826-(posy-korekta.y)/32.1506;
				//alert(x+' , '+y); return;
				window.location=document.getElementById('menuMapaLik').href+"#"+x+','+y;
			});
	};
	
	//--START
	inicjuj();
	return MO;
}




//================================LinkiPodMenu==========================================
//linki a
function LinkiPodMenu() { 
	var MO={};
	var timeout;
	
	
	var inicjuj = function(){
		bindujEventy();
	};
	
	var bindujEventy = function(){	
		if($('#linkiPodMenu .sekcja').length==0) { setTimeout(function(){ bindujEventy(); }, 400); return;}
		
		$('#linkiPodMenu .sekcja').mouseover(function(){
			//alert("over");
			clearTimeout(timeout);
			var that=this;					
			timeout=setTimeout(function(){ $('.podsekcja').each(function(){ $(this).removeClass('on'); }); $(that).find('.podsekcja').addClass('on'); }, 300);						
		});


		$('#linkiPodMenu .sekcja').mouseout(function(){
			//alert("out");
			clearTimeout(timeout);
			var that=this;
			timeout=setTimeout(function(){ 	$('.podsekcja').each(function(){ $(this).removeClass('on'); }); }, 200);		
		});	
	};
	
	//--START
	inicjuj();
	return MO;
}



$(function(){ 

 //-----LinkiPodMenu---------
 if (document.getElementById('linkiPodMenu') || document.getElementById('linkiPodMenuD')){	LinkiPodMenu(); }
 
  //-----banery Reklamowe---------
 if (document.getElementById('baneryReklamowe')){	pobierzReklamyCurl(true); setInterval(function(){pobierzReklamyCurl(false);}, 10000);  } 
 
 //-------karuzela top obiektów --------
  if (document.getElementById('topObiekty')){ karuzelaTopObiekty(); }
 
 
 //-----Mapa Link Wsp---------
 if (document.getElementById('mapaLink')){	MapaLinkWsp(); } 
});
