function so_clearInnerHTML(obj) {
	// perform a shallow clone on obj
	nObj = obj.cloneNode(false);
	// insert the cloned object into the DOM before the original one
	obj.parentNode.insertBefore(nObj,obj);
	// remove the original object
	obj.parentNode.removeChild(obj);
}

/* init xmlhttp calls */
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
var http = createRequestObject();

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

//
// Returns an array of all tags that have a specific class
//
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function imagePreview()
{
	
	if (!document.getElementsByTagName){ return; }
	var images = document.getElementsByTagName("img");

	// loop through all anchor tags
	for (var i=0; i<images.length; i++)
	{
		var image = images[i];

		if (image.getAttribute("name") && (image.getAttribute("rel") == "preview"))
		{
			image.onmouseover = function () 
			{
				this.onmousemove = move;
				showPreview(this,'static');
			}
			image.onmouseout = function ()
			{
				binPreview(this);
			}
		} else if (image.getAttribute("name") && (image.getAttribute("rel") == "dynpreview"))
		{
			image.onmouseover = function () 
			{
				this.onmousemove = move;
				showPreview(this,'dyn');
				
			}
			image.onmouseout = function ()
			{
				binPreview(this);
			}
		}
	}
}

var noImage = 0;
	
function showPreview(image,sourceType)
{
	
	var newdiv = document.getElementById("imagepreview"); //get div to output rollover image into
	
	var loadimage = document.getElementById("rollover"); //get image in div imagePreview

	if(sourceType == "dyn"){
		/* get the image ajax styleeeeee*/
		
		//alert("afghaf");
		
		loadimage.src = "/images/loader.gif";

		/*handleResponse = function(){
			if(http.readyState == 4){
				if(http.responseText=='/images/spacer.gif'){
					noImage = 1;
					binPreview();
					alert("1");
				}else{
					loadimage.src = http.responseText;
					alert(loadimage.src = http.responseText);
					noImage = 0;
					alert("2");
				}
				if(noImage!=1){
					newdiv.style.position = "absolute";
					newdiv.style.border = "1px solid black";
					newdiv.style.lineheight = "0";
					alert("3");
				} else {
					newdiv.style.position = "absolute";
					newdiv.style.border = "0px";
					newdiv.style.lineheight = "0";	
					alert("4");
				}

			}		
		};*/

		//http.open('get', '/_assets/cfm/glamourHover.cfm?id=' + image.name);
		/*http.open('get', '/images/fabricSwatches/rolloverZoom/' + image.name);
		http.onreadystatechange = handleResponse;
		http.send(null);
		alert(image.getAttribute("name"));	
		loadimage.src = '/images/fabricSwatches/rolloverZoom/' + image.getAttribute("name");
		alert(loadimage.src);*/
		
		//showRollover = function(){
			//alert("ajhaghaghagahg");
		newdiv.style.position = "absolute";
		newdiv.style.border = "1px solid black";
		newdiv.style.lineheight = "0";
		loadimage.src = '/images/fabricSwatches/rolloverZoom/' + image.getAttribute("name");
		//}
		
	}else{
		/* get the image from a static folder */
		loadimage.src = '/images/fabricSwatches/rolloverZoom/' + image.getAttribute("name");
		//alert("ajhgajhgajhgaaggaghafghf");
	}

}

function binPreview(image)
{
	var previewDiv = document.getElementById("imagePreview");
	var previewImage = document.getElementById("rollover");

	previewImage.src = '/images/spacer.gif';
	previewDiv.style.left="-1000px";	
}

function move(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
		winW = document.body.offsetWidth;
		winH = document.body.offsetHeight;
		}
	}
	
	// Reset position if images falls outside the edges
	imageRollover = document.getElementById('rollover');
	if ((posx + imageRollover.width) > winW) {
		posx = posx - imageRollover.width - 40;
	}
	if ((posy + imageRollover.height) > winH) {
		posy = posy - imageRollover.height - 40;
	}

	previewDiv = document.getElementById('imagePreview');
	previewDiv.style.top = posy + 20 + 'px';
	previewDiv.style.left = posx + 20 + 'px';
}

addLoadEvent(imagePreview);


function swapImage(name, ele)
{
	var image = document.getElementById('lifestyle');

	image.src = 'images/lifestyle/' + name + '.jpg';
	
	var h2 = document.getElementsByTagName('h2');
	for(i=0; i < h2.length; i++)
	{
		h2[i].style.color = 'black';
	}
	
	ele.parentNode.childNodes[3].style.color = "red";
	
	sofaname = document.getElementById('sofaname');
	sofaname.innerHTML = capitalizeMe(name) + ' sofa';
	
	$('productlink').style.display = 'block';
}
		
function capitalizeMe(val) {
    newVal = '';
	val = val.split(' ');
	for(var c=0; c < val.length; c++) {
		newVal += val[c].substring(0,1).toUpperCase() +
		val[c].substring(1,val[c].length) + ' ';
       }
    return newVal;
}


//make dropdown work in IE6 and shift drop-down to left if required
function addHoverClass(){

	$('#mainNav li').hover(
		
		function () {
			
			//calculate offset edge of #mainNav
			var mainNav = $('#mainNav');
			var mainNavOffset = mainNav.offset({border: true}).left + mainNav.width();
			
			//set defaults - issues with width() in IE6
			var childWidth = 150;
			var childListOffset = 0;
			
			//calculate the offset edge of the child dl
			var childElement = $(this).children('dl');
			var childListLeft = findPosition(this)[0];
			var initChildWidth = childElement.width();
			if(initChildWidth != null) {
				childWidth = initChildWidth;
			}
			
			childListOffset = childListLeft + childWidth;
			
			//if child dl offset edge is beyond offset edge of #mainNav, shift to the left
			if(childListOffset > mainNavOffset) {	
				$(this).addClass('hover_right');
			} else {	
				$(this).addClass('hover');
			}
	
		}, 
		function () {
			$(this).removeClass('hover');
			$(this).removeClass('hover_right');
		}
			
	);

}

//find position of element
function findPosition( oElement ) {
	if( typeof( oElement.offsetParent ) != 'undefined' ) {
		
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
			
			posX += oElement.offsetLeft;
			posY += oElement.offsetTop;
			
		}
		
		return [ posX, posY ];
		
	} else {
		
		return [ oElement.x, oElement.y ];
		
	}
}


$(document).ready(function() {
		
	addHoverClass();		
	
});

