/*--------------------------------------------------------------------------*
 * 
 * Kaboodle(C) Integration
 * 
 * Version 1.0.0
 * 
 * Copyright (C) 2009 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/

var ProductKaboodle = {
	BUTTON: '<a href="javascript:(function(){var s=document.createElement(\'script\');s.type=\'text/javascript\';s.src=\'http://www.kaboodle.com/zg/addbutton.js?v=0.1\';document.getElementsByTagName(\'head\')[0].appendChild(s);})();"> <img src="http://www.kaboodle.com/ht/mk/img/kbutton1.gif" width="129" height="26" border="0" alt="Add To Kaboodle" /></a>',
	
	load: function() {
		//Skip if under SSL to avoid obnoxious warnings about mixed content
		if('https:' == document.location.protocol) {
			return;
		}
		
						
		//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) {
			
			//Start everything when the window loads
			Event.observe(window, 'load', ProductKaboodle.windowLoadProduct);			
		}		
	},
		
	windowLoadProduct: function(element) {
		var elements = null;		
		var element = null;
		var container = new Element('div');
				
		//Nothing available, exit
		if(obj == null) {
			return;
		}
		
		//Get element to add below, shareThis if it is present, pricebox if it is not
		elements = $$('.sharethis');
		if (elements == null || elements.size() <= 0) {
			elements = $$('table.colors_pricebox');
			if( elements == null || elements.size() <= 0) {
				return;
			}
		}
		
		//Add below the last in the collection
		element = elements.last();
		if (element == null) {
			return;
		}

		//Add a class to the container
		container.addClassName('kaboodle');
		
		//Insert the button code string into the container
		container.insert(ProductKaboodle.BUTTON);

		//Add container after the element		
		element.insert({after: container});
	}	
};

/*Start up after page load*/
try {
	ProductKaboodle.load();
}
catch(e) {/*No-op*/}