
$(document).ready(function($){
startup();
accord();
lside();
overlay_be();
filie_mapy();
be_forms();
});




function startup() {
	//odpalamy menu
	$("ul#nav").superfish();
	// rozwijane a.submenu jest nieklikalne
	$("ul#nav a.submenu").click(function (event) {
		event.preventDefault();
	});
	// odpalamy tooltipy
	$("#o-nas .gg a[title], #support-nav a.tip[title]").tooltip({ position: "bottom center"});
	
	// jesli aktywna jest podstona submenu bocznego to bedzie ono automatycznie rozwiniete
	if ($('#lside ul.level_2 li.active').length || $('#lside ul.level_3 li.active').length) {
			//console.log(alert($(this)));
			if ($('#lside li.active').parent('ul').is('ul.level_2')) {
				$('#lside li.active').parent('ul').css('display','block');
			} else {
				//$('#lside li.active').parent('ul').prev('a').addClass('open');
				$('#lside li.active').parent('ul').css('display','block').parent('li').parent('ul').css('display','block');
			}
	};
	// jesli to opera lub ie to dodaj do body klase "opie"
	if ($.browser.msie || $.browser.opera) {
		$('body').addClass('opie');
	}
	
	// jesli szerokosc strony jest mniejsza od 1224px to przenosimy #lside na prawo a jesli jest wiekszy to dodajemy dla IE do body klase .ie-wide
	var maskWidth = $(window).width();
	if (maskWidth < 1224) {
		$('#lside').insertBefore('#aside h2:eq(0)');
	 } else {
		if ($.browser.msie) {
			$('body').addClass('ie-wide');
		}
	 }
	 if ($('.ce_gallery').length || $('.ce_news_gallery').length) {
	 	$(".ce_gallery a, .ce_news_gallery a").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'titleShow'		:	false,
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.9,
		});
	 }
	 
	 if ($('.mod_galleryviewer').length) {
		$(".mod_galleryviewer a[rel=lightbox]").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'titleShow'		:	false,
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayColor'		: '#000',
			'overlayOpacity'	: 0.9
		});
	 }
	 
	 // "pojawiam sie i znikam" - values w inputach
	 $('#newsletter input.text, form#search input.text').ToggleInputValue();
	 
	 // odpalamy wyszukiwanie czasopism
	 if ($('#czaso_lista').length) {
	 	$('#czaso_lista table').liveFilter('slide');
 		//$('#czaso_lista table tr').change(function() {
		//	if ($('#czaso_lista table tr').is(':visible')) {
				//$(this).parent().addClass('dodaj');
				//$("ul").append("<li>" + ($("ul").has("li").length ? "Yes" : "No") + "</li>");
			//}else{
				//$(this).parent().removeClass('dodaj');
			//}
		//});
	 }
};

// automatyczny resize przy zmianie szerokosci okna
function lside() {
	$(window).resize(function(){
		var contentResize = $(window).width();
		if (contentResize < 1224) { 
			$('#container #lside').insertBefore('#aside h2:eq(0)');
			if ($.browser.msie) {
				$('body').removeClass('ie-wide');
			}
		}else{
			$('#aside #lside').insertBefore('#content');
			if ($.browser.msie) {
				$('body').addClass('ie-wide');
			}
		}
	});
};

// akordeon w bocznym menu
function accord() {
$("#lside a.submenu").click(function (event) {
		event.preventDefault();
		//$(this).parent();
		$(this).parent().siblings('li.submenu').children('ul').slideUp().siblings('a').removeClass('open');
		$(this).toggleClass('open');
		$(this).next('ul').slideToggle("slow");
	});
};

// teksty w inputach wyszukiwarki i newslattera
$.fn.ToggleInputValue = function(){
    return $(this).each(function(){
        var Input = $(this);
        var default_value = Input.val();

        Input.focus(function() {
           if(Input.val() == default_value) Input.val("");
        }).blur(function(){
            if(Input.val().length == 0) Input.val(default_value);
        });
    });
}

