Cufon.set('fontFamily', 'MetaBookCE-Roman', 'fontStyle', 'normal').replace(
		'.box-side h3');
Cufon.replace('.nagl-page');
Cufon
		.replace('#h_aktualnosci span,.galeria-kultura h3,.dzieci-r-box h3,.box-m-lp h4');

Cufon.replace('.bemowo #sub-nav a span', {
	color : '#ffffff',
	hover : true,
	hoverables : {
		span : true
	},
	hover : {
		color : '#ffa245'
	}
});
Cufon.replace('.administracja #sub-nav a span', {
	color : '#ffffff',
	hover : true,
	hoverables : {
		span : true
	},
	hover : {
		color : '#ff8d39'
	}
});
Cufon.replace('.kultura #sub-nav a span', {
	color : '#ffffff',
	hover : true,
	hoverables : {
		span : true
	},
	hover : {
		color : '#ff8d39'
	}
});
Cufon.replace('.sport #sub-nav a span', {
	color : '#ffffff',
	hover : true,
	hoverables : {
		span : true
	},
	hover : {
		color : '#ff8d39'
	}
});
Cufon.replace('.dzieci #sub-nav a span', {
	color : '#ffffff',
	hover : true,
	hoverables : {
		span : true
	},
	hover : {
		color : '#fdb158'
	}
});
Cufon.replace('.zdrowie #sub-nav a span', {
	color : '#ffffff',
	hover : true,
	hoverables : {
		span : true
	},
	hover : {
		color : '#fdb158'
	}
});

function initInputLabelFromInput(input_id) {
	var text = $(input_id).val();
	$(input_id).val(text);
	$(input_id).focus(function() {
		if ($(this).val() == text) {
			$(this).addClass('focus');
			$(this).val("");
		}
	});
	$(input_id).blur(function() {
		if ($(this).val() == '') {
			$(this).removeClass('focus');
			$(this).val(text);
		}
	});
}

function gallery() {
	var prev_img = null;
	var next_img = null;
	var _preview = $("#gallery_preview");

	$("ul.nav-sort a.a_prev", _preview).click(function(e) {
		e.preventDefault();
		selectImg(prev_img);
	});
	$("ul.nav-sort a.a_next", _preview).click(function(e) {
		e.preventDefault();
		selectImg(next_img);
	});

	var selectImg = function(nr) {
		if (nr === null)
			return false;
		$("ul.list-gallery li > div").each(function(i, el) {
			var _el = $(el);
			if (nr == $("a", _el).attr('nr')) {
				document.location.hash = nr;
				_el.addClass('hover');
				var _src = $("a", _el).attr('href');
				if ($("img", _preview).attr('src') == _src)
					return false;
				var desc = $("img", _el).attr('alt');

				setSibling(nr);
				$("img", _preview).stop().fadeTo(200, 0, function() {
					$(this).attr('data-nr', nr).attr('data-fullsize', $("a", _el).attr('data-fullsize'));					
					$("p", _preview).text(desc);
					$(this).attr('src', _src).load(function() {
						$(this).stop().fadeTo(200, 1);
					});
				});
			} else {
				_el.removeClass('hover');
			}
		});
	};

//old gallery
//	$("ul.list-gallery div a").each(function(i) {
//		$(this).attr('nr', i);
//	}).click(function(event) {
//		event.preventDefault();
//		selectImg($(this).attr('nr'));
//	});
	$("ul.list-gallery div a").each(function(i) {
		$(this).attr('nr', i);
	})

	function setSibling(nr) {
		if (nr == 0)
			$("ul.nav-sort a.a_prev", _preview).addClass('disabled');
		else {
			$("ul.nav-sort a.a_prev", _preview).removeClass('disabled');
			prev_img = nr - 1;
		}

		if (nr == $("ul.list-gallery li > div").length - 1)
			$("ul.nav-sort a.a_next", _preview).addClass('disabled');
		else {
			$("ul.nav-sort a.a_next", _preview).removeClass('disabled');
			next_img = nr * 1 + 1;
		}
	}
	var currentHash = '';
	var selectImgByHash = function() {
		currentHash = document.location.hash;
		var hash = currentHash;
		if (hash != '' && hash != '#0') {
			if (hash.charAt(0) == '#')
				hash = hash.substr(1);
			selectImg(hash);
		} else {
			hash = '';
			selectImg(0);
			}
		
	};

//old gallery
//	setInterval(function() {
//		if (currentHash != document.location.hash) {
//			selectImgByHash();
//		}
//	}, 500);
//
//	if (document.location.hash != '' && document.location.hash != '#0')
//		selectImgByHash();
//	else
//		setSibling(0);
}

