Shadowbox.init({
	animate:			true,
	animSequence:		"wh",
	overlayColor:		"#ffffff",
    language:			"en",
    overlayOpacity:		"1",
    viewportPadding:	"15",
    continuous:			true,
    onFinish:			function(){ checkImageInLightbox(); jQuery('#sb-body-inner img').one('click',Shadowbox.next); },
	onOpen:				function(){ checkImageInLightbox(); },
	onChange:			function(){ checkImageInLightbox(); }
});

function ajaxToGallery(id, action, permalink, path, logo){	
	jQuery.getJSON(THICKBOX2PDF_URL + 'addToGallery.php', { id: id, action: action, permalink: permalink, path: path, logo: logo }, function(json){
		jQuery(".lbCounter").text('(' + json.count + ')');
		if(json.result == 'removed'){
			if(json.id == 'all' || json.count == 0){
				jQuery("#content").html('<div class="hentry p1 page publish author-admin category-uncategorized untagged comments-open pings-open y2010 m02 d10 h16 slug-lightbox" id="post-2"><h1 class="entry-title">Lightbox</h1><div class="entry-content"><div id="" class="ngg-galleryoverview"><p>you have no images in your lightbox!</p></div></div></div>');
			} else {
				jQuery("#ngg-image-" + json.id).remove(); // remove the single post
			}
		}
		if(json.result == 'added'){
			jQuery("#sb-nav-lightbox").text('added to lightbox');
		}
		lightbox_session = json.ids;
	});
}

function addToGallery(id, permalink, path, logo){
	ajaxToGallery(id, 'add', permalink, path, logo);
}

function removeFromGallery(id){
	ajaxToGallery(id, 'remove');
}

function addFromShadowbox(elm){
	var image_abs_path = jQuery('#sb-player').attr('src');
	var lightbox_element = lightbox_json[image_abs_path];
	addToGallery(lightbox_element[0], lightbox_element[1], lightbox_element[2], lightbox_element[3]);
}

function checkImageInLightbox(){
	var image_abs_path = jQuery('#sb-player').attr('src');
	var lightbox_element = lightbox_json[image_abs_path];
	
	// return if image could not be found
	if(lightbox_element == undefined){
		jQuery("#sb-nav-lightbox").text('add to lightbox');
		return;
	}
	
	// get image id
	var image_id = lightbox_element[0];

	// search within the images in the session
	if(lightbox_session != null && lightbox_session[image_id] != undefined){
		jQuery("#sb-nav-lightbox").text('added to lightbox');
	} else {
		jQuery("#sb-nav-lightbox").text('add to lightbox');
	}
}

