// Initialize.
$(document).ready(function() {		   
	initDropdown();
	initReadMore();
	initTreeExpander();
	initAccordion();
	initTabs();
	//initPanel();
});

function initPanel() {
	
	$('a[href^=mailto:]').addClass('mailtoLink');
	$('a[href^=http:]').addClass('externalLink');
	$('a[href$=.pdf]').addClass('pdfLink');
	$('a[href$=.doc]').addClass('docLink');
	
	$('table').each(function() {
		$(this).find('tr:last td').addClass('lastTr');
		$(this).find('tr').find('input:last').addClass('lastBtn');
		$(this).find('tr').find('input:first').addClass('firstBtn');
	});
	
	//$('table tr:last').addClass('red');
	$('.help').find('div:last').addClass('marginBottom30');
	$('div#filesList').hide();
	$('a#filesListBtn').bind('click', toggleFilesList); }
	
function toggleFilesList() {
	$('div#filesList').toggle(); }
/*
var message = 'Spoon!';
$('#foo').bind('click', {msg: message}, function(event) {
  alert(event.data.msg);
});
message = 'Not in the face!';
$('#bar').bind('click', {msg: message}, function(event) {
  alert(event.data.msg);
});
*/
function initReadMore() {
	//zapisac w zmiennej tekst aktualny. podzielic go na pokazujacy i ukrywajacy
	//np. aktualny powinien byc: Ukryj/Pokaz - opcje oddzielone znacznikiem rozpoznawanym przez skrypt
	$('a.readMore').each(function(i) {
		this.textStr = $(this).text();
		this.textArr = this.textStr.split('|');
		this.text1 = this.textArr[0];
		this.text2 = this.textArr[1];
		//alert('text1: ' + this.text1 + ' text2:' + this.text2);
		$(this).parent().next().hide();
		//$(this).bind('click', {text1: this.text1 }, toggleText);
		$(this).text(this.text1);
		$(this).bind('click', {text1: this.text1, text2: this.text2 }, function(event) {
  			//alert(event.data.text2);
			$(this).parent().next().toggle();
			if($(this).text() == event.data.text1) {
				$(this).text(event.data.text2);
			} else {
				$(this).text(event.data.text1); 
			}
			return false;
		});
	});
}
//-----------------------------------------------------------------------------------
function initDropdown() {
	
	// Does element exist?
	if (!$('ul.dropdown').length) {

		// If not, exit.
		return;
	}

	// Add listener for hover.
	$('ul.dropdown li.dropdownTrigger').hover(function() {

		// Show subsequent <ul>.
		$(this).find('ul').fadeIn(1);
	},
	function() {

		// Hide subsequent <ul>.
		$(this).find('ul').hide();
	});
}

//-------------------------------------------

function initTreeExpander() {

	//Jesli element listy nie ma a.treeTrigger, czyli nie powoduje rozwiniecia,
	//znaczy to, ze jest ostatecznym linkiem i ma miec inny wyglad
	$('.tree li').not($('.tree li').has('a.treeTrigger')).addClass('treeSlug');
	
	// Does element exist?
	if (!$('ul.tree').length) {
		// If not, exit.
		return; }

	// Expand and collapse buttons
	$('.treeControls a.expandAll, .treeControls a.collapseAll').click(function() {
		if ($(this).hasClass('expandAll')) {
				$(this).parent('p').next('ul').find('a.treeTrigger').addClass('treeTriggerExpanded').end().find('ul').addClass('treeExpanded');
				return false;
		} else {
				$(this).parent('p').next('ul').find('a.treeTrigger').removeClass('treeTriggerExpanded').end().find('ul').removeClass('treeExpanded'); }

		// Nofollow.
		this.blur();
		return false; });

	// Listen for tree clicks.
	$('ul.tree a.treeTrigger').live('click', function() {
		// Is the next <ul> hidden?
		if ($(this).next('ul').is(':hidden')) {
			$(this).addClass('treeTriggerExpanded').next('ul').addClass('treeExpanded');
		} else {
			$(this).removeClass('treeTriggerExpanded').next('ul').removeClass('treeExpanded'); }
		// Nofollow.
		this.blur();
		return false; });

	// Add class for last <li>.
	$('ul.tree li:last-child').addClass('last');

	// Change state of trigger.
	$('ul.treeExpanded').prev('a').addClass('treeTriggerExpanded'); }
//------------------------------------------------------------------------------------

function initTabs() {

	// Does element exist?
	if (!$('ul.tabs').length) {
		// If not, exit.
		return; }
		
	// Reveal initial content area(s).
	$('div.tabContentWrapper').each(function() {
		$(this).find('div.tabContent:first').show().siblings().hide(); 
	});

	// Listen for click on tabs.
	$('ul.tabs a').click(function() {

		// If not current tab.
		if (!$(this).hasClass('current')) {

			// Change the current indicator.
			$(this).addClass('current').parent('li').siblings('li').find('a.current').removeClass('current');

			// Show target, hide others.
			$($(this).attr('href')).show().siblings('div.tabContent').hide();
		}

		// Nofollow.
		this.blur();
		return false;
	});
}

//---------------------------------
// Initialize.
function initAccordion() {

	// Does element exist?
	if (!$('dl.accordion').length) {

		// If not, exit.
		return;
	}

	// Gather all accordions.
	$('dl.accordion').each(function() {

		// Reveal first accordion item.
		$(this).find('dt:first a').addClass('accordion_expanded').end().find('dd:first').show();
		$(this).find('dd').not('dt:first').hide();

		// Added to round corners via CSS.
		$(this).find('dt:last').addClass('last');
	});

	// Event listener for click.
	$('dl.accordion dt a').click(function() {

		// Get parent <dl>.
		var $dl = $(this).parents('dl:first');

		// Get the next <dd>.
		var $dd = $(this).parent('dt').next('dd');

		// Class final <dt>.
		function findLast() {
			if ($dl.find('dd:last').is(':hidden')) {
				$dl.find('dt:last').addClass('last');
			}
		}

		// Is it visible?
		if ($dd.is(':hidden')) {

			// Expand the <dd>, hide others.
			$dd.show().siblings('dd:visible').hide();

			// Change arrow state, remove class="last" from <dt>.
			$(this).addClass('accordion_expanded').parent('dt').removeClass('last').siblings('dt').find('a').removeClass('accordion_expanded');
		}

		// Nofollow.
		this.blur();
		return false;
	});
}