function initDateFilter(from, to, url) {
	$(document).ready(function() {
		dateFilter(from, to, url);
	});
	function dateFilter() {
		var li = $("<li>");
		li.append("<a><span>Ograniczone czasowo</span></a>");
		var dod = $("<input>");
		dod.attr("type", "text");
		var ddo = dod.clone();
		dod.val(from).attr("id", "dod");
		ddo.val(to).attr("id", "ddo");
		myDate(dod);
		myDate(ddo);
		li.append(dod.wrap("<label>")).append(ddo.wrap("<label>"));
		$("#dateFilter").append(li);
	}

	function myDate(el) {
		$(el).datepicker( {
			dateFormat : 'yy-mm-dd',
			onClose : function(val) {
				var fod = $("#dod").val().replace("-", "").replace("-", "");
				var fdo = $("#ddo").val().replace("-", "").replace("-", "");
				location.href = url + 'date:' + fod + '-' + fdo;
			}
		});
	}
}

function tabInit() {
	$(document).ready(function() {
		sTab();
	});
}
function sTab() {
	var getHash = function(lnk) {
		var href = lnk.attr('href');
		var pos = href.indexOf('#');
		if (pos > -1 && href.length > pos + 1) {
			return href.substr(pos + 1);
		}
		return '';
	};

	var selectTab = function(tab, noHash) {
		var _this = $(tab);
		$('.tab-menu > li.active').add('.tab-content > div.active')
				.removeClass('active');
		_this.addClass('active');
		var tabid = _this.attr('tab');
		var cla = ".tab-content > div[tab=" + tabid + "]";
		$(cla).addClass('active');

		if (!noHash) {
			var lnk = _this.find('a');
			var hash = getHash(lnk);
			if (hash.length > 0)
				document.location.hash = hash;
		}
	};

	$('.tab-menu li').each(function(i) {
		$(this).attr('tab', i);
	}).click(function(event) {
		event.preventDefault();
		selectTab(this);
	});

	$('.tab-content > div').each(function(i) {
		$(this).attr('tab', i);
	});

	var tmp = $(".tab-menu li.active");
	if (tmp.length) {
		tmp.attr('defaultTab', '1');
	} else {
		tmp = $(".tab-menu li").eq(0).attr('defaultTab', '1');
	}
	// selectTab(tmp, true);

	var currentHash = '';
	var selectTabByHash = function() {
		currentHash = document.location.hash;
		var hash = currentHash;
		if (hash == '') {
			var tab = $(".tab-menu li[defaultTab=1]");
			// if (!tab.hasClass('active'))
			selectTab(tab, true);
		}
		if (hash.charAt(0) == '#')
			hash = hash.substr(1);
		if (hash.length > 0) {
			$(".tab-menu li").each(function() {
				var _this = $(this);
				var lnk = _this.find('a');
				var _hash = getHash(lnk);
				if (_hash.length > 0 && _hash == hash)
					selectTab(_this, true);
			});
		}
	};

	setInterval(function() {
		if (currentHash != document.location.hash) {
			selectTabByHash();
		}
	}, 500);

	selectTabByHash();
}

function initPrint() {
	$("#print").click(function() {
		window.print();
		return false;
	});
}

function initBack() {
	$("#back").click(function() {
		history.back();
		return false;
	});
}

function initUp() {
	$('#up').click(function() {
		$.scrollTo($("#branding"), 1000);
		return false;
	});
}

function fbBox(){
	$('#fb-box-button').toggle(function(){
		$('#fb-box').animate({
			left: 0
		});
	},
	function(){
		$('#fb-box').animate({
			left: -250
		});
	});
	
}

function initialize() {
	initInputLabelFromInput("#search");
	initInputLabelFromInput(".newsletter .inp input");
	initPrint();
	initUp();
	initBack();
	gallery();
	$('.lightbox').lightBox();
	initModalGallery()
	fbBox();
}
$(document).ready(initialize);






if (!Object.keys) {
    Object.keys = function (obj) {
        var keys = [],
            k;
        for (k in obj) {
            if (Object.prototype.hasOwnProperty.call(obj, k)) {
                keys.push(k);
            }
        }
        return keys;
    };
}

function ModalBox(className){
	this.container = null;
	this.overlay = null;
	var _this = this;
	var create = function(){
		_this.overlay =  $('<div></div>').addClass('modalBox-overlay').bind('click', function(){
			_this.hide.call(_this);
		});		
		_this.container = $('<div></div>').addClass('modalBox-container');
		if(className){
			_this.overlay.addClass(className);
			_this.container.addClass(className);
		}
	}
	create();
}
ModalBox.prototype.show = function(){
	this.overlay.appendTo($(document.body)).show();
	this.container.appendTo($(document.body)).show();
	return this;
}
ModalBox.prototype.refreshPosition = function(){
	var windowHeight = $(window).height();
	var boxHeight = $(this.container).outerHeight();

	//if(boxHeight > windowHeight){
		topPosition = 20;
		position = 'absolute';
		
	//}else{
	//	topPosition = Math.round((windowHeight - boxHeight)/2);
		//topPosition = Math.round((windowHeight - 670)/2);
		//position = 'fixed';
	//	position = 'absolute';
	//}
	$(this.container).css('position', position);
	$(this.container).animate({
		top: topPosition
	}, 0);
	return this
}
ModalBox.prototype.setContent = function(content){
	$(this.container).empty().html(content); 
	this.refreshPosition();
	$(window).scrollTop(0);
	return this
}

