$(function(){
	/**lib**/
	function setCookie(name, value, days) {
		if ( !name || !value ) {
			return false;
		}
		days = parseInt(days)
		if ( days > 0 ) {
			var date = new Date();
			date.setTime(date.getTime()+( days * 24 * 60 * 60 * 1000 ));
			var e = "; expires="+date.toGMTString();
		} else {
			var e = "";
		}
		document.cookie = escape(name)+"="+escape(value)+e+"; path=/";
		return true;
	};
	/**end of lib**/

	$('.more, .zapisz, .trenerzy, .program, .powrot, .grey_btn, .opacityHover, .button').hover(function(){
		$(this).stop().fadeTo(300, 1);
	}, function(){
		$(this).stop().fadeTo(300, 0.8);
	});
	
	$('#user').focus(function(){
		if ( $(this).val() == 'UŻYTKOWNIK:') {
			$(this).val('');
		}
	}).blur(function(){
		if ( $(this).val() == '' ) {
			$(this).val('UŻYTKOWNIK:');
		}	
	});	
	$('#password').focus(function(){
		if ( $(this).val() == 'HASŁO:') {
			$(this).val('');
		}
	}).blur(function(){
		if ( $(this).val() == '' ) {
			$(this).val('HASŁO:');
		}	
	});
	
	//	link .pdf przy formularzu zapisu na szkolenie
	$('#fssForm a[href*="regulamin.pdf"]').click(function(e){
		window.open(this.href);
		e.preventDefault();
	});
	
	//	paginacja
	var linkActive = 0;
	if ( $('#paginacja').length ) {
		var ileStron = $('#paginacja a').length-3;

		function pokazStrone(id) {
			$('#strona'+linkActive).stop().fadeOut(function(){
				$('#linkStrona'+linkActive).removeClass('active');
				$('#strona'+id).stop().fadeIn();
				$('#linkStrona'+id).addClass('active');
				linkActive = id;
				if ( linkActive == ileStron ) {
					$('#linkStronaNext').addClass('disabled');
				} else {
					$('#linkStronaNext').removeClass('disabled');
				}
				if ( linkActive == 0 ) {
					$('#linkStronaPrev').addClass('disabled');
				} else {
					$('#linkStronaPrev').removeClass('disabled');
				}
				window.location.hash = id;
			});
		};
		
		//	obsluga linkow do stron
		$('#paginacja a.gotoStrona').click(function(e){
			var id = $(this).attr('href').slice(1);
			pokazStrone(id);
			e.preventDefault();
		});
		//	obslua linkow poprzedni, nastepny
		$('#linkStronaPrev').click(function(e){
			var id = parseInt(linkActive)-1;
			if ( id < 0 ) return false;			
			pokazStrone(id);
			e.preventDefault();
		});
		$('#linkStronaNext').click(function(e){
			var id = parseInt(linkActive)+1;
			if ( id > ileStron ) {
				return false;		
			}
			pokazStrone(id);
			e.preventDefault();
		});
		
		//	pokaz pierwsza strone, o ile nie ma żadnej w URL
		var hash = parseInt( window.location.hash.slice(1) );
		//alert('hash: '+hash+' , ileStron: '+ileStron);
		if ( hash > -1 && hash <= ileStron ) {
			pokazStrone(hash);
		} else {
			pokazStrone(0);
		}
		
	} else {
		//	pokaz strone 0
		$('#strona0').show();
	};
	
	//	link .pdf przy formularzu zapisu na szkolenie
	$('#fssForm a[href*="regulamin.pdf"]').click(function(e){
		window.open(this.href);
		e.preventDefault();
	});
	
	//	fb box
	var pxActive = '0';
	var pxInActive = '-245px';
	var timeToHide = 500;
	
	var $fb = $('#fb_box');
	var $fbImg = $('#show_fb_box');
	var timerID = null;
	
	$fbImg.hover(function(){
		clearTimeout(timerID);
		$fb.animate({ 'left':pxActive }, 400);
		$fbImg.data('active', true);
	}, function(){
		timerID = setTimeout(function(){
			$fb.animate({ 'left':pxInActive }, 400);
		}, timeToHide);
	});
	
	$fb.find('iframe').hover(function(){
		clearTimeout(timerID);
	}, function(){
		timerID = setTimeout(function(){
			$fb.animate({ 'left':pxInActive }, 400);
		}, timeToHide);
	});

	//	linki
	$('a').click(function(e){
		if ( !$(this).hasClass('button') ) {
			if ( $(this).attr('href').indexOf( window.location.host ) < 0 && $(this).attr('href').charAt(0) != '#'  && $(this).attr('href').charAt(0) != '/'  ) {
				window.open( $(this).attr('href') );
				e.preventDefault();
			}
		} else {
			if ( $(this).attr('href').indexOf( window.location.host ) < 0 && 
				$(this).attr('href').indexOf('http:') == 0 ) {
				window.open( $(this).attr('href') );
				e.preventDefault();
			} 
		}
	});
	
	//	katalog box
	$('#btn_katalog_box').colorbox({ inline: true, href: '#f_pobierz_katalog' });
	var $adresKore = $('#adresKore');
	$('input[name=formaKatalogu]').click(function(){
		var ic = $('input[name=formaKatalogu]:checked').val();
		if ( ic == 'cd' || ic == 'pendrive' ) {
			$adresKore.slideUp(function(){
				$.colorbox.resize();
			});
		} else {
			$adresKore.slideDown(function(){
				$.colorbox.resize();			
			});
		}
	});
	
	//	katalog box send... 
	$('#f_pobierz_katalog_form_wyslij').click(function(e){
		var _self = $(this);
		if ( _self.data('active') == true ) {
			//alert('Trwa wysyłanie...');
			return false;
		}
		_self.data('active', true);
		$.ajax({
			url: '/aj/katalog/zamow',
			data: {
				formaKatalogu : $('input[name=formaKatalogu]:checked').val(),
				imie : $('#imie').val(),
				nazwisko : $('#nazwisko').val(),
				stanowisko : $('#stanowisko').val(),
				nazwaFirmy : $('#nazwaFirmy').val(),
				adres : $('#adres').val(),
				kodpocztowy : $('#kodpocztowy').val(),
				miasto : $('#miasto').val(),
				telefon : $('#telefon').val(),
				email : $('#email').val()
			},
			dataType: 'json',
			error: function(XMLHttpRequest, textStatus, errorThrown) { },
			type: 'post',
			success: function(data, textStats, xmlHttpRequest) {
				if ( data.status ) {
					$.colorbox({ inline: true, href: '#f_pobierz_katalogTH' });
				} else { 
					//	wyswietlenie bledow
					$('#f_pobierz_katalog_ErrorContent').html(data.msg);
					$('#f_pobierz_katalog_Error').fadeIn();
					//	klasy dla inputow
					$('#f_pobierz_katalog_form input, #f_pobierz_katalog_form textarea').removeClass('error');
					for( i in data.errors)  {
						if ( $('#'+i+'').length ) { 
							$('#'+i+'').addClass('error');
						}
					}
					$.colorbox.resize();
				}
				_self.data('active', false);
			}
		});
		e.preventDefault();
	});

	//	szkolenia podział data <> sekcja
	$('.szkoleniaGsoPodzial').live('click', function(e){
		var href = $(this).attr('href').slice(1);
		var cookieVal = '';
		if ( href == 'sekcja' ) cookieVal = 's';
		else if ( href == 'data' ) cookieVal = 'd';		
		setCookie('szkoleniaGsoPodzial', cookieVal, 365);
		window.location.reload();
		e.preventDefault();
	});
	
});
