$(document).ready(function(){
	$('.comicBox li.prevComic a').bind('click', Comic.prev);
	$('.comicBox li.nextComic a').bind('click', Comic.next);
	Comic.init();
});

var Comic = {
	allLoaded:		false,
	data: 			null, 
	history:		[],
	historyPos:		0,
	photoBaseURL:	Config.siteurl+'/i/comix',
	
	init: function() {
		Comic.photos = homepageComix; 
		Comic.history.push(Comic.photos.shift());
	},
	
	navi: function() {
		if(Comic.photos.length == 0) {
			if(typeof Comic.history[Comic.historyPos+1] != 'undefined') {
				var next = Comic.history[Comic.historyPos+1];
				
				if($('.comicBox .nextComic a').length > 0) {
					$('.comicBox .nextComic a').attr('href', '#comic-'+next.uid);
				} else {
					$('.comicBox .nextComic').html('<a href="#comic-'+next.uid+'">Następny</a>').bind('click', Comic.next);
				}
				
			} else {
				$('.comicBox .nextComic').html('<span>Następny</span>');
			}
		}
		
		if(Comic.historyPos == 0) {
			$('.comicBox .prevComic').html('<span>Poprzedni</span>');
		} else {
			var prev = Comic.history[Comic.historyPos-1];
			if($('.comicBox .prevComic a').length > 0) {
				$('.comicBox .prevComic a').attr('href','#comic-'+prev.uid);
			} else {
				$('.comicBox .prevComic').html('<a href="#comic-'+prev.uid+'">Poprzedni</a>').bind('click', Comic.prev);
			}
		}
	},
	
	next: function() {
		
		var dt = Comic.getFromHistory('next');
		
		if(dt == null) {
			if(dt = Comic.photos.shift()) {
				Comic.history.push(dt);
				Comic.historyPos = Comic.history.length - 1;
			}
		}
		
		if(!dt) {
			Comic.navi();
			return false;
		}
		
		$('#comicImage').fadeOut(300, function(){
			$('#comicLink').attr('href', Config.siteurl+'/gruba-kreska/komiks/'+dt.uid);
			$('#comixPreview').attr('src', dt['img_p']);
			$(this).attr('src', dt.img).fadeIn(300);
		})
		
		if(Comic.photos.length < 2 && Comic.allLoaded == false) {
			Comic.loadData(dt);
		}

		Comic.navi();
		return false;
	},
	
	prev: function() {
		var dt = Comic.getFromHistory('prev');
		
		if(dt == null) {
			Comic.navi();
			return false;
		}
		
		$('#comicImage').fadeOut(300, function(){
			$('#comicLink').attr('href', Config.siteurl+'/gruba-kreska/komiks/'+""+dt.uid);
			$('#comixPreview').attr('src', dt['img_p']);
			$(this).attr('src', dt.img).fadeIn(300);
		});
		
		Comic.navi();
		return false;
	},
	
	getFromHistory: function(s) {
		var s = s || 'next';
		if(s == 'next' && typeof Comic.history[Comic.historyPos+1] != 'undefined') {
			return Comic.history[++Comic.historyPos];
		}
		else if(s == 'prev' && typeof Comic.history[Comic.historyPos-1] != 'undefined') {
			return Comic.history[--Comic.historyPos];
		}
		return null;
	},
	
	loadData: function(dt) {
		var dt = Comic.photos[Comic.photos.length-1] || dt || null;
		if(dt) 
		{
			$.ajax({
	            type:       'POST',
	            url:        Config.siteurl+'/comix/get-home-comix',
	            data:       {last:dt.created},
	            dataType:   'json',
	            error:      Comic._processResponse,
	            success:    Comic._processResponse
	        });
        }
	},
	
	_processResponse: function(data, status) {
		if(status == 'success') {
			if(data == '0') {
				Comic.allLoaded = true;
				return true;
			}
			var t;
			for(var i = 0; i < data.length; i++) {
				if(jQuery.inArray(data[i], Comic.photos) == -1) {
					data[i]['uid']   = Common.castToString(data[i]['uid']);
					data[i]['img']   = Comic._getPhotoURL(data[i], 'small');
					data[i]['img_p'] = Comic._getPhotoURL(data[i], 'main');
					Comic.photos.push(data[i]);
					Common.loadImage(data[i]['img']);
					Common.loadImage(data[i]['img_p']);
					console.debug(data[i]);
				}				
			}
		}
	},
	
	_getPhotoURL: function(dt, t) {
		t = t || 'small';
		return Comic.photoBaseURL+'/'+dt.id+'/'+dt.uid+'_'+t+'.jpg';
	}
};