ModalBox.prototype.destroy = function(){
	$(this.overlay).remove();
	$(this.container).remove();
}
ModalBox.prototype.hide = function(){
	$(this.overlay).hide();
	$(this.container).hide();
}
ModalBox.prototype.setZIndex = function(index){
	$(this.overlay).css('z-index',index);
	$(this.container).css('z-index',index+1);
} 

window.MB = ModalBox;


var ModalGallery = function(){
	this.modalBox = new ModalBox('modal-gallery');
	this.container = null;
	this.imageWrapper = null;
	this.image = null;
	this.thumbsList = null;
	this.thumbs = {};
	this.currentNumber = 0;
	
	var _this = this;
	function create(){
		_this.container = $('<div></div>').addClass('modalGallery-container');
		_this.imageWrapper = $('<div />').addClass('modalGallery-image-wrapper').appendTo(_this.container);
		_this.image = $('<img />').addClass('modalGallery-image').appendTo(_this.imageWrapper);
		_this.thumbsList = $('<ul />').addClass('modalGallery-thumbs').appendTo($('<div />').addClass('modalGallery-thumbs-wrapper').appendTo(_this.container));
		_this.nextB = $('<span />').addClass('next').click($.proxy(_this.next, _this)).appendTo($(_this.imageWrapper));
		_this.prevB = $('<span />').addClass('prev').click($.proxy(_this.prev, _this)).appendTo($(_this.imageWrapper));
		
	}
	create();

}
ModalGallery.prototype.show = function(){
	this.modalBox.show().setContent(this.container);
}
ModalGallery.prototype.next = function(){
	if(this.currentNumber < Object.keys(this.thumbs).length-1){
		this.changeImg(parseInt(this.currentNumber)+1);	
	}
}
ModalGallery.prototype.prev = function(){
	if(this.currentNumber > 0){
		this.changeImg(parseInt(this.currentNumber)-1);	
	}
}
ModalGallery.prototype.showImage = function(src){
	$(this.image).attr('src', src);
//	this.imageWrapper.animate({
//		'height': $(this.image).height()+'px'
//	},200);
}
ModalGallery.prototype.renderThumbs = function(){
	var _this = this;
	$.each(this.thumbs, function(k,v){
		var li = $('<li />').attr('data-fullsize', v.fullSize).attr('data-number', k);
		$('<img />').attr('src', v.src).appendTo(li);
		li.appendTo(_this.thumbsList);
	});
}
ModalGallery.prototype.loadThumbs = function(thumbs){
	this.thumbs = thumbs;
	this.renderThumbs();
	var _this = this;

	$(this.thumbsList).find('li').bind('click', function(){
		_this.changeImg($(this).attr('data-number'));
	})
}
ModalGallery.prototype.changeImg = function(number){
	this.currentNumber = number;
	this.showImage(this.thumbs[number].fullSize);
	if(number == 0){
		this.prevB.hide();
	}else{
		this.prevB.show();
	}
	if(number == Object.keys(this.thumbs).length-1){
		this.nextB.hide();
	}else{
		this.nextB.show();
	}	
	this.changeThumbs();
	//this.modalBox.refreshPosition();
}
ModalGallery.prototype.changeThumbs = function(){
	var all = $(this.thumbsList).find('li');
	var current = all.filter('li[data-number="'+this.currentNumber+'"]');
	all.removeClass('current');
	current.addClass('current');
	this.refreshThumbsPosition();
}
ModalGallery.prototype.refreshThumbsPosition = function(){
	var leftPosition = (this.currentNumber - 3)*104;
	

	if(!(this.currentNumber < 3 || this.currentNumber >= (Object.keys(this.thumbs).length) - 3)){
		$(this.thumbsList).animate({
			left: -leftPosition
		});		
	}

}

window.MG = ModalGallery;

function initModalGallery(){


	$('.list-gallery a.img').bind('click', function(e){
		e.preventDefault();
		e.stopPropagation();
		window.modalGallery = modalGallery = new ModalGallery();
		var thumbs = {};
		$('.list-gallery a.img').each(function(k,v){
			var thumb = {
					src: $(v).find('img').attr('src'),
					fullSize: $(v).attr('data-fullsize')
			}
			thumbs[$(v).attr('nr')] = thumb;
		});
		modalGallery.loadThumbs(thumbs);

		modalGallery.changeImg($(this).attr('nr'));
		modalGallery.show();
	});
}

