/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2009 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/
var ProductContentAddition = {
	URL: '/v/product_content/get_product_content.asp',
	SIDEBAR_ID: 'sidebar',
	PRODUCT_SIDEBAR_ID: 'productdetails_sidebar',
	
	load: function() {
		try {
			//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) {
				ProductContentAddition.addPrerequisites();

				//Start on window load			
				Event.observe(window, 'load', ProductContentAddition.windowLoadProduct);
			}
		}
		catch(e) {/*Ignore*/}
	},

	addPrerequisites: function() {
		//AddThis library
		document.write('<script type="text/javascript" src="http://s7.addthis.com/js/200/addthis_widget.js"></script>');
		
		//Sequential banners library
		document.write('<script type="text/javascript" src="/v/vspfiles/templates/Jungle/js/sequential_banner.js"></script>');
		document.write('<script type="text/javascript" src="/v/product_content/js/right_side_banners.js"></script>');
	},

	windowLoadProduct: function() {
		ProductContentAddition.displaySideBar();
		ProductContentAddition.displayAddThisContent();
		ProductContentAddition.displayGeneralContent();
	},

	displaySideBar: function() {
		var element = null;
		
		element = $(ProductContentAddition.SIDEBAR_ID);
		if(element != null) {
			element.show();
		}
		
		element = $(ProductContentAddition.PRODUCT_SIDEBAR_ID);
		if(element != null) {
			element.show();
		}
	},

	displayGeneralContent: function() {
		var productCode = null;
		var element = null;
		var elements = null;
		var container = new Element('div', {id: 'content_general'});
		
		//Get the product code from the URL
		productCode = ProductContentAddition.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();
		
		
		//Get element to add below
		elements = $$('table.colors_pricebox');
		if(elements == null || elements.size() <= 0) {
			return;
		}
		element = elements.last();
		if(element == null) {
			return;
		}

		//Add container after the element		
		element.insert({after: container});
		
		//Insert data through Ajax call
		new Ajax.Updater(container, ProductContentAddition.URL, {
			parameters: {productCode: productCode},
			method: 'get',
			evalJS: false,
			evalJSON: false,
			encoding: 'ISO-8859-1'
		});		
	},
	
	displayAddThisContent: function() {
		var container = $('product_photo_zoom_url');
		var element = new Element('div', {id: 'content_addthis'});
		
		//Make sure something is available
		if(container == null) {
			container = $('product_photo');
		}
		//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 content to element
		element.update('<a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s7.addthis.com/static/btn/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a>');

		//Add element after container
		container.insert(element);
	},

	/*--------------------------------------------------------------------------*
	 * 
	 * Category Zoom (Whitney) - getProductCodeFromURL
	 * 
	 * Version 3.0.0
	 * 
	 * 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(/\/productdetails\.asp\?(?:[\&]?.*\=.*)*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;
	}
	/*--------------------------------------------------------------------------*/	
};

/*--------------------------------------------------------------------------*
 * AddThis account
 *--------------------------------------------------------------------------*/
var addthis_pub = 'condomjungle';
/*--------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------*
 * Startup immediately
 *--------------------------------------------------------------------------*/
ProductContentAddition.load();
/*--------------------------------------------------------------------------*/