jQuery(function() {
	$("*:first-child").addClass("first-child");
	$("*:last-child").addClass("last-child");
	$(document).pngFix();
	
	$("#category").linkselect({
		change: function (_, val) {
			location.href = val
		},
		fixedWidth: true
	});

	$("div.news-bottom div.rating").each(function () {
		var box = $(this)
		$(this).find("a").click(function () {
			// mozna glosowac tylko raz bez odswiezenia strony
			box.find("a").remove()
			$.post($(this).attr("href"), { rating: $(this).text() }, function (result) {
				box.find("ul.score-box").addClass("score-box-disabled")
				box.find("span.score").css({ width: result/5*100 + "%" })
			})
			return false
		})
	})
	
	$("a.zoom").fancybox({
		zoomOpacity: true,
		// overlayShow: false,
		zoomSpeedIn: 500,
		zoomSpeedOut: 500,
		titlePosition: 'over'
	});
	
	$("#gallery").each(function () {
		var photos = $("#gallery div.photos img.foto")
		photos.slice(1).hide()
		var go = function (img) {
			if (img.length < 1) return
			photos.hide()
			img.show()
			$("#gallery a.prev-photo")[img.prev().length > 0 ? "removeClass" : "addClass"]("disabled")
			$("#gallery a.next-photo")[img.next().length > 0 ? "removeClass" : "addClass"]("disabled")
			photos.filter(":visible").css({ cursor: (img.next().length > 0 ? "pointer" : "default") })
			$("#gallery .title").text(img.attr("alt"))
			$("#gallery .index").text(photos.index(img)+1)
		}
		$("#gallery a.prev-photo").click(function () {
			var item = photos.filter(":visible").eq(0).prev()
			if (item.length > 0) $.history.load(item.attr("id"))
			return false
		})
		$("#gallery a.next-photo").add(photos).click(function () {
			var item = photos.filter(":visible").eq(0).next()
			if (item.length > 0) $.history.load(item.attr("id"))
			return false
		})
		go(photos.eq(0))
		$.history.init(function (hash) {
			go((hash && $("#"+hash).length > 0) ? $("#"+hash) : photos.eq(0))
		})
	})
	
	$("#topmenu li").hover(function () {
		$(this).addClass("hover");
		$('> .dir',this).addClass("open");
		$('ul:first',this).css('visibility', 'visible');
	}, function () {
		$(this).removeClass("hover");
		$('.open',this).removeClass("open");
		$('ul:first',this).css('visibility', 'hidden');
	});
		
 	if ($.browser.msie && parseInt($.browser.version) <= 6) {
		$("#topmenu li.lvl1").hoverClass("lvl1-hover")
		$("#topmenu li.lvl1-parent").hoverClass("lvl1-parent-hover")
	}
	
	$("div.box-sonda").each(function () {
		var box = $(this)
		
		if ($.cookie(box.attr("id"))) {
			box.find("div.poll-form").hide()
			box.find("div.poll-results").show()
		}
		
		box.find("form").submit(function () {
			$(this).find(".loader").show()
			$.cookie(box.attr("id"), 1, { expires: 30, path: '/' });
			box.find("div.poll").load($(this).attr("action"), $(this).serialize())
			return false
		})
	})
	
	$("#comments a.cite").click(function () {
		var f = $("form#add-comment textarea[name=text]")
		f.val(f.val() + $(this).parent().find("span.text").text())
		return false
	})
	
	$("div.show-random-post").each(function () {
		var items = $(this).find("div.show")
		var r = Math.floor(Math.random()*items.length)
		items.eq(r).show()
	})
	
	$("div.slideshow").slideshow({
		elems: "> div.slide",
		speed: 1000,
		interval: 3500,
		prev: "a.prev",
		next: "a.next"
	});

	$("#video-box").slideshow({
		elems: "> div.slide",
		speed: 1000,
		interval: 3500,
		nums: "ul.nums"
		// prev: "a.prev",
		// next: "a.next"
	});

	$(".box-tabs ul.menu").each(function () {
		var anchors = $(this).find("> li a")
		anchors.not(".current").map(function () { $($(this).attr("href")).hide() })
		anchors.click(function () {
			anchors.map(function () {
				$($(this).attr("href")).hide()
				$(this).removeClass("current")
			})
			$($(this).attr("href")).show()
			$(this).addClass("current")
			return false
		})
	})
	
	$("#search-form input.submit, #main form.search div.btn input").click(function () {
		$(this.form).submit()
		return false
	})

	$("form#add-comment").submit(function () {
		var form = $(this)
		form.find("div.btn").append('<span class="loader"></span>')
		$.post(form.attr("action"), form.serialize(), function (html) {
			form.find("ul.errors").remove()
			form.find("div.btn span.loader").remove()
			if ($(html).is("ul.errors")) {
				form.find("h3").after(html)
			} else if ($(html).is("div.comment")) {
				$("#comments").show().append(html)
				form.get(0).reset()
			}
		})
		return false
	})

	$("div.embed-video textarea").hide()
	$("div.embed-video textarea").focus(function () {
		this.select()
	})
	$("div.embed-video a").click(function () {
		$(this).parent().find("textarea").toggle("fast")
		return false
	})
});

(function($) {
	$.fn.hoverClass = function (className) {
		return this.each(function () {
			$(this).hover(
				function () { $(this).addClass(className) },
				function () { $(this).removeClass(className) }
			);
		})
	}
})(jQuery);

/*
 * Copyright (c) 2010, Jan Dudek
 * Licensed under MIT license.
 *
 * Usage example:
 *		$("#slides").slideshow({
 *			elems: "> .slide",
 *			nums: "> ul.n",
 *			speed: 1000,
 *			interval: 2500
 *		})
 */
(function($) {
	$.fn.slideshow = function (opt) {
		return this.each(function () {
			var interval = null
			var $this = $(this);
			var elems = $this.find(opt.elems);
			var nums = $this.find(opt.nums);

			if (elems.length < 2) return;
			elems.hide().css({ position: "absolute", top: 0, left: 0 }).addClass("slide").
				eq(0).show().addClass("current");
			nums.find("li:first a").addClass("current");

			function switchTo(elem) {
				elems.filter(".current").fadeOut(opt.speed);
				elems.removeClass("current")
				elem.addClass("current").fadeIn(opt.speed);
				nums.find("li a")
					.removeClass("current")
					.filter(function() {
						return $(this).attr("href") == "#" + elem.attr("id")
					})
					.addClass("current")
			}
			function next() {
				var e = elems.filter(".current");
				if (e.next().is(".slide"))
					switchTo(e.next());
				else
					switchTo(elems.filter(":first"));
			}
			function prev() {
				var e = elems.filter(":visible");
				if (e.prev().is(".slide"))
					switchTo(e.prev());
				else
					switchTo(elems.filter(":last"));
			}
			nums.find("li a").click(function() {
				if (interval) clearInterval(interval)
				switchTo($($(this).attr("href")))
				return false
			});
			if (opt.prev) {
				$this.find(opt.prev).show().click(function () {
					prev()
					if (interval) clearInterval(interval)
					return false
				})
			};
			if (opt.next) {
				$this.find(opt.next).show().click(function () {
					next()
					if (interval) clearInterval(interval)
					return false
				})
			};
			interval = setInterval(next, opt.interval)
		})
	}
})(jQuery);

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

