/**
 * @author dgorny, rsikora
 */
var _LLANG = Array();
_LLANG['err_400'] = "Wystapił nieokreślony błąd, spróbuj ponownie za chwilę.";


var checkNominationText = 0;

function checkNominationForm(){
    
    if ($('textarea#whyNomination').val().length == '0'){
        checkNominationText++;
        $('#uzasadnienieNomination').html("Uzasadnij swoją ocenę. Jeśli nie chcesz tego zrobić,naciśnij przycisk \"Zapisz ocenę\"");
        if (checkNominationText == 2){
            $('#uzasadnienieNomination').html(" ");
            checkNominationText = 0;
            //nomination_text_form
            $('#nomination_text_form').html("Dziękujemy za ocenę zdjęcia");
            $('#zapisz-ocene').remove();
            $('#nomForm').submit();
            $('#uzasadnienieOcenyNom').remove();
        }
    }
    else {
        checkNominationText = 0;
        $('#uzasadnienieNomination').html(" ");
        $('#nomination_text_form').html("Dziękujemy za ocenę zdjęcia");
        $('#zapisz-ocene').remove();
        $('#nomForm').submit();
        $('#uzasadnienieOcenyNom').remove();
    }

}

function checkNomination(id){
    $.ajax({
            type: "POST",
            url: "index.php?id=125",
            cache: false,
            dataType: "text",
            data: {
                    'tx_nlib_pi1[action]':'check_nomination',
                    'tx_nlib_pi1[photo_uid]':id
            },
            success: function (data, textStatus) { 
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            },
            complete: function (XMLHttpRequest, textStatus) {
                $('#nomination_button').remove();
                $('#nomination_text').html("Dziękujemy za nominowanie zdjęcia.");

               // $('#nomination_button').css("display","none");
            }
    });
    
}

