jQuery.noConflict();

function autoPositionElements(hash) {
	positionBuyingBox(hash);
	matchTitleHeight(hash);
	matchItemDimensions(hash);
	paginationPosition(hash);	
}

function stopSlider(hash) {
	jQuery('#merchandisingsuite_'+hash).data('scrollable').stop();	
	jQuery('#merchandisingsuite_'+hash).data('scrollable').seekTo(0, 1);
}
function matchItemDimensions(hash) {
	
	//Get the largest items element outer height

	//Auto set each slides width to that of the containers.
	var itemWidth = jQuery('#merchandisingsuite-slides-container_'+hash).outerWidth();
	jQuery('#merchandisingsuite-slides_'+hash+' .merchandisingsuite-slide').each(function(index, child) {
	    jQuery(child).width(itemWidth);
	});

	var itemHeight = 0;
	jQuery('#merchandisingsuite-slides_'+hash+' .merchandisingsuite-slide .outer-padder').each(function(index, child) {
	    if(jQuery(child).outerHeight() > itemHeight) {
	    	itemHeight = jQuery(child).outerHeight();
	    }
	});

	//Apply the largest height to the container and the item elements.
	var slides = jQuery('#merchandisingsuite-slides-container_'+hash+' .merchandisingsuite-slide').size();
	var rows = jQuery('#merchandisingsuite-slides-container_'+hash+' .merchandisingsuite-slide tr').size();
	var rows = (rows/slides);
	var containerHeight = (itemHeight*rows);
	jQuery('#merchandisingsuite-slides-container_'+hash).height(containerHeight);
	jQuery('#merchandisingsuite-slides_'+hash+' .merchandisingsuite-slide .item-wrapper').each(function(index, child) {
		jQuery(child).height(itemHeight);
	});
}

function paginationPosition(hash) {
	
	//Horizontally center pagination element
	var pagination_center = jQuery('#merchandisingsuite-pagination-container'+hash+'.center');
	pagination_center.css('margin-left','-'+(pagination_center.width()/2)+'px');
	
	//Vertically center pagination element
	var pagination_middle = jQuery('#merchandisingsuite-pagination-container'+hash+'.middle');
	pagination_middle.css('margin-top','-'+(pagination_middle.height()/2)+'px');
}

function matchTitleHeight(hash) {
	var titlesHeight = 0;
	var titles = jQuery('#merchandisingsuite-slides_'+hash+' .name');
	titles.each(function(index, child) {
	    if(jQuery(child).height() > titlesHeight) {
	    	titlesHeight = jQuery(child).height();
	    }
	});
	titles.each(function(index, child) {
		jQuery(child).height(titlesHeight);
	});
	
}

function positionBuyingBox(hash) {
	//Get the largest buying box element outer height
	var buyingboxHeight = 0;
	var buyingBoxes = jQuery('#merchandisingsuite-slides_'+hash+' .buying-box');
	var l = 0;
	buyingBoxes.each(function(index, child) {
		console.log("Box "+(l++)+"'s height: "+jQuery(this).outerHeight());
	    if(jQuery(this).outerHeight() > buyingboxHeight) {
	    	buyingboxHeight = jQuery(this).outerHeight();
	    }
	});
	
	if(console) console.log(buyingboxHeight);
	
	//Make all buying boxes the same height
	/*buyingBoxes.each(function(index, child) {
		jQuery(child).height(buyingboxHeight);
	});*/

	jQuery('#merchandisingsuite-slides_'+hash+' .outer-padder').each(function(index, child) {
		jQuery(child).css('padding-bottom', buyingboxHeight+'px');
	});
}
