function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function initMenu() {
	$('.menu ul ul').hide();
	var loc = window.location.href.split('/');
	loc = loc[loc.length - 1];

	//hack dla galerii
	if(window.location.href.indexOf('foto') != -1)
		loc = 'foto';

	//hack dla sklepu
	else if(window.location.href.indexOf('sklep') != -1)
		loc = '';

	//hack dla press
	else if(window.location.href.indexOf('press') != -1)
		loc = '';

	//hack dla audio
	else if(window.location.href.indexOf('audio') != -1)
		loc = '';

	//hack dla szukaja
	else if(window.location.href.indexOf('szukaj') != -1)
		loc = '';

	//jezyk
	if(window.location.href.indexOf('english') != -1)
		setCookie('lang', 'en', 30);

	if(loc.length > 0) {
		$('.menu ul ul li a').each(function() {
			var title = $(this).attr('title');
			title = title.replace(' ', '-');
			if(loc == title) {
				$(this).parent().parent().show();
				$(this).css('background-color', 'red');
			}
		});
	}
	else {
		//blog
        $('.current_page_item a').css('background-color', 'red');
        $('.current_page_item a').css('color', 'white');
	}

	/*$('.menu li a').hover(function() {
		$(this).css('background-color', 'red');
	}, function() {
		$(this).css('background-color', 'white');
	});*/
	$('.menu li a:parent:not([class=current_page_item])').hover(function() {

		/*if(loc == '' && ($(this).attr('title') == 'blog'
			|| $(this).attr('title') == 'sklep'
			|| $(this).attr('title') == 'szukaj'))
		return false;*/


		//wywalamy dzieci galerii foto
		$('.page-item-54 ul').remove();

		$(this).css('background-color', 'red');
		$(this).css('color', 'white');
	}, function() {
		if(loc == '' && ($(this).attr('title') == 'blog'
			|| $(this).attr('title') == 'sklep' || $(this).attr('title') == 'press' || $(this).attr('title') == 'audio'
			|| $(this).attr('title') == 'szukaj'))
		{
			if($(this).parent().hasClass('current_page_item') === false) {
				$(this).css('background-color', 'white');
				$(this).css('color', 'red');
				return false;
			}
			else {
				$(this).css('background-color', 'red');
				$(this).css('color', 'white');
				return false;
			}
		}

    	$(this).css('background-color', 'white');

		if(loc == '')
			$('.menu li a').not('[title=blog]').not('[title=sklep]').not('[title=szukaj]').not('[title=press]').not('[title=audio]').css('color', 'red');
		else
			$('.menu li a').css('color', 'red');

		$('.menu li .children a').css('color', 'black');

		$('.menu ul ul li a').each(function() {
                var title = $(this).attr('title');
				title = title.replace(' ', '-');
                if(loc == title) {
                        //$(this).parent().parent().show();
                        $(this).css('background-color', 'red');
                }

        });

	});

	$('.menu li a').click(function() {
		$('.menu li a').removeClass('selected');

		//wywalamy dzieci galerii foto
		$('.page-item-54 ul').remove();

		/*$('#menu li[class*=level1][class!=selected] > a').hover(function() {
			$(this).attr('oldcolor', $(this).css('color'));
			$(this).css('color', 'white');
		}, function() {
			$(this).css('color', $(this).attr('oldcolor'));
		});
		*/
		$('.menu li a').not('[title=blog]').not('[title=sklep]').not('[title=szukaj]').not('[title=press]').not('[title=audio]').css('color', 'red');
		$('.menu li .children a').css('color', 'black');

		/*
		$('#menu li a:not([class=selected])').hover(function() {
			$(this).css('background-color', 'red');
		}, function() {
			$(this).css('background-color', 'white');
		});*/

		$(this).addClass('selected');
		$(this).css('color', 'white');

		var checkElement = $(this).next();

		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			//return false;
			checkElement.slideUp('normal');
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('.menu ul ul:visible').slideUp('normal');
			checkElement.slideDown('normal');
			return false;
		}

		var title = $(this).attr('title');
		if(title == 'imprezy' || title == 'info' || title == 'galeria' || title == 'about')
			return false;
	});

	//podlinkowanie chmurki w poscie
	$('h2').click(function() {
		var link = $(this).children('a').attr('href');
		if(link) window.location.href = link;

	});

	//hover na chmure
	$('h2').hover(function() {
		var link = $(this).children('a').attr('href');
		if(link) $(this).css('cursor', 'pointer');
	});

	//--------------------------------------------------------------------------
	//podświetlienie kategorii aktualnej w poscie
	//jesteśmy w podglądzie posta
	if($('.single-post').length > 0) {
		if($('.single-post .category-news').length > 0)
			$('#chmurki #category-news img').css('opacity', '1');

		if($('.single-post .category-technosoul').length > 0)
			$('#chmurki #category-technosoul img').css('opacity', '1');

		if($('.single-post .category-eloktronika').length > 0)
			$('#chmurki #category-eloktronika img').css('opacity', '1');

		if($('.single-post .category-hiphop').length > 0)
			$('#chmurki #category-hiphop img').css('opacity', '1');

		if($('.single-post .category-artlifestyle').length > 0)
			$('#chmurki #category-artlifestyle img').css('opacity', '1');

		if($('.single-post .category-technologia').length > 0)
			$('#chmurki #category-technologia img').css('opacity', '1');
	}
}

