var bg_switcher = false;

var bgc = 6;
var bg_last = bgi = 0;
var bgs, bgsl;

var qsl, qsli;

var interval;

// w tej zmiennej trzymamy obiekt mapki
var bm;

$(window).load(function() {
	if (bg_switcher)
	{
		var preload = [
			'gfx/yes/yes0.png',
			'gfx/bg/bg1.jpg',
			'gfx/yes/yes1.png',
			'gfx/bg/bg2.jpg',
			'gfx/yes/yes2.png',
			'gfx/bg/bg3.jpg',
			'gfx/yes/yes3.png',
			'gfx/bg/bg4.jpg',
		];
		$(document.createElement('img')).bind('load', function(){
			if(preload[0]) this.src = preload.shift();
		}).trigger('load');
	}
	
	if ($('#map1').length>0)
	{
		// lista markerów widocznych na samym początku
		markers = [
				{
					"lat":51.1024891,"lng":17.0319201, 	// połozenie markera
					"title":"TWISTER POLAND", 				// tytuł
					"body":"50-011 Wrocław ul. Kościuszki 27<br>"+
						"tel/fax +48/71/ 343-90-26; 343-90-27<br>"+
						"www.moneytransfer.pl",	// opis/treść 
					visible: true,						// czy widoczny? 
					icon:0								// ikona z tablicy icons
				}
		];
		
		// uruchamiane mapki
		$('#map1').bMap({
			mapCenter:  [51.1024891,17.0319201],			// środek mapy
			mapZoom:     16,				// powiększenie - 1 najdalej, 19 - najbliżej
			maxAutoZoom: 16,
			mapType:     G_NORMAL_MAP,		// typ mapy G_NORMAL_MAP G_SATELLITE_MAP G_HYBRID_MAP G_PHYSICAL_MAP
			loadMsg: "<h2>Ładowanie...</h2>",
			markClick: false, 				// czy ma byc dodawany marker po klinięciu?
			randMark: false,					// czy losować marker po kliknięciu w mapkę i dodaniu nowego markera?
			markers: markers
		});
		
		bm = $('#map1').data('bMap');
		
		bm.layerMgrArray[0].data[0].clickf();
	}
				
});


$(function() {
	bgs = $('#backgrounds');
	
	qsl = $('#questions li').hide();
	
	if ($.browser.msie)
	{
		$('input[type=text], input[type=password]').addClass('text');
		$('button, input[type=submit]').addClass('button');
		$('#content a[href^="mailto:"]').css('font-weight', 'bold');
		$('#bottom-nav li:first-child').addClass('first');
		$('#questions li img').each(function() {
			src = $(this).attr('src');
			$(this).replaceWith("<div class='ie_img'><span style='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\""+src+"\", sizingMethod=\"crop\")'></span></div>");
			//alert(src);
			//$(this).find('span').css(' !important");
			//alert($(this).find('span').css('filter'));
		});
		qsli = $('#questions li div.ie_img');
	} else {
		qsli = $('#questions li img');
	}
	
	bg_switcher = (bgs.length>0);
	
	if (bg_switcher)
	{
		for (i=1;i<bgc;i++)
		{
			bgs.append('<li style="background-image:url(gfx/bg/bg'+i+'.jpg);"></li>');
		}
		
		bgsl = $('#backgrounds li');
		
		interval = setInterval(nextBg, 6000);
		
		setTimeout(show_q, 0);
		
		$('#icons li').each(function(i) {
			$(this).click(function() {
				clearInterval(interval);
				bgi = i-1;
				nextBg();
				interval = setInterval(nextBg, 6000);
			});
		});
	}
	
});

function nextBg()
{
	qsl.fadeOut(250);
	
	bgi++;
	
	$(bgsl[bg_last]).fadeOut(1000);
	bgi = bgi%bgc;
	$(bgsl[bgi]).fadeIn(1005);
	
	bg_last = bgi;
	
	$('#icons li.active').removeClass('active');
	$('#icons li:eq('+bgi+')').addClass('active');
	
	setTimeout(show_q, 250);
}


function show_q()
{
	$(qsli[bgi]).hide();
	
	$(qsl[bgi]).fadeIn(1000, function() {
		setTimeout(function() {
			$(qsli[bgi]).fadeIn(1000);
		}, 1000);
	});/*.find('h3').css({'font-size': '3.2em'}).animate({'font-size': '2.3em'});*/
} 

function nextBg2()
{
	bgi++;
	bgi = bgi%bgc;
	
	bg.fadeOut(1000);
	bgn.fadeIn(1005, function()
	{
		bg.attr('id', 'background-next');
		bgn.attr('id', 'background');
		
		bg = $('#background');
		bgn = $('#background-next');
		
		bgn.css('backgroundImage', 'url(gfx/bg/bg'+bgi+'.jpg)');
	});
}


