function pokazStrone(numer){
	if(lang == 'pl')
	{
		if($('#showAll').html() != 'Pokaż całość &gt;&gt;')
		{
			$('#showAll').html('Pokaż całość &gt;&gt;');
		}
	}
	else
	{
		if($('#showAll').html() != 'Show all &gt;&gt;')
		{
			$('#showAll').html('Show all &gt;&gt;');
		}
	}

	numer = parseInt(numer);
	$.ajax({
		type: "POST",
		url: "ajax_newsy.php",
		data: "newsId="+newsId+"&strona="+numer,
		success: function(wynik){
			$('#cala_strona').children().first().html(wynik);
			$('#cala_strona').children().first().attr('id', 'tresc_'+numer);
			$('#tresc_'+numer).show();
			$('#calosc').hide();
	
			if (numer > 1)
			{
				$('.wstecz').attr('id','site_'+(numer-1));
			}
			else
			{
				$('.wstecz').attr('id','site_'+(numer));
			}
			
			if (numer < newsMaxStron)
			{
				$('.dalej').attr('id','site_'+(numer+1));
			}
			else
			{
				$('.dalej').attr('id','site_'+(numer));
			}
			
			for(i=0;i<=newsMaxStron;i++)
			{
				if(i != numer)
				{
					$('#page_'+i).html(i);
				}
				else
				{
					$('#page_'+i).html('<strong>'+i+'</strong>');
				}
			}
			
			return false;
	}
	});
}

function pokazCalosc()
{
	$('#cala_strona').children().first().hide();
	$('#calosc').show();
	
	return false;
}

function mycarousel_initCallback(carousel)
{
	jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

	
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        //carousel.startAuto(0);
    	carousel.next();
        return false;
    });

    carousel.buttonPrev.bind('click', function() {
        //carousel.startAuto(0);
    	carousel.prev();
        return false;
    });
    /*$('.jcarousel-item').mouseenter(function(){
    	$(this).children('p').show();
    });
    $('.jcarousel-item').mouseleave(function(){
    	$(this).children('p').hide();
    });*/

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


