jQuery(document).ready(function() {
	
	jQuery('table.collapsable thead').each(function() {
		jQuery(this).addClass('closed').next('tbody').attr('style', 'display:none');
	});
	
	jQuery('table.collapsable thead').click(function (event) {
		if (jQuery(this).hasClass('closed')) {
			jQuery(this).next('tbody').attr('style', 'display:table-row-group');
			jQuery(this).removeClass('closed');
			
		} else {
			jQuery(this).next('tbody').attr('style', 'display:none');
			jQuery(this).addClass('closed');
		}
	});
	
	jQuery('table.collapsable thead').eq(0).click();
	
	if(document.getElementById('layout')) {
		jQuery('#layout img').reflect({ height: 0.39, opacity: 0.5 });
	}
	if(document.getElementById('reflectionView')) {
		jQuery('.layout img').reflect({ height: 0.39, opacity: 0.5 });
	}
		
	
	
	
	jQuery('.tx-buproducts .panoviews .panoviewMenu div:first').addClass('act');
	
	jQuery('.tx-buproducts .panoviews .panoviewMenu div').each(function() {
		var elementHeight = jQuery(this).children().height();
		if(elementHeight == 36) {
			jQuery(this).children('a').css("padding", "7px 0 0 10px");
			jQuery(this).css("line-height", "13px");
		}
		
	});
	
	
	
	// Keep panoview menu item active after clicking.
	jQuery('.tx-buproducts .panoviews .panoviewMenu a').click(function(event) {
		jQuery(this).parents('.panoviewMenu').children('div.act').removeClass('act');
		jQuery(this).parent('div').addClass('act');
		
		var href = jQuery(this).attr('href');
		doAjaxRequestPanoview(href);
		event.preventDefault();
		return false;
		
	});
	jQuery('.tx-buproducts .panoviews .panoviewMenu a:first').trigger('click');
	
});


function doAjaxRequestPanoview(href) {
	cleanUpHtml();
	jQuery.ajax({
		type: "POST",
		url: href,
		dataType: "json",
		success: function(data) {
			if(data.header) {
				jQuery('#bucore_products_panoviewHeading').html(data.header); 
	        }	
	        if(data.layoutImage) {
				jQuery('#bucore_products_layoutContainer').html(data.layoutImage); 	
	        }
	        if(data.panoview) {
	        	jQuery('#bucore_products_panoviewContainer').html(data.panoview); 
	        }
		}
	});
}

function cleanUpHtml() {
	jQuery('#bucore_products_layoutContainer').html(''); 
	jQuery('#bucore_products_panoviewHeading').html(''); 
	jQuery('#bucore_products_panoviewContainer').html(''); 
}
