// function to execute after data is returned and dom is ready
var populateProduct = function() {

	jQuery(document).ready(function($)
	{      
		// categories bar
		$('ul#categories li:first').tplFromData(BP.data.stores.categoriesStore);
		
		// main product image
		if(BP.data.stores.productMainImagesStore)
		{
			$('div.productImage').tplFromDataReplace(BP.data.stores.productMainImagesStore);
		}
		else
		{
			$('div.productImage a').remove();
		}
		
		$('div#pagetitle').tplFromMarkup(BP.data.stores.productStore);
		$('td#product_info_bar').tplFromMarkup(BP.data.stores.productStore);
		$('td#description').tplFromMarkup(BP.data.stores.productStore);
		$('td#our_price').tplFromMarkup(BP.data.stores.productStore);
		$('div#menu').tplFromMarkup(BP.data.stores.productStore);
		
		// populate product images	
		$.each(BP.data.stores.productImagesStore, function(index, value) {
			$('span.smallProductImage:first').tplFromDataAppend(BP.data.stores.productImagesStore[index]);
		});
		
		// remove image thumb template
		$('span.smallProductImage:first').remove();
		
		// fill product name into lightbox links
		if(BP.data.stores.productImagesStore.length)
		{		
			var galleryTitle = BP.data.stores.productStore.productmanufacturer_name + ' - ' + BP.data.stores.productStore.name;
			$('a.pirobox_gall').attr('title', galleryTitle);
		}
		
		// product image lightboxes
		$().piroBox(
		{
	      my_speed: 300, //animation speed
	      bg_alpha: 0.5, //background opacity
	      radius: 4, //caption rounded corner
	      scrollImage : true, // true == image follows the page _|_ false == image remains in the same open position
	                           // in some cases of very large images or long description could be useful.
	      slideShow : 'false', // true == slideshow on, false == slideshow off
	      pirobox_next : 'piro_next', // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
	      pirobox_prev : 'piro_prev', // Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
	      close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
	     });	
	});

}

// start loading data
BP.fetch.getCategories();

var product_id = jQuery.url.param("id");
BP.fetch.getProduct(product_id, {'bundle_images':true}, populateProduct);
    
