/*--------------------------------------------------------------------------*
 * 
 * Volusion(C) PowerReviews(C) Integration (Dakota)
 * 
 * Version 1.0.8
 * 
 * Copyright (C) 2009 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*
 * Settings
 *--------------------------------------------------------------------------*/
var DakotaSettings = {
	ENABLED: false,
	
	MERCHANT_ID: null,
	PRODUCT_REVIEW_SNIPPET_DOCUMENT: null,
	PRODUCT_REVIEW_DOCUMENT: null,		
	OVERRIDE_STYLE_SHEET_HTML: null,
	USE_BASE_STYLE_SHEET: true,
	USE_MERCHANT_OVERRIDE_STYLE_SHEET: true,
	
	CATEGORY_REVIEW_SNIPPET_DOCUMENT: null,
	PRODUCTS_CONTAINER_ID: 'MainForm',
	MORE_INFO_FILE_NAME: 'Bullet_MoreInfo.gif',
	ADD_TO_CART_FILE_NAME: 'btn_addtocart_small.gif',
	NO_PHOTO_FILE_NAME: 'nophoto.gif',
	CATEGORY_REVIEW_SNIPPET_CSS_CLASS: null,
	CATEGORY_MINIMUM_REVIEWS_TO_DISPLAY: 0,
	PRODUCT_MINIMUM_REVIEWS_TO_DISPLAY: 0
};
/*--------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------*
 * Main Module
 *--------------------------------------------------------------------------*/
