/*
History

14 June 2011 : 	Change row reference from " #accCustomWidget" to "left-nav-2_s"
				Change row reference from "left-nav-7" to "left-nav-2_s"

*/
$(document).ready(function(){	
		//temp using this to differentiate between testing and live
		//<meta name="PageType" content="Product" > -- include this one the product page custom head tags		
		//if(window.location.hash.match("creaftest")){
			// Load relationships.csv via ajax
			
			//logic flow
			//when both widgets has products show both
			//when only one widgets has products show on top
			//when both is not there, show the default
			
			//load all widgets into one div
			//just keep appending the html
			if($('meta[name=PageType]').attr('content') == 'Product'){
				//per dan dan request, turning this off
				//loadRelationshipWidget('/images/accesory_relationships.csv', '#left-nav-6', 'accCustomWidget');
				
				//if the accessory widget didn't load then replace the default amazon widget with the upsell
				if ($("#accCustomWidget").length > 0){
					loadRelationshipWidget('/images/upsell_relationships.csv','#left-nav-2_s', 'upCustomWidget');
				}
				else{
					loadRelationshipWidget('/images/upsell_relationships.csv','#left-nav-2_s', 'upCustomWidget');
				}
				$("#left-nav-2_s table tr td").attr("align", "center");
				$("#left-nav-2_s table tr td.fg").addClass("right_nav_header");
			}
			
		//}//end if hash checking
	});
	
	function loadRelationshipWidget(sourceFile, appendDiv, divName){
			var file = $.ajax({ type: "GET", url: sourceFile, async: false }).responseText;
			var lines = file.split("\n");
			var szhtml = "";								
				
			//use a flag to check if there's any products, 
			//if not the whole widget will not be shown at all
			var showWidget = 0;					
			
			//pull the child products that is tied to this current product via relationship defined the relationships.csv
			for ( var i = 1 ; i < lines.length ; ++i ) { //first line has column labels
				var line = lines[i];
				var tokens = line.split(",");
				if ( !(tokens.length >= 3) ) continue;
				var parent = tokens[0].replace(/\"/g,"");
				
				if ( parent != wba_product.asin ) continue;
				
				var label = tokens[1].replace(/\"/g,"");
				var query = "/api/products/";
				
				for ( var c = 2 ; c < tokens.length ; c++ ) {
					var asin = tokens[c].replace(/([^-A-Z0-9]|\")/g, "");
					if(asin.length > 0){
						query += asin + "-M"; // asins must be appended with "-A" or "-M" (associate/merchant)
						if ( c < tokens.length-1 )
						query += ",";
					}
				}//end for
					
				//create query to pull the information for these product vis amazon internal web service
				query += "?products.properties=offersSummary,imageSets";

				//format the data pulled from the web service
				var asins = {};
				szhtml += "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td class=\"fg\">"+label+"</td></tr>";
					
				var data2 = $.ajax({ type: "GET", url: query, async: false }).responseText;;
				var r = eval('(' + data2 + ')');
				
				//JSON is natively supported by javascript - sweet
				for ( i in r.products ) {
					var p = r.products[i];
					
					var accessoryPrice = p.offersSummary.lowestNewPrice?"$"+(p.offersSummary.lowestNewPrice/100.0).toFixed(2):"";
					
					//check to see if there's any inventory for this product
					if (p.offersSummary.totalNew < 1){
						continue;
					}
					
					szhtml += "<tr><td><div id='" + divName + "' class=\"accessory\"><a href=\"/"+makeURL(p.title)+"/M/"+p.asin+".htm\" style=\"text-decoration:none\"><img src=\""+p.imageSets[0].mediumImage.url+"\" alt='" +p.title + "'><br/><span style=\"color:#146094; text-decoration:none\">" + p.title + "</a><br/><span style=\"color:#c00; font-weight:bold;\"> " + accessoryPrice + " </span><br/><a href='/addToCart.htm?asin="+p.asin+"'><img src='/images/button_addToCart.gif' alt='Add to Cart'/></a></div></td></tr>";
					
					showWidget = 1;
				}//end for
				
				szhtml += "</table>";
				
				if (showWidget==1){	
					$(appendDiv).html(szhtml);
				}
			}//end for
	}//end function
	
	function makeURL(str){
		var clean = str.replace(/[^\.A-Z0-9_\s]/ig, "").split(/\s/g);
		var url = "";
		for(i=0;i<clean.length&&i<6;i++){
			url += clean[i]
			if(i<clean.length-1&&i<5)
			url += "-";
		}
		return url;
	}
