// ----------- do not edit below this line ---------- //

if (!window.console){
	var console = { log:function(comm){ if (Config.devMode == true && $('consoleDiv')) $('consoleDiv').innerHTML = $('consoleDiv').innerHTML+"<br />\n"+comm;/*alert(comm);*/} }
}

String.implement({ 
	toHash: function(){ 
		var hash = new Hash(); 
		this.split('&').each(function(el){
			var t = el.split('=');
			t[0] = t[0].replace("http://maps.google.com/staticmap?center", 'latlng');
			hash.include(t[0].replace(/amp;/, ''), t[1]);  
		});
		return hash; 
	}
});

window.addEvent("domready", function() {

	//tworzenie #consoleDiv pod ie
	if (Browser.Engine.trident && Config.devMode == true) new Element('div',{'id':'consoleDiv'}).setStyles({'position':'absolute','z-index':'13','top':'0','left':'0','background':'#fff','color':'#000'}).inject($('container'), 'before').appendText('consoleDiv');

	// ie bg fix 
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	
	//categories - ostatnie li
	$$('#categories li:last-child').addClass('last-child');
	
	//accordion
	
	//przygotowywanie kolekcji
	$$('#categories ul').each(function(el){
		el.addClass('accordion')
		el.getPrevious().addClass('toggle');		
	});
	//wykrywanie podmenu do rozwiniecia
	accordion_to_show = 99;
	$$('.accordion').each(function(ul, i){
		ul.getElements('.actv').each(function(){
			accordion_to_show = i;			
		})
	})
	//zdejmowanie eventu onclick z a1, a2 w menu kategorii		
	$$('#categories a.toggle').each(function(a){
		a.erase('href');
	})
	
	//accordion - tworzenei instancji
	var myAccordion = new Accordion($$('.toggle'), $$('.accordion'), { 
		show: accordion_to_show,
		opacity: true,
		alwaysHide: true
	});
	
	//sort - obsluga formularza sortujacego
	if ($('sort')) {
		var form = $('sort');
		var select = $('sort').getElement('select');
		var submit = $('sort').getElement('input[type=submit]');
		
		submit.setStyle('display', 'none');
		select.addEvent('change', function(ev){
			console.log(ev);
			form.submit();
		})		
		
	}
	
	//pagenawigator - ostatnia pozycja
	if ($('pagenavigator')) $('pagenavigator').getLast().addClass('last-child');
	
	//h2 - zakraglenia (osadzanie spanow)
	$$('h2').each(function(el) {
		//console.log(el.getStyle('background-color'));
		if (el.getStyle('background-color') == '#fcfae6') 
			el.set('html', '<span class="bottom">'+el.get('html')+'</span>');
	})
	
	//Decorations - osadzanie ozdobnikow
	$each(Config.Decorations.elements, function(props) { 
		$$(props.containers).each(function(el){
			if (props.where == 'bottom' || props.where == null) 
				el.innerHTML += props.symbol;
			else
				el.innerHTML = props.symbol + el.innerHTML;
		})
	})
	
	//ProductsPlayers
	 $$('.playerMenuWrapper').each(function(mw, i) {
	 	 window['productPlayer'+i] = new ProductPlayer({
		 	playerName: Config.ProductPlayers.names[i-1],
		 	visibleMenuItems: Config.ProductPlayers.visibleMenuItems,
		 	menuRev: mw.getPrevious('.rev'),
		 	menuWrapper: mw,
			menu: mw.getFirst('.playerMenu'),
			menuFf: mw.getNext('.ff'),
			contentWrapper: mw.getNext('.playerContentWrapper'),
			content: mw.getNext('.playerContentWrapper').getFirst('.playerContent'),
			delay: Config.ProductPlayers.delay,
			transition_time: Config.ProductPlayers.transition_time
		});
	})
	
	//zakladki z h2 na /kontakt/
	if (window.location.pathname == '/kontakt/') {
		
		//tworzenie zakladek
		var h2s = $$('#content h2');
		var tabs = new Element('ul', {
			'class': 'tabs'
		});
		h2s.each(function(h2, i){
			h2.set('id', '_h2_'+i);
			new Element('li').inject(tabs);
			var a = new Element('a', {
				href: '#_h2_'+i,
				text: h2.get('text'),
				events: {
					'click': function(ev){
						new Event(ev).stop();
						$$('ul.tabs li').removeClass('active');
						$$('div.panel').removeClass('shown');						
						$(a.getParent()).addClass('active');
						$('div_h2_'+i).addClass('shown');
					}
				}
			}).inject(tabs.getLast());
		}) 
		tabs.inject($$('h1')[0], 'after');
		tabs.getFirst().addClass('active');
		
		//owijanie dzialow w divy
		 h2s.each(function(h2, i){
		 	var h2content = new Array();
		 	var el = h2;
		 	while (el.getNext() && el.getNext().get('tag') != 'h2') {
		 		h2content.push(el.getNext());
		 		var el = el.getNext();
		 	}
		 	console.log($$(h2content));
		 	//$$(h2content).destroy();
		 	var panel = new Element('div', {
		 		'class': 'panel',
		 		id: 'div_h2_'+i 	
		 	}).adopt($$(h2content)).inject(h2, 'after');
		 	panel.grab(h2, 'top');
		 })
		 $('div_h2_0').addClass('shown');
	}
	
	//owijanie map statycznych map googla w divy
	var Gmaps = $$('img[src^=http://maps.google.com/staticmap]');
	var i = 1;
	Gmaps.each(function(Gmap){
		new Element('div', {'id': 'Gmap'+i}).wraps(Gmap); //unknown runtime error w ie
		//new Element('div').wraps(div);	
		i++;	
		
	})	
	
	//negocjuj cene
	$$('a.negotiate').each(function(el){
		el.addEvent('click', function(ev){
			new Event(ev).stop();
			//console.log($('container').getSize().y);
			if (Browser.Engine.trident) $('sort').getElement('select').setStyle('display', 'none');
			var bg = new Element('div', {
				id: "negotiate-price-bg",
				styles: {
					height: $('container').getSize().y
				}
			}).inject(document.body, 'bottom');
			var container = new Element('div', {
				id: "negotiate-price-container"
			}).inject(document.body, 'bottom');
			var wrapper = new Element('div', {
				id: "negotiate-price-wrapper",
				'class': "loading",
				styles: {
					'margin-top': window.getScroll().y + 100
				}
			}).inject(container);
			var close = new Element('span', {
				id: 'negotiate-price-close',
				events: {
					'click': function(){
						bg.destroy();
						container.destroy();
						$$('.fc-tbx').destroy();
						if (Browser.Engine.trident) $('sort').getElement('select').setStyle('display', 'inline');
					}
				}	
			}).inject(wrapper);
			var negotiate = new Element('div', {
				id: "negotiate-price",
				html: "<p><em><small>Trwa ładowanie, poczekaj proszę...</small></em></p>"
			}).inject(wrapper);
			
			var formRequest = new Request.HTML({
				url: el.get('href'), 
				method: 'get',
				update: negotiate,
				onSuccess: function() {
					//wrapper.removeClass('loading');
					$('negotiate-price').getElement('.submit').set('value', '');
					var form = $$('form.negotiatiate-price')[0];
					var formcheck = new FormCheck(form);
					form.addEvent('submit', function(ev2){
						new Event(ev2).stop();
						form.set('send', {
							/*method: 'get',*/
							onSuccess: function(responseText) {
								negotiate.set('html', responseText);								
							}
						});
						form.send();
					})					
				}
			})./*get()*/send("ajax=true");
		})		
	})
	
	if ($('negotiate-price-inner')) {
		$('negotiate-price-inner').getElement('.submit').set('value', '');	
	}
	
	$$('.related li:last-child').addClass('last-child');

}) //koniec ondomready