var Dakota = {
	load: function() {
		//Not enabled, exit
		if(!DakotaSettings.ENABLED) {
			return;
		}
		
		//Always skip if under SSL
		if('https:' == document.location.protocol) {
			return;
		}
		
		//PRODUCT				
		//Only load on the product detail page (per Volusion KB)
		if (location.pathname.toLowerCase() == '/productdetails.asp' ||
			location.pathname.toLowerCase().indexOf('-p/') != -1 ||
			location.pathname.toLowerCase().indexOf('_p/') != -1) {
			//Load the require data	
			Dakota.addPrerequisites();
			
			//Start everything when the window loads
			Event.observe(window, 'load', Dakota.windowLoadProduct);
		} 
		//CATEGORY
		//Only load on the category page (per Volusion KB)		
		else if (location.pathname.toLowerCase() == '/searchresults.asp' ||
				location.pathname.toLowerCase().indexOf('-s/') != -1 ||
				location.pathname.toLowerCase().indexOf('_s/') != -1) {
			//Load the require data
			Dakota.addPrerequisites();
			
			//Start everything when the window loads
			Event.observe(window, 'load', Dakota.windowLoadCategory);			
		} 		
	},
	
	addPrerequisites: function() {
		//Output the JS file to use
		document.write('<scr' + 'ipt type="text/javascript" src="http://cdn.powerreviews.com/repos/' 
			+ DakotaSettings.MERCHANT_ID
			+ '/pr/pwr/engine/js/full.js' + '"><\/scr' + 'ipt>');
		
		//Force adding the CSS files
		if(DakotaSettings.USE_BASE_STYLE_SHEET) {
			document.write('<link id="prBaseStylesheet" type="text/css" href="http://cdn.powerreviews.com/repos/' 
				+ DakotaSettings.MERCHANT_ID 
				+ '/pr/pwr/engine/pr_styles_review.css" rel="stylesheet"/>');	
		}			
		if(DakotaSettings.USE_MERCHANT_OVERRIDE_STYLE_SHEET) {
			document.write('<link id="prMerchantOverrideStylesheet" type="text/css" href="http://cdn.powerreviews.com/repos/' 
				+ DakotaSettings.MERCHANT_ID
				+ '/pr/pwr/engine/merchant_styles2.css" rel="stylesheet"/>');
		}
		if(DakotaSettings.OVERRIDE_STYLE_SHEET_HTML != null) {
			document.write(DakotaSettings.OVERRIDE_STYLE_SHEET_HTML);
		}		
	},
	
	windowLoadCategory: function() {
		var contentAreaElement = null;
				
		//Get the content area element
		contentAreaElement = $(DakotaSettings.PRODUCTS_CONTAINER_ID);		
		if(contentAreaElement == null) {
			return;
		}
		
		//Create a temporary class for creating instances
		var TemporaryClass = Class.create(DakotaSettings.CATEGORY_REVIEW_SNIPPET_DOCUMENT, {});
		
		//Go through all anchors that are available
		anchors = contentAreaElement.select('a');
		anchors.each(function(anchor) {
			var productCode = null;
			var images = null;
			var image = null;
			
			//Get the product code from the URL, else skip
			productCode = Dakota.getProductCodeFromURL(anchor.href);			
			if(productCode == null) {
				return;
			}
			
			//Make the product code upper case only
			productCode = productCode.toUpperCase();
						
			//Get all the images
			images = anchor.select('img');
			
			//Must have one image, skip if not
			if(images == null || images.size() != 1) {
				return;
			}			
			image = images.first();
			
			//Special image to skip!
			if(image.src.toLowerCase().indexOf(DakotaSettings.MORE_INFO_FILE_NAME.toLowerCase()) != -1) {
				return;
			}
			if(image.src.toLowerCase().indexOf(DakotaSettings.ADD_TO_CART_FILE_NAME.toLowerCase()) != -1) {
				return;
			}
			//Not this products photo; make sure this is not a no photo image
			if(image.src.toLowerCase().indexOf(DakotaSettings.NO_PHOTO_FILE_NAME.toLowerCase()) == -1) {
				if(image.src.toUpperCase().indexOf(productCode) == -1) {
					return;
				}				
			}
			
			//Load PowerReviews
			POWERREVIEWS.display.snippet(
				new TemporaryClass(DakotaSettings.CATEGORY_REVIEW_SNIPPET_CSS_CLASS, anchor), 
				{
					pr_page_id: productCode, 
					pr_read_review: anchor.href + '#ReviewHeader',
					pr_snippet_min_reviews: DakotaSettings.CATEGORY_MINIMUM_REVIEWS_TO_DISPLAY
				}
			);
		});		
	},
	
	windowLoadProduct: function() {
		var productCode = null;
		
		//Get the product code from the URL
		productCode = Dakota.getProductCodeFromURL(window.location.href);
		
		//Make sure a product code is available
		if(productCode == null) {
			return;
		}
		
		//Make the product code upper case only
		productCode = productCode.toUpperCase();
		
		//Display review
		POWERREVIEWS.display.snippet(DakotaSettings.REVIEW_SNIPPET_DOCUMENT, {
			pr_page_id: productCode,
			pr_snippet_min_reviews: DakotaSettings.PRODUCT_MINIMUM_REVIEWS_TO_DISPLAY
		});
	    POWERREVIEWS.display.engine(DakotaSettings.REVIEW_DOCUMENT, {pr_page_id: productCode});		
	},
	
	/*--------------------------------------------------------------------------*
	 * 
	 * Category Zoom (Whitney) - getProductCodeFromURL
	 * 
	 * Version 2.0.4
	 * 
	 * Copyright (C) 2008 Brand Labs LLC
	 * 
	 * This library is free software; you can redistribute it and/or
	 * modify it under the terms of the GNU Lesser General Public
	 * License as published by the Free Software Foundation; either
	 * version 2.1 of the License, or any later version.
	 * 
	 * This library is distributed in the hope that it will be useful,
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	 * Lesser General Public License for more details.
	 * 
	 * You should have received a copy of the GNU Lesser General Public
	 * License along with this library; if not, write to the Free Software
	 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
	 * 02110-1301  USA
	 * 
	 *--------------------------------------------------------------------------*/
	getProductCodeFromURL: function(url) {
		var matches = null;
		
		//Make sure we do not have a null
		if(url == null) {
			return null;
		}
		
		//Check non-SEO URL
		matches = url.match(/[?&]productcode(?:\=([^&]*))?/i);	
		if(matches != null && matches.length >= 2) {
			return unescape(matches[1]);
		}
		
		//Check SEO URL
		matches = url.match(/(?:_p|-p)\/(.+)\.htm/i);	
		if(matches != null && matches.length >= 2) {
			return unescape(matches[1]);
		}
	
		//No product code available
		return null;
	}
	/*--------------------------------------------------------------------------*/	
};
/*--------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------*
 * Proxy for built-in document
 *--------------------------------------------------------------------------*/
var DocumentProxy = {
	anchors: document.anchors,
	forms: document.forms,
	images: document.images,
	links: document.links,
	body: document.body,
	cookie: document.cookie,
	domain: document.domain,
	lastModified: document.lastModified,
	referrer: document.referrer,
	title: document.title,
	URL: document.URL,
	
	close: function() {
		document.close();
	},
	
	getElementById: function(id) {
		return document.getElementById(id);
	},
	
	getElementsByName: function(name) {
		return document.getElementsByName(name);
	},
	
	getElementsByTagName: function(tagname) {
		return document.getElementsByTagName(tagname);
	},
	
	open: function() {
		document.open();
	},
	
	write: function(content) {		
	},
	
	writeln: function(content) {
		this.write(content); this.write('\n');
	}
};
/*--------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------*
 * Document that outputs only to a single location (Abstract)
 *--------------------------------------------------------------------------*/
