$(document).ready(function() {
	setUpMenu();
	setUpTabbedMenu();
	addRightLogoPart();
	tabify();
	runNewsRotator();
	runInitiativesRotator();
	setUpZoomInOut();
	appendCommentsCount();
	sortableTable();
	initAudioPlayer();
	initNewsPhoto();
});

function clearInputBox(o, initValue) {
	if(o.value == initValue)
		o.value = '';
	return false;
}

function searchBox(o) {
	return clearInputBox(o, 'Szukaj w serwisie');
}

function setUpZoomInOut() {
	var zoomTo = function (i) {
		$('.left-column p, #news-column, #page-content td').css('font-size', i + 'px');
		return false;
	}
	$('#link-zoomin').click(function () {
		return zoomTo(parseInt($('#news-column').css('font-size')) + 2);
	});
	$('#link-zoomout').click(function () {
		return zoomTo(parseInt($('#news-column').css('font-size')) - 2);
	});
	$('#link-zoom1').click(function () {
		return zoomTo(10);
	});
	$('#link-zoom2').click(function () {
		return zoomTo(12);
	});
	$('#link-zoom3').click(function () {
		return zoomTo(14);
	});
}

function setUpMenu () {
	$('ul.menu > li').hover(
		function() {
			$(this).addClass('active').find('.submenu-container').show();
		},
		function() {
			$(this).removeClass('active').find('.submenu-container').hide();
		}
	);
	$('ul.menu li .submenu-container').hide();

	if (!$.browser.msie)
		$('.ie-hack').css('background-color', 'inherit');
};

function setUpTabbedMenu () {
	$('ul.tabbed-menu > li ul').hide();
	$('ul.tabbed-menu > li').click(function() {
		var $this = $(this);
		$this.closest('ul.tabbed-menu').find('li.tabbed-active').removeClass('tabbed-active').find('ul').hide();
		$this.addClass('tabbed-active').find('ul').show();
	});
	$('ul.tabbed-menu > li.tabbed-active').click();
};

function logout()
{
	$.ajax({
		url: "/inc/ajax/logout.php",
		type: 'GET',
		dataType: 'html',
		success : function()
		{
			document.location = "/serwis/typowanie";
		}
	});
}

function otworz(url, width, height)
{
	var Win = window.open(url,"displayWindow","width=" + width + ",height=" + height + ",resizable=0,scrollbars=no,menubar=no");
}

function dodajkomentarz(id)
{
	otworz('/inc/ajax/komentarz.php?id=' + id, 395, 335);
}

function d(id) {
	return document.getElementById(id);
}

function runNewsRotator() {
	if ($('.news-presenter').length > 0) {
		runGenericRotator('.news-presenter .news', function(i) {
			$('.news-presenter').removeClass('active')
				.eq(i).addClass('active');
		}, '.news-presenter');
	}
};

function runInitiativesRotator() {
	runGenericRotator('#initiatives .rotator-header li', function(i) {
		var $currActive = $('#initiatives .rotator-header li').removeClass('active')
			.eq(i).addClass('active');
		$('#initiatives .rotator-content').hide();
		$($currActive.find('a').attr('href')).show();
	});
	$('#initiatives .rotator-header li a').click(function () { return false; });
}

function runGenericRotator(hoverSelector, onRotate, elementSelector) {
	if (elementSelector == undefined)
		elementSelector = hoverSelector;

	var rotateActive = -1;
	var doRotate = function () {
		onRotate(rotateActive);
	};
	var rotate = function () {
		rotateActive = (rotateActive + 1) % $(elementSelector).length;
		doRotate();
	};

	rotate();

	var anim = window.setInterval(rotate, 5000);

	$(hoverSelector).mouseover(function () {
		window.clearInterval(anim);
		rotateActive = $(this).prevAll(hoverSelector).length;
		doRotate();
	}).mouseout(function () {
		anim = window.setInterval(rotate, 5000);
	});
};

function tabify() {
	$('.tab-content h2').remove();
	$('.tab-content').hide();

	$('ul.tabs li a').click(function () {
		var $this = $(this);

		if ($this.attr('href').charAt(0) != '#')
			return true;

		var $prevActiveLi = $this.closest('ul.tabs').find('li.active').removeClass('active');
		$($prevActiveLi.find('a').attr('href')).hide();
		$prevActiveLi.prev('li').removeClass('last');
		var $newActiveLi = $this.closest('li').addClass('active').prev('li').addClass('last');
		$($this.attr('href')).show();
		this.blur();
		return false;
	});

	$('.tabs .active a').click();
}

function appendCommentsCount() {
	if ($('#comments-form').length > 0) {
		var commentsCount = parseInt($.trim($('#comments-form h3').html().split(':')[1]));
		$('#link-comments a').append(' <span>(' + commentsCount + ')</span>');
	}
}

function sortableTable() {
    $.tablesorter.addParser({
        id: 'commas',
        is: function(s) { return false; },
        format: function(s) { return s.replace(',', '.').replace('NS', '0'); },
        type: 'numeric'
    });

	$table = $('#stats .sortable').tablesorter({
		sortList: [[7,1]],
		sortInitialOrder: 'desc',
		headers: {
			0: { sorter: false },
			7: { sorter: 'commas' },
			8: { sorter: 'commas' },
			9: { sorter: 'commas' },
			10: { sorter: 'commas' }
		}
	});
}

function initAudioPlayer() {
	AudioPlayer.setup("/inc/3rdparty/audio-player/player.swf", {
		width: 82,
		animation: 'no',
		noinfo: 'yes'
	});
}

function embedPlayer(idx, url) {
	AudioPlayer.embed("audioplayer_" + idx, {soundFile: url});
}

function addRightLogoPart() {
	if (($.browser.msie && parseInt($.browser.version, 10) < 8) || $(window).width() < 1000)
		return;

	$('#main-part .sizing-container')
		.css({ position: "relative", "padding-right" : "20px" })
		.append('<div id="skyscraper-container"></div>');
	$('ul.menu li .submenu-container').css({ left: 0 });

	if (!$.browser.safari)
		$('#main-part').css({overflow : "hidden"});
}

function initNewsPhoto() {
	$imgContainer = $('.img-container');
	$('.img-container a').lightBox({
		imageLoading: '/inc/3rdparty/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: '/inc/3rdparty/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: '/inc/3rdparty/lightbox//lightbox-btn-prev.gif',
		imageBtnNext: '/inc/3rdparty/lightbox/lightbox-btn-next.gif',
		txtImage: 'Zdjęcie',
		txtOf: 'z',
		imageBlank: '/inc/3rdparty/lightbox/lightbox-blank.gif'
	});
	$imgContainer.mouseenter(function () {
		$imgContainer.css({'position': 'relative'}).append('<div id="zoom-button">powiększ</div>');
	}).mouseleave(function () {
		$imgContainer.css({'position': 'inherit'})
		$('#zoom-button').remove();
	});
}

