function setupCompanyPreview() {
	var pozycja = $('input[name="firma_lokalizacja"]').val();

	if ($('#map23').lenght == 1 && pozycja != undefined)
	{
		var lat = pozycja.substr(1, pozycja.indexOf(',') - 1);
		var lon = pozycja.substr(pozycja.indexOf(',') + 2);
		lon = lon.substr(0, lon.length - 1);

		var mapa = new GMap2(document.getElementById("map23"));
		mapa.setCenter(new GLatLng(lat, lon), 10);
		mapa.addOverlay(new GMarker(new GLatLng(lat, lon)));
		mapa.setUIToDefault();
	}

	selectBookmark('b1');

	$('.company .bookmark img').mouseenter(function(){
		$(this).addClass('on');
	}).mouseleave(function(){
		$(this).removeClass('on');
	});

	$('.companyPreview .bookmarks a').unbind('click');
	$('.companyPreview .bookmarks a').click(function(){

		var size = 120;
		var id = $(this).attr('id');

		$('.companyPreview .bookmark').hide();
		$('.companyPreview .bookmark[class~="' + id + '"]').show();
		$('.companyPreview .bookmark .yellowTop').css({'background-position' : size * (id.charAt(1) - 1) + 10 + 'px 0'});
		$('.companyPreview .bookmarks a').removeClass('on');
		$('.companyPreview .bookmarks a#' + id).addClass('on');

		return false;
	});

	$('#firmaWyslij').click(function(){
		$('#layer, #ofertaFormularz').show();
		$('#layer').css({
			'height' : $(window).height() > $(document).height() ? $(window).height() : $(document).height() + 30
		});
		$('#ofertaFormularz').css({
			'left' : $(window).width() / 2 - 200,
			'top' : window.pageYOffset + $(window).height() / 2 - $('#ofertaFormularz').height() / 2
		});

		return false;
	});

	$('#ofertaFormularzWyslij').click(function(){
		var dane = {
			'rodzaj_akcji': 'polec-firme-znajomemu',
			'firma_nazwa': $('#firma_nazwa').val(),
			'imie_nazwisko': $('#imie_nazwisko').val(),
			'email_nadawcy': $('#email_nadawcy').val(),
			'imie_nazwisko_odbiorcy': $('#imie_nazwisko_odbiorcy').val(),
			'email_odbiorcy': $('#email_odbiorcy').val(),
			'tresc': $('#tresc').val()
		};

		$.post("page,oferta-wyslij.html", dane, function(data) {
			$('#ofertaFormularz .email_nadawcy, #ofertaFormularz .tresc, #ofertaFormularz .komunikat').hide();
			$('#email_nadawcy, #email_odbiorcy, #tresc').css({
				'border': '1px solid #cececc'
			});

			if(!data.ok) {
				for(x in data.bledy) {
					$('#' + x).css({
						'border': '1px solid #fb4d4e'
					});
					$('#ofertaFormularz .' + x).text(data.bledy[x]);
					$('#ofertaFormularz .' + x).show();
				}
			} else {
				$('#polec-naglowek').text('Wiadomość wysłana pomyślnie');
			}
		}, 'json');

		return false;
	});

	$('#ofertaFormularzZamknij').click(function(){
		$('#layer, #ofertaFormularz').hide();

		return false;
	});
}

function selectBookmark($id)
{
	$('.bookmark').hide();
	$('.bookmark[class~="' + $id + '"]').show();
	$('.bookmark .yellow-arrow').css({
		'background-position' : 120 * ($id.charAt(1) - 1) + 10 + 'px 0'
	});

	$('.bookmarks a').removeClass('on');
	$('.bookmarks a#' + $id).addClass('on');

	$('#edit input[name="edit"]').val($id);
}

function zaznaczMiasto(idm)
{
	var pytanie =
	{
		'idw' : $('select[name="wojewodztwo"]').val()
	};

	$.post(WEBROOT + 'page,pobierz_miasta.html', pytanie, function(odpowiedz){

		$('select[name="miasto"] option:gt(0), select[name="miejscowosc"] option:gt(0)').remove();

		$.each(odpowiedz, function(val, text){
			$('select[name="miasto"], select[name="miejscowosc"]').append(
				$('<option></option>').val(text.idm).html(text.nazwa)
				);
		});

		$('select[name="miasto"] option[value="' + idm + '"], select[name="miejscowosc"] option[value="' + idm + '"]').attr('selected', 'selected');

	}, 'json');
}

