// File: readXML.js

// Prevent conflicts with Prototype
var $j = jQuery.noConflict();

// Start function when DOM has completely loaded 
// Sorting by node
$j(document).ready(function() {
$j.ajax({
  type: "GET",
  url: "skus.xml",
  dataType: "xml", //Make sure that you specify the type of file you expecting (XML)
  complete: function(data) {
	
	// Build an HTML string
	myHTMLOutput = '';
	myHTMLOutput += '<div id="contentWrapper">';
	
	var count = jXML.getCount(data,{"Product":"product"});
	var n = (count["Product"]);
	var n2 = 1;
	var sortvalue = "DESC";
	
   var json = $j.xmlToJSON(data.responseXML); //Please notice that we use responseXML here which is DOMDocument object
  //Now I want to sort all rows by node col1
   var out = "";
   for(var i = 0; i < json.products[0].product.length; i++) {
   	productSku = json.products[0].product[i].sku[0].Text;
	productDescription = json.products[0].product[i].description[0].Text;
	productRating = json.products[0].product[i].rating[0].Text;
	productPrice = json.products[0].product[i].price[0].Text;
	productPromoPrice = json.products[0].product[i].promo_price[0].Text;
	productShipping = json.products[0].product[i].shipping[0].Text;
	productAvailability = json.products[0].product[i].availabilty[0].Text;
	productNumber = n2++;
	productTotal = n;
	
	mydata = BuildProductHTML(productTotal,productNumber,productSku,productDescription,productRating,productPrice,productPromoPrice,productShipping,productAvailability);
   	myHTMLOutput = myHTMLOutput + mydata;
   }
   //alert(mydata);
   myHTMLOutput += '</div>';
   $j("#ProductContent").empty().append(myHTMLOutput); 
}
});
});

function BuildProductHTML(productTotal,productNumber,productSku,productDescription,productRating,productPrice,productPromoPrice,productShipping,productAvailability){
	//Set campaign tracking variables so i only change it here next time
	campaignId = '0316_Aff_Recon';
	
	
	// Build HTML string and return a 5 columns
	output = '';
	productAvailabilityHTML = '';
	opentag = '<div class="product">';
	closetag = '</div>';
	
	if (productNumber % 5 == 0) 
		closetag = '</div><div class="clearContainer"></div><div class="separator"></div>';
		opentag = '<div class="product">';
	
	if (productShipping == 1) {
		productShippingImg = 'SHIP-PIS.gif';
	} else if (productShipping == 2) {
		productShippingImg = 'SHIP-FREE.gif';
	} else if (productShipping == 0) {
		productShippingImg = 'clear.gif';
	}
	
	
	// Set star image based on rating
	switch(productRating) {
		case "0":
			productRatingImg = '&nbsp;';
			productRatingCpy = '&nbsp;';
			break;
		case "4":
			productRatingImg = '<img src="images/r40-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 4 out of 5';
			break;
		case "4.1":
			productRatingImg = '<img src="images/r42-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 4.1 out of 5';
			break;
		case "4.2":
			productRatingImg = '<img src="images/r42-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 4.2 out of 5';
			break;
		case "4.3":
			productRatingImg = '<img src="images/r43-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 4.3 out of 5';
			break;
		case "4.4":
			productRatingImg = '<img src="images/r44-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 4.4 out of 5';
			break;
		case "4.5":
			productRatingImg = '<img src="images/r45-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 4.5 out of 5';
			break;		
		case "4.6":
			productRatingImg = '<img src="images/r46-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 4.6 out of 5';
			break;
		case "4.7":
			productRatingImg = '<img src="images/r47-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 4.7 out of 5';
			break;
		case "4.8":
			productRatingImg = '<img src="images/r48-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 4.8 out of 5';
			break;
		case "4.9":
			productRatingImg = '<img src="images/r49-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 4.9 out of 5';
			break;
		case "5":
			productRatingImg = '<img src="images/r50-5.gif" alt="Stars" width="75" height="14" border="0" />';
			productRatingCpy = ' 5 out of 5';
			break;
		default:
			productRatingImg = '&nbsp;';
			productRatingCpy = '&nbsp;';
			break;
	}
	
	//Is the pip on promo price?	
	if (productPromoPrice == '') {
		price = productPrice;
		} else {
		price = '<span style="text-decoration:none;">'+productPrice+'</span> <br><span style="color:#CC0000;">'+productPromoPrice+'</span>';
	}
	
	if (productAvailability == 'In-Stock') {
		productAvailabilityHTML = '<div class="buyButton"><a href="http://www.homedepot.com/webapp/wcs/stores/servlet/BuildLinkToHomeDepot?linktype=product&id='+productSku+'&cm_sp=hd_email-_-'+campaignId+'-_-PIP-_-'+productSku+'"><img src="images/btn_ShopNow.gif" alt="&gt; Shop Now" width="110" height="23" border="0" /></a></div>';
	} else if (productAvailability == 'Out-Of-Stock') {
		productAvailabilityHTML = '<div class="not-available">Out Of Stock Online</div>';
	} 
	
	// Actually putting together the HTML
	output += opentag;
	output += '<div><a href="http://www.homedepot.com/webapp/wcs/stores/servlet/BuildLinkToHomeDepot?linktype=product&id='+productSku+'&cm_sp=hd_email-_-'+campaignId+'-_-PIP-_-'+productSku+'"><img src="images/'+productSku+'.jpg" alt="'+productDescription+'" width="150" height="150" border="0" /></a></div>';
	output += '<div class="aboutProdLRG"><a href="http://www.homedepot.com/webapp/wcs/stores/servlet/BuildLinkToHomeDepot?linktype=product&id='+productSku+'&cm_sp=hd_email-_-'+campaignId+'-_-PIP-_-'+productSku+'">'+ productDescription +'</a></div>';
	output += '<div class="aboutRate">'+ productRatingCpy +'</div>';
	output += '<div class="stars">'+ productRatingImg +'</div>';
	output += '<div class="price">'+ price +'</div>';
	// output += '<div>'+ productPromoPrice +'</div>';
	output += '<div class="shippingBox"><img src="images/'+ productShippingImg +'" alt="Free Shipping" width="127" height="29" border="0" /></div>';
	output += productAvailabilityHTML;
	output += closetag;
	return output;
}

var jXML = {
    getCount: function(data,nodes) {
        var response = {};
        for (var node in nodes) {
            response[node] = $(nodes[node],data).length;
        }
        return response;
    
    }};