function setEvents(){
	
	$(".js_close_msg").bind('click', function() {
		var msgBox = $(this).parent();
		$(msgBox).fadeOut('slow', function(){
			$(msgBox).remove();
		});
		
		var rel = $(this).attr('rel');
		
		$('#msg').hide();
		
		if( rel ){
			$.ajax({
				type: "POST",
				url: "index.php?id=125",
				cache: false,
				dataType: "text",
				data: {
					'tx_nlib_pi1[action]':'close_msg',
					'tx_nlib_pi1[msg_id]':rel
				},
				success: function (data, textStatus) { 
					if(data != 'OK') {
						$(msgBox).fadeIn();
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					$(msgBox).fadeIn();
				},
				complete: function (XMLHttpRequest, textStatus) {
				   
				}
			});
		}
		return false;
	});
	
	

}
	
function komunikat( type, msg ){
			
	var okno = $('#msg').get(0);
	
	if( ! okno ){
		return false;
	}
	
	var classMsg = 'e';
	if( type ){
		classMsg = '';
	}
		
	if( $('#msg > ul').get(0) ){
		//add new info
		var item = "<li class='"+classMsg+"'><p>"+msg+"</p><a href='#close_msg' title='zamknij' class='js_close_msg' >[X]</a></li>";
		$(item).insertAfter('#msg > ul > li:last'); 
		$('#msg').show();
		setEvents();
	}
	else{
		//insret list
		var item = '<ul><li class="'+classMsg+'"><p>'+msg+'</p><a href="#close_msg" title="zamknij" class="js_close_msg" >[X]</a></li></ul>';
		$('#msg').html( item );
		$('#msg').show();
		setEvents();
	}
	
}



var tx_nfeuserregister_pi1_form = {
	loading_checkUsername: false,
	checking_timer: null,
	actualy_checking: null,
		
	init: function() {
	
		$('input[name="prenumerator"]').bind('change click', function(e) {
			if($(this).val() == 1) {
				$('#js_pren_info_instead').hide();
				$('#js_pren_info').fadeIn();
			} else {
				$('#js_pren_info').hide();
				$('#js_pren_info_instead').fadeIn();
			}
		});
		
	
		$('#nazwauzytkownika').bind('keyup', function(e) {
			$('#js_usercheck_stat').hide();
			if($('#nazwauzytkownika').val().length == 0) {
				return;
			}
			if(tx_nfeuserregister_pi1_form.actualy_checking == $('#nazwauzytkownika').val()) return;
			
			
			if(!tx_nfeuserregister_pi1_form.validateUsername($('#nazwauzytkownika').val())) {
				$('#js_usercheck_stat').removeClass('i-wolna');
				$('#js_usercheck_stat').addClass('i-zajeta');
				$('#js_usercheck_stat').text('Zawiera niedozwolone znaki');
				if($('#js_usercheck_stat').css('display') == 'none') $('#js_usercheck_stat').show();
				return;
			}
			
			
			if($('#nazwauzytkownika').val().length < 4) {
				$('#js_usercheck_stat').removeClass('i-wolna');
				$('#js_usercheck_stat').addClass('i-zajeta');
				$('#js_usercheck_stat').text('Nazwa jest za krótka');
				if($('#js_usercheck_stat').css('display') == 'none') $('#js_usercheck_stat').show();
				return;
			}
			if($('#nazwauzytkownika').val().length > 12) {
				$('#js_usercheck_stat').removeClass('i-wolna');
				$('#js_usercheck_stat').addClass('i-zajeta');
				$('#js_usercheck_stat').text('Nazwa jest za długa');
				if($('#js_usercheck_stat').css('display') == 'none') $('#js_usercheck_stat').show();
				return;
			}
			clearTimeout(tx_nfeuserregister_pi1_form.checking_timer);
			tx_nfeuserregister_pi1_form.checking_timer = setTimeout( function(){
				tx_nfeuserregister_pi1_form.checkUsername();
			}, 1500 );
		});
		
	},
	
	checkUsername: function() {
		if(tx_nfeuserregister_pi1_form.loading_checkUsername) return;
		tx_nfeuserregister_pi1_form.loading_checkUsername = true;
		tx_nfeuserregister_pi1_form.actualy_checking = $('#nazwauzytkownika').val();
		$.ajax({
           type: "POST",
           url: "/typo3conf/ext/n_feuser_register/pi1/tx_nfeuserregister_usercheck.php",
           cache: false,
           dataType: "text",
           data: {
		   		'username': tx_nfeuserregister_pi1_form.actualy_checking
           },
           success: function (data, textStatus) { 
        	   tx_nfeuserregister_pi1_form.checkUsername_success(data);
			},
           error: function (XMLHttpRequest, textStatus, errorThrown) {
				tx_nfeuserregister_pi1_form.checkUsername_error(textStatus, errorThrown);
			},
           complete: function (XMLHttpRequest, textStatus) {
				tx_nfeuserregister_pi1_form.loading_checkUsername = false;
				tx_nfeuserregister_pi1_form.actualy_checking = null;
           }
       });
	},
	
	checkUsername_success: function(data) {
		//console.debug('checkUsername_success: '+data);
		if(data == 'NOT_EXISTS') {
			$('#js_usercheck_stat').removeClass('i-zajeta');
			$('#js_usercheck_stat').addClass('i-wolna');
			$('#js_usercheck_stat').text('Nazwa jest wolna');
		} else {
			$('#js_usercheck_stat').removeClass('i-wolna');
			$('#js_usercheck_stat').addClass('i-zajeta');
			$('#js_usercheck_stat').text('Nazwa jest zajęta');
		}
		if($('#js_usercheck_stat').css('display') == 'none') $('#js_usercheck_stat').show();
	},
	
	checkUsername_error: function(textStatus, errorThrown) {
		alert('checkUsername_error: '+textStatus+' | '+errorThrown);
	},
	
	validateUsername: function(alphane)
	{
		var numaric = alphane;
		for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh == 95)
		  {
		  }
		  else	{
			 return false;
		  }
		}
		return true;
	}

}