/* wylaczenie entera */
function stopRKey(evt) {
	var evt = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}
document.onkeypress = stopRKey;

$(document).ready(function() {
	$(document).keyup(function(e) {
		if(e.keyCode == 27) {
			$('#fullscreen_background').remove();
			$('body').css('overflow-y', 'scroll');
		}
	});

	var el = $('.post')[0];
	if(el) {
		if($(el).is('.category-news')) {
			$('#chmurki #category-news img').css('opacity', '1');
			$('#chmurki #category-technosoul img').css('opacity', '0.25');
			$('#chmurki #category-eloktronika img').css('opacity', '0.25');
			$('#chmurki #category-hiphop img').css('opacity', '0.25');
			$('#chmurki #category-artlifestyle img').css('opacity', '0.25');
			$('#chmurki #category-technologia img').css('opacity', '0.25');
		}

		else if($(el).is('.category-technosoul')) {
			$('#chmurki #category-news img').css('opacity', '0.25');
			$('#chmurki #category-technosoul img').css('opacity', '1');
			$('#chmurki #category-eloktronika img').css('opacity', '0.25');
			$('#chmurki #category-hiphop img').css('opacity', '0.25');
			$('#chmurki #category-artlifestyle img').css('opacity', '0.25');
			$('#chmurki #category-technologia img').css('opacity', '0.25');
		}

		else if($(el).is('.category-eloktronika')) {
			$('#chmurki #category-news img').css('opacity', '0.25');
			$('#chmurki #category-technosoul img').css('opacity', '0.25');
			$('#chmurki #category-eloktronika img').css('opacity', '1');
			$('#chmurki #category-hiphop img').css('opacity', '0.25');
			$('#chmurki #category-artlifestyle img').css('opacity', '0.25');
			$('#chmurki #category-technologia img').css('opacity', '0.25');
		}

		else if($(el).is('.category-hiphop')) {
			$('#chmurki #category-news img').css('opacity', '0.25');
			$('#chmurki #category-technosoul img').css('opacity', '0.25');
			$('#chmurki #category-eloktronika img').css('opacity', '0.25');
			$('#chmurki #category-hiphop img').css('opacity', '1');
			$('#chmurki #category-artlifestyle img').css('opacity', '0.25');
			$('#chmurki #category-technologia img').css('opacity', '0.25');
		}

		else if($(el).is('.category-artlifestyle')) {
			$('#chmurki #category-news img').css('opacity', '0.25');
			$('#chmurki #category-technosoul img').css('opacity', '0.25');
			$('#chmurki #category-eloktronika img').css('opacity', '0.25');
			$('#chmurki #category-hiphop img').css('opacity', '0.25');
			$('#chmurki #category-artlifestyle img').css('opacity', '1');
			$('#chmurki #category-technologia img').css('opacity', '0.25');
		}

		else if($(el).is('.category-technologia')) {
			$('#chmurki #category-news img').css('opacity', '0.25');
			$('#chmurki #category-technosoul img').css('opacity', '0.25');
			$('#chmurki #category-eloktronika img').css('opacity', '0.25');
			$('#chmurki #category-hiphop img').css('opacity', '0.25');
			$('#chmurki #category-artlifestyle img').css('opacity', '0.25');
			$('#chmurki #category-technologia img').css('opacity', '1');
		}
	}

	if($('body').hasClass('single-post'))
		window.scroll(0,0);

	$(document).bind('scroll', function(event, delta) {
		$('.post').each(function(index, el) {
			var kategoria = '';
			//var mouse = event.layerY;
			var mouse = $(document).scrollTop() + 50;
			var height = $(el).position().top + $(el).height();
			//console.log("mouswheel: " + mouse);

			if(mouse > height) return true;

			if($(el).is('.category-news') && mouse < height && mouse > $(el).position().top) {
				$('#chmurki #category-news img').css('opacity', '1');
				$('#chmurki #category-technosoul img').css('opacity', '0.25');
				$('#chmurki #category-eloktronika img').css('opacity', '0.25');
				$('#chmurki #category-hiphop img').css('opacity', '0.25');
				$('#chmurki #category-artlifestyle img').css('opacity', '0.25');
				$('#chmurki #category-technologia img').css('opacity', '0.25');
				return false;
			}

			else if($(el).is('.category-technosoul')) {
				$('#chmurki #category-news img').css('opacity', '0.25');
				$('#chmurki #category-technosoul img').css('opacity', '1');
				$('#chmurki #category-eloktronika img').css('opacity', '0.25');
				$('#chmurki #category-hiphop img').css('opacity', '0.25');
				$('#chmurki #category-artlifestyle img').css('opacity', '0.25');
				$('#chmurki #category-technologia img').css('opacity', '0.25');
				return false;
			}

			else if($(el).is('.category-eloktronika')) {
				$('#chmurki #category-news img').css('opacity', '0.25');
				$('#chmurki #category-technosoul img').css('opacity', '0.25');
				$('#chmurki #category-eloktronika img').css('opacity', '1');
				$('#chmurki #category-hiphop img').css('opacity', '0.25');
				$('#chmurki #category-artlifestyle img').css('opacity', '0.25');
				$('#chmurki #category-technologia img').css('opacity', '0.25');
				return false;
			}

			else if($(el).is('.category-hiphop')) {
				$('#chmurki #category-news img').css('opacity', '0.25');
				$('#chmurki #category-technosoul img').css('opacity', '0.25');
				$('#chmurki #category-eloktronika img').css('opacity', '0.25');
				$('#chmurki #category-hiphop img').css('opacity', '1');
				$('#chmurki #category-artlifestyle img').css('opacity', '0.25');
				$('#chmurki #category-technologia img').css('opacity', '0.25');
				return false;
			}

			else if($(el).is('.category-artlifestyle')) {
				$('#chmurki #category-news img').css('opacity', '0.25');
				$('#chmurki #category-technosoul img').css('opacity', '0.25');
				$('#chmurki #category-eloktronika img').css('opacity', '0.25');
				$('#chmurki #category-hiphop img').css('opacity', '0.25');
				$('#chmurki #category-artlifestyle img').css('opacity', '1');
				$('#chmurki #category-technologia img').css('opacity', '0.25');
				return false;
			}

			else if($(el).is('.category-technologia')) {
				$('#chmurki #category-news img').css('opacity', '0.25');
				$('#chmurki #category-technosoul img').css('opacity', '0.25');
				$('#chmurki #category-eloktronika img').css('opacity', '0.25');
				$('#chmurki #category-hiphop img').css('opacity', '0.25');
				$('#chmurki #category-artlifestyle img').css('opacity', '0.25');
				$('#chmurki #category-technologia img').css('opacity', '1');
				return false;
			}
		});
	});

	/*$(document).bind('scroll', function(event) {
		console.log("scroll: " + $(document).scrollTop());
	});*/

	//wywalamy dzieci galerii foto
	$('.page-item-54 ul').remove();

	//przewijanie po kliku na obrazek
	$('.single_url_image').click(function(event) {
		var left_url = $(this).attr('back_nr');
		var right_url = $(this).attr('forward_url');
		var url = window.location.href;
		var lewo = (event.layerX < 300);
		var loc = window.location.href.split('/');

		loc = loc[loc.length - 1];
		url = url.replace(loc, '');

		if(lewo) {
			url += 'foto' + left_url;
		}
		else {
			url += 'foto' + right_url;
		}

		window.location.href = url;
		return false;
	});

	initMenu();

	//poprawiamy wp-navi
	$('.wp-pagenavi').appendTo('#container');
});

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById('video-player');
  console.log(ytplayer);
  ytplayer.addEventListener('onStateChange', 'onytplayerStateChange');
}

function onytplayerStateChange(newState) {
   console.log("Player's new state: " + newState);
}

