/* PANGEA ORGANICS */
/* Product Page Functions
----------------------------------------------------------------------------- */


	$(window).load(function() {

/* CHANGE PAGE COLOR
----------------------------------------------------------------------------- */


	changePageColor(tag.replace(/-/g, " "));


/* ADD LABEL TO BACKORDERED TEXT FIELD
----------------------------------------------------------------------------- */


	$("div.widget-item div.backordered #email").val("Enter your e-mail address.").attr("title", "Enter your e-mail address.");

	$("div.widget-item div.backordered input[type=text]").click(function() {

		var label = $(this).attr("title");
		if (label == $(this).val()) {

			$(this).val("");

		}

	});

	$("div.widget-item div.backordered input[type=text]").blur(function() {

		var label = $(this).attr("title");
		if ($(this).val() == "") {

			$(this).val(label);

		}

	});


/* LOAD PRODUCT PHOTO VIEWER PATTERN
----------------------------------------------------------------------------- */


	$('div.widget-item.display:not(.photos)').cliqueItemWidget({

		checkStock: true,
		pressure: true,

		browser: 'slideshow', // thumbnails / slideshow
		removeEmptyThumbs: true, // Removes rows of empty thumbnails
		viewer: true, 
		preview: true, // Shows enlarged images when hovering thumbnails
		debug: false

	});


/* APPEND PRODUCT OUNCE SIZE TO SKU NUMBER
----------------------------------------------------------------------------- */


	if ($('div.widget-item.display div.details p:first:contains("oz")').length > 0) {

		var size = $('div.widget-item.display div.details p:first:contains("oz")').text();
		$('div.widget-item.display div.topic').find('small').append(' / ' + size);
		$('div.widget-item.display div.details p:first:contains("oz") + hr').remove();  // Removes tab marker next to product size text
		$('div.widget-item.display div.details p:first:contains("oz")').remove();  // Removes old product size text

	}


/* ADD BROWSE LINK TO PRODUCT TYPE
----------------------------------------------------------------------------- */


	$('div.widget-item.display div.topic ul.tags li').each(function() {
	
		var browseLink = $(this).text().replace(/\s/g, "_");
		$(this).wrapInner('<a href="/browse/' + browseLink + '"></a>');
	
	});


/* SPLIT PRODUCT DETAILS INTO TABS
----------------------------------------------------------------------------- */


	if ($('div.widget-item.display div.details div.markup').length > 0) {

		var tabs;  // Array of tabs with product details
		var marker = "----------";  // Horizontal rule for splitting product details

		// Fill each tab with product details
		tabs = $('div.widget-item.display div.details div.markup').html().replace(/<hr>/g, marker).replace(/<HR>/g, marker).split(marker);

		// Prepare container for tabs
		$('div.widget-item.display div.details').addClass('tabbed');
		$('div.widget-item.display div.details').prepend('<ul class="tabs"></ul>');

		for (var i = 0; i < tabs.length; i++) {

			if (i > 0) {

				// Add panel to tabbed container
				$('div.widget-item.display div.details').append('<div>' + tabs[i] + '</div>');
	
				// Check for heading in newly added panel
				if ($('div.widget-item.display div.details div:last h4').length > 0) {
	
					var tabName = $('div.widget-item.display div.details div:last h4').text();  // Tab name
					var panelID = $('div.widget-item.display div.details div:last h4').text().replace(/ /, "").toLowerCase();  // ID of panel
	
					// Append tab
					$('div.widget-item.display div.details ul.tabs').append('<li><a class="tab ' + panelID + '" href="#' + panelID + '">' + tabName + '</a></li>');
	
					// Add ID + Markup class to panel
					$('div.widget-item.display div.details div:last').attr('id', panelID).attr('class', 'markup');
	
				}

			}

		}

		// Merge product name and tagline with Key Benefits panel
		if ($('div.widget-item.display div.details div#overview').length > 0) {
			$('div.widget-item.display div.topic h3, div.widget-item.display div.topic em').prependTo('div.widget-item.display div.details div#overview');
		} else {
			$('div.widget-item.display div.topic h3, div.widget-item.display div.topic em').prependTo('div.widget-item.display div.details div#keybenefits');
		}

		// Add link break before "with" in product name
		var productName = $('div.widget-item.display div.details div#keybenefits h3').text().replace(/ with/, "<br />with");
		$('div.widget-item.display div.details div#keybenefits h3').html(productName);

		// Remove old details container
		$('div.widget-item.display div.details div.markup:first').remove();

	}


/* ADD PRODUCT TYPE DESCRIPTION TO TABBED CONTAINER
----------------------------------------------------------------------------- */

/*
	if ($('div.widget-tag.display ul.tags li.description:not(.featured)').length > 0) {
	
		var tabName = $('div.widget-tag.display ul.tags li.description:not(.featured) h3').text();  // Tab name
		var panelID = $('div.widget-tag.display ul.tags li.description:not(.featured) h3').text().replace(/ /, "").toLowerCase();  // ID of panel

		$('div.widget-tag.display ul.tags li.description:not(.featured) h3').remove();

		// Append tab
		$('div.widget-item.display div.details ul.tabs').append('<li><a class="tab ' + panelID + '" href="#' + panelID + '">' + tabName + '</a></li>');

		// Add panel to tabbed container
		$('div.widget-item.display div.details').append('<div id="' + panelID + '">' + $('div.widget-tag.display ul.tags li.description:not(.featured)').html() + '</div>');

		// Remove tag container
		$('div.widget-tag.display').remove();
	
	}*/


/* ADD GLOSSARY LINKS TO INGREDIENTS
----------------------------------------------------------------------------- */


	if ($('div.widget-item.display div.details #ingredients').length > 0) {

		$.get('http://www.pangeaorganics.com/feeds/Glossary.xml?items=100', function(data) {

			var Glossary = [];  // Array of ingredient Glossary
			var productIngredients; // Array of product's ingredients
			var ingredientLinks = ""; // Product's ingredients with matching links
			var i = 0;
	
			// Load Glossary
			$(data).find('topic').each(function() {

				var generalName = $(this).find('headline').text();
				var latinName = $(this).find('byline').text().replace(/By /,"");
				var url = $(this).find('url-name').text();

				var ingredient = [];  // Array of ingredient details
				ingredient[0] = generalName.toUpperCase();
				ingredient[1] = latinName.toUpperCase();
				ingredient[2] = url;

				// Nullify empty indices
				if (ingredient[0] == "") { ingredient[0] = "undefined"; }
				if (ingredient[1] == "") { ingredient[1] = "undefined"; }
				if (ingredient[2] == "") { ingredient[2] = "undefined"; }

				Glossary.push(ingredient);
		
			});
	
			// Load product's ingredients
			productIngredients = $('div.widget-item.display div.details #ingredients p:first').text().split(', ');

			for (i = 0; i < productIngredients.length; i++) {
			
				for (var j = 0, match = false; j < Glossary.length; j++) {

					// Checks for a matching scientific or general name, then wraps ingredient with corresponding link
/*					if ( productIngredients[i].match(Glossary[j][1])) {		*/
//|| productIngredients[i].replace(/\(/,"").replace(/\)/,"").replace(/\*/g,"").match($Glossary[j][0]) 
/*						ingredientLinks += '<a href="/ingredient/ingredient_glossary/' + Glossary[j][2] + '">' + productIngredients[i] + '</a>';

						// Add comma after ingredient
						if (i < productIngredients.length - 1) {
						
							ingredientLinks += ", ";
						
						}
						
						match = true;

					}		*/

					// If no match, appends linkless ingredient
/*					else {	*/
					
						if ((j == Glossary.length - 1) && !match) {

							ingredientLinks += productIngredients[i];

							// Add comma after ingredient
							if (i < productIngredients.length - 1) {
						
								ingredientLinks += ", ";
							
							}
						
						}
					
/*					}*/
					
				}
			
			}

			$('div.widget-item.display div.details #ingredients p:first').html(ingredientLinks);

		});	
				
	}


/* UNHIDE CONTAINER
----------------------------------------------------------------------------- */


	$('div.corner').show();
	$('div.widget-item.display').show();



/* END LOAD FUNCTION
----------------------------------------------------------------------------- */	


	});
