// Start - recommendation section -- Deepak

var productId = "";
var recommTitle = "";
var author = "";
var narrator = "";
var faithfullness = "";
var oid = "";
var feature_desc = "";
var content_type = "";
var runningTime = "";
var price = "";
var coverImage = "";

function productDetail(){	
	this.productId = "";
	this.recommTitle = "";
	this.author = "";
	this.narrator = "";
	this.faithfullness = "";
	this.oid = "";
	this.feature_desc = "";
	this.content_type = "";
	this.runningTime = "";	
	this.price = "";	
	this.coverImage = "";	
	
}

var baseUrl = window.location.protocol + "//" + window.location.host;

function populateRecommendations(req){	
	if(req.responseXML.getElementsByTagName("recommendations")[0] != null){
		var updateHTML = "";
		var responseXML = req.responseXML;
		var rootElement = responseXML.getElementsByTagName("recommendations")[0];
		var recommTitle = rootElement.getElementsByTagName("reco_title")[0].firstChild.nodeValue
		var products = rootElement.getElementsByTagName("product");
		
		document.getElementById("userRecomm").className = "prodRecommendations";				

		if(products.length > 0){
			if(recommTitle != ""){
				document.getElementById("userRecomm").innerHTML += "<h2>" + recommTitle + "</h2>"
			}
			var numRecommToDisplay = (document.getElementById("userRecomm").offsetWidth > 750) ? 4 : 3;
			var recommStyleWidth = (numRecommToDisplay == 4) ? "Items4": "Items3";

			var recommToShow = (products.length <= numRecommToDisplay) ? products.length : numRecommToDisplay;

			for(i = 0; i < recommToShow; i++){
				var product = products[i];
				
				var prodDetail = new productDetail();

				productDetail.productId = product.attributes.getNamedItem("id").value;
				productDetail.recommTitle = getNodeValue(product, "title");
				productDetail.author = getNodeValue(product, "author");
				productDetail.narrator = getNodeValue(product, "narrator");
				productDetail.faithfullness = getNodeValue(product, "faithfullness");
				productDetail.oid = getNodeValue(product, "oid");
				productDetail.feature_desc = getNodeValue(product, "feature_desc");
				productDetail.content_type = getNodeValue(product, "content_type");
				productDetail.runningTime = getNodeValue(product, "length");
				productDetail.price = getNodeValue(product, "price");
				productDetail.coverImage = getNodeValue(product, "cover_image");
				
				updateHTML += "<div id=\"reccItem" + i + "\" class=\"recomendItem " + recommStyleWidth + "\" >" + getResommOneItemHtml(productDetail) + "</div>";;
				
			}
		}	
		document.getElementById("userRecomm").innerHTML += updateHTML;			
		
	}	
}

function getNodeValue(product, nodeName){
	if(product.getElementsByTagName(nodeName)[0].firstChild)
		return product.getElementsByTagName(nodeName)[0].firstChild.nodeValue;
	else
		return "";
}

function getResommOneItemHtml(productDetail){
	
	var pid = productDetail.productId;
	var flyoutTitle = productDetail.recommTitle;
	var shortAuthor = productDetail.author;
	var strnarrator = productDetail.narrator;
	var flyoutDesc = productDetail.feature_desc;
	var version = productDetail.content_type;
	var runningTimeDesc = productDetail.runningTime;
	var faithfullness = productDetail.faithfullness;	
	var price = productDetail.price;	
	var coverImage = productDetail.coverImage;
	
	var soundFileLocation = "http://audible.edgeboss.net/download/audible/content/" + pid.replace(/_/g,"/").toLowerCase() + "/" + pid.toLowerCase() + "_sample.mp3";
	
	var playerId = playerIDCount++;
	
	var oneItemHTML=	getFlyoutHtml(productDetail) +
				"<div class=\"reccAudVisual\">" +
					"<div class=\"reccCover\">" +
						"<a href=\"" + baseUrl + "/adbl/site/products/ProductDetail.jsp?productID=" + pid + "&amp;recom=yes&amp;loomia_si=1&amp;BV_UseBVCookie=Yes\">" +
							"<img onmouseout=\"UnTip();\" onmouseover=\"javascript:myTip('pop_" + pid + "');\" onerror=\"javascript:this.src='/images/site/endeca_search/small_frame_nosearch60x60.gif'\" alt=\"\" src=\"" + coverImage + "\"/>" +
						"</a>" +
					"</div>" +
					"<div class=\"reccSample\">" +
						"<div class=\"sampleText\"/>" + 
							"<div class=\"sampleBtn\">" +								
								"<object width=\"40\" height=\"12\" align=\"left\" id=\"audioplayer"+ playerId + "\" data=\"/audioplayer/soundplayerAltered.swf\" type=\"application/x-shockwave-flash\" title=\"\">" +
									"<param value=\"/audioplayer/soundplayerAltered.swf\" name=\"movie\"/>" +
									"<param value=\"high\" name=\"quality\"/>" +
									"<param value=\"transparent\" name=\"wmode\"/>" +
									"<param value=\"TL\" name=\"salign\"/>" +
									"<param value=\"#FFFFFF\" name=\"bgcolor\"/>" +
									"<param value=\"true\" name=\"menu\"/>" +
									"<param value=\"playerID="+ playerId + "&amp;soundFile=" + soundFileLocation + "&amp;rightbg=0x9dbb41&amp;leftbg=0x9dbb41&amp;bg=0xeefac8&amp;lefticon=0xffffff&amp;righticon=0xffffff&amp;border=0x000000&amp;slider=0x9dbb41\" name=\"flashvars\"/>" +
									"<param value=\"audiobook\" name=\"prodname\"/>" +
								"</object>" +
							"</div>" +
						"</div>" +
					"</div>" +
				"</div>" +
				"<div class=\"reccTitle\">" +
					"<a onclick=\"s_objectID='PD Recommendations'\" href=\"" + baseUrl + "/adbl/site/products/ProductDetail.jsp?productID=" + pid + "&amp;recom=yes&amp;loomia_si=1&amp;BV_UseBVCookie=Yes\">" + flyoutTitle + "</a>" +
				"</div>" +
					"<div class=\"reccFaithful\">" + faithfullness + "</div>" +
					"<div class=\"reccAuthor\">" + shortAuthor + "</div>" +
				"<div class=\"reccPrice\">" + price + "</div>" +
				"<div class=\"reccCart\">" +
					"<a href=\"" + baseUrl + "/adbl/site/shop/cart.jsp?addItem=" + pid + "&amp;rcItem=" + pid + "&amp;loomia_si=1&amp;BV_UseBVCookie=Yes\" style=\"display: block;\" title=\"\" class=\"act_btns btn_4616 add_to_cart_4616\">Add To Cart</a>" +
				"</div>";			
	return oneItemHTML;
}

