   /***********************************************\
   **  Copyright (c)2008 D@nieL-K for Meblo-Styl  **
   **                                             **
   **  Kopiowanie i rozpowszechnianie             **
   **  wyłącznie za zgodą autora!                 **
   \***********************************************/

function preloadImages() {
	if (document.images) {
		for (var i = 0; i < preloadImages.arguments.length; i++) {
			(new Image()).src = preloadImages.arguments[i];
		}
	}
};

var mebloStyl = {
	currentImage: 1,
	currentPage: '',
	galleryLocation: './zdjecia/',
  interval: new Object(),
	timeout: 5000,

	bindPageAjax: function() {
		$('a.ajax').click(function(){
			var $ths = $(this);
			if(mebloStyl.currentPage != $ths.attr('href')) {
				mebloStyl.loader('show');
				$.ajax({
					url: './pageHandler.php', type: 'POST', data: 'action=get&page='+$ths.attr('href'), dataType: 'html', timeout: 5000,
					success: function(data){
						document.title= 'Meblo-Styl: '+$ths.attr('title');
						$('#pageTitle').text($ths.attr('title'));
						$('#contentInner').html(data);
						tb_init('a.thickbox');
						mebloStyl.loader('hide');
	          mebloStyl.currentPage = $ths.attr('href');
					},
					error: function(){
						alert('Wystąpił nieoczekiwany błąd! Spróbuj ponownie za chwilę.');
						mebloStyl.loader('hide');
					}
				});
			}
			return false;
		});

	},

	changeImage: function(uri) {
			$('#menuImg').css({backgroundImage: $('#menuImgInner').css('backgroundImage')});
			$('#menuImgInner').css({backgroundImage: 'none', opacity: 0});
			$('#menuImgInner').css({backgroundImage: 'url("'+uri+'")'}).animate({opacity: 1}, 250);
	},

	loader: function(what) {
		switch(what) {
			case 'init':
				$('body').append($('<div id="overlay"></div><div id="loader">Ładowanie . . .</div>').css('display', 'none'));
				break;
			case 'show':
				$('#overlay, #loader').fadeIn(150);
				break;
			case 'hide':
				$('#overlay, #loader').fadeOut(450);
				break;
		}
	},

	slideShow: function(what) {
		switch(what) {
			case 'start':
				mebloStyl.interval = window.setInterval(function() {
					if(mebloStyl.currentImage == 8) mebloStyl.currentImage = 1;
					mebloStyl.changeImage(mebloStyl.galleryLocation+'pokaz/'+mebloStyl.currentImage+'.jpg');
					mebloStyl.currentImage++;
				}, mebloStyl.timeout);
				break;
			case 'stop':
				window.clearInterval(mebloStyl.interval);
				break;
		}
	}
};

$(window).bind('load', function () {
	preloadImages('./images/logoms.jpg', './images/welcome.jpg', './images/o_firmie.jpg', './images/oferta.jpg', './images/galeria_zdjec.jpg', './images/kontakt_i_lokalizacja.jpg', './images/meble_kuchenne.jpg', './images/garderoby_i_szafy.jpg', './images/meble_sklepowe_i_biurowe.jpg', './images/pozostale.jpg', mebloStyl.galleryLocation+'pokaz/1.jpg', mebloStyl.galleryLocation+'pokaz/2.jpg', mebloStyl.galleryLocation+'pokaz/3.jpg', mebloStyl.galleryLocation+'pokaz/4.jpg', mebloStyl.galleryLocation+'pokaz/5.jpg', mebloStyl.galleryLocation+'pokaz/6.jpg', mebloStyl.galleryLocation+'pokaz/7.jpg');
	setTimeout(mebloStyl.slideShow, mebloStyl.timeout, 'start');
	mebloStyl.loader('init');

	$('#menuMenu1 a, #menuMenu2 a').mouseover(function(){
		mebloStyl.slideShow('stop');
		mebloStyl.changeImage('./images/'+$(this).attr('rel')+'.jpg');
	}).mouseout(function(){
		mebloStyl.slideShow('start');
	});

	$('#menuMenu1 a, #menuMenu2 a, #menuMenu3 a, a.ajax').click(function(){
		var $ths = $(this);
		if(mebloStyl.currentPage != $ths.attr('rel')) {
			mebloStyl.loader('show');
			$.ajax({
				url: './pageHandler.php', type: 'POST', data: 'action=get&page='+$ths.attr('rel'), dataType: 'html', timeout: 5000,
				success: function(data){
					document.title= 'Meblo-Styl: '+$ths.text().replace(/» /,'');
					$('#pageTitle').text($ths.text().replace(/» /,''));
					$('#contentInner').html(data);
					tb_init('a.thickbox');
					mebloStyl.bindPageAjax();
					mebloStyl.loader('hide');
          mebloStyl.currentPage = $ths.attr('rel');
				},
				error: function(){
					alert('Wystąpił nieoczekiwany błąd! Spróbuj ponownie za chwilę.');
					mebloStyl.loader('hide');
				}
			});
		}
		return false;
	});
});