var tx_nttnewstweak = {
	leftBtn: '#js_article_photos_left',
	rightBtn: '#js_article_photos_right',
	container: '#js_article_photos_container',
	canShow: 5,
	animateMotion: true,
	notActiveBtnClass: 'na',
	
	width: 2000,
	elementsArr: null,
	currentPos: 0,
	
	init: function() {
		tx_nttnewstweak.width = 0;
		tx_nttnewstweak.elementsArr = new Array();
		$(this.container).find('li').each(function(i) {
			tx_nttnewstweak.elementsArr[i] = tx_nttnewstweak.width;
			tx_nttnewstweak.width += parseInt($(this).css('width'))+parseInt($(this).css('margin-left'))+parseInt($(this).css('margin-right'));
		});
		$(this.container).css('width', tx_nttnewstweak.width);
		
		$(this.leftBtn).bind('click', function() {
			if(!$(this).hasClass(tx_nttnewstweak.notActiveBtnClass) && tx_nttnewstweak.currentPos < (tx_nttnewstweak.elementsArr.length-tx_nttnewstweak.canShow)) {
				tx_nttnewstweak.currentPos++;
				tx_nttnewstweak.gotoPos();
			}
			return false;
		});
		$(this.rightBtn).bind('click', function() {
			if(!$(this).hasClass(tx_nttnewstweak.notActiveBtnClass) && tx_nttnewstweak.currentPos > 0) {
				tx_nttnewstweak.currentPos--;
				tx_nttnewstweak.gotoPos();
			}
			return false;
		});
	},
	
	gotoPos: function() {
		if(tx_nttnewstweak.animateMotion) {
			$(this.container).animate( { 'marginLeft': '-'+tx_nttnewstweak.elementsArr[tx_nttnewstweak.currentPos]+'px' }, 'slow' );
		} else {
			$(this.container).css( 'margin-left', '-'+tx_nttnewstweak.elementsArr[tx_nttnewstweak.currentPos]+'px' );
		}
		if(tx_nttnewstweak.currentPos <= 0) {
			$(this.rightBtn).addClass(tx_nttnewstweak.notActiveBtnClass);
		} else {
			$(this.rightBtn).removeClass(tx_nttnewstweak.notActiveBtnClass);
		}
		if(tx_nttnewstweak.currentPos >= (tx_nttnewstweak.elementsArr.length-tx_nttnewstweak.canShow)) {
			$(this.leftBtn).addClass(tx_nttnewstweak.notActiveBtnClass);
		} else {
			$(this.leftBtn).removeClass(tx_nttnewstweak.notActiveBtnClass);
		}
	}
		
}