var FixedLocationDocument = Class.create(DocumentProxy, {
	initialize: function(containerId) {
		this.containerId = containerId;
		this.container = null;
	},
	
	createContainer: function() {
		var element = null;
		
		if(this.container != null) {
			return this.container;
		}
		
		element = $(this.containerId);
		
		//If the element exists, just return it
		if(element != null) {
			this.container = element; //Store the element			
			return element;
		}
		
		//Create the element
		element = new Element('div', {id: this.containerId});
		
		//Add the element to whereever is needed
		this.addElementToFixedLocation(element);
		
		return element;
	},
	
	addElementToFixedLocation: function(element) {		
	},
	
	write: function(content) {
		//Retrieve or create the container
		var container = this.createContainer();
		
		//Add the content to the container
		container.insert(content);
	}
});
/*--------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------*
 * Output directly below product photo
 *--------------------------------------------------------------------------*/
var BelowProductPhotoDocument = Class.create(FixedLocationDocument, {
	IMAGE_ELEMENT_ID: 'product_photo',
	ANCHOR_ELEMENT_ID: 'product_photo_zoom_url',
	
	addElementToFixedLocation: function(element) {
		var container = $(this.ANCHOR_ELEMENT_ID);
		
		//Make sure something is available
		if(container == null) {
			container = $(this.IMAGE_ELEMENT_ID);
		}
		//If still do not have a container, exit
		if(container == null) {
			return;	
		}		
		
		//Add element after container
		container.insert({after: element});
	}
});
/*--------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------*
 * Output directly below product description
 *--------------------------------------------------------------------------*/
var BelowProductDescriptionDocument = Class.create(FixedLocationDocument, {
	FORM_NAME: 'MainForm',
	
	addElementToFixedLocation: function(element) {
		var containers = document.getElementsByName(this.FORM_NAME);
		var container = null;
		
		//Make sure something is available
		if(containers.length <= 0) {
			return;
		}
		
		//Get the element object
		container = $(containers[0]);		
		if(container == null) {
			return;
		}
		
		//Add the element	
		container.insert(element);
	}
});
/*--------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*
 * Output directly below product description (either form or description box)
 *--------------------------------------------------------------------------*/
var BelowProductDescriptionAdvancedDocument = Class.create(FixedLocationDocument, {
	FORM_NAME: 'MainForm',
	DESCRIPTION_CLASS: 'colors_descriptionbox',
	
	addElementToFixedLocation: function(element) {
		var containers = document.getElementsByName(this.FORM_NAME);
		var container = null;
		var elements = null;		
		
		//Make sure something is available
		if(containers.length <= 0) {
			return;
		}
		
		//Get the element object
		container = $(containers[0]);		
		if(container == null) {
			return;
		}
		
		//Locate the description box (if available)
		elements = container.select('.'+this.DESCRIPTION_CLASS);
		if(elements == null || elements.size() <= 0) {
			//Add the element to the form, since no other options exists
			container.insert(element);
			return;
		}
		container = elements.last();
		
		//Add the element, after the container
		container.insert({after: element});
	}
});
/*--------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*
 * Output directly below product photo and buttons
 *--------------------------------------------------------------------------*/
var BelowProductPhotoAndButtonDocument = Class.create(FixedLocationDocument, {
	IMAGE_ELEMENT_ID: 'product_photo',
	ANCHOR_ELEMENT_ID: 'product_photo_zoom_url',
	
	addElementToFixedLocation: function(element) {
		var container = $(this.ANCHOR_ELEMENT_ID);
		
		//Make sure something is available
		if(container == null) {
			container = $(this.IMAGE_ELEMENT_ID);
		}
		//If still do not have a container, exit
		if(container == null) {
			return;	
		}		
		
		//Go to parent
		container = container.up();
		//Nothing exit
		if(container == null) {
			return;
		}

		//Add element after container
		container.insert(element);
	}
});
/*--------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*
 * Fixed location anonymous container
 *--------------------------------------------------------------------------*/
var AnonymousFixedLocationDocument = Class.create(FixedLocationDocument, {
	initialize: function(containerClass, location) {
		this.container = null;
		this.containerClass = containerClass;
		this.location = location;
	},
	
	createContainer: function() {
		var element = null;
		
		if(this.container != null) {
			return this.container;
		}
		
		//Create the element
		element = new Element('div');		
		
		//Store the created container
		this.container = element;
		
		//Add the element to whereever is needed
		this.addElementToFixedLocation(element);
		
		//Add a class name to the element
		element.addClassName(this.containerClass);		
		
		return element;
	},
	
	addElementToFixedLocation: function(element) {		
	}
});
/*--------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*
 * Below location for anonymous container
 *--------------------------------------------------------------------------*/
var BelowAnonymousLocationDocument = Class.create(AnonymousFixedLocationDocument, {
	addElementToFixedLocation: function(element) {
		if(this.location == null) {
			return;
		}
		
		this.location.insert({after: element});
	}
});
/*--------------------------------------------------------------------------*/