var contentData,postLinkText,leadInText;
var resultCount=0;

function process(data) {
	var contentData="";
	var posCount=0;

	for(i=0;i<=data.product.length-1;i++) {
		//do not show a product that is the same as the one being viewed

		if (data.product[posCount]!=undefined) {
			if(productSKU!=data.product[posCount].sku){
				contentData=contentData+data.product[posCount].html;
				resultCount=resultCount+1
			}
			posCount=posCount+1;
			if (posCount>=5 || posCount>=data.product.length) break;
		}
	}

   contentHTML=leadInText+'<div class="productListSidePane">' + contentData + '<hr/></div>' + postLinkText;
   $("#marketing-product-content").html(contentHTML);
   if (data.product.length>2) $("#product-marketing-spot").show();
}

$(document).ready(function() {

leadInText="<p>You may also like...</p>";
moreOfferLink="/cheap/search-results/results&query=destination+is+\""+ productDestination + "\"";
postLinkText="<p><a href='" + moreOfferLink + "'>more related offers</a></p>";

var labelValue = 'Related Offers';
var categoryQuery="product_category is \"" + productCategory + "\" "; // Category is set for all except Air
if (productCategory.toLowerCase()=="holiday package") labelValue="More Holiday Deals";
if (productCategory.toLowerCase()=="air transportation") {
	categoryQuery="";
	labelValue="Related Offers";
};
if (productCategory.toLowerCase()=="accommodation") labelValue="More Hotel Deals";
if (productCategory.toLowerCase()=="tour") labelValue="More Tour Deals";
if (productCategory.toLowerCase()=="cruise") labelValue="More Cruise Deals";
if (productCategory.toLowerCase()=="ground transportation") labelValue="More Car Deals";

$("#marketing-product-label").html(labelValue);


tagLineBegin="<securequery template=\"nzfc/tag/productListSidePane\">";
tagLineNum="first 6 ";  //get 6 in case the current product is in the list
tagLineDest="destination is \"" + productDestination + "\" ";
tagLineCat=categoryQuery;
tagLineSort="sort price+ ";
tagLineEnd="</securequery>";

// Main query
contentQuery=tagLineBegin + tagLineNum + tagLineDest + tagLineCat + tagLineSort + tagLineEnd;
$.post("/ajax/sitecat", { content: contentQuery },
 function(data){
	process(data);

	// Query to fill extra products. 
	if (resultCount <= 1) {
		labelValue="Related offers";
		//Query with no category
		contentQuery=tagLineBegin + tagLineNum + tagLineDest + tagLineSort + tagLineEnd;
		$.post("/ajax/sitecat", { content: contentQuery },
		function(fillData){
			process(fillData);
		},"json");
	}


  },"json");
 
});