var _config = {
	base_url :null,
	controller :null,
	action :null,
	user_logged :null,
	lang :null,

	init_common : function(){
		this.base_url = location.href.match(/^(https?:\/\/[^\/]*\/)/)[1];

		$.ajaxSetup({
			cache :false
		});

		this.init();
	},

	init : function(){
	}
};

var _main = {
	_this :null,

	init_common : function(){

		this.init();

		var controller = _config.controller.charAt(0).toUpperCase() +
		_config.controller.substr(1).replace(/-[a-z]/ig, function(s){
			return s.substr(1, 1).toUpperCase();
		});
		if (typeof (window[controller]) == "object") {
			this._this = window[controller];
			var action = _config.action.replace(/-[a-z]/ig, function(s){
				return s.substr(1, 1).toUpperCase();
			}) + "Action";
			if (typeof (window[controller]["init"]) == "function") {
				window[controller]["init"]();
			}
			if (typeof (window[controller][action]) == "function") {
				window[controller][action]();
			}
		}
	},

	init : function(){

		// Zamiana miasta na select, jeżeli wybrano województwo.
		if (parseInt($('select[name="wojewodztwo"]').val()) == 0) {
			$('#miastoInput').show();
			$('#miastoSelect').hide();
		} else {
			$('#miastoInput').hide();
			$('#miastoSelect').show();
		}

		$('select[name="wojewodztwo"]').change(function(){
			if ($(this).val() == 0) {
				$('#miastoInput').show();
				$('#miastoSelect').hide();
			} else {
				$('#miastoInput').hide();
				$('#miastoSelect').show();
			}
		});

		// Dymki pomocy
		$('.pomoc').tooltip({
			delay: 0,
			showURL: false,
			positionLeft: true,
			extraClass: 'tooltip'
		});

		$('input[type="hidden"], input.hidden').hide();
		$('input[type="checkbox"], input[type="radio"]').css({
			'border': 'none'
		});

		// Lightbox
		$('#artykul a.zdjecia_mini').lightBox({
			imageBtnClose: WEBROOT  + 'template/img/light_close.gif',
			imageBtnPrev: WEBROOT  + 'template/img/light_prev.gif',
			imageBtnNext: WEBROOT  + 'template/img/light_next.gif',
			imageLoading: WEBROOT  + 'template/img/loading.gif',
			containerResizeSpeed: 350,
			txtImage: 'zdj. ',
			txtOf: '/'
		});

		$('.b2 .lightbox1').lightBox({
			imageBtnClose: WEBROOT  + 'template/img/light_close.gif',
			imageBtnPrev: WEBROOT  + 'template/img/light_prev.gif',
			imageBtnNext: WEBROOT  + 'template/img/light_next.gif',
			imageLoading: WEBROOT  + 'template/img/loading.gif',
			containerResizeSpeed: 350,
			txtImage: 'zdj. ',
			txtOf: '/'
		});

		$('.b3 .lightbox1').lightBox({
			imageBtnClose: WEBROOT  + 'template/img/light_close.gif',
			imageBtnPrev: WEBROOT  + 'template/img/light_prev.gif',
			imageBtnNext: WEBROOT  + 'template/img/light_next.gif',
			imageLoading: WEBROOT  + 'template/img/loading.gif',
			containerResizeSpeed: 350,
			txtImage: 'zdj. ',
			txtOf: '/'
		});

		$('.b4 .lightbox1').lightBox({
			imageBtnClose: WEBROOT  + 'template/img/light_close.gif',
			imageBtnPrev: WEBROOT  + 'template/img/light_prev.gif',
			imageBtnNext: WEBROOT  + 'template/img/light_next.gif',
			imageLoading: WEBROOT  + 'template/img/loading.gif',
			containerResizeSpeed: 350,
			txtImage: 'zdj. ',
			txtOf: '/'
		});

		$('.b4 .lightbox2').lightBox({
			imageBtnClose: WEBROOT  + 'template/img/light_close.gif',
			imageBtnPrev: WEBROOT  + 'template/img/light_prev.gif',
			imageBtnNext: WEBROOT  + 'template/img/light_next.gif',
			imageLoading: WEBROOT  + 'template/img/loading.gif',
			containerResizeSpeed: 350,
			txtImage: 'zdj. ',
			txtOf: '/'
		});

		$('.b5 .lightbox1').lightBox({
			imageBtnClose: WEBROOT  + 'template/img/light_close.gif',
			imageBtnPrev: WEBROOT  + 'template/img/light_prev.gif',
			imageBtnNext: WEBROOT  + 'template/img/light_next.gif',
			imageLoading: WEBROOT  + 'template/img/loading.gif',
			containerResizeSpeed: 350,
			txtImage: 'zdj. ',
			txtOf: '/'
		});

		$('.b6 .lightbox1').lightBox({
			imageBtnClose: WEBROOT  + 'template/img/light_close.gif',
			imageBtnPrev: WEBROOT  + 'template/img/light_prev.gif',
			imageBtnNext: WEBROOT  + 'template/img/light_next.gif',
			imageLoading: WEBROOT  + 'template/img/loading.gif',
			containerResizeSpeed: 350,
			txtImage: 'zdj. ',
			txtOf: '/'
		});

		// Flash (warunek dla IE)
		if (document.getElementById("partnersFlash123") != null)
		{
			swfobject.embedSWF(WEBROOT + "template/flash/Awards.swf",
				"partnersFlash123", "150", "50", "9.0.0", "",
				{
					Time: "2",
					Delay: "5"
				}, {
					wmode: "transparent"
				}, {});
		}

		// Zdjęcia - IE hover hack
		$('.shortcut img, .artList .thumb, .art_pictures_bottom img, #content .news img').mouseenter(function(){
			$(this).addClass('on');
		});

		$('.shortcut img, .artList .thumb, .art_pictures_bottom img, #content .news img').mouseleave(function(){
			$(this).removeClass('on');
		});

		// RSS menu
		$('#rssHolder a.rss').mouseenter(function(){
			$('#rssHolder ul').show();
		});

		$('#rssHolder').mouseleave(function(){
			$('#rssHolder ul').hide();
		});

		// Wybór roku i miesiąca w aktualnościach
		$('#newsSelect select').change(function(){
			$('#newsSelect').submit();
		});

		// Forma logowania
		$('.signinBtn').click(function(){

			$('#signin input').val('');
			$('#loginMsgError').text('');

			$('#layer, #signin').show();
			$('#signin').css({
				position: 'absolute',
				left: ($(window).width() / 2 - 220) + 'px',
				top: ($(window).height() / 2 - 135) + $(window).scrollTop() + 'px'
			});

			$('#layer').css({
				'height' : $(window).height() > $(document).height() ? $(window).height() : $(document).height() + 30
			});

			return false;
		});

		$('#signin #top a').click(function(){
			$('#layer, #signin').hide();
			return false;
		});

		$('#layer').click(function(){
			$('#layer, #ofertaFormularz, #signin').hide();
		});

		$('#loginForm').submit(function(){
			$.post("page,login.html", $("#loginForm").serialize(),
				function(data){
					if(data == 'OK'){ //prawidłowe zalogowanie
						$('#layer, #signin').hide();
						window.location.reload(true);
					} else { //błędne logowanie
						$('#loginMsgError').html(data);
					}
				}
			);

			return false;
		});

		$('.loginForm').submit(function(){
			$.post("page,login.html", $(".loginForm").serialize(),
				function(data){
					if(data == 'OK'){ //prawidłowe zalogowanie
						$('#layer, #signin').hide();
						window.location = WEBROOT + _config.afterLoginGoTo;
					} else { //błędne logowanie
						$('.loginMsgError').html(data);
					}
				}
			);
			return false;
		});

		$('#loginButton').click(function(){
			$('#loginMsgError').text('Proszę czekać...');
			$('#loginForm').submit();

			return false;
		});

		$('.loginButton').click(function(){
			$('.loginMsgError').text('Proszę czekać...');
			$('.loginForm').submit();

			return false;
		});

		$('#remindButton, .remindButton').click(function(){
			$.post("page,przypomnij_haslo.html",{
				mail_przypomnij: $("input[name=mail_przypomnij]").val()
			},
			function(data){
				if(data == 'OK'){ //prawidłowe przypomnienie hasła
					$("input[name=mail_przypomnij]").val("");
					$('#remindMsg, .remindMsg').html('hasło wysłane na podany e-mail');
				} else { //błędne przypomnienie
					$('#remindMsg, .remindMsg').html(data);
				}
			}
			);
			return false;
		});

		$('#login_log, .login_log, input[name="login_log"], input[name="haslo_log"]').keydown(function(e){
			if (e.keyCode == 13) {
				$('#loginButton').click();
				$('.loginButton').click();
			}
		});

		$("#remindLink").click(function(){
			$.post("page,przypomnij_haslo.html",{
				mail_przypomnij: $('#login_log').val()
			},
			function(data){
				if(data == 'OK'){ //prawidłowe przypomnienie hasła
					$('#login_log').val("");
					$('#remindMsg, .remindMsg').html('hasło wysłane na podany e-mail');
				} else { //błędne przypomnienie
					$('#remindMsg, .remindMsg').html(data);
				}
			}
			);
			return false;
		});

		$(".remindLink").click(function(){
			$.post("page,przypomnij_haslo.html",{
				mail_przypomnij: $('.login_log').val()
			},
			function(data){
				if(data == 'OK'){ //prawidłowe przypomnienie hasła
					$('.login_log').val("");
					$('#remindMsg, .remindMsg').html('hasło wysłane na podany e-mail');
				} else { //błędne przypomnienie
					$('#remindMsg, .remindMsg').html(data);
				}
			}
			);
			return false;
		});

		// Komentarze w artykułach
		$('#showComments').click(function(){
			$('#showComments').css({
				'visibility' : 'hidden'
			});
			$('#addComment').show();

			return false;
		});

		// Wybór daty na liście imprez
		$.datepicker.setDefaults({
			minDate: '+3d',
			firstDay: '1',
			dateFormat: 'yy-mm-dd',
			changeYear: true,
			dayNames: ['Niedziela', 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota'],
			dayNamesMin: ['Nd', 'Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So'],
			monthNames: ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień']
		});

		$('.pagWithSelect .date').datepicker();

		$('.pagWithSelect .date').change(function(){
			$('.pagWithSelect form').submit();
		});

		// Drukowanie
		$('.print').click(function(){
			xferWindow = window.open('http://budujesz-kupujesz.pl/drukuj.php','popWin','scrollbars=1,height=600,width=750,top=0,left=0');
			xferWindow.opener = self;

			return false;
		});

		// Wyszukiwarka - wybór działu
		$('#searchResults .search_head .right select').change(function(){
			if ($(this).val() == 'wszystkie')
			{
				$('#searchResults #dzial').show();
			}
			else
			{
				$('#searchResults #dzial').hide();

				$('#searchResults .' + $(this).val()).show();
			}
		});

		// Formumlarze
		$('#przetargi form a.button').click(function(){
			$('#searchAuctions').submit();

			return false;
		});

		$('#catalogFormSearch a.button').click(function(){
			$('#catalogFormSearch').submit();

			return false;
		});

		// Miasta
		$('select[name="wojewodztwo"], select[name="firma_wojewodztwo"]').change(function(){

			var pytanie =
			{
				'idw' : $('select[name="wojewodztwo"], select[name="firma_wojewodztwo"]').val()
			};

			$.post(WEBROOT + 'page,pobierz_miasta.html', pytanie, function(odpowiedz){

				$('select[name="miasto"] option:gt(0), select[name="miejscowosc"] option:gt(0), select[name="firma_miejscowosc"] option:gt(0)').remove();

				$.each(odpowiedz, function(val, text){
					$('select[name="miasto"], select[name="miejscowosc"], select[name="firma_miejscowosc"]').append(
						$('<option></option>').val(text.idm).html(text.nazwa)
						);
				});

			}, 'json');
		});

		//
		// Hack na FCK
		//
		$.each($('.fck td'), function(){
			var value = $(this).attr('bgcolor');

			if (value)
				$(this).css('background-color', value);
		});

		$.each($('.fck img'), function(){
			var value = $(this).attr('align');

			if (value)
				$('.fck img').css('text-align', 'left');
		});

		$.each($('.fck td'), function(){
			var value = $(this).attr('valign');

			if (value)
				$(this).css('vertical-align', value);
		});

		$.each($('.fck table'), function(){
			var value = $(this).attr('cellpadding');

			if (value)
				$(this).css('border-spacing', value + 'px');

			value = $(this).attr('border');

			if (value) {
				$(this).css('border', value + 'px solid #999')
				.find('td').css('border', value + 'px solid #999');
			}
		});

		// Komentarze
		$('.submitCom .button').click(function(){
			$('#addCommentForm').submit();
			return false;
		});

		$('#comments .tresc_krotka').click(function(){
			var comment = $(this).parent().next();

			if (comment.animate({
				height: 'toggle'
			}).is(':visible'))
				comment.css('background-color', '#f2f2f2');
			else
				comment.css('background-color', '#fff');

			return false;
		});

		$('#comments .odpowiedz a').click(function(){
			$('#showComments').click();
			$('#addComment input[name="p"]').val($(this).attr('href').substr(1));

			window.location.hash = '#add';

			return false;
		});
	}
};

$(document).ready( function(){
	_config.init_common();
	_main.init_common();
});
