/* 
Coder: VC
Date: June 2009
Notes: JS for gallery. 
Requires: JQuery
 */
var total_gallery_items = 0;
var total_gallery_photo_items = 0;

$(function() {

	//Add carousel if more than 3
	total_gallery_items = jQuery('#image_listing li').length;
	total_gallery_photo_items = jQuery('#image_listing li .thumblink').length;
	
	if(total_gallery_items > 3){
		jQuery('<div id="gallery-scroller"><a href="#" class="prev">Prev</a><a href="#" class="next">Next</a><p class="info">1-3 of '+total_gallery_items+'</p></div>').
		insertBefore('#image_listing');
		jQuery('#image_listing').wrap('<div class="carousel_wrapper"></div>');
		setbrowsecount(jQuery('#image_listing li'));
		
		jQuery(".carousel_wrapper").jCarouselLite({
			btnNext: "#gallery-scroller .next",
			btnPrev: "#gallery-scroller .prev",
			vertical: false,
			scroll: 3,
			//scroll: 1,
			//circular: false,
			visible: 3,
			beforeStart:function(a){
				tb_remove();
				jQuery('a.thickbox, area.thickbox, input.thickbox').unbind();
			},
			afterEnd: function(a) {
				tb_init('a.thickbox, area.thickbox, input.thickbox');
				setbrowsecount(a);
			}
		});
	};

	//horrid hack alert: manually reposition thickbox absolutely, so the page can be scrolled to see the extra comments below the picture.
	function repositionThickbox(){
		offset=$('#TB_window').offset();
		$('#TB_window').css({position : 'absolute', marginTop: offset.top, top:'50px'});
	}
	
	$('#image_listing a.thickbox').unbind();	
	tb_init('#image_listing a.thickbox', repositionThickbox);
	imgLoader = new Image();
	imgLoader.src = "/display_images/thickbox_loading.gif";
	
});



function setbrowsecount(firstli){
	var firstind = parseInt(jQuery(firstli).attr('class').split('_')[1]);
	var lastind = firstind + 2;
	if(lastind > total_gallery_items){
		lastind = lastind - total_gallery_items;
	};
	var firstphotoind = jQuery('.gallerycol_'+firstind).find('.thumblink img').attr('class').split('_')[1];
	var lastphotoind = parseInt(jQuery('.gallerycol_'+lastind).find('.thumblink img').attr('class').split('_')[1]);
	if(lastphotoind < total_gallery_photo_items){
		lastphotoind = lastphotoind+1;
	};
	
	jQuery('#gallery-scroller .info').text(firstphotoind + '-' + lastphotoind + ' of ' + total_gallery_photo_items);

}
