﻿/**
 * Print email link (protection from spambots)
 * If javascript is disabled, nothing is visible
 *
 * @param string p1 Part 1 - this is username
 * @param string p2 Part 2 - this is domain and TLD
 * @param string name (optional) Display value
 */
function writemail(p1, p2, name)
{
	if (typeof(name) == 'undefined')
	{
			name = p1+'@'+p2;
	}
	document.write('<a href="mailto:'+p1+'@'+p2+'">'+name+'</a>');
}





/**
 * jQuery functions in public part
 */
// property for menu 
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

// property for gallery slider 
var totalSlides = 0;
var contentSlides = '';
var slideWidth = 600;
var slideSpeed = 300;
var searchField = [];
searchField["ru"] = 'Поиск на сайте';
searchField["kk"] = 'Поиск на сайте';
$(document).ready(function()
{	
	/*========================================
	HEADER FUNCTIONS */
	
	// open link list
	$('div#header div.navi li.main-li').hover(function() {
		$('div#header div.navi div.navi-list:visible').hide();
		$('div.navi-list', this).show();
		$('a.main-btn', this).addClass('active');
	},
	function() {
		$('div.navi-list', this).hide();
		$('a.main-btn', this).removeClass('active');
	});
	
	// change team list category
	$('div#header div.category-switch a').click(function(){
		$('div#header ul.team-list-1').toggle();
		$('div#header ul.team-list-2').toggle();
		$('div#header div.choice-1').toggle();
		$('div#header div.choice-2').toggle();
	});
	
	// search
	$('#header .searchbox #SearchKeyword').focus(function(){
		if ($(this).val() == searchField[lang])
		{
			$(this).val('');
		}
	});
	
	$('#header .searchbox #SearchKeyword').blur(function() {
		if ($(this).val() == '')
		{
			 $(this).val(searchField[lang]);
		}
	});
	/*========================================
	MAIN MENU FUNCTIONS */
	$('#main-menu > li').hover(function() {
		// check timer
		var slide;
		var ggg = false;

		
		if(closetimer)
		{  
			window.clearTimeout(closetimer);
			closetimer = null;
		}
		// hide previus submenu
		if(ddmenuitem) ddmenuitem.hide();
		$('#main-menu > li a.lvl0').removeClass('hover');
		
		// show current submenu
		ddmenuitem = $(this).find('ul').show();
		$('a.lvl0', this).addClass('hover');
		$("ul[id=level3]").hide();
			$("ul[id=level3]").prev().hover(function (){
				if(ggg)
					ggg.hide();
				ggg = $(this).next("ul[id=level3]");
				ggg.show();
			}, 
			function () {
				var slide = false;
				$(this).next("ul[id=level3]").hover(function () {
					slide = true;
				},
				function () {
					slide = false;
					$(this).hide();
				});
				setTimeout('if(!slide) $(this).next("ul[id=level3]").hide()', 500);
			});
	}, 

	function() {
		// set timeout for submenu
		closetimer = window.setTimeout(function(){
			if(ddmenuitem) ddmenuitem.hide();
			$('#main-menu > li a.lvl0').removeClass('hover');
		}, timeout);
	});

	$(document).click(function() {
		// hide submenu
		if(ddmenuitem) ddmenuitem.hide();
	});
	/*========================================
	SIDEBAR FUNCTIONS */
	// games tabs 
	$('div.sidebar div.games div.tabs a.last-btn').click(function(){
		$('div#game-last').show();
		$('div#game-next').hide();
		$(this).addClass('active');
		$('div.sidebar div.games div.tabs a.next-btn').removeClass('active');
		return false;
	});
	
	$('div.sidebar div.games div.tabs a.next-btn').click(function(){
		$('div#game-next').show();
		$('div#game-last').hide();
		$(this).addClass('active');
		$('div.sidebar div.games div.tabs a.last-btn').removeClass('active');
		return false;
	});
	
	// games -> next game 
	sidebarGameNext();
	
	$('div.sidebar h4 a').click(function(){
		
		$('div.sidebar-areas:visible').slideUp();
		
		$('div#' + $(this).attr('rel')).slideDown();
		
		$('div.sidebar h4 a:hidden').css('display', 'block');
		$('div.sidebar h4 span:visible').css('display', 'none');
		
		$('div#' + $(this).attr('rel')).prev().children('a').css('display', 'none');
		$('div#' + $(this).attr('rel')).prev().children('span').css('display', 'block');
		return false;
	});
	// games -> prev game 
	sidebarGameLast();
	
    sidebarSnowCalendar();
    
	// calendar
	sidebarCalendar();
	
	// teams statistic
	sidebarTeamsStatistic();
	
	// players statistic
	sidebarPlayersStatistic();
	
	/* tag dropdown for news */
	$('div.sidebar .change-tag').change(function(){
		if($(this).val() != '')
		{
			document.location = webroot + lang + '/news/tag/' + $(this).val();
		}
	});
	createDropDown();	
	
	/*========================================
	START PAGE FUNCTIONS */
	// news
	$('div#home div.news h2 a').click(function(){	
		$('div#home div.news div.inner:visible').slideUp();
		$('div#' + $(this).attr('rel') + ' div.inner').slideDown();
		
		$('div#home div.news h2 a:hidden').css('display', 'block');
		$('div#home div.news h2 span:visible').css('display', 'none');
		
		var xxx = $('div#' + $(this).attr('rel') + ' h2 a').css('display', 'none');
		var yyy = $('div#' + $(this).attr('rel') + ' h2 span').css('display', 'block');
		
		return false;
	});
	
	// multimedia tabs
	$('div#home div.multimedia div.tabs a.gallery-btn').click(function(){
		$('div.multimedia div.video').hide();
		$('div.multimedia div.gallery').show();
		$('div.multimedia diva.tabs a.video-btn').removeClass('active');
		$(this).addClass('active');
		return false;
	})
	$('div#home div.multimedia div.tabs a.video-btn').click(function(){
		$('div.multimedia div.gallery').hide();
		$('div.multimedia div.video').show();
		$('div.multimedia div.tabs a.gallery-btn').removeClass('active');
		$(this).addClass('active');
		return false;
	})
	
	/*========================================
	INNER PAGE FUNCTIONS */
	// show game info in calendar
	$('div#schedules div.entry').hover(
	  function () {
		$(this).children('div.tooltip').show();
	  },
	  function () {
		$(this).children('div.tooltip').hide();
	  }
	);
	
	// game review and statistic tabs
	$('div#schedule div.tabs a').click(function(){
		$('div#schedule div.inner div.list:visible').hide();
		$('div#schedule div.inner div.' + $(this).attr('rel')).show();
		$('div#schedule div.tabs a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
	
	// show player small img in player list
	$('a.player-info').hover(
	  function () {
		$(this).next('div.player-tooltip').show();
	  },
	  function () {
		$(this).next('div.player-tooltip').hide();
	  }
	);
	
	// gallery slider
	if($('div.slideshow-area').length > 0)
	{
		var totalWidth = 0;
		var scrollAmount = 0;
		contentSlides = $('.slideshow-area .slideshow-content');
		
		// count totalSlides
		contentSlides.each(function(i){	
			totalSlides++;
		});
		
		// calculate total width and scrool amout
		totalWidth = totalSlides * slideWidth;
		scrollAmount = (currentSlide - 1) * slideWidth;
		
		// set to slider
		$('.slideshow-area .slider').width(totalWidth);
		$('.slideshow-area .slideshow').attr({scrollLeft: scrollAmount});
		
		// set next and prev button events
		$('.slideshow-area .prev').click(showPreviousSlide);
		$('.slideshow-area .next').click(showNextSlide);
		
		// fancybox
		$('a.zoom').fancybox({
			'padding'		: 0,
			'overlayColor'	:	'#08223f',
			'overlayOpacity':	0.8
		});
	}
	
	// game statistic tabs
	$('#team-statistic div.tabs a').click(function(){
		$('#team-statistic div.inner div.list:visible').hide();
		$('#team-statistic div.inner div.' + $(this).attr('rel')).show();
		$('#team-statistic div.tabs a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
	
});


/*========================================
SIDEBAR FUNCTIONS */

// Next game
function sidebarGameNext()
{
	if($('span.countdown').length > 0) {
		$('span.countdown').countdown({
			until: time,
			labels:['л.', 'м.', 'н.', 'д.', 'ч.', 'мин.', 'сеk.'],
			labels1:['л.', 'м.', 'н.', 'д.', 'ч.', 'мин.', 'сеk.']
		});
	};
	// next games
	$('div.sidebar div.games a.next-next').click(function(){
		$('div#game-next').html('<div class="loading"><img src="/img/game-loading-blue.gif" alt="loading" /></div>');
		id = $(this).attr('rel');
		$.get(
			webroot + lang + '/schedules/next_game/' + id , 
			function(data) {
			if(data != 'NOK'){
				$('div#game-next').html(data);
				sidebarGameNext();
			}
		});
		return false;
	});
	
	// prev games
	$('div.sidebar div.games a.next-prev').click(function(){
		$('div#game-next').html('<div class="loading"><img src="/img/game-loading-blue.gif" alt="loading" /></div>');
		id = $(this).attr('rel');
		$.get(
			webroot + lang + '/schedules/next_game/' + id , 
			function(data) {
			if(data != 'NOK'){
				$('div#game-next').html(data);
				sidebarGameNext();
			}
		});
		return false;
	});
}

// Last game
function sidebarGameLast()
{	
	// next games
	$('div.sidebar .games a.last-next').click(function(){
		$('div#game-last').html('<div class="loading"><img src="/img/game-loading-blue.gif" alt="loading" /></div>');
		id = $(this).attr('rel');
		$.get(
			webroot + lang + '/schedules/last_game/' + id , 
			function(data) {
			if(data != 'NOK'){
				$('div#game-last').html(data);
				sidebarGameLast();
			}
		});
		return false;
	});
	
	// prev games
	$('div.sidebar div.games a.last-prev').click(function(){
		$('div#game-last').html('<div class="loading"><img src="/img/game-loading-blue.gif" alt="loading" /></div>');
		id = $(this).attr('rel');
		$.get(
			webroot + lang + '/schedules/last_game/' + id , 
			function(data) {
			if(data != 'NOK'){
				$('div#game-last').html(data);
				sidebarGameLast();
			}
		});
		return false;
	});
}




function sidebarSnowCalendar()
{
	$('div.sidebar div#snow-calendar-area div.month-switch a.next').click(function(){
		// display loading
		var loading = $('<div class="loading"><img src="/img/sidebar-loading-blue.gif" alt="loading" /></div>');
		height = $('div#snow-calendar-area div.inner').height();
		padding = parseInt( (height / 2) - 15);
		$(loading).css('height', (height - padding));
		$(loading).css('padding-top', padding);
		$('div#snow-calendar-area div.inner').html(loading);
		
		// load new calendar month
		date = $(this).attr('rel');
		$.get(
			webroot + lang + '/schedules/calendar/' + date , 
			function(data) {
			if(data != 'NOK'){
				$('div#snow-calendar-area').html('');
				$('div#snow-calendar-area').append(data);
				sidebarSnowCalendar();
			}
		});
		return false;
	});
	
	$('div.sidebar div#snow-calendar-area div.month-switch a.prev').click(function(){
		// display loading
		var loading = $('<div class="loading"><img src="/img/sidebar-loading-blue.gif" alt="loading" /></div>');
		height = $('div#snow-calendar-area div.inner').height();
		padding = parseInt( (height / 2) - 15);
		$(loading).css('height', (height - padding));
		$(loading).css('padding-top', padding);
		$('div#snow-calendar-area div.inner').html(loading);
		
		// load new calendar month
		date = $(this).attr('rel');
		$.get(
			webroot + lang + '/schedules/calendar/' + date , 
			function(data) {
			if(data != 'NOK'){
				$('div#snow-calendar-area').html(data);
				sidebarSnowCalendar();
			}
		});
		return false;
	});
}




// Calendar
function sidebarCalendar()
{
	$('div.sidebar div#calendar-area div.month-switch a.next').click(function(){
		// display loading
		var loading = $('<div class="loading"><img src="/img/sidebar-loading-blue.gif" alt="loading" /></div>');
		height = $('div#calendar-area div.inner').height();
		padding = parseInt( (height / 2) - 15);
		$(loading).css('height', (height - padding));
		$(loading).css('padding-top', padding);
		$('div#calendar-area div.inner').html(loading);
		
		// load new calendar month
		date = $(this).attr('rel');
		$.get(
			webroot + lang + '/schedules/calendar/' + date , 
			function(data) {
			if(data != 'NOK'){
				$('div#calendar-area').html('');
				$('div#calendar-area').append(data);
				sidebarCalendar();
			}
		});
		return false;
	});
	
	$('div.sidebar div#calendar-area div.month-switch a.prev').click(function(){
		// display loading
		var loading = $('<div class="loading"><img src="/img/sidebar-loading-blue.gif" alt="loading" /></div>');
		height = $('div#calendar-area div.inner').height();
		padding = parseInt( (height / 2) - 15);
		$(loading).css('height', (height - padding));
		$(loading).css('padding-top', padding);
		$('div#calendar-area div.inner').html(loading);
		
		// load new calendar month
		date = $(this).attr('rel');
		$.get(
			webroot + lang + '/schedules/calendar/' + date , 
			function(data) {
			if(data != 'NOK'){
				$('div#calendar-area').html(data);
				sidebarCalendar();
			}
		});
		return false;
	});
}

// Teams Satistic
function sidebarTeamsStatistic()
{
	$('div.sidebar div#statistic-teams-area div.season-switch a.next').click(function(){
		// check rel value
		id = $(this).attr('rel');
		if(id == '')
		{
			return false;
		}
		// display loading
		var loading = $('<div class="loading"><img src="/img/sidebar-loading-blue.gif" alt="loading" /></div>');
		height = $('div#statistic-teams-area div.inner').height();
		padding = parseInt( (height / 2) - 15);
		$(loading).css('height', (height - padding));
		$(loading).css('padding-top', padding);
		$('div#statistic-teams-area div.inner').html(loading);
		
		// load new teams statistic season
		$.get(
			webroot + lang + '/statistics/sidebar_teams/' + id , 
			function(data) {
			if(data != 'NOK'){
				$('div#statistic-teams-area').html(data);
				sidebarTeamsStatistic();
			}
		});
		return false;
	});
	
	$('div.sidebar div#statistic-teams-area div.season-switch a.prev').click(function(){
		// check rel value
		id = $(this).attr('rel');
		if(id == '')
		{
			return false;
		}
		// display loading
		var loading = $('<div class="loading"><img src="/img/sidebar-loading-blue.gif" alt="loading" /></div>');
		height = $('div#statistic-teams-area div.inner').height();
		padding = parseInt( (height / 2) - 15);
		$(loading).css('height', (height - padding));
		$(loading).css('padding-top', padding);
		$('div#statistic-teams-area div.inner').html(loading);
		
		// load new teams statistic season
		$.get(
			webroot + lang + '/statistics/sidebar_teams/' + id , 
			function(data) {
			if(data != 'NOK'){
				$('div#statistic-teams-area').html(data);
				sidebarTeamsStatistic();
			}
		});
		return false;
	});
}

// Players Satistic
function sidebarPlayersStatistic()
{
	$('div.sidebar div#statistic-players-area div.season-switch a.next').click(function(){
		// check rel value
		id = $(this).attr('rel');
		if(id == '')
		{
			return false;
		}
		// display loading
		var loading = $('<div class="loading"><img src="/img/sidebar-loading-blue.gif" alt="loading" /></div>');
		height = $('div#statistic-players-area div.inner').height();
		padding = parseInt( (height / 2) - 15);
		$(loading).css('height', (height - padding));
		$(loading).css('padding-top', padding);
		$('div#statistic-players-area div.inner').html(loading);
		
		// load new players statistic season
		$.get(
			webroot + lang + '/statistics/sidebar_players/' + id , 
			function(data) {
			if(data != 'NOK'){
				$('div#statistic-players-area').html(data);
				sidebarPlayersStatistic();
			}
		});
		return false;
	});
	
	$('div.sidebar div.statistic-players div.season-switch a.prev').click(function(){
		// check rel value
		id = $(this).attr('rel');
		if(id == '')
		{
			return false;
		}
		// display loading
		var loading = $('<div class="loading"><img src="/img/sidebar-loading-blue.gif" alt="loading" /></div>');
		height = $('div#statistic-players-area div.inner').height();
		padding = parseInt( (height / 2) - 15);
		$(loading).css('height', (height - padding));
		$(loading).css('padding-top', padding);
		$('div#statistic-players-area div.inner').html(loading);
		
		// load new players statistic season
		$.get(
			webroot + lang + '/statistics/sidebar_players/' + id , 
			function(data) {
			if(data != 'NOK'){
				$('div#statistic-players-area').html(data);
				sidebarPlayersStatistic();
			}
		});
		return false;
	});
}

// Tag custom dropdown
function createDropDown(){
	var source = $('.change-tag');
    var selected = source.find('option[selected]');  // get selected <option>
    var options = $('option', source);  // get all <option> elements
    
	// create <dl> and <dt> with selected value inside it
    $('.sidebar .tags').append('<dl id="target" class="dropdown"></dl>')
    $('#target').append('<dt><a href="#">' + selected.text() + 
        '<span class="value">' + selected.val() + 
        '</span></a></dt>')
    $('#target').append('<dd><ul></ul></dd>')
	
    // iterate through all the <option> elements and create UL
    options.each(function(){
        $('#target dd ul').append('<li><a href="#">' + 
            $(this).text() + '<span class="value">' + 
            $(this).val() + '</span></a></li>');
    });
	
	source.hide();
	
	$('.dropdown dt a').click(function() {
        $('.dropdown dd ul').toggle();
		return false;
    });

    $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (! $clicked.parents().hasClass('dropdown'))
            $('.dropdown dd ul').hide();
	});
                        
    $('.dropdown dd ul li a').click(function() {
        var text = $(this).html();
        $('.dropdown dt a').html(text);
        $('.dropdown dd ul').hide();
		
		document.location = webroot + lang + '/news/tag/' + $(this).find('span.value').html();
	});
}
/*========================================
END SIDEBAR FUNCTIONS */

/*========================================
MAIN FUNCTIONS */

/* Functions for image gallery slider */
function showPreviousSlide()
{
	// set loading step
	if(countImage < 7)
	{
		step = Math.floor((countImage - 1)/2) + 1;
	}
	else
	{
		step = 4;
	}
	
	// set image number to load
	if((currentImage - step) < 1)
	{
		imgNr =(currentImage - step) + countImage;
	}
	else
	{
		imgNr = currentImage - 4;
	}
	
	// if image not loaded, then load
	if(arrShows[imgNr] == 0)
	{
		totalSlides++;
		currentSlide++;
		
		totalWidth = (totalSlides) * slideWidth;
		scrollAmount = (currentSlide - 1) * slideWidth;
		
		$('.slider ul').prepend('<li class="slideshow-content"><img src="'+arrImages[imgNr][0]+'" alt="'+arrImages[imgNr][2]+'" alt="'+arrImages[imgNr][2]+'"/></li>');
		
		$('.slideshow-area .slider').width(totalWidth);
		$('.slideshow-area .slideshow').attr({scrollLeft: scrollAmount});
		
		arrShows[imgNr] = 1;
	}
	
	// if this is first image then append the last
	if(currentSlide == 1)
	{
		currentSlide++;
		$('.slider ul').prepend($('.slider ul > li').eq(-1));
		scrollAmount = (currentSlide - 1) * slideWidth;
		$('.slideshow-area .slideshow').attr({scrollLeft: scrollAmount});
	}
	
	// change currnet image number
	if(currentImage == 1)
	{
		currentImage = countImage;
	}
	else
	{
		currentImage--;
	}
	
	currentSlide--;
	
	updateContentHolder();
	updateZoom();
	
	return false;
}

function showNextSlide()
{
	// set loading step
	if(countImage < 7)
	{
		step = Math.floor((countImage - 1)/2) + 1;
	}
	else
	{
		step = 4;
	}
	
	// set image number to load
	if((currentSlide + step) > countImage)
	{
		imgNr = 1;
	}
	else
	{
		imgNr = currentImage + 4;
	}
	// if image not loaded, then load
	if(arrShows[imgNr] == 0)
	{
		totalSlides++;
		
		totalWidth = (totalSlides) * slideWidth;
		
		$('.slider ul').append('<li class="slideshow-content"><img src="'+arrImages[imgNr][0]+'" alt="'+arrImages[imgNr][2]+'" alt="'+arrImages[imgNr][2]+'"/></li>')
		arrShows[imgNr] = 1;
		
		$('.slideshow-area .slider').width(totalWidth);
	}
	
	// if this is last image then append the first
	if(currentSlide == countImage)
	{
		currentSlide--;
		$('.slider ul').append($('.slider ul > li').eq(0));
		scrollAmount = (currentSlide - 1) * slideWidth;
		$('.slideshow-area .slideshow').attr({scrollLeft: scrollAmount});
	}
	
	// change currnet image number
	if(currentImage == countImage)
	{
		currentImage = 1;
	}
	else
	{
		currentImage++;
	}
	
	currentSlide++;
	
	updateContentHolder();
	updateZoom();
	
	return false;
}

function updateContentHolder()
{
  scrollAmount = (currentSlide - 1) * slideWidth;
  
  //alert(scrollAmount);
  $('.slideshow-area .slideshow').animate({scrollLeft: scrollAmount}, slideSpeed);
}


function updateZoom()
{
	$('.slideshow-area .zoom').attr('href',arrImages[currentImage][1]);
}


/*========================================
END MAIN FUNCTIONS */
