$(function () {
	$("#content table.channels td.channel").tooltip({
		body: function () {
			return $(this).find("img.logo").outerHTML() +
				'<div class="text">\
					<h2>' + $(this).find("span.name").text() + '</h2>\
					' + $(this).find("div.text").html() + '\
				</div>'
		},
		position: function () {
			$("#tooltip").css({
				top: $(this).offset().top - $("#tooltip").height(),
				left: $(this).offset().left + $(this).width() - 20
			})
		}
	})
		
	$("#content table.packs td:has(div.logos img), \
	   #configurator div.box-tv li:has(div.logos img)").each(function () {
		if ($(this).find("img.logo").length > 30) {
			$(this).find("img.logo").each(function () {
				$(this).attr("width", ($(this).attr("width") * 0.6))
				$(this).attr("height", ($(this).attr("height") * 0.6))
			})
		}
	})
	$("#content table.packs td:has(div.logos img)").tooltip({
		body: function () {
			return $(this).find("div.logos").outerHTML()
		},
		position: function () {
			$("#tooltip").css({
				width: "auto",
				maxWidth: 400
			})
			$("#tooltip").css({
				top: $(this).offset().top - $("#tooltip").height(),
				left: $(this).offset().left + $(this).width() - 120
			})
		}
	})

	$("#configurator div.box-tv li:has(div.logos img)").tooltip({
		body: function () {
			return $(this).find("div.logos").outerHTML()
		},
		position: function () {
			$("#tooltip").css({
				width: "auto",
				maxWidth: 240
			})
			$("#tooltip").css({
				top: $(this).offset().top - $("#tooltip").height() - 25,
				left: $(this).offset().left + parseInt($(this).css("paddingLeft")) + 2
			})
		}
	})
});

$(function () {
	$("#content div.filters input:checkbox").click(function () {
		var trim = function (s) {
			return s.replace(/^\s+/, '').replace(/\s+$/, '')
		}
		var s = $(this).parent().text()
		$("#content table.channels tr")
			.filter(function () {
				return trim($(this).find("td.category").text()) == trim(s)
			})[$(this).is(":checked") ? "show" : "hide"]()
	})
});

/*! Copyright (c) 2006 Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*/
(function($) {
	// Returns whether or not a result set has results in it
	$.fn.outerHTML = function() {
		return $('<div>').append( this.eq(0).clone() ).html();
	};
})(jQuery);

$(function() {
	$("#slides").slideshow({
		elems: "> .slide",
		speed: 1500,
		interval: 4000,
	})
});

(function($) {
	$.fn.slideshow = function (opt) {
		return this.each(function () {
			var interval = null
			var $this = $(this);
			var elems = $this.find(opt.elems);

			if (elems.length < 2) return;
			elems.hide().css({ position: "absolute", top: 0, left: 0 }).addClass("slide")
			     .eq(0).show().addClass("current");

			function switchTo(elem) {
				elems.filter(".current").fadeOut(opt.speed);
				elems.removeClass("current")
				elem.addClass("current").fadeIn(opt.speed);
			}
			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"));
			}
			interval = setInterval(next, opt.interval)
		})
	}
})(jQuery);


(function($) {
	$.fn.tooltip = function (opts) {
		return this.each(function () {
			html = '\
				<div id="tooltip">\
					<div class="tooltip-n"><!-- --></div>\
					<div class="tooltip-s"><!-- --></div>\
					<div class="tooltip-w"><!-- --></div>\
					<div class="tooltip-e"><!-- --></div>\
					<div class="tooltip-nw"><!-- --></div>\
					<div class="tooltip-ne"><!-- --></div>\
					<div class="tooltip-sw"><!-- --></div>\
					<div class="tooltip-se"><!-- --></div>\
					<div class="content"></div><!-- .content -->\
				</div><!-- #tooltip -->\
			'

			$(this).hover(function () {
				$("#tooltip").remove()
				var content = opts.body.call(this)
				$(html).appendTo("body").find("div.content").append(content)
				opts.position.call(this)
			}, function () {
				$("#tooltip").addClass("remove")
				setTimeout(function () {
					$("#tooltip.remove").remove()
				}, 100)
			})
		})
	}
})(jQuery);