function getFlyoutHtml(productDetail){
	var pid = productDetail.productId;
	var flyoutTitle = productDetail.recommTitle;
	var shortAuthor = productDetail.author;
	var strnarrator = productDetail.narrator;
	var flyoutDesc = productDetail.feature_desc;
	var version = productDetail.faithfullness;
	var runningTimeDesc = productDetail.runningTime;
	
	var HTMLOutput = "";
		HTMLOutput +=	"<div id=\"pop_" + pid +"\" class=\"popupContainer\">";
		HTMLOutput +=		"<div class=\"flyoutContainer\">";
		HTMLOutput +=			"<div id=\"flyOutTitle\" class=\"flyoutTitle\">";
		HTMLOutput +=				flyoutTitle;
		HTMLOutput +=			"</div>";							
		
		if (shortAuthor != null && shortAuthor != ""){ 	
			HTMLOutput +=		"<div id=\"writtenBy\">";
                	HTMLOutput +=			"<span>By</span>";
			HTMLOutput +=			"<span id=\"flyOutAuthor\" class=\"flyoutAuthor\"> "+ shortAuthor+"</span>";
	      	HTMLOutput +=		"</div>";										
		}
		if (strnarrator != null && strnarrator != ""){ 
	     		HTMLOutput +=		"<div id=\"narratedBy\">";
		 	HTMLOutput +=			"<span>Narrated by</span>";									
		 	HTMLOutput +=			"<span id=\"flyOutNarrator\" class=\"flyoutNarrator\"> "+strnarrator +"</span>";
		 	HTMLOutput +=		"</div>";
		}
		HTMLOutput +=		"<div><hr /></div>";

		HTMLOutput +=		"<div class=\"flyoutContent\" id=\"flyOutDesc\">"+ flyoutDesc +"</div>";
	    HTMLOutput +=		"<div><hr /></div>";
		HTMLOutput +=		"<div style=\"height:15px;\">";
		HTMLOutput +=			"<ul style=\"list-style-type:none;margin:0px;padding:0px\">";
		HTMLOutput +=				"<li id=\"flyOutType\" class=\"flyoutFooterType\">"+ version +"</li>";
              HTMLOutput +=				"<li id=\"flyOutLength\" class=\"flyoutLength\">"+ runningTimeDesc +" </li>";
		HTMLOutput +=	 		"</ul>";	
		HTMLOutput +=		"</div>";
		HTMLOutput +=	"</div>";
		HTMLOutput +="</div>";
		
		return HTMLOutput;
	
}



function loadRecomm(){
	sendRequest(recommAjaxUrl,populateRecommendations);	
}


//retrievecookie.js
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

var myCookie = getCookie('currentActionCode');	

function myTipInfo(popUpid){
	var coordinates = getAnchorPosition(popUpid);
	var x = 0;
	var y = 0;
	var browserName=navigator.userAgent;	
	var divWidth = 321;
	var divHeight = 315;
	var bckimagepath='/images/everest/site/softwarepage/110408/aax_popup_background_.png';
	if((browserName.indexOf("MSIE 6.0"))!=-1){
		x=coordinates.x + 100;
		y=coordinates.y + 200; 
	}else if((browserName.indexOf("MSIE 7.0"))!=-1){
		x=coordinates.x + 100;
		y=coordinates.y + 200;
	}else{
		x=coordinates.x + 100;
		y=coordinates.y + 200;
	} 
	TagToTip("audioFileReviewInfo",FIX,[x,y], BGIMG,"'" + bckimagepath + "'",FOLLOWMOUSE,false,WIDTH, divWidth, HEIGHT,divHeight,PADDING,0,SHADOWWIDTH,0,TITLEPADDING,0,BORDERWIDTH,0,BGCOLOR,'transparent',FADEIN,0);
}