function mycarousel_initCallbackFilm(carousel)
{
	jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

	
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
    	//carousel.startAuto(0);
    	carousel.next();
        return false;
    });

    carousel.buttonPrev.bind('click', function() {
    	//carousel.startAuto(0);
    	carousel.prev();
        return false;
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


var licznikSponsorzy = 1;
var sponsorzyTime = 0;
var liczbaSponsorow = $('.sponsorzy_main').children().size();
var forgotten = 0;
function zmienSponsorzy(){
	var liczbaSponsorow = $('.sponsorzy_main').children().size();
	licznikSponsorzy++;
	if(licznikSponsorzy>liczbaSponsorow)
	{
		licznikSponsorzy = 1;
	}
	if(licznikSponsorzy > 1)
	{
		if($('#spon'+licznikSponsorzy).html() !== ''){
			if(forgotten > 0){
				$('#spon'+forgotten).removeClass('sponsorzy_bold');
				forgotten = 0;
			}
			$('#spon'+(licznikSponsorzy - 1)).removeClass('sponsorzy_bold');
		}else{
			if(forgotten === 0){
				forgotten = licznikSponsorzy - 1;
			}
		}
		$('#sponbox_'+(licznikSponsorzy - 1)).hide();
	}else
	{
		if(forgotten > 0){
			$('#spon'+forgotten).removeClass('sponsorzy_bold');
			forgotten = 0;
		}
		$('#spon'+liczbaSponsorow).removeClass('sponsorzy_bold');
		$('#sponbox_'+liczbaSponsorow).hide();
	}
	$('#spon'+licznikSponsorzy).addClass('sponsorzy_bold');
	$('#sponbox_'+licznikSponsorzy).css('display', 'inline-block');
}
function showHint(xPos, yPos, text){
	document.getElementById('hintText').style.display = '';
	document.getElementById('hintText').style.top = (yPos)+"px";
	document.getElementById('hintText').style.left = (xPos)+"px";
	document.getElementById('hintText').innerHTML = text;
}

function hideHintById(movieId){
	if(document.getElementById('hintText' + movieId)){
		document.getElementById('hintText' + movieId).style.display = 'none';
		document.getElementById('hintShadow' + movieId).style.display = 'none';
	}
}

var preloaderContent = '<div class="preloader"></div>';

function hideHint(id){
    if (id) {
		timeHint = setTimeout(function () {
            $('#' + id).hide();
		}, 200);
    }
}

function showMovieHint(referer, id, dieFast){
	if (window.timeHint !== undefined) {
		clearTimeout(timeHint);
	}
if (dieFast) {
    return;
}

size = document.body.clientWidth;

$(".movieHint[id!=mhint_"+id+"]").hide();
$('.personHint').hide();
$('.seansHint').hide();

var xy = $(referer).offset();
if( xy.left + 300 >= size) {
        $('#mhint_'+id+'').find('.dymek').css({'margin-left': '250px'}).attr('src', '../img/dymek2a.gif');
$('#mhint_'+id+'').css({top: xy.top, left: xy.left-300});
} else {
    $('#mhint_'+id).css({top: xy.top, left: xy.left});
}

$('#mhint_'+id).show();
if($('#mhint_'+id+'_c').text() == ''){
    $('#mhint_'+id+'_p').html(preloaderContent);
$.ajax(
{
method: 'get',
//url:'http://'+document.location.host +'/film/pobierzHint.php',
url:'http://'+document.location.host +'/pobierzHint.php',
data: { fi: id },
success: function(data){
$('#mhint_'+id+'_c').html(data);
$('#mhint_'+id+'_p').hide();
},
failure: function(){}
});
}
}

function setHintPositionById(num, movieId, hintPos) {
	var newMid = movieId;
	var xPos = 0;
	var yPos = 0;
	xPos = getPosX('hintReferer' + movieId);
	yPos = getPosY('hintReferer' + movieId);
	if(hintPos == 'blimp') {
		newMid = movieId.substr(0, movieId.length - 1);
		xPos = 100;
		if(!posY[movieId]) {
			var yPosMinus = getObj('phint_' + newMid) ? getPosY('phint_' + newMid) : getPosY('mhint_' + newMid);
			posY[movieId] = yPosMinus;
		}
		yPos -= posY[movieId];
	}
	var ocena = num;
	if(hintPos != 'off') {
		if((hintPos != 'blimp') || ((hintPos == 'blimp') && getPosY('mhint_'+newMid))) {
			ocena = getOcena(num)+" ("+num+")";
		}
	}
	if(hintPos == 'obok') {
		showHintById((xPos + 2 + 12 * starsSize), yPos, ocena, movieId);
	} else if(hintPos == 'obok2') {
		showHintById((xPos + 72 + 12 * starsSize), yPos, ocena, movieId);
	} else if(hintPos == 'rank') {
		showHintById((xPos + 90), (yPos + 24), ocena, movieId);
	} else {
		showHintById(xPos, (yPos + 24), ocena, movieId);
	}
}

function checkLang()
{
	zmienna = $('#jezyk').attr('src');
	if(zmienna == 'img/en_a.png')
	{
		return 'pl';
	}
	else
	{
		return 'en';
	}
}

function filmShowPhoto(a) {
	$('#filmGrafikaPowiekszenie').html('<img src="'+a.href+'" alt="" />');
	return false;
}



$(document).ready(function() {
	var timeHint = 0;

	$('.codeLink').bind('click', function() {
		$('#captchaImg').attr('src', 'narzedzia/captcha.php?d='+$(this).attr('href'));
		$(this).attr('href', Math.floor(Math.random()*200) + '.php');
		return false;
	});
	$('a').each(function() {
		var href = $(this).attr('href');
		if(typeof href != 'undefined')
		{
			var outer = false;
	//		console.log($(this).attr('rel'));
			if(($(this).attr('rel') == '') && (typeof $(this).attr('onclick') == 'undefined')) {
				hrefTmp = href.replace('http://', '');
				hrefTmp = hrefTmp.replace('https://', '');
				if(hrefTmp != href) {
					outer = true;
				} else {
					var ext = hrefTmp.substr(hrefTmp.lastIndexOf('.') + 1, 3);
					if((ext == 'doc') || (ext == 'pdf')) {
						outer = true;
					}
				}
			}
			if(outer) {
				$(this).bind('click', function() {
					window.open($(this).attr('href'), '');
					return false;
				});
			} else {
				return true;
			}
		}
	});

   sponsorzyTime = setInterval("zmienSponsorzy();", 3000);
    
    $(".sponsorzy").bind("mouseleave",function(){
    	clearTimeout(sponsorzyTime);
    	sponsorzyTime = setInterval("zmienSponsorzy();", 3000);
    }).bind("mouseenter",function(){
    	clearTimeout(sponsorzyTime);
    });
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		padding: 12
	});
	
	jQuery('#mycarousel').jcarousel({
		scroll: 1,
		auto: 4,
		visible: 4,
		wrap: 'circular',
        initCallback: mycarousel_initCallback
    });
	
	jQuery('#mycarousel_film').jcarousel({
		scroll: 1,
		auto: 4,
		visible: 3,
		wrap: 'circular',
        initCallback: mycarousel_initCallbackFilm
    })
	
	$('#showAll').click(function(){
		if($('#showAll').html() == 'Pokaż całość &gt;&gt;' || $('#showAll').html() == 'Show all &gt;&gt;')
		{
			pokazCalosc();
			if(lang == 'pl')
			{
				$('#showAll').html('Zwiń treść &gt;&gt;');
			}
			else
			{
				$('#showAll').html('Hide &gt;&gt;');
			}
		}
		else
		{
			pokazStrone(1);
			if(lang == 'pl')
			{
				$('#showAll').html('Pokaż całość &gt;&gt;');
			}
			else
			{
				$('#showAll').html('Show all &gt;&gt;');
			}
		}
		
		return false;
	});
	
	$('#sponbox_1').css('display', 'inline-block');
	$('#spon1').addClass('sponsorzy_bold');
	
	$('.sponsorzy_wybor').click(function(){
		var i = 0;
		var id = $(this).attr('id').substring(4,5);
		//sponsorow = $('.sponsorzy_main').children().size();
		
		$('#spon'+licznikSponsorzy).removeClass('sponsorzy_bold');
		$('#sponbox_'+licznikSponsorzy).hide();
		
		if(id > 1)
		{
			$('#spon'+(id - 1)).removeClass('sponsorzy_bold');
			$('#sponbox_'+(id - 1)).hide();
		}
		else
		{
			$('#spon'+liczbaSponsorow).removeClass('sponsorzy_bold');
			$('#sponbox_'+liczbaSponsorow).hide();
		}

		$('#spon'+id).addClass('sponsorzy_bold');
		$('#sponbox_'+id).css('display', 'inline-block');
		licznikSponsorzy = id;

		
		return false;
	});
	
 	var time;
 	$('div.ShowTool').hide('fast');

 	$('div.spolecznosc').click(function(){
 		$('div.ShowTool').toggle('slow');
 	});
 	
 	$('div.spolecznosc_film').click(function(){
 		$('div.ShowTool').toggle('slow');
 	});

 	$('div.ShowTool > a').click(function(){
 		$('div.ShowTool').hide('slow');
 	});

    $("div.ShowTool").bind("mouseleave",function(){
    	time = setTimeout(function() { $('div.ShowTool').hide('slow'); },1000);
    }).bind("mouseenter",function(){
    	clearTimeout(time);
        $('div.ShowTool').show('fast');
    });
    
//    $('.jcarousel-skin-tango li').mouseenter(function(){
//    	$(this).children('p').show();
//    });
//    $('.jcarousel-skin-tango li').mouseleave(function(){
//    	$(this).children('p').hide();
//    });
    
    $('.wstecz').click(function(){
    	if ($(this).attr('id').substring(5,6) >= 1)
    	{
    		pokazStrone($(this).attr('id').substring(5,6));
    	}
    	
    	return false;
    });
    
    $('.dalej').click(function(){
    	pokazStrone($(this).attr('id').substring(5,6));
    	
    	return false;
    });

    
    $('.strona').click(function(){
    	var stronaId = $(this).attr('id').substring(5,6);
    	pokazStrone(stronaId);
    	
    	return false;
    });
    
    
    $('.bilety_submenu li').each(function(){
    	if(!$(this).children('a').hasClass('bilety_active'))
    	{
    		var adres = '';
    		var new_adres = '';
    		$(this).mouseenter(function(){
    			adres = $(this).children('a').children('img').attr('src');
    			new_adres = adres.substring(0,adres.length-4);
    			$(this).children('a').children('img').attr('src',new_adres+'_a.jpg');
        	});
        	$(this).mouseleave(function(){
        		$(this).children('a').children('img').attr('src',adres);
        	});
    	}
	});
    
  $('#jezyk').mouseenter(function(){
		adres = $(this).attr('src');
		new_adres = adres.substring(0,adres.length-6);
		$(this).attr('src',new_adres+'.png');
	});
	$('#jezyk').mouseleave(function(){
		$(this).attr('src',adres);
	});
	
	$('.nieaktywne').css('opacity', 0.5);
	
	$('#email_newsletter').focus(function(){
		if ($('#email_newsletter').attr('value') == 'E-mail')
		{
			$('#email_newsletter').attr('value', '');
		}
	}).blur(function(){
		if ($('#email_newsletter').attr('value') == '')
		{
			$('#email_newsletter').attr('value', 'E-mail');
		}
	});
	
	$('#szukaj').focus(function(){
		if ($('#szukaj').attr('value') == 'Szukaj' || $('#szukaj').attr('value') == 'Search')
		{
			$('#szukaj').attr('value', '');
		}
	}).blur(function(){
		if ($('#szukaj').attr('value') == '')
		{
			var jezyk = checkLang();
			if(jezyk == 'pl')
			{
				$('#szukaj').attr('value', 'Szukaj');
			}
			else
			{
				$('#szukaj').attr('value', 'Search');
			}
		}
	});
	

		$('#newsl_dodaj').click(function() {
			var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
			var jezyk = checkLang();
			if (!regex.test($('#email_newsletter').attr('value')))
			{
				if(jezyk == 'pl')
				{
					$.popup.show('Błąd', 'Niepoprawny adres e-mail.');
				}
				else
				{
					$.popup.show('Error', 'Wrong e-mail address.');
				}
				return;
			 }
			 
			if($('#email_newsletter').attr('value'))
			{
				$.ajax({
					type: "POST",
					url: "ajax_newsletter.php",
					data: "email="+$('#email_newsletter').attr('value'),
					success: function(wynik){
							if(jezyk == 'pl')
							{
								$.popup.show('Informacja', 'Zapisano adres e-mail.');
							}
							else
							{
								$.popup.show('Information', 'Added e-mail address.');
							}
							$('#email_newsletter').attr('value', 'E-mail');
							return false;
					}
				});
				
				return;
			}
			else
			{
				if(jezyk == 'pl')
				{
					$.popup.show('Błąd', 'Brak danych.');
				}
				else
				{
					$.popup.show('Error', 'Empty data.');
				}
				return false;
		 	}
		});
	$('.printLink').bind('click', function() {
		window.open($(this).attr('href'), '');
		return false;
	});
	
	$('.formularzStudentShow').bind('click',function(){
		$('.formularzStudentBox').show();
		$('.formularzRodzinaBox').hide();
	});
	
	$('.formularzRodzinaShow').bind('click',function(){
		$('.formularzStudentBox').hide();
		$('.formularzRodzinaBox').show();
	});

});
