function galleryImage(idx) {
    $('#big-image-' + visible_idx).fadeOut('fast', function() {
    	$('#big-image-' + idx).fadeIn('slow');
    	makeSmallActive(idx);
    	makeSmallInactive(visible_idx);
    	$('#download-link').attr({
    		'href' : 'http://bastiansick.stijlroyal.de/download.php?filename='+$('#big-image-' + idx).attr('ref')+'&as='+$('#big-image-' + idx).attr('rel')
    	});
    	visible_idx = idx;
    	window.location.hash = '#' + idx;
    });    
}

function makeSmallActive(idx) {
    $('#small-image-' + idx).attr({
    	'onmouseover': "this.src='" + $('#small-image-' + idx).attr('rel') + "'",
    	'onmouseout': "this.src='" + $('#small-image-' + idx).attr('rel') + "'",
    	'src': $('#small-image-' + idx).attr('rel')
    });
    
    $('.image_descriptions').hide();
    $('#image-description-' + idx).show();
}

function makeSmallInactive(idx) {
    $('#small-image-' + idx).attr({
    	'onmouseover': "this.src='" + $('#small-image-' + idx).attr('rel') + "'",
    	'onmouseout': "this.src='" + $('#small-image-' + idx).attr('ref') + "'",
    	'src': $('#small-image-' + idx).attr('ref')
    });
}

function galleryImagePrev() {
	if (visible_idx > 1) {
		galleryImage(visible_idx - 1);
		$('div.scrollWrapper').scrollTo($('#small-image-' + (visible_idx - 1)), {offset: {top: 0, left: -50}, axis: 'x'});
	} else {
		galleryImage(max_idx);
		$('div.scrollWrapper').scrollTo($('#small-image-' + max_idx), 400, {offset: {top: 0, left: -50}, axis: 'x'});
	}
}

function galleryImageNext() {
	if (visible_idx < max_idx) {
		galleryImage(visible_idx + 1);
		$('div.scrollWrapper').scrollTo($('#small-image-' + (visible_idx + 1)), 400, {offset: {top: 0, left: -50}, axis: 'x'});
	} else {
		galleryImage(1);
		$('div.scrollWrapper').scrollTo($('#small-image-1'), 400, {offset: {top: 0, left: -50}, axis: 'x'});
	}
}

function showCompleteText(entryId) {
	$('#textlink-' + entryId).hide();
	$('#text-' + entryId).slideDown('slow');
}

function hideCompleteText(entryId) {
	$('#text-' + entryId).slideUp('slow', function() {$('#textlink-' + entryId).show();});
}

jQuery.fn.extend({
	highlight: function(search, insensitive, hls_class){
		var regex = new RegExp("(<[^>]*>)|(\\b"+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +")", insensitive ? "ig" : "g");
		return this.html(this.html().replace(regex, function(a, b, c){
			return (a.charAt(0) == "<") ? a : "<span class=\""+ hls_class +"\">" + c + "</span>";
		}));
	}
});

$(document).ready(function() {
    $.localScroll({
    	hash: true,
    	margin: true,
    	duration: 800,
    	easing: 'swing'
    });
    
    $('a.fancybox').fancybox({
    	'title': $($(this).children()[0]).attr('alt'),
    	'titlePosition': 'inside',
    	'overlayColor': '#000',
    	'overlayOpacity': 0.7
    });
});
