
/**
 * Activates the carousel belonging to a gallery
 * IMPORTANT: IF intended to interact with a gallery, the 
 * activate-gallery.js script MUST have been loaded PRIOR to this one.
 * @author lennart@neofonie.de
 */
$(function () {
	
	// init carousel..
	$(".scrollable").scrollable({
		items: '#carousel_items',
		horizontal: true,
		loop: false,
		inactiveClassNext: 'X',
		inactiveClassPrev: 'X'
	});

	// disable 'prev' button..
	//$('#scrollable_prev').addClass('inactive_prev');
	
	var IMG_TOP_DISTANCE = 40,
		HALF_ITEMS_DIV_HEIGHT =  Math.ceil( $('#carousel_items').height() / 2);

	// handler assigned to img+span (if existing)
	var hoverHandler = function() {
			var img = this.parentNode.__img;
			img.style.marginTop = this.parentNode.__overtopmargin;
			img.className = 'scrlHover';

			this.parentNode.style.width = 'auto';
			return false;
		},

		// assigned to parent A of img+span
		outHandler = function() {
			this.__img.className = '';
			this.style.width = '';

			if ( (this.className||'').indexOf('active_over')>=0) return;
			this.__img.style.marginTop = this.__deftopmargin;
		};

	// init carousel images (hover size behavior)..
	$('div#carousel_items a img').each(function () {
		var	halfImgHeight = Math.ceil(this.height / 2),
			defTopMargin = (HALF_ITEMS_DIV_HEIGHT - halfImgHeight - IMG_TOP_DISTANCE),	// default margin-top
			overTopMargin = (defTopMargin - 12) + 'px';	// margin-top on mouseover

		defTopMargin += 'px';
		this.parentNode.__deftopmargin = defTopMargin;	// defauilt marginTop
		this.parentNode.__overtopmargin = overTopMargin;	// marginTop on hover
		this.parentNode.__img = this;
		this.style.marginTop = defTopMargin;

		$('>img, >span', this.parentNode).bind('mouseover',hoverHandler);
		$(this.parentNode).bind('mouseout',outHandler);
	});

});