function overlay_be() {
// komunikat po akceptacji tokenu ma klase $('.mod_subscribe .confirm') i to na front poprzez jquery tools
// oraz tez na front jesli jest błędny adres email wpisany do newslettera $('#newsletter p.error')
if ($('.mod_subscribe .confirm').length) {
	$('.mod_subscribe .confirm').overlay();
}

if ($('#newsletter p.error').length) {
	$('#newsletter p.error').addClass('simple_overlay').attr('id', 'erro').insertBefore('#wrapper');
	$('#newsletter p.error').overlay();
	var api = $("#erro").overlay({oneInstance: false, api: true}); 
		api.load();
}


}

function filie_mapy() {
// odpalamy mapy googla dla filii
if ($('#content .mapy-filie').length) {
	$("#mapa-filia-1").gMap({ 
							  controls: false,
							  markers: [{ latitude: 54.17593096547091,
	                              longitude: 19.395192861557007,
	                              html: "Filia Biblioteki Elbląskiej nr 1<br/>ul. Brzeska 5",
	                              popup: true }],
	                  zoom: 16 })
	$("#mapa-filia-2").gMap({ 
							  controls: false,
							  markers: [{ latitude: 54.15071709025114,
	                              longitude: 19.42285180091858,
	                              html: "Filia Biblioteki Elbląskiej nr 2<br/>Al. Grunwaldzka 89a",
	                              popup: true }],
	                  zoom: 16 })
	$("#mapa-filia-3").gMap({ 
							  controls: false,
							  markers: [{ latitude: 54.17810991883244,
	                              longitude: 19.420877695083618,
	                              html: "Filia Biblioteki Elbląskiej nr 3<br/>ul. Piłsudskiego 17",
	                              popup: true }],
	                  zoom: 16 })
	$("#mapa-filia-4").gMap({ 
							  controls: false,
							  markers: [{ latitude: 54.187119645327186,
	                              longitude: 19.41636085510254,
	                              html: "Filia Biblioteki Elbląskiej nr 4<br/>ul. Ogólna 59",
	                              popup: true }],
	                  zoom: 16 })
	$("#mapa-filia-6").gMap({ 
							  controls: false,
							  markers: [{ latitude: 54.16397921748195,
	                              longitude: 19.41285252571106,
	                              html: "Filia Biblioteki Elbląskiej nr 6<br/>ul. Słoneczna 29",
	                              popup: true }],
	                  zoom: 16 })
	$("#mapa-filia-7").gMap({ 
							  controls: false,
							  markers: [{ latitude: 54.15765337991008,
	                              longitude: 19.404247999191284,
	                              html: "Filia Biblioteki Elbląskiej nr 7<br/>ul. Hetmańska 16/20",
	                              popup: true }],
	                  zoom: 16 })
	$("#mapa-filia-8").gMap({ 
							  controls: false,
							  markers: [{ latitude: 54.14928445083392,
	                              longitude: 19.40834641456604,
	                              html: "Filia Biblioteki Elbląskiej nr 8<br/>ul. Mielczarskiego 22/24",
	                              popup: true }],
	                  zoom: 16 })
	$("#mapa-glowna").gMap({ 
							  controls: false,
							  markers: [{ latitude: 54.15745862676752,
	                              longitude: 19.393916130065918,
	                              html: "Biblioteka Elbląska<br/>ul. św. Ducha 3-7",
	                              popup: true }],
	                  zoom: 16 })
	
	}
}



