$(document).ready(function(){	//ODKL.init();
/* rows color interchange */
//   $(".table tr, .concerts tr").filter(":odd").addClass(" odd");
//   $(".table tr, .concerts tr").filter(":even").addClass(" even");
/* /rows color interchange */
parseTable();
/* last-child emulation for all */
  $("div.events li:last-child").addClass(" last-child");
/* /last-child emulation for all */

/* -------------------------- Переключатель табов ---------------- */
var tabContainers = $('div.tabs-content > div.tc-item');
tabContainers.hide().filter(':first').show();

$('div.tab-menu ul a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.tab-menu ul li').removeClass('active');
		$($(this).parent().get(0)).addClass('active');
		return false;
}).filter(':first').click();
/* -------------------------- /Переключатель табов -------------- */
/* -------------------------- fancybox -------------------------- */
$("a.fancy_gal_photo").fancybox();
/* -------------------------- /fancybox ------------------------- */

/* -------------------------- galleria -------------------------- */
/*$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability

$('ul.gallery_demo').galleria({
  history   : true, // activates the history object for bookmarking, back-button etc.
  clickNext : true, // helper for making the image clickable
  insert    : '#main_image', // the containing selector for our main image
  onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

    // fade in the image & caption
    if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
      image.css('display','none').fadeIn(1000);
    }
    caption.css('display','none').fadeIn(1000);

    // fetch the thumbnail container
    var _li = thumb.parents('li');

    // fade out inactive thumbnail
    _li.siblings().children('img.selected').fadeTo(500,0.3);

    // fade in active thumbnail
    thumb.fadeTo('fast',1).addClass('selected');

    // add a title for the clickable image
    image.attr('title','Next image >>');
  },
  onThumb : function(thumb) { // thumbnail effects goes here

    // fetch the thumbnail container
    var _li = thumb.parents('li');

    // if thumbnail is active, fade all the way.
    var _fadeTo = _li.is('.active') ? '1' : '0.3';

    // fade in the thumbnail when finnished loading
    thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

    // hover effects
    thumb.hover(
      function() { thumb.fadeTo('fast',1); },
      function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
    )
  }
});*/
/* -------------------------- /galleria ------------------------- */

/* -------------------------- pagination ------------------------ */
//how much items per page to show
var show_per_page = 2;
//getting the amount of elements inside content div
var number_of_items = $('#pager_content').children().size();
//calculate the number of pages we are going to have
var number_of_pages = Math.ceil(number_of_items/show_per_page);

//set the value of our hidden input fields
$('#current_page').val(0);
$('#show_per_page').val(show_per_page);

//now when we got all we need for the navigation let's make it '

/*
what are we going to have in the navigation?
  - link to previous page
  - links to specific pages
  - link to next page
*/
var navigation_html = '<a class="previous_link" href="javascript:previous();"><img src="img/arrow-left.gif" alt="пред." title="пред." /></a>';
var current_link = 0;
while(number_of_pages > current_link){
  navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
  current_link++;
}
navigation_html += '<a class="next_link" href="javascript:next();"><img src="img/arrow-right.gif" alt="след." title="след." /></a>';

$('#page_navigation').html(navigation_html);

//add active_page class to the first page link
$('#page_navigation .page_link:first').addClass('active_page');

//hide all the elements inside content div
$('#pager_content').children().css('display', 'none');

//and show the first n (show_per_page) elements
$('#pager_content').children().slice(0, show_per_page).css('display', 'block');
/* -------------------------- /pagination ----------------------- */

});

function previous(){
  new_page = parseInt($('#current_page').val()) - 1;
  //if there is an item before the current active link run the function
  if($('.active_page').prev('.page_link').length==true){
    go_to_page(new_page);
  }
}

function next(){
  new_page = parseInt($('#current_page').val()) + 1;
  //if there is an item after the current active link run the function
  if($('.active_page').next('.page_link').length==true){
    go_to_page(new_page);
  }
}
function go_to_page(page_num){
  //get the number of items shown per page
  var show_per_page = parseInt($('#show_per_page').val());

  //get the element number where to start the slice from
  start_from = page_num * show_per_page;

  //get the element number where to end the slice
  end_on = start_from + show_per_page;

  //hide all children elements of content div, get specific items and show them
  $('#pager_content').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');

  /*get the page link that has longdesc attribute of the current page and add active_page class to it
  and remove that class from previously active page link*/
  $('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');

  //update the current page input field
  $('#current_page').val(page_num);
}

/* функция поиска и обработки таблиц */
function parseTable() {
// коллекция всех таблиц в документе
var tbls = document.getElementsByTagName("TABLE");
for (var i=0; i<tbls.length; i++) {
	if (tbls[i].className == 'table' || tbls[i].className == 'concerts') { //нас интересуют только таблицы с классом "table" и "concerts"
	// последовательнный перебор всех таблиц в коллекции
		for (var r=0; r<tbls[i].rows.length; r++) {
		// последовательнный перебор всех рядов в каждой таблице
			if (!(1&r)) {
			// для нечетных рядов таблицы присваиваем класс "odd"
				tbls[i].rows[r].className = 'even';
			}
			else{
			// для четных рядов - присваиваем класс "even"
				tbls[i].rows[r].className = 'odd';
				}
		}
	}
}
}