$(document).ready(function() {
	tx_nfeuserregister_pi1_form.init();

	//Overlay.init();
	
	/* Ikonka świadcząca o nowym kontencie w submenu*/
	$('.sub li a').each( function(i){
		if( $(this).html() == 'Komentarze' ){
			$(this).addClass('nowe');
		} 
	});
	
	/* Ikonka samolotu */
	$('#m- li > a[title="Bilety"]').parents('li:first').addClass('rezerwacje');
	
	/* Logowanie w topie */
	$('#js_remember_check').bind('click', function () {
		$(this).toggleClass('s');
		if($(this).hasClass('s')) {
			$('#permaloginHiddenField').attr("value", 1); 
		} else {
			$('#permaloginHiddenField').attr("value", 0);
		}
		return false;
	});
	/*$('#js_top_login_link').bind('click', function () {
		$('#js_top_search_form').hide();
		$('#js_top_login_link').hide();
		$('#js_top_loginform').show();
		$('#js_top_search_link').show();
		return false;
	});*/
	$('#js_top_search_link').bind('click', function () {
		$('#js_top_loginform').hide();
		$('#js_top_search_link').hide();
		$('#js_top_search_form').show();
		$('#js_top_login_link').show();
		return false;
	});
	
	/* Pokazywanie chmurki komunikatu */
	if( $('.i-err').get(0) ){
		$('.i-err').hover(
			function(){
				$(this).find('div:last').show();
			},
			function(){
				$(this).find('div:last').hide();
			}
		);
		
		$('.i-err > a').click( 	function(){ 
			return false;					
		});
		
	}
	
	tx_nttnewstweak.init();
	
	/* podlaczenie lightbox'a */
	if( $('[rel="lightbox"]').get(0) ){
		
		$('[rel="lightbox"]').lightBox(
		{
			overlayBgColor: '#000',
			overlayOpacity: 0.6,
			imageLoading: '/fileadmin/templates/img/lightbox-ico-loading.gif',
			imageBtnClose: '/fileadmin/templates/img/lightbox-btn-close.gif',
			imageBtnPrev: '/fileadmin/templates/img/lightbox-btn-prev.gif',
			imageBtnNext: '/fileadmin/templates/img/lightbox-btn-next.gif',
			imageBlank: '/fileadmin/templates/img/lightbox-blank.gif'
			
		}
		);
	}
	
	if( $('[rel="lightbox_profil"]').get(0) ){
		
		$('[rel="lightbox_profil"]').lightBox(
		{
			overlayBgColor: '#000',
			overlayOpacity: 0.6,
			imageLoading: '/fileadmin/templates/img/lightbox-ico-loading.gif',
			imageBtnClose: '/fileadmin/templates/img/lightbox-btn-close.gif',
			imageBtnPrev: '/fileadmin/templates/img/lightbox-btn-prev.gif',
			imageBtnNext: '/fileadmin/templates/img/lightbox-btn-next.gif',
			imageBlank: '/fileadmin/templates/img/lightbox-blank.gif'
			
		}
		);
	}
	
	/*ukrytanie komunikatu */
	if( $('#msg').get(0) ){
		if( $('#msg li').get(0) ){
			$('#msg').show();
		}
	}
	
	/* zwijanie i rozwijanie pierwszej listy na stronie foto */
	if( $('.JQ_toggle').get(0) ){
		$('.JQ_toggle').bind( 'click', function(){
			$(this).parents('div:first').parents('div:first').find('.c').toggle();
			
			if( $(this).parents('div:first').hasClass('hs') ){
				$(this).parents('div:first').removeClass('hs').addClass('hr');
			}
			else{
				$(this).parents('div:first').removeClass('hr').addClass('hs');
			}
                        $("#ocenaZdjeciaN").css('display','none');
			
		});
	}
	if( $('.JQ_toggle_n').get(0) ){
		$('.JQ_toggle_n').bind( 'click', function(){
			$(this).parents('div:first').parents('div:first').find('.n').toggle();
			if( $(this).parents('div:first').hasClass('hs') ){
				$(this).parents('div:first').removeClass('hs').addClass('hr');
			}
			else{
				$(this).parents('div:first').removeClass('hr').addClass('hs');
			}
                        $("#ocenaZdjeciaC").css('display','none');
			
		});
	}

	
	/* zwijanie  i rozwijanie listy producentów w rankingu foto */
	if( $('.JQ_toggle_producent').get(0) ){
		/*
		$('.JQ_toggle_producent h4').bind( 'click', function(){
			$(this).find('+ ul')
				.toggle()
				.siblings("ul").hide();			
		});
		*/
		/* rozwinięcie listy producenta dla ogladanego zdjecia */
		var id = $('.fr > .arc li:first').attr('id');
		id = id.substring( 1 );
			
		$('#a'+id).parents('ul:first').show();
	}
	
});

var Overlay = {

	init: function(){
		var document_height = $(document).height();
		var document_width = $(document).width();
		var divTag = document.createElement("div");
		divTag.id = "overlay";
		divTag.style.position = "absolute";
		divTag.style.zIndex ="9999";
		divTag.style.width = document_width+"px";
		divTag.style.height = document_height+"px";
		divTag.style.backgroundColor ="#000";
		divTag.style.top ="0px";
		divTag.style.left ="0px";
		divTag.style.opacity ="0.5";
		divTag.style.display ="none";
		document.body.appendChild(divTag);
		$('#overlay').css( {filter:"alpha(opacity=70)"});
	},

	show: function(){
		$('#overlay').show();
		$('object, embed').css('visibility','hidden');
	},

	hide: function(){
		$('#overlay').hide();
		$('object, embed').css('visibility','visible');
	}
}

function getInnerDimensions() {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}

	return [x,y];
}

function getScrollingOffset() {
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}

	return [x,y];
}

