try {
	var cssmenuids = ["cssmenu1"]
	var csssubmenuoffset = -1

	function createcssmenu2() {
		for (var i=0; i<cssmenuids.length; i++) {
			var ultags=document.getElementById(cssmenuids[i]).getElementsByTagName("ul")
			for (var t=0; t<ultags.length; t++){
				ultags[t].style.top=ultags[t].parentNode.offsetHeight+csssubmenuoffset+"px";
				ultags[t].parentNode.onmouseover = function() {
					this.style.zIndex = 100;
					this.getElementsByTagName("ul")[0].style.visibility = "visible";
					this.getElementsByTagName("ul")[0].style.zIndex = 0;
				};
				ultags[t].parentNode.onmouseout = function() {
					this.style.zIndex=0;
					this.getElementsByTagName("ul")[0].style.visibility = "hidden";
					this.getElementsByTagName("ul")[0].style.zIndex = 100;
				};
			}
		}
	};

	if (window.addEventListener) {
		window.addEventListener("load", createcssmenu2, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", createcssmenu2);
	}

	/**
	 * Funkcje systemowe
	 */
	var System = {
		/**
		 * Zalecany do podmiany z przyciskiem "submit" w formularzu
		 */
		getButtonLoader: function() {
			return $('<img src="/skins/default/i/ajax-button-loader.gif" />');
		},

		/**
		 *
		 */
		getBlockLoader: function() {
			return $('<img src="/skins/default/i/ajax-block-loader.gif" id="system-loader-block"/>');
		},

        /**
         *
         */
        showMessage: function(msg, type, parent) {
            var $com = $('<table class="message-' + type + '" cellpadding="0" cellspacing="0"><tr><td class="message-ico">&nbsp;</td><td class="message-container">' + msg + '</td></tr></table>');
            $com.insertBefore(parent);
            setTimeout("$('.message-" + type + "').slideUp(500, function(){$('.message-" + type + "').remove();});", 4000);
        }
	};

	/**
	 * Newsletter
	 */
	var Newsletter = {
		// Inicjalizacja
		init: function() {
			if ($('.newsletterform').length > 0) {
				$('.newsletterform').submit(function() {
					if ($("#usr_email").attr("value").length > 0) {
						Newsletter.submit($(this));
					} else {
						alert('Proszę wprowadzić adres e-mail.');
					}
					return false;
				});
			}
		},

		// Wysyłanie wiadomości
		submit: function(form) {
			var btn = form.find('input[type=submit]');
			var loader = System.getButtonLoader();
			$.ajax({
				url: form.attr('action'),
				data: form.serialize(),
				type: 'post',
				dataType: 'html',
				beforeSend: function() {
					btn.hide();
					btn.parent().append(loader);
				},
				success: function(response) {
					var parent = form.parent()
					parent.html(response);
					Newsletter.init();
				},
				error: function() {
					btn.show();
					loader.remove();
				}
			});
		}
	};

	/* KOMENTARZE */
	var Comments = {
		link: false,

		init: function() {
            
			// odpowiedz
			$('.comment-answer').live('click', function() {
                
				$('.comment-form-answer').each(function() {
					$(this).parent('.comment-form-block').replaceWith(Comments.link);
				});

				Comments.link = $(this);
				var loader = System.getBlockLoader();
				var parent = Comments.link.parent();
				$.ajax({
					url: Comments.link.attr('href'),
					beforeSend: function() {
						parent.html(loader);
					},
					success: function(response) {
					   parent.html(response);
					   Comments.initAnswer();
					},
					error: function() {
						alert('Dodanie komentarza nie powiodło się');
					}
				});

				return false;
			});

			// dodanie nowego komentarza
			$('#comment-form').submit(function(){
				Comments.add($(this));
				return false;
			});

			Comments.last($("#comment-list"));
		},

		//
		initAnswer: function() {
			$(".comment-form-answer").submit(function(){
				Comments.answer($(this));
				return false;
			});
		},

		// Dodanie komentarza
		add: function($t) {
			var btn = $t.find("input[type=submit]");
			var loader = System.getButtonLoader();
			$.ajax({
				url: $t.attr("action"),
				data: $t.serialize(),
				type: "post",
				dataType: "html",
				beforeSend: function() {
					btn.hide();
					btn.parent().append(loader);
				},
				success: function(response) {
                    if ($(response).find(".errors").length > 0) {
                        $t.parent().replaceWith(response);
                        Comments.init();
                    } else {
                        $t.parent().slideUp(1000, function(){
                            $t.parent().remove();
                        });

                        Comments.prepend($("#comment-list"), response);
                        Comments.last($("#comment-list"));
                        System.showMessage("Dodano komentarz", "success", "#comment-list");
                    }
				},
				error: function(request, response) {
					btn.show();
					loader.remove();
				}
			});
		},

		// Odpowiedź
		answer: function($t) {
			var btn = $t.find('input[type=submit]');
			var loader = System.getButtonLoader();
			$.ajax({
				url: $t.attr("action"),
				data: $t.serialize(),
				type: "post",
				dataType: "html",
				beforeSend: function() {
					btn.hide();
					btn.parent().append(loader);
				},
				success: function(response) {
                    if ($(response).find(".errors").length > 0) {
                        $t.parent().replaceWith(response);
                        Comments.initAnswer();
                    } else {
                        Comments.prepend($t.parents('.comment-frame:first'), response);
                        $t.parent('.comment-form-block').replaceWith(Comments.link);
                    }
				},
				error: function() {
					btn.show();
					loader.remove();
				}
			});
		},

		// Czyszczenie pól formularza
		clear: function($t) {
			$t.find('ul.errors').remove();
			$t.find('input.text, textarea').attr("value", "");
		},

		// Dodanie nowego wpisu
		prepend: function($t, data) {
			if ($t.parent('li').length == 0) {
				if ($t.prev('ul').length == 0) {
					$t.prepend(data);
				} else {
					$t.prev('ul').prepend(data);
				}
			} else {
				if ($t.next('ul').length == 0) {
					$t.after('<ul>' + data + '</ul>');
					$t.parent().find('li.new').addClass('last');
				} else {
					$t.next('ul').find('li:first').before(data);
				}
			}
		},

		// Zakańczanie
		last: function($ul) {
            var sub = $ul.find("ul");
            if (sub.length > 0) {
                sub.each(function() {
                    $(this).find('li:last-child').addClass("last");
                });
            }
		}
	};

	/* ANKIETY */
	var Poll = {
		// Inicjalizacja
		init: function() {
			if ($('.poll-form').length > 0) {
				$('.poll-form').submit(function() {
					if ($(this).serialize().length > 0) {
						Poll.submit($(this));
					} else {
						alert('Wybierz przynajmniej jedną odpowiedź.');
					}

					return false;
				});
			}
		},

		// Wysłanie formularza
		submit: function(form) {
			var btn = form.find('input[type=submit]');
			var loader = System.getButtonLoader();
			$.ajax({
				url: form.attr('action'),
				data: form.serialize(),
				type: 'post',
				dataType: 'html',
				beforeSend: function() {
					btn.hide();
					btn.parent().append(loader);
				},
				success: function(response) {
					form.parent().fadeOut(500, function(){
						form.replaceWith(response);
					}).fadeIn(500);
				},
				error: function() {
					btn.show();
					loader.remove();
				}
			});
		}
	};

    /* TOOLBAR */
    var Toolbar = {
        openIndex: null,
        init: function() {
            if ($("#toolbar").length > 0) {
                $("#toolbar .toolbar-menu-top a").click(function(){
                    if ($(this).hasClass("close")) {
                        $("#toolbar .toolbar-container").slideUp("slow");
                        $(this).parents("ul").find("li.selected").removeClass("selected");
                        return false;
                    }

                    var container = $("#toolbar .toolbar-container");
                    if (container.css("display") == "none") {
                        container.slideDown("slow");
                    }

                    $(this).parents("ul").find("li").each(function(){
                        if ($(this).hasClass("selected")) {
                            $(this).removeClass("selected");
                        }

                        var anachor = $(this).find("a");
                        if (!anachor.hasClass("close")) {
                            $(anachor.attr("class")).css("display", "none");
                        }
                    });

                    var $id = $($(this).attr("class"));
                    var $url = $(this).attr("href");
                    if ($id.html().length < 1) {
                        $.ajax({
                            url: $url,
                            beforeSend: function() {
                                $id.html(System.getBlockLoader());
                            },
                            success: function(response) {
                                $id.html(response);
                            },
                            error: function() {
                                $id.html("");
                            }
                        });
                    }

                    $(this).parent().addClass("selected");
                    $($(this).attr("class")).css("display", "block");
                    return false;
                });
            }
        },

        /* WYSYLANIE DANYCH */
        send: function($form) {
            var btn = $form.find("input.submit");
            var loader = System.getButtonLoader();
            $.ajax({
                url: $form.attr("action"),
                type: "post",
                data: $form.serialize(),
                beforeSend: function() {
                    btn.hide();
					btn.parent().append(loader);
                },
                success: function(response) {
                    $form.parent().parent().html(response);
                },
                error: function() {
                    btn.show();
                    loader.remove();
                }
            });
        },

        /* FORMULARZ PYTANIA */
        initFormQuestion: function() {
            $("#toolbar-form-question").submit(function(){
                Toolbar.send($(this));
                return false
            });
        },

        /* FORMULARZ BLEDU */
        initFormError: function() {
            $("#toolbar-form-error").submit(function(){
                Toolbar.send($(this));
                return false
            });
        },

        /* FORMULARZ POPOZYCJI */
        initFormProposal: function() {
            $("#toolbar-form-proposal").submit(function(){
                Toolbar.send($(this));
                return false
            });
        },

        /*  */
        openTab: function(tabIndex) {
            $("#toolbar .toolbar-menu-top").find('li:eq(' + tabIndex + ')').find('a').click();
        }
    };

	/* LOGOWANIE */
	/*var Login = {
		// Inicjalizacja
		init: function() {
			if ($('.login-form').length > 0) {
				$('.login-form').submit(function(){
					if ($("#login").attr("value").length > 0) {
						Login.submit($(this));
					} else {
						alert('Proszę podać login i hasło do konta.');
					}
					return false;
				});
			}
		},

		// Wysłanie formularza z logowaniem
		submit: function(form) {
			var btn = form.find('input[type=submit]');
			var loader = System.getButtonLoader();
			var parent = form.parent().parent();
			$.ajax({
				url: form.attr("action"),
				data: form.serialize(),
				type: 'post',
				dataType: 'json',
				beforeSend: function() {
					btn.hide();
					btn.parent().append(loader);
				},
				success: function(response) {
					if (response.result == 1) {
						location.reload(true);
					} else {
						window.location = '/login';
					}
				},
				error: function() {
					btn.show();
					loader.remove();
				}
			});
		}
	}*/

    var Ratings = {
        init : function() {
            $('div.rate-stars a').click(function() {
                $('div.ratings div.ratings_info').html(System.getButtonLoader());
                $(this).stop();
                var url = $(this).attr('href');
                var data = {ajax : 1};
                $.getJSON(url, data, function(response){
                    $('div.ratings span.ratings_num').text(response.ratings_num);
                    $('div.ratings span.ratings_value').text(response.ratings_value);
                    $('div.ratings div.ratings_info').html('<p>'+response.ratings_info+'</p>');
                });
                return false;
            });
        }
    };

    /* GALERIA */
    var Gallery = {
        init: function() {
            $("#gallery-search-send").click(function(){
                if ($("#gallery-search").attr("value").length < 1) {
                    alert("Proszę wprowadzić słowo lub zdanie.");
                    return false;
                }
            });
        }
    };

	$(document).ready(function(){
		$('.print, .ico-print').click(function(){
			 window.open($(this).attr('href'), 'TPN', 'height=600,width=800,scrollbars=1');
			 return false;
		});

        if($('.facebook-login').length > 0) {
            
            $('.facebook-login').click(function(){
                var screenX    = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft;
                var screenY    = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop;
                var outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth;
                var outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22);
                var width    = 600;
                var height   = 320;
                var left     = parseInt(screenX + ((outerWidth - width) / 2), 10);
                var top      = parseInt(screenY + ((outerHeight - height) / 2.5), 10);

                var newWindow = window.open($(this).attr('href'), '', 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);

                if (window.focus) {
                    newWindow.focus();
                }
                
                return false;
            });
        }

        // Powiększanie zdjęcia w galerii.
		$('.photo .image img').mouseover(function(){
			$('.zoom-in').css("display", "block");
		}).mouseout(function(){
			$('.zoom-in').css("display", "none");
		});

        // Usuwanie zdjecia z galerii uzytkownika.
		$('.usergallery-delete').click(function(){
            var parent = $(this).parents('div.gallery-table');
            var copy = parent.html();
			if (confirm('Czy jesteś pewien/pewna, że chcesz usunąć zdjęcie ?')) {
				$.ajax({
					url: $(this).attr("href"),
                    beforeSend: function() {
                        parent.html(System.getBlockLoader());
                    },
					success: function(response) {
						parent.replaceWith(response);
					},
                    error: function() {
                        parent.replaceWith(copy);
                        alert('Wystąpił błąd podczas połączenia. Zdjęcie nie zostało usunięte.');
                    }
                });
			}
			return false;
		});

        // AJAXOWA PAGINACJA
		$('.ajax-pagination a').live('click', function(){
			var me = $(this).parents('div.gallery-table');
			$.ajax({
				url : $(this).attr("href"),
				beforeSend: function() {
					$(me).animate({opacity: 0}, 'fast');
				},
				success: function(response) {
					me.html(response);
				},
				complete: function(){
					$(me).animate({opacity: 1}, 'fast');
				}
			});
			return false;
		});

        // Slider galerii
		$('div.arrow a').live('click', function(){
			var me = $(this).parents('div.slide');
			$.ajax({
				url: $(this).attr("href"),
				beforeSend: function() {
					$(me).animate({opacity: 0}, 'fast');
				},
				success: function(response) {
					me.replaceWith(response);
				},
				complete: function(){
					$(me).animate({opacity: 1}, 'fast');
				}
			});
		   return false;
		});

        /* POWROT */
        $('.ico-backward').click(function(){
            history.back();
            return false;
        });

		/* NEWSLETTER */
		Newsletter.init();

		/* ANKIETY */
		Poll.init();

		/* LOGOWANIE */
		//Login.init();

		/* KOMENTARZE */
		Comments.init();

        /* GLOSOWANIE */
        //Ratings.init();

        /* TOOLBAR */
        Toolbar.init();

        /* GALERIA */
        Gallery.init();

        /* MULTIMEDIA */
        $("#multimedia").click(function(){
            var $modal = $('<div id="multimedia-dialog-modal"></div>');
            $.get(
                $(this).attr("href"),
                function(response) {
                    $modal.html(response).appendTo("body");
                    multimediaInit($modal);
                }
            );
            return false;
        });
	});
} catch(e) {
    //@todo Dorobic raportowanie bledow do plikow.
	alert('Błąd krytyczny. Proszę o kontakt z administratorem.');
}
