/*
		File:			ProductHelper class
		Version:	2.8
		Author:		Eric Shepherd
*/

function ProductHelper(element) {
	this._element = element ? element : document.getElementById('product-media');
	this._thumbThumbnailClassString = 'product';
	this._thumbThumbnailClass = /product/;
	this._thumbImageClass = /product-image/;
	this._thumbThumbnailContainer = 'dl';
	this._thumbProductNameElement = 'dt';
	this._thumbProductDescriptionElement = 'dd';
	this._prodThumbnailContainer = 'product-thumbnails';
	this._prodThumbnailElements = 'li';
	this._prodThumbnailLinks = 'a';
	this._activeClass = 'active';
	this._activeRegexp = /active/;
	this._productShotId = 'product-shot';
	this._productShotImageContainer = 'dt';
	this._productShotCaptionContainer = 'dd';
	this._initialImageIndex = 0;
	this._storage = '';
}

ProductHelper.prototype.setThumbThumbnailClassString = function(value) {
	this._thumbThumbnailClassString = value;
}

ProductHelper.prototype.setThumbThumbnailClass = function(value) {
	this._thumbThumbnailClass = value;
}

ProductHelper.prototype.setThumbImageClass = function(value) {
	this._thumbImageClass = value;
}

ProductHelper.prototype.setThumbThumbnailContainer = function(value) {
	this._thumbThumbnailContainer = value;
}

ProductHelper.prototype.setThumbProductNameElement = function(value) {
	this._thumbProductNameElement = value;
}

ProductHelper.prototype.setThumbProductDescriptionElement = function(value) {
	this._thumbProductDescriptionElement = value;
}

ProductHelper.prototype.setProdThumbnailContainer = function(value) {
	this._prodThumbnailContainer = value;
}

ProductHelper.prototype.setProdThumbnailElements = function(value) {
	this._prodThumbnailElements = value;
}

ProductHelper.prototype.setProdThumbnailLinks = function(value) {
	this._prodThumbnailLinks = value;
}

ProductHelper.prototype.setActiveClass = function(value) {
	this._activeClass = value;
}

ProductHelper.prototype.setActiveRegexp = function(value) {
	this._activeRegexp = value;
}

ProductHelper.prototype.setProductShotId = function(value) {
	this._productShotId = value;
}

ProductHelper.prototype.setProductShotImageContainer = function(value) {
	this._productShotImageContainer = value;
}

ProductHelper.prototype.setProductShotCaptionContainer = function(value) {
	this._productShotCaptionContainer = value;
}

ProductHelper.prototype.setInitialImageIndex = function(value) {
	this._initialImageIndex = value;
}

ProductHelper.inactiveLink = new Array();

ProductHelper.AutoInit = function()
{
	if (!SupportTest.hasDom) return;
	var element = document.getElementById('product-media');
	if (typeof element != 'undefined') {
		var _ProductHelper = new ProductHelper(element);
		_ProductHelper.init();
	}
}

ProductHelper.prototype.init = function()
{ 
	
	if (!SupportTest.hasDom) return;
	this.addLinkElements();
	var theThumbnailContainer = document.getElementById(this._prodThumbnailContainer);
	
	if (theThumbnailContainer) {
		var theEls = theThumbnailContainer.getElementsByTagName(this._prodThumbnailElements);
		var theLinks = theThumbnailContainer.getElementsByTagName(this._prodThumbnailLinks);
		theEl = theEls[this._initialImageIndex];
		for (var i=0; i<theLinks.length; i++) {
			var thisReference = this;
			EventHelper.AddEvent(theLinks[i], 'click', swapPtr = function(e) {thisReference.swapImages(this, thisReference, e); }, false);
			if (SupportTest.isSafari) theLinks[i].onclick = EventHelper.CancelClickSafari;
		}
		this._storage = DomHelper.ActivateElement(theEl, this._storage);
	}
}

ProductHelper.prototype.addLinkElements = function()
{
	if (!SupportTest.hasDom) return; 
	var theThumbnailArray = DomHelper.GetElementsByClassName(this._thumbThumbnailClassString, this._thumbThumbnailContainer);
	var theProductName; 
	var theProductDescriptions; 
	var theProductLink;
	var theProductHref;
	var theNewLink;
	for (var j=0; j<theThumbnailArray.length; j++) {
		theProductName = theThumbnailArray[j].getElementsByTagName(this._thumbProductNameElement)[0];
		theProductDescriptions = theThumbnailArray[j].getElementsByTagName(this._thumbProductDescriptionElement);
		theProductLink = theProductName.getElementsByTagName('a')[0];
		theProductHref = theProductLink.href;
		for (var k=0; k<theProductDescriptions.length; k++) {
			if (theProductDescriptions[k].className.match(this._thumbImageClass)) {
				var theImg = theProductDescriptions[k].getElementsByTagName('img')[0];
				theNewLink = document.createElement('a');
				theNewLink.href = theProductHref;
				theNewLink.appendChild(theImg);
				theProductDescriptions[k].appendChild(theNewLink);
			}
		}
		DomHelper.SpawnHovers(theProductLink, theNewLink);
	}
}