function be_forms() {
// lokalizacja dla validacji
$.tools.validator.localize("pl", {
	'*'			: 'Nieprawidłowa wartość',
	':email'  	: 'Wpisz poprawny adres e-mail',
	':number' 	: 'Wartość musi być liczbą',
	':url' 		: 'Nieprawidłowy adres URL',
	'[max]'	 	: 'Wprowaź wartość mniejszą niż $1',
	'[min]'		: 'Wprowaź wartość większą niż $1',
	'[required]'	: 'To pole jest wymagane'
});
// formularz "Zapytaj bibliotekarza"
if ($('form#f9').length) {		
	$('select#ctrl_54,textarea#ctrl_56').attr('required', 'required');
	$('input#ctrl_55').remove();
	$('<input type="email" value="" class="text mandatory" id="ctrl_55" required="required" name="email" />').insertAfter('form#f9 label[for=ctrl_55]');	
	$("form#f9").validator({
				lang: 'pl',
				position: 'top right',
				offset: [0, -240]
				});
	//zamiana selecta na imput i odwrotnie
	var wybory = $('form#f9 select#ctrl_54');
	$('form#f9 select#ctrl_54').live("change",function() {
		if ($(this).val() == 4) {
			selectZapytaj();
			zamienNaSelect(wybory);
		}
	});
	if ($('form#f9 label[for=ctrl_54]').next('p.error').length) {
		selectZapytaj();
		zamienNaSelect(wybory);
	}
}
// formularz "Wypożyczalnia Miedzybiblioteczna"
if ($('form#f11').length) {
	var currentYear = (new Date).getFullYear();
	$('form#f11 input#ctrl_65').attr('max', function() {
		return currentYear
	}).attr('min', '1900');
	$('input#ctrl_63,input#ctrl_64,input#ctrl_67').attr('required', 'required');
	$('input#ctrl_68').remove();
	$('<input type="email" value="" class="text mandatory" id="ctrl_68" required="required" name="email" />').insertAfter('form#f11 label[for=ctrl_68]');
	$("form#f11").validator({
					lang: 'pl',
					position: 'top right',
					offset: [0, -240]
					});

}
// formularz "Zaproponuj książkę"
if ($('form#f10').length) {
	var currentYear = (new Date).getFullYear();
	$('form#f10 input#ctrl_60').attr('max', function() {
		return currentYear
	}).attr('min', '1900');
	$('input#ctrl_58,input#ctrl_59').attr('required', 'required');
	$("form#f10").validator({
					lang: 'pl',
					position: 'top right',
					offset: [0, -240]
					});

}
// formularz kontaktowy
if ($('form#f8').length) {
	$('input#ctrl_50').remove();
	$('<input type="email" value="" class="text mandatory" id="ctrl_50" required="required" name="email" />').insertAfter('form#f8 label[for=ctrl_50]');	
	$('input#ctrl_49,textarea#ctrl_51').attr('required', 'required');
	$("form#f8").validator({
					lang: 'pl',
					position: 'top right',
					offset: [0, -240]
					});

}
}

function selectZapytaj() {
	if ($('form#f9 label[for=ctrl_54]').next('p.error').length) {
		$('form#f9 select#ctrl_54').remove();
		$('<input type="text" value="" class="text innyinput mandatory" id="ctrl_54" required="required" name="tytul" /><span class="usun">usuń</span>').insertAfter($('form#f9 label[for=ctrl_54]').next('p.error'));
	}else{
		$('form#f9 select#ctrl_54').remove();
		$('<input type="text" value="" class="text innyinput mandatory" id="ctrl_54" required="required" name="tytul" /><span class="usun">usuń</span>').insertAfter('form#f9 label[for=ctrl_54]');
	}
}

function zamienNaSelect(wybory) {
	$("form#f9 span.usun").click(function(){
			//console.log($('form#f9 label[for=ctrl_54]'));
			$('form#f9 input#ctrl_54').remove();
			$('form#f9 span.usun').remove();
			if ($('form#f9 label[for=ctrl_54]').next('p.error').length) {
				$('form#f9 label[for=ctrl_54]').next('p.error').remove();
				$(wybory).insertAfter('form#f9 label[for=ctrl_54]');
			}else{
				$(wybory).insertAfter('form#f9 label[for=ctrl_54]');			
			}
			$('form#f9 select#ctrl_54 option:eq(0)').attr("selected", "selected");
	});
}