window.addEvent("load", function() {

	$each(Config.Flashes, function(props, id) { //osadzanie flaszy
		
		if ($(props.container) && Browser.Plugins.Flash.version >= props.required_version && window.location.pathname == '/') {
			window[id] = new Swiff(props.swf, {
				container: props.container,
				id: id,
				name: id, //pozwala na dzialanie setVariable!
				width: props.width,
				height: props.height,
				params: {
					'bgcolor': props.bg_color,
					'allowFullScreen': true,
					'wmode': props.wmode
				}
			})
		}
	})

	//osadzanie map googla 
	//http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAZFEZc4Myz8Eeq-JP1NW6_xSaHVGyaExFKlj588Ud5Gmniam3bBTjlvFSwwyJOt1kwdZMUi7pexyRiw
	var Gmaps = $$('img[src^=http://maps.google.com/staticmap]');
	if (Gmaps.length > 0) { 
		//google.load("maps", "2");
		var i = 1;	
		Gmaps.each(function(Gmap){
				
			//if (Gmap /*&& Gmap.getParent().get('id') == 'Gmap'*/) {
				//if (!Gmap.getParent('div.Gmap') {
					
				//}
				var src = Gmap.get('src');
				Gmap = Gmap.getParent();
				
				window.onunload = GUnload; // przy wyjsciu ze strony zwalniamy pamiec 
				if (GBrowserIsCompatible()) {  
					//obrabiamy zrodlo mapy statycznej
					var h = src.toHash();
					var size = h.size.split('x');
					var latlng = h.latlng.split(',');
								
					var map = new GMap2(document.getElementById("Gmap"+i)/*Gmap*/, { 
						size: new GSize(size[0], size[1]) 
					});  
					map.addControl(new GLargeMapControl());  
					map.addControl(new GMapTypeControl());  
					var point = new GLatLng(latlng[0], latlng[1]);
					map.setCenter(point, h.zoom.toInt(), G_NORMAL_MAP);  
					map.addOverlay(new GMarker(point/*, icon*/));
					map.enableScrollWheelZoom();
					i++;			
				}
			//}
		})
	}
	$('seo4').addEvent('click', function(event) {
    	location.href = "http://seo4.net/";
    	new Event(event).stop();
	})
	
}); //koniec onload