ProductHelper.prototype.swapImages = function(el, thisReference, e) 
{
	var theLink = el.href;
	var theMediaTypes = el.className.match(/mt_\w+/);
	var theMediaType = theMediaTypes != null ? theMediaTypes[0] : null;
	var theThumbnailContainer = document.getElementById(this._prodThumbnailContainer);
	var theThumbnailElements = theThumbnailContainer.getElementsByTagName(this._prodThumbnailElements);
	for (i=0; i<theThumbnailElements.length; i++) {
		if (theThumbnailElements[i].className.match(this._activeRegexp)) {
			DomHelper.DeactivateElement(theThumbnailElements[i], this._storage);
		}
	}
	var activeEl = DomHelper.AscendDom(el, this._prodThumbnailElements);
	this._storage = DomHelper.ActivateElement(activeEl, this._storage);
	var theShot = document.getElementById(this._productShotId); 
	var theImageParent = theShot.getElementsByTagName(this._productShotImageContainer)[0]; 
	var theCaptionEl = theShot.getElementsByTagName(this._productShotCaptionContainer)[0]; 
	var theNewCaption = el.title.replace('\\','');
	this.replaceText(theCaptionEl, theNewCaption);
	this.replaceMedia(theImageParent, theLink, theMediaType, el); 
	EventHelper.CancelDefault(e);
}

ProductHelper.prototype.replaceText = function(el, text)
{
	el.innerHTML = text;
}

ProductHelper.prototype.replaceMedia = function(el, href, mediatype, linkElement)
{
	var index = this.getIndex(href);
	var productnumber = this.getProductNumber(href);
	this.setLargerImageHref(index);	
	if ((mediatype == null) || ((typeof UFO != 'object') && (typeof swfobject != 'object')) ) mediatype = 'default';
	switch(mediatype) {
		case 'mt_productshotdemo' : 
			this.renderFlashProductShot(productnumber, index, el, linkElement);
			break;
		default : 
			this.renderProductShot(href, el);
			break;
	}
}

ProductHelper.prototype.getIndex = function(href)
{
	var pathparts = href.split('/');
	var filename = pathparts[pathparts.length-1];
	var fileparts = filename.split('_');
	var last = fileparts[fileparts.length-1];
	var dots = last.split('.');
	return dots[0];
}

ProductHelper.prototype.getProductNumber = function(href) 
{
	var pathparts = href.split('/');
	var filename = pathparts[pathparts.length - 1];
	var fileparts = filename.split('_');
	return fileparts[0];
}

ProductHelper.prototype.setLargerImageHref = function(index) 
{
	var theLargerLinks = DomHelper.GetElementsByClassName('product-view-larger', this._productShotCaptionContainer, this._element);
	if (theLargerLinks.length > 0) {
		var theLink = theLargerLinks[0].getElementsByTagName('a')[0];
		var theHref = theLink.getAttribute('href');
		theHref = theHref.replace(/image=\w*/, 'image=' + index);
		theLink.setAttribute('href', theHref);
	}
}

ProductHelper.prototype.renderFlashProductShot = function(productnumber, index, el, linkElement)
{
	//movieDemo.flashvars = 'audioPath=/content/v4/us/audio/&imgPath=/img/product_shots/&pn='+productnumber+'&index='+index+'';
	// TODO: get the US out of there...
	//UFO.create(movieDemo, el.id);
	
	// flashImage - a public variable defined on the page. e.g. \pages\v5\us\imaginext\product.aspx
	if(typeof UFO == 'object'){
		flashImage.movie = linkElement.rel;
		UFO.create(flashImage, el.id);
	}
	if(typeof swfobject == 'object'){
		flashImage.flashArgs.swfUrl = linkElement.rel;
		swfobject.embedSWF(
				flashImage.flashArgs.swfUrl,
				flashImage.flashArgs.id, 
				flashImage.flashArgs.width, 
				flashImage.flashArgs.height, 
				flashImage.flashArgs.version, 
				flashImage.flashArgs.expressInstallSwfUrl, 
				flashImage.flashVars, 
				flashImage.flashParams, 
				flashImage.flashAttrs
			);
	}
}

ProductHelper.prototype.renderProductShot = function(href, el)
{
	var theImage = document.createElement('img');
	var tmp = new Date();
	var suffix = tmp.getTime();
	theImage.src = href +'?' + suffix;
	
	var theSpan = document.createElement('span');
	theSpan.id = 'product-image-flash';
	theSpan.style.visibility = "visible";
	theSpan.appendChild(theImage);
	
	var theChildren = el.childNodes;
	try {
		var c = el.removeChild(theChildren[0]);
	} catch (e) {
	}
	imageAppended = false;
	if (SupportTest.isIE) {
		var imageLoaded = setInterval(appendImage, 1000);
	
		function appendImage() {
			if (imageAppended == false) {
				el.appendChild(theSpan);
				imageAppended = true;
				clearInterval(imageLoaded);
			}
		}
	}
	theImage.onload = function() {
		if (imageAppended == false) {
			el.appendChild(theSpan);
			imageAppended = true;
			if (SupportTest.isIE) {
				clearInterval(imageLoaded);
			}
		}
	}
}