var ignore = -1;

$(document).ready(function() {
	
	//----PDF VIEWER--------------------------------------------------------------------------
	
	$(".pdf-viewer a.preview").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600,
		'width'		:	700,
		'autoScale'		:	false,
		'speedOut'		:	200,
		'overlayShow'	:	true
	});

	slider=$('.pdf-viewer').bxSlider({
		displaySlideQty: 2,
		moveSlideQty: 2,
		infiniteLoop:false,
		pager:true,
		pagerType: 'short',
		pagerShortSeparator: 'z',
		hideControlOnEnd: true
	});

	$('#input_go_to_page').keypress(function(e)
	{
		var code= (e.keyCode ? e.keyCode : e.which);
		if (code == 13){
			$('#btn-go-to-page').trigger('click');
		};

	});


	$('#btn-go-to-page').click(function(e){
			e.preventDefault();
			var page= parseInt($(this).parents('dd').find('#input_go_to_page').val());
			var total= slider.getSlideCount()-2;

			if(page % 2){
				page--;
			}

			if(isNaN(page))
			{
				return;
			}
			else if(page > total){
				page= total +1;
			}
			else if(page < 0){
				page= 0;
			}

			slider.goToSlide(page);

		});

	$('#btn-go-to-first').click(function(e){
			e.preventDefault();
			$(this).parents('dl').find('input[name=go_to_page]').val('1');
			slider.goToFirstSlide();
	});

	$('#btn-go-to-last').click(function(e){
			e.preventDefault();
			var total= slider.getSlideCount()-1;

			if(total % 2){
				total--;
			}

			$(this).parents('dl').find('input[name=go_to_page]').val(total);
			slider.goToSlide(total);
	});
	
	
	//----Event Collector--------------------------------------------------------------------------

	//Datepicker
	var eventDates= $( "#start_date,  #end_date" ).datepicker({
		changeYear: true,
		changeMonth: true,
		onSelect: function( selectedDate ) {
				var option = this.id == "start_date" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				eventDates.not( this ).datepicker( "option", option, date );
			}
	});


	//---------------------------------------------------------------------------------------
	
	$('#survey').submit(function() {
		
		var url = $(this).attr('href');
		
		var attr = this;
		
		$.post('/survey/vote/', $(this).serialize(), function() {
			$(attr).parent().html('<p>Dziękujemy za oddanie głosu.</p>');
		});
		
		return false;
	});
	
	//Gallery
	$('a.btn-gallery').colorbox({rel:'gallery', current:"Zdjęcie {current} z {total}"});

	$(".btn-set-favorite").click(function(){
		var bookmarkUrl = window.location;
		var bookmarkTitle = window.title;
     
		if ($.browser.mozilla) // For Mozilla Firefox Bookmark
		{
			window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
		}
		else if($.browser.msie || $.browser.webkit) // For IE Favorite
		{
			window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
		}
		else if($.browser.opera ) // For Opera Browsers
		{
			$(this).attr("href",bookmarkUrl);
			$(this).attr("title",bookmarkTitle);
			$(this).attr("rel","sidebar");
			$(this).click();
		}
		
		return false;
	});
	//---------------------------------------------------------------------------------------
	
	$('.forumdelete').click(function() {
		
		var obj = this;
		
		$.post($(this).attr('href'), {ajax: 1}, function() {
			$(obj).parents('li').remove();
			location.reload();
		});
		
		return false;
	});
	
	$('.commentsdelete').click(function() {
		
		var obj = this;
		
		$.post($(this).attr('href'), {ajax: 1}, function() {
			$(obj).parents('li').remove();
		});
		
		return false;
	});
	
	// External Links
	$('a[href][rel=external]').attr('target', '_blank');

	// Checkbox
	$('input:checkbox').screwDefaultButtons({
             checked: "url(/public/site/images/bg_input_checkbox_checked.png)",
             unchecked: "url(/public/site/images/bg_input_checkbox.png)",
             width: 15,
             height: 14
          });
		  
	//Datepicker
	$( "#birthdate" ).datepicker({changeYear: true, changeMonth: true, yearRange: '1920:c'});

	//---------------------------------------------------------------------------------------

	//Login
	$('.btn-login, #btn-login').click(function(e){
		e.preventDefault();

		$.colorbox({inline:true, href:"#box-login", overlayClose:false});
	});
	
	$('#form-login').submit(function() { 
		$this= $(this);
		var options= {
			dataType: 'json', 
			scrolling:false, 
			success: 
				function(response){
					if(response.code=='error'){
						$this.find('.error').html(response.data);
						$.colorbox.resize();
					}
					else if(response.code == 'redirect'){
						window.location= response.data;
						return;
					}
				}
		};
	
		$(this).ajaxSubmit(options); 
		return false;
	});

	//---------------------------------------------------------------------------------------

	//News pager
	$('.pager li').click(function(e){
		e.preventDefault();
		
		$(this).siblings().removeClass('selected');
		$(this).addClass('selected');

		var index= $(this).index();
		var $articles= $(this).parents('.box-generic').children('.article');
		var $current= $articles.eq(index);
		
		if($current.length){
			$articles.hide();
			$current.show();
		}
		
		if($(this).parent().hasClass('calendar')) {
			
			if(index == 0 || index == 3) {
				$('.first-box').show();
				$('.second-box').hide();
			} else if(index == 1) {
				$('.first-box').hide();
				$('.second-box').show();
			} else {
				$('.first-box').hide();
				$('.second-box').hide();
			}
			
		}
		
		ignore = $(this).parent('.pager').index();
	});
	
	//---------------------------------------------------------------------------------------
	
	//Forum links
	$('.table-forum tr td').click(function(e){
		e.preventDefault();
		var href= $(this).parent('tr').find('a.topic').attr('href')
		window.location= href;
	});
	
	//---------------------------------------------------------------------------------------
	
	//Forum show answer box
	$('.box-forum-topic .nav .btn-answer').live('click', function(e){
		e.preventDefault();
		$(this).parents('li').find('.box-answer').show();
	});
	
	
	//Forum hide answer box
	$('.box-forum-topic ul li .btn-hide-answer').live('click', function(e){
		e.preventDefault();
		$(this).parents('li').find('.box-answer').hide();
	});
	
	/*
	//Forum send  answer
	$('.box-forum-topic ul li .btn-send-answer').live('click', function(e){
		e.preventDefault();
		var $this= $(this);
		var $form= $(this).parent('form');
		var textarea=  $form.find('textarea').val();
		if(textarea){
			$.post($form.attr('action'), {content: textarea}, function(response){
				$this.parents('ul').append(response);
				$form.find('textarea').val('');
			});
		}
	});
	*/
	
	//---------------------------------------------------------------------------------------
	
	//Forum show answer box (main)
	$('.box-forum-topic .entry-details  .btn-answer').click(function(e){
		e.preventDefault();
		$(this).parents('td').find('.box-answer').show();
	});
	
	//Forum hide answer box (main)
	$('.box-forum-topic .entry-details .btn-hide-answer').click(function(e){
		e.preventDefault();
		$(this).parent('div').hide();
	});
	
	/*
	//Forum send  answer (main)
	$('.box-forum-topic .entry-details  .btn-send-answer').click(function(e){
		e.preventDefault();
		var $this= $(this);
		var $form= $(this).parent('form');
		var textarea=  $form.find('textarea').val();
		if(textarea){
			$.post($form.attr('action'), {content: $form.find('textarea').val()}, function(response){
				$this.parents('tr').prev().find('ul').append(response);
				$form.find('textarea').val('');
			});
		}
	});
	*/
	
	//---------------------------------------------------------------------------------------
	
	//Form newsletter
	$('#form-newsletter').bind('submit', function(){
	
		var email= $(this).find('input[name=email]').val();
		var $this= $(this);
	
		if(email){
			$.post($this.attr('action'), {content: email}, function(response){
				
				// JSON DUMMY
				response= {code:1, content:'<p class="error">- Pole E-mail musi zawiera\u0107 poprawny adres email.</p>'};
	
				$this.append("<p>"+response.content+"</p>");
				$this.find('input[name=email]').val('');
			});
		}
	
		return false;	
	});
	
	
	
	
	$('input[name="ajax"]').val('1');
	
	$('.commonform').submit(function() {
		
		var url = $(this).attr('action');
		
		var form = $(this);
		
		$.post(url, $(this).serialize(), function(data) {
			
			$(form).find('.ajaxmessage').show();
			
			if(data.ok == 0) {
				$(form).find('.ajaxmessage').removeClass('green').addClass('red');
				$(form).find('input[type="password"]').val('');
			} else {
				$(form).find('.ajaxmessage').removeClass('red').addClass('green');
				$(form).find('input[type="text"]').val('');
				$(form).find('input[type="password"]').val('');
				$(form).find('textarea').val('');
			}
			
			$(form).find('.ajaxmessage').html(data.message);
			
			if(data.reload) {
				location.reload();
			}
			 $.colorbox.resize();

		}, 'JSON');
		
		return false;
	});
	
	
	setInterval("move_pager()", 5000);
	
});

function move_pager() {
	$('.pager').each(function() {
		if(!$(this).hasClass('ignored')) {
			var cur = $(this).find('li.selected').index();
			var size = $(this).find('li').size() - 1;
			
			if(ignore == $(this).index()) {
				ignore = -1;
			} else {
				cur++;
				
				if(cur > size) cur = 0;
				
				$(this).find('li:eq('+cur+')').click();
			}
		}
	});	
}

