//
//-------------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (c) Copyright IBM Corp. 2006
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//-------------------------------------------------------------------
//

//
// ***
// * This javascript library contains the methods required to display
// * the product review floating pop-up on the product lister page
// ***
//
var dialog = null;


// function to show the review pop up
function showReviewInfoPopUp(event, element, name, image, starImageLocation, rating, headline, reviewer_name, location, pro1, pro2, pro3, bestUse1, bestUse2, bestUse3) {
	createReviewInfoPopUp(event, element, name, image, starImageLocation, rating, headline, reviewer_name, location, pro1, pro2, pro3, bestUse1, bestUse2, bestUse3);
}


// function to hide the review popup
function hideReviewInfoPopUp(element) {
	if (dialog != null) {
		dialog.style.visibility = "hidden";
		dialog = null;
	}
}


// function to create the review popup - calls in the various other elements
function createReviewInfoPopUp(event, element, name, image, starImageLocation, rating, headline, reviewer_name, location, pro1, pro2, pro3, bestUse1, bestUse2, bestUse3) {
	try {	
		if (pro1 != "" || bestUse1 != "") {
			if (dialog == null) {
				var body = document.getElementsByTagName("body")[0];
				dialog = document.createElement("div");
				dialog.id = "reviewPopup";
				
				dialog.appendChild(createHeaderElement());
			
				var leftsideTop = document.createElement("div");
					leftsideTop.className = "leftside_top";
					leftsideTop.appendChild(createImageElement(image, name));
				dialog.appendChild(leftsideTop);

				var rightsideTop = document.createElement("div");
					rightsideTop.className = "rightside_top";
					if (rating != "") {
						rightsideTop.appendChild(createRatingElement(starImageLocation, rating));
					}	
					if (headline != "") {
						rightsideTop.appendChild(createHeadlineElement(headline, reviewer_name, location));
					}					
				dialog.appendChild(rightsideTop);

				dialog.appendChild(createClearBothDiv());				
				dialog.appendChild(createProductNameElement(name));
			
				dialog.appendChild(createCustomerOpinionsTitle());
			
				if (pro1 != "") {
					var lowerSection = document.createElement("div");
					lowerSection.className = "lower_section";
						lowerSection.appendChild(createProsElement(pro1, pro2, pro3));
					dialog.appendChild(lowerSection);
				}				
				if (bestUse1 != "") {
					var lowerSection = document.createElement("div");
					lowerSection.className = "lower_section";	
						lowerSection.appendChild(createBestUsesElement(bestUse1, bestUse2, bestUse3));
					dialog.appendChild(lowerSection);
				}

	
				body.appendChild(dialog);			
				var scrollPosition = getScrollingPosition();			
				var viewPortSize = getViewportSize();
				
				if ((event.clientY + 350) > (viewPortSize[1])) {
					var newHeight = event.clientY - ((event.clientY + 360) - viewPortSize[1]);
					dialog.style.top = scrollPosition[1] + newHeight + "px";
					dialog.style.left = event.clientX + scrollPosition[0] + 5 + "px";
				} else if ((event.clientX + 410) > (viewPortSize[0])){
					var newWidth = event.clientX - ((event.clientX + 420) - viewPortSize[0]);
					dialog.style.left = scrollPosition[0] + newWidth + "px";			
					dialog.style.top = event.clientY + scrollPosition[1] + 40 + "px";
				} else {	
					dialog.style.left = event.clientX + scrollPosition[0] + 5 + "px";
					dialog.style.top = event.clientY + scrollPosition[1] + 5 - 60 + "px";
				}

				setTimeout ('makeVisible()', 2000);
			}
		}
	} catch(error) {
		return true;
	}
	return false;
}


function createClearBothDiv() {
	clearbothDisplay = document.createElement("div");
		clearbothDisplay.className = "clearboth";
	return clearbothDisplay;
}


// function to create the standard review pop-up header
function createHeaderElement() {
	var headerDisplay = document.createElement("div");
		headerDisplay.className = "header";
		var headerContent = document.createElement("h1");
		headerContent.appendChild(document.createTextNode("Review Snapshot"));
	headerDisplay.appendChild(headerContent);
	return headerDisplay;
}


// function to create the review popup's header
function createProductNameElement(name) {
	var productNameDisplay = document.createElement("div");
		productNameDisplay.className = "productName";
		var productName = document.createElement("h2");
		productName.appendChild(document.createTextNode(name));
	productNameDisplay.appendChild(productName);
	return productNameDisplay;
}


// function to create the review popup's image
function createImageElement(image, name) {
	var productImage = document.createElement("img");
		productImage.src = image;
		productImage.alt = name;
	return productImage;
}


// function to create the 'our customer think that' text
function createCustomerOpinionsTitle() {
	var opinionsTitleDisplay = document.createElement("div");
		opinionsTitleDisplay.className = "customer_opinions";
		var opinionsTitle = document.createElement("h5");
		opinionsTitle.appendChild(document.createTextNode("What our customers think"));
	opinionsTitleDisplay.appendChild(opinionsTitle);
	return opinionsTitleDisplay;
}


