// function to execute after data is returned and dom is ready
function populateProduct() {
	
	jQuery(document).ready(function($)
	{      
		// remove any highlights from top nav
		jQuery('ul#nav li').removeClass('selected');		
		
		// category nav
		jQuery('#categoriesTpl').tmpl(BP.data.stores.categoriesStore, {
			categoryLink: BP.fm.categoryLink
		}).appendTo('ul.list1');
		
		// product details
		jQuery('#productDetailsTpl').tmpl(BP.data.stores.productStore).appendTo('div#proddesc');
		
		// product cart buttons
		jQuery('#productCartBtnsTpl').tmpl(BP.data.stores.productStore, {
			urlEncodeName: BP.fm.urlEncodeProductName
		}).prependTo('div#buttons');
		
		// manufacturer info
		jQuery('#manufacturerInfoTpl').tmpl(BP.data.stores.productStore).appendTo('div#manufacturerinfo');
		
		// main product image
		jQuery('#productMainImageTpl').tmpl(BP.data.stores.productMainImagesStore, {
			mainProductImage: BP.fm.productPageProductMainImage
		}).appendTo('div.mainimage');
			
		jQuery('#productThumbsTpl').tmpl(BP.data.stores.productImagesStore, {
			thumbProductImages: BP.fm.productPageProductImageThumbSize,
			fullProductImages: BP.fm.productPageProductImageFullSize
		}).appendTo('ul#thumbs');

		jQuery().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
		      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
		});

		// 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;
			jQuery('a.pirobox_gal').attr('title', galleryTitle);
		}

		    
		// hide ecommerce buttons for non-ecommerce products
		var productPrice = BP.data.stores.productStore.our_price;
		var manufacturerName = BP.data.stores.productStore.productmanufacturer_name;
		if(manufacturerName === "* GT-RR Demo Car" || manufacturerName === "* Customer Car" || productPrice == 0 ) {
			jQuery('div#pricebox').remove();
			jQuery('div#buttons').empty();
		}
		
		// add facebook like button with dynamic url
		jQuery('div.social').eq(1).append('<iframe src="http://www.facebook.com/plugins/like.php?href='+BP.util.urlencode(location.href)+'&amp;layout=button_count&amp;show_faces=false&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>');
					    
	});

}

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

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