if (SC && !SC.callbacks.pageLoad) SC.callbacks.pageLoad = new SC.Callback();

/*
 *	Call right before the </body> tag.
	<script type="text/javascript">
		if (SC.callbacks.pageLoad) SC.callbacks.pageLoad.run({t: window});
	</script>
*/


// highlight area
function scHighlightArea(el) {
	if (!el) return;
	if (el.runningAnim) el.runningAnim.stop();

	function colorIsTransparent(color) {
		var rgba = getRGBA(color); // Safari returns a RGBA color with alpha 0
		return (typeof color === 'undefined' || color === '' || color === 'transparent' || (rgba && parseInt(rgba[4]) === 0));
	}

	function getRGBA(color) {
		return color.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)/);
	}

	function colorToHex(color) {
		if (typeof color !== 'string') return;
		var hexChars = '0123456789abcdef',
		    rgb = getRGBA(color),
		    toHex = function(num) { return hexChars.charAt(Math.floor(num / 16)) + hexChars.charAt(num % 16); };
		return (rgb && rgb.length >= 4) ? (toHex(rgb[1]) + toHex(rgb[2]) + toHex(rgb[3])) : color.replace(/^#/, '');
	}

	function findBackground(el) {
		if (!el) return false;
		var a = SC.getStyle(el, 'background-color') || SC.getStyle(el, 'backgroundColor');
		return colorIsTransparent(a) ? findBackground(el.parentNode) : a
	}

	var storedBackground = SC.getStyle(el, 'background-color') || SC.getStyle(el, 'backgroundColor'),
	    areaBackground = findBackground(el);
	el.runningAnim = new SC.Anim(el, {
		del: 1,
		styles: {
			backgroundColor: {
				start: 'ffffcc',
				end: colorToHex(areaBackground) || 'ffffff'
			}
		},
		cb: function() {
			el.runningAnim = null;
			el.style.backgroundColor = colorIsTransparent(storedBackground) ? '' : storedBackground;
		}
	});
	el.runningAnim.run();
}

// realtime price
SC.callbacks.pageLoad.add(function() {
	var pit = SC.$('product-info-table');
	if (!pit) return;

	var priceBox = SC.$('pitSalePriceBx') || SC.$('pitPriceBx');
	if (!priceBox) return;

	var basePrice = SC.forceFloat(priceBox.innerText || priceBox.textContent),
	    selects = SC.getByTag('select', pit),
	    priceGrabber = /\([\s+\.\d\$-]+\)\s*$/,
	    priceCleaner = /\$/,
	    getOptionPrice = function(select) {
		if (!select.disabled && typeof select.value === 'string') {
			var matches = select.value.match(priceGrabber);
			return matches ? SC.forceFloat(matches[0].replace(priceCleaner, '')) : 0;
		}
	    },
	    calculatePrice = function() {
		var price = basePrice;
		for (var j = 0, s; s = selects[j]; j++)
			price += getOptionPrice(s);
		SC.orphan(priceBox, true);
		SC.appElem(document.createTextNode(SC.toPrice(price)), priceBox);
	    };

	for (var i = 0, select; select = selects[i]; i++)
		SC.addEvt(select, 'change', function(e) {
			var oldPrice = priceBox.innerText || priceBox.textContent;
			calculatePrice();
			var newPrice = priceBox.innerText || priceBox.textContent;
			if (oldPrice !== newPrice) scHighlightArea(priceBox);
		});
	calculatePrice();
	// and one more for IE, who doesn't set the SELECT's values until after the window.onload event
	if (SC.Browser.name === 'ie') SC.addEvt(window, 'load', calculatePrice);
});

