var RawCarousel = {
	foundCarousel: false,
	carousels: [
		{
			id: '#promosList',
			minimum: 4
		},
		{
			id: '#galleryList',
			minimum: 4
		}

		
	],
	init: function(){
		for (i in this.carousels)
		{
			// check for carousel item and if it is available add the carousel widget
			this.carousels[i].Items = glow.dom.get(this.carousels[i].id);
			// only add the carousel widget if there is at least the minimum number of items 
			if (this.carousels[i].Items.length != 0 && this.carousels[i].Items.children().length >= this.carousels[i].minimum)
			{
				this.carousels[i].added = true;
				this.carousels[i].Carousel = new Carousel(this.carousels[i].id, {loop: true} );
				this.carousels[i].Items.before('<a class="control prev" href="#" title="previous promo" onclick="return RawCarousel.previous('+ i +');"><span class="arrow"><span></span></span></a>');
				this.carousels[i].Items.after('<a class="control next" href="#" title="next promo" onclick="return RawCarousel.next('+ i +');"><span class="arrow"><span></span></span></a>');
				//glow.events.addListener(this.carousels[i].Items.parent(), 'mouseover', function() { RawCarousel.resize(); });
				this.foundCarousel = true;
			}			
		}
		this.resize();
	},
	previous: function(itemNo)
	{
		this.carousels[itemNo].Carousel.move(1);
		return false;
	},
	next: function(itemNo)
	{
		this.carousels[itemNo].Carousel.move(-1);
		return false;
	},
	resize: function(){				

		if (this.foundCarousel)
		{
			for (i in this.carousels)
			{				
				if (this.carousels[i].added)
				{				
					var maxHeight = 0;
					var children = this.carousels[i].Items.children();
					var topElementsChildren = this.carousels[i].Items.parent().children();
					
					// reset default heights
					for (j = 0; j<children.length; j++)
					{
						var thisElem = children.slice(j,j+1).children().slice(0,1);
						thisElem.css('min-height','');
					}
								
					for (j = 0; j < topElementsChildren.length; j++) {
						var thisElem = topElementsChildren.slice(j,j+1);
						thisElem.css('height',''); // set height of promo
					}
					
					// get max height of children
					for (j=0; j<children.length; j++)
					{
						var thisHeight = children.slice(j,j+1).height();
						maxHeight = (thisHeight > maxHeight)?thisHeight:maxHeight;
					}
					
					// set min-height of a tags
					for (j = 0; j<children.length; j++)
					{
						var thisElem = children.slice(j,j+1).children().slice(0,1);
						thisElem.css('min-height',maxHeight + 'px'); // set min-height of <a>
					}

					// set height of left/right arrows and promos container					
					for (j = 0; j<topElementsChildren.length; j++)
					{
						var thisElem = topElementsChildren.slice(j,j+1);
						var borderHeight = parseInt(thisElem.css('border-top-width')) + parseInt(thisElem.css('border-bottom-width'));
						var marginTop = thisElem.css('margin-top');
						var marginBottom = thisElem.css('margin-bottom');
						var marginHeight = (marginTop.match(/\d+/) ? parseInt(marginTop) : 0) + (marginBottom.match(/\d+/) ? parseInt(marginTop) : 0);
						var setHeight = maxHeight - borderHeight - marginHeight;
						thisElem.css('height',setHeight + 'px'); // set height of promo
					}
					
					
				}
			}			
		}
	}
}

bbcjs.addOnLoadItem(function(){
	glow.ready(function(){
		RawCarousel.init();
	});
});
