// JavaScript Document

$(document).ready(function () {
	$('#kontakttoggler')
		.click(function () { $('#kontakt').animate({width: '100%'},'fast'); })
		.mouseout(function () { $('#kontakt').animate({width: 0},'fast'); })
		;
//	$('#blog').click( function() {setpointer(335, 46); });
//	$('#film').click( function() {setpointer(552, 46); });
//	$('#internet').click( function() {setpointer(624, 92); });
//	$('#pasja').click( function() {setpointer(744, 56); });
	controller();
});

function setpointer(left,width)
{
	$('#pointer').stop().animate({'left': left, 'width': width });
}

function readHash (tail) {
	if (tail)
		return location.hash.substr(2).split('/');
	else
		return location.hash.substr(2).split('/')[0];
};

function controller() {
	$.post('server/stat.php',{location: location.hash});
	$('#fancy_close').click();
	switch (readHash()) {
		case 'foto':
			setpointer(408,115);
			fadeOut(page.foto.load);
			break;
		case 'pasja':
			setpointer(744, 56);
			fadeOut(page.pasja.load);
			break;
		case 'internet':
			setpointer(624, 92);
			fadeOut(page.internet.load);
			break;
		case 'film':
			setpointer(552, 46);
			fadeOut(page.film.load);
			break;
		case 'blog':
			setpointer(335, 46);
			fadeOut(page.foto.load);
			break;
		default:
			fadeOut(function () { $('<img />').attr({src:"kasia.jpg",id:"introImage"}).hide().load(function () { $(this).fadeIn(); }).appendTo('#contents'); });
			setpointer(0,0);
			break;
	}

}

function fadeOut(fn) {
	$('#contents').fadeOut(function () { $('#contents').empty().show(); fn();});
}

var page = {
	foto : {
		load : function () {
			var h = readHash(true);
			switch (h[1])
			{
				case 'view':
					$.get('server/foto.php',{mode: 'feed',id:h[2]},page.foto.exact,'json');
					break;
				default:
					$.get('server/foto.php',{mode: 'list'},page.foto.list,'json');
					break;
			}
		},
		list : function (data) {
			$.each(data,function (i,el) {
				var tytul = el.tytul.replace(/ - /g,'-').replace(/ /g,'-');
				var line = $('<div />')
					.addClass('line')
					.appendTo('#contents')
					;
				line
					.wrap('<a />')
					.parent()
					.attr({href : '#/blog/view/'+el.id+'/'+tytul})
					.wrap('<div />')
					.parent()
					.css({width:760})
					;
				$('<div />')
					.addClass('date')
					.text(el.dataPublikacji)
					.appendTo(line)
					;
				$('<h2 />')
					.text(el.tytul)
					.appendTo(line)
					;
				line.append(el.krotkiTekst);
				line.find('.date').css({lineHeight: line.height()+'px', height: line.height()});
			});
			$('#contents').wrapInner('<div class="items" />').wrapInner('<div class="scrollable" style="height: 360px" />');
			$('<div class="navi" />').appendTo('#contents');
			$('<a class="next" />').appendTo('#contents');

			$('<a class="prev" />').appendTo('#contents');
			var menuMemory = parseInt($.cookie(location.hash));
			$.fx.off = true;
			$('.scrollable').scrollable({
 		       vertical:true,  
   			   size: 6,
   			   next: 'a.next',
   			   prev: 'a.prev',
					 api : true,
					 clickable : false,
					 onSeek: function () {
						 var pg = this.getIndex();
						 $.cookie(location.hash,pg);
					 }
			}).move(menuMemory);
			$.fx.off = false;
		},
		exact : function (data) {
			var line = $('<div />')
				.addClass('wideline')
				.appendTo('#contents')
				;
//			$('<div />')
//				.addClass('date')
//				.text(data.text.dataPublikacji)
//				.appendTo(line)
//				;
			$('<h2 />')
				.text(data.text.tytul)
				.appendTo(line)
				;
			var content = $('<div />').addClass('content').appendTo('#contents');
//			content.append(data.text.dlugiTekst);
			line.find('.date').css({lineHeight: '30px', height: 30});
			content.append('<br />');
			$.each(data.images,function (i,el) {
				if (i % 4 == 0)
					$('<div class="tile" />').appendTo('.content');
				$('<img />')
					.addClass('thumbnail')
					.attr({src: data.base+el.katalog+'/thumbnail.'+el.plik})
					.appendTo('.content .tile:last')
					.wrap('<a />')
					.parent()
					.addClass('thumbnail')
					.attr({href: data.base+el.katalog+'/'+el.plik, rel:'galeria', title: el.tytul})
					;
			});
			$('.content a[rel]').fancybox();
			if (data.text.dlugiTekst != '') $('<div class="tile" />').css({height:115}).html(data.text.dlugiTekst).insertBefore('.content .tile:first');
//			alert(data.text.dlugiTekst);
			
			content.find('.tile').wrapAll('<div class="items" />').parent().wrap('<div class="scrollable" />');
			$('<div class="navi" />').appendTo('.content');
			$('<a class="next" />').appendTo('.content');
			$('<a class="prev" />').appendTo('.content');
			$('<a class="back" />').attr({href : '#/foto'}).text('powrót').prependTo('.wideline');
			var menuMemory = parseInt($.cookie(location.hash));
			$.fx.off = true;
			$('.scrollable').scrollable({
 		       vertical:true,  
   			   nextPage: 'a.next',
   			   prevPage: 'a.prev',
   			   next: '#123',
   			   prev: '#345',
   			   size: 3,
					 api : true,
					 clickable : false,
					 onSeek: function () {
						 var pg = this.getIndex();
						 $.cookie(location.hash,pg);
					 }
			}).move(menuMemory);
			$.fx.off = false;
		}
	},
	pasja : {
		load : function () {
			$('#contents').load('pasja.htm');
		}
	},
	internet : {
		load : function () {
			$('#contents').load('internet.htm');
		}
	},
	film : {
		load : function () {
			$('#contents').load('film.htm');
		}
	}
}

function stat() {
	$.post('server/stat.php');
}

setInterval('stat()',20000);

var hashListener = {
	hash : location.hash,
	stack : [],
	interval : 10,
	add : function (fn) {
		if (!$.isFunction(fn)) fn = function () {};
		this.stack.push(fn);
	},
	check : function () {
		if (this.hash != location.hash)
		{
			this.hash = location.hash;
			for (i in this.stack)
				this.stack[i]();
		}
	},
	begin : function () {
		setInterval('hashListener.check()',this.interval);
	}
};
hashListener.begin();
hashListener.add(controller);
