
window.hbv = window.hbv || {};

/** 
 * Object managing the hbv image gallery
 * Call ::init() *before* DOM load (i.e. inline)
 * @author lennart@neofonie.de
 */
hbv.Gallery = {

	currentIndex: 0,
	ITEM_COUNT: $('#page_img').children('img').length,
	
	jNext: 0,		// next image button
	jPrev: 0,		// prev image button
	jNumCurrent: 0,	// span for current image#
	jNumLast: 0,	// span for total # of images

	/**
	 * Initializes the gallery.
	 * To be called on page load
	 * @param bool pageReady (opt) indicates that init has been invoked after the DOM is ready
	 * 
	 */
	init: function(pageReady) {
		
		// called before page loaded entirely?
		if (!pageReady) {
			$('#page_next').text(this.ITEM_COUNT);
			$( function(){hbv.Gallery.init(1);} ); // schedule second call for 'after DOMready"
			return;
		}
		
		var me = this;

		// bind next button..
		this.jNext = $('#next').bind('click', function() {
			hbv.Gallery.next();
			return false;
		});
		
		// bind prev button..
		this.jPrev = $('#prev').bind('click', function() {
			hbv.Gallery.prev();
			return false;
		});

		this.jNumCurrent = $('#page_prev');
		this.jNumLast = $('#page_next');

		// current is set in template hbv.galleries_master, reload Ad-Tags
		if (current != null) {
			this.setCurrent((current));
		} else {
			this.setCurrent(0);
		}
	},

	/**
	 * Switches to the next image..
	 */
	next: function() {
		this.currentIndex = (this.currentIndex+1) % this.ITEM_COUNT;
		this.setCurrent(this.currentIndex);
	},

	/**
	 * Switches to the previous image..
	 */
	prev: function() {
		if (--this.currentIndex<0) this.currentIndex = this.ITEM_COUNT-1;
		this.setCurrent(this.currentIndex);
	},


	/**
	 * Switches the gallery to the specified picture.
	 * @param number index (0..ITEM_COUNT-1)
	 */
	setCurrent: function(index) {
		ivw_fliege(index);
		this.currentIndex = index;

		/*
		// check prev button..
		if (index==0)
			this.jPrev.addClass('inactive_prev');
		else 
			this.jPrev.removeClass('inactive_prev');

		// check next button..
		if (index==this.ITEM_COUNT-1)
			this.jNext.addClass('inactive_next');
		else 
			this.jNext.removeClass('inactive_next');
		*/

		// update page number
		this.jNumCurrent.text(index+1);

		// hide all pics+subtitles
		$('#page_img img, #page_img small').addClass('hide');

		// reveal current pic+subtitle
		$('#page_img img:eq('+index+'), #page_img img:eq('+index+')+small').removeClass('hide');
		$('#page_subtitle > div').addClass('hide').filter(':eq('+index+')').removeClass('hide');

		// update big image's width..
		var w = $('#page_img img:eq('+index+')').attr('width');
		$('#page_img').css('width', w);

		// update carousel...
		$("#carousel_items a").removeClass('active').filter(':eq('+index+')').addClass('active');
	},

	/**
	 * Returns true if the gallery's showing the first image
	 * Used by scrollable.js -> ::next()
	 */
	atBegin: function() {
		return (this.currentIndex==0);
	},


	/**
	 * Returns true if the gallery's showing the last image
	 * Used by scrollable.js -> ::prev()
	 */
	atEnd: function() {
		return (this.currentIndex==this.ITEM_COUNT-1);
	}

}

function ivw_fliege(index) {
	var ivw = new Array('/media/img/trans.gif', '2', '3', '4', '5', '6', '7', '8', '9');
	if (document.getElementById('ivw')) {
		document.getElementById('ivw').src = ivw[index];
	}
}



// init the gallery...
hbv.Gallery.init();

// check the current position and sets the reload link
function reloadAds() {
		adReloadCount += 1;
		if (adReloadCount == 3) {
			var next = 1;
			var prev = 1;
			var url = location.href.replace(/#/, "");
			
			
			if ((hbv.Gallery.currentIndex+2) > hbv.Gallery.ITEM_COUNT) {
				next = 1;
			} else {
				next = (hbv.Gallery.currentIndex+2);
			}
			if ((hbv.Gallery.currentIndex) == 0) {
				prev = hbv.Gallery.ITEM_COUNT;
			} else {
				prev = (hbv.Gallery.currentIndex);
			}
			
			if (location.href.match(/\?/)) {
				hrefNext = url.replace(/\?.*/, "?i="+(next));
				hrefPrev = url.replace(/\?.*/, "?i="+(prev));
			} else {
				hrefNext = url+"?i="+(next);
				hrefPrev = url+"?i="+(prev);
			}
			$('#next').unbind('click');
			$('#next').attr("href", hrefNext);
			$('#prev').unbind('click');
			$('#prev').attr("href", hrefPrev);
		}
}