// function to create the review popup's rating element
function createRatingElement(starImageLocation, rating) {
	var ratingDisplay = document.createElement("div");
		ratingDisplay.className = "rating";
		var ratingText = document.createElement("h3");
		ratingText.appendChild(document.createTextNode("Rating:"));
		ratingDisplay.appendChild(ratingText);
		
		var ratingImage = document.createElement("img");
		if (rating == 5) {
			ratingImage.src = starImageLocation + "5_stars_yellow.gif";
			ratingImage.alt = "5";
		} else if (rating == 4.5) {
			ratingImage.src = starImageLocation + "4.5_stars_yellow.gif";
			ratingImage.alt = "4.5";		
		} else if (rating == 4) {
			ratingImage.src = starImageLocation + "4_stars_yellow.gif";
			ratingImage.alt = "4";		
		} else if (rating == 3.5) {
			ratingImage.src = starImageLocation + "3.5_stars_yellow.gif";
			ratingImage.alt = "3.5";		
		} else if (rating == 3) {
			ratingImage.src = starImageLocation + "3_stars_yellow.gif";
			ratingImage.alt = "3";		
		} else if (rating == 2.5) {
			ratingImage.src = starImageLocation + "2.5_stars_yellow.gif";
			ratingImage.alt = "2.5";		
		} else if (rating == 2) {
			ratingImage.src = starImageLocation + "2_stars_yellow.gif";
			ratingImage.alt = "2";		
		} else if (rating == 1.5) { 
			ratingImage.src = starImageLocation + "1.5_stars_yellow.gif";
			ratingImage.alt = "1.5";		
		} else if (rating == 1) {
			ratingImage.src = starImageLocation + "1_stars_yellow.gif";
			ratingImage.alt = "1";		
		} else {
			ratingImage.src = starImageLocation + "no_stars_yellow.gif";
			ratingImage.alt = "unknown";
		}
		ratingDisplay.appendChild(ratingImage);
	return ratingDisplay;
}


// function to display the review headline
function createHeadlineElement(headline, name, location) {
	var headlineDisplay = document.createElement("div");
		headlineDisplay.className = "headline";
		var headlineText = document.createElement("p");
		headlineText.appendChild(document.createTextNode("\"" + headline + "\""));
	var headlineWriter = document.createElement("p");
		headlineWriter.className = "strong";
		headlineWriter.appendChild(document.createTextNode(name + " - " + location));		
	headlineDisplay.appendChild(headlineText);
	headlineDisplay.appendChild(headlineWriter);
	return headlineDisplay;
}


// function to create the review popup's pros list
function createProsElement(pro1, pro2, pro3) {
	var prosDisplay = document.createElement("div");
		var prosTitleDisplay = document.createElement("div");
		prosTitleDisplay.className = "list_title";
			var prosTitle = document.createElement("h4");
			prosTitle.appendChild(document.createTextNode("Pros: "));
		prosTitleDisplay.appendChild(prosTitle);
	prosDisplay.appendChild(prosTitleDisplay);	

		var prosListDisplay = document.createElement("div");
		prosListDisplay.className = "list_details";		
			var prosList = document.createElement("ul");	
			if (pro1 != "") {
			var pro1Display = document.createElement("li");
				pro1Display.appendChild(document.createTextNode(pro1));
				prosList.appendChild(pro1Display);
			}
			if (pro2 != "") {
				var pro2Display = document.createElement("li");
				pro2Display.appendChild(document.createTextNode(", " + pro2));
				prosList.appendChild(pro2Display);
			}
			if (pro3 != "") {
				var pro3Display = document.createElement("li");
				pro3Display.appendChild(document.createTextNode(", " + pro3));
				prosList.appendChild(pro3Display);
			}
		prosListDisplay.appendChild(prosList);
	prosDisplay.appendChild(prosListDisplay);
	return prosDisplay;
}


// function to create the review popup's bestUses list
function createBestUsesElement(bestUse1, bestUse2, bestUse3) {
	var bestUsesDisplay = document.createElement("div");
		var bestUsesTitleDisplay = document.createElement("div");
		bestUsesTitleDisplay.className = "list_title";
			var bestUsesTitle = document.createElement("h4");
			bestUsesTitle.appendChild(document.createTextNode("Best Uses: "));
		bestUsesTitleDisplay.appendChild(bestUsesTitle);
	bestUsesDisplay.appendChild(bestUsesTitleDisplay);	
	
		var bestUsesListDisplay = document.createElement("div");
		bestUsesListDisplay.className = "list_details";		
			var bestUsesList = document.createElement("ul");	
			if (bestUse1 != "") {
				var bestUse1Display = document.createElement("li");
				bestUse1Display.appendChild(document.createTextNode(bestUse1));
				bestUsesList.appendChild(bestUse1Display);
			}	
			if (bestUse2 != "") {
				var bestUse2Display = document.createElement("li");
				bestUse2Display.appendChild(document.createTextNode(", " + bestUse2));
				bestUsesList.appendChild(bestUse2Display);
			}
			if (bestUse3 != "") {
				var bestUse3Display = document.createElement("li");
				bestUse3Display.appendChild(document.createTextNode(", " + bestUse3));
				bestUsesList.appendChild(bestUse3Display);
			}
		bestUsesListDisplay.appendChild(bestUsesList);
	bestUsesDisplay.appendChild(bestUsesListDisplay);
	return bestUsesDisplay;
}


// function to make the review popup visible
function makeVisible() {
	if (dialog != null) {
		dialog.style.visibility = "visible";
	}
}


// function to get the window's scoll position on the current page
function getScrollingPosition() {
	var position = [0, 0];
	if (typeof window.pageYOffset != 'undefined') {
		position = [window.pageXOffset, window.pageYOffset];
	} else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0) {
		position = [document.documentElement.scrollLeft, document.documentElement.scrollTop];
	} else if (typeof document.body.scrollTop != 'undefined') {
		position = [document.body.scrollLeft, document.body.scrollTop];
	}
	return position;
}


// function to get the window size (viewport size)
function getViewportSize() {
	var size = [0, 0];
	if (typeof window.innerWidth != 'undefined') {
		size = [window.innerWidth, window.innerHeight];
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
	} else {
		size = [document.getElementsByTagName('body')[0].clientWidth, document.getElementsByTagName('body')[0].clientHeight];
	}
	return size;
}
	
	