/**
* common library of javascript functions
*/

function addEvent(obj, evType, fn)
{
    if (obj.addEventListener)
    {
        obj.addEventListener(evType, fn, true);
        return true;
    }
    else if (obj.attachEvent)
    {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    }
    else
    {
        return false;
    }
}

/**
* fix alpha pngs on a page (turn on transparency for IE)
*/
function fixAlphaPng() {
    var src     = '';
    var ua      = '';
    var width   = '';
    var height  = '';
//    var rpng    = new RegExp('\.(png|jpg)$');
	var rpng    = new RegExp('\.(png)$');
    var rmsie   = new RegExp('msie');
    var rmac    = new RegExp('mac');

    for (var i = 0; i < document.getElementsByTagName('IMG').length; i++) {
        src     = document.getElementsByTagName('IMG')[i].getAttribute('src');
        ua      = navigator.userAgent.toLowerCase();
        width   = document.getElementsByTagName('IMG')[i].getAttribute('width');
        height  = document.getElementsByTagName('IMG')[i].getAttribute('height');

        if (src.match(rpng) && ua.match(rmsie) && !ua.match(rmac)) {
        	document.getElementsByTagName('IMG')[i].setAttribute('src', '/images/spacer.gif');
            document.getElementsByTagName('IMG')[i].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\', sizingMethod=scale)';
           	document.getElementsByTagName('IMG')[i].style.width = width + 'px';
            document.getElementsByTagName('IMG')[i].style.height = height + 'px';            
        }
    }
}
addEvent(window, 'load', fixAlphaPng);

/* Nav Fix for IE hover */
startList = function() {
	if (document.all&&document.getElementById) {
		//Top Nav
		if (document.getElementById("nav")) {
			topNavRoot = document.getElementById("nav");
			for (i=0; i<topNavRoot.childNodes.length; i++) {
				node = topNavRoot.childNodes[i];
				//alert(node.nodeName);
				if (node.nodeName=="LI") {
					//Sub UL
					for (j=0; j<node.childNodes.length; j++) {
						subTopNavRoot = node.childNodes[j];
						if (subTopNavRoot.nodeName=="UL") {
							for (k=0; k<subTopNavRoot.childNodes.length; k++) {
								subnode = subTopNavRoot.childNodes[k];
								if (subnode.nodeName=="LI") {
									subnode.onmouseover=function() {
										this.className="over "+this.className;
									}
									subnode.onmouseout=function() {
										this.className=this.className.replace("over ", "");
									}
								}
							}
						}
					}
					node.onmouseover=function() {
						//alert(1);
						this.className="over "+this.className;
					}
					node.onmouseout=function() {
						this.className=this.className.replace("over ", "");
					}
				}
			}
		}
		
		//Left Nav
		if (document.getElementById("nav2")) {
			lftNavRoot = document.getElementById("nav2");
			for (i=0; i<lftNavRoot.childNodes.length; i++) {
				node = lftNavRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className="over "+this.className;
					}
					node.onmouseout=function() {
						this.className=this.className.replace("over ", "");
					}
				}
			}
		}
		//Right Nav
		if (document.getElementById("nav3")) {
			rtNavRoot = document.getElementById("nav3");
			for (i=0; i<rtNavRoot.childNodes.length; i++) {
				node = rtNavRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className="over "+this.className;
					}
					node.onmouseout=function() {
						this.className=this.className.replace("over ", "");
					}
				}
			}
		}
	}
}
addEvent(window, 'load', startList);


/*
Redirect to no cookies page
*/
function noCookies(redirect) {
	var tmpcookie = new Date();
	chkcookie = (tmpcookie.getTime() + '');
	document.cookie = "chkcookie=" + chkcookie + "; path=/";
	if (document.cookie.indexOf(chkcookie,0) < 0) {
		window.location = redirect;
	} else {
		return("true");
	}
}

/*
Clear default value of text input
*/
function clearField(thefield) {
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
}


/*
General image swap function
*/
function imgSwap(id, src) {
    var rpng    = new RegExp('\.png$');
    var rmsie   = new RegExp('msie');
    var rmac    = new RegExp('mac');
    var ua      = navigator.userAgent.toLowerCase();
	if (document.getElementById(id)) {
		document.getElementById(id).setAttribute('src', src);
	}
	if (document.getElementById(id) && src.match(rpng) && ua.match(rmsie) && !ua.match(rmac)) {
		fixAlphaPng();
	}
}


/*
PNG filter image swap function - moved functionality to imgSwap
*/
function pngSwap(id, src) {
	imgSwap(id, src)
}


/*
Your Hair rollovers
*/
function yrhrRoll(img, cnt) {
	imgSwap('roll'+cnt, 'images/'+img);
}

/*
Left nav image map rollovers
*/
function imgMapRoll(num, over) {
	if (over) {
		document.getElementById('nav2_'+num).className+=' over';
	} else {
		document.getElementById('nav2_'+num).className=document.getElementById('nav2_'+num).className.replace('over', '')
	}
}

/*
pop up new scrollable, resizable window
*/
function popup(url,w,h) {
	if (!w) {w='340';}
	if (!h) {h='340';}
	newPopup = window.open (url, 'infusiumpop', 'resizable,scrollbars,width='+w+',height='+h);
	newPopup.focus();
}

/*
home news rollover
*/
var prevcnt=0;
function showNews(cnt) {
	imgSwap('newsimg', 'images/rinse'+cnt+'.gif');
	document.getElementById('newstxt'+prevcnt).style.display='none';
	document.getElementById('newstxt'+cnt).style.display='block';
	prevcnt=cnt;
}

/*
creates the label popups
*/
function OpenLabelPopup(strTitle, strSrc)
{
	var objPopup = window.open("/our_collections/label.jsp", "Label", 'scrollbars,resizable,height=721,width=476');
	
	// Dynamically create the HTML 
	objPopup.document.write('<html>\n');
	objPopup.document.write('\t<head>\n')
	objPopup.document.write('\t\t<title>' + strTitle + '</title>\n');
	objPopup.document.write('\t\t<link rel="stylesheet" type="text/css" href="/includes/global.css">\n');		
	objPopup.document.write('\t</head>\n');
	objPopup.document.write('\t<body>\n')		
	objPopup.document.write('\t\t<div id="CloseLinkContainer">\n');
	objPopup.document.write('\t\t\t<a onclick="javascript:window.close()" href="#"><img src="/images/arrow_off.gif">CLOSE WINDOW</a>\n');
	objPopup.document.write('\t\t</div>\n')
	objPopup.document.write('\t\t<div id="LabelImageContainer">\n');
	objPopup.document.write('\t\t\t<img src="' + strSrc + '" alt="' + strTitle + '" />\n');
//	objPopup.document.write('\t\t\t<img src="images/label_repairandrenew_3.jpg" alt="' + strTitle + '" />\n');
	objPopup.document.write('\t\t</div>\n');
	objPopup.document.write('\t</body>\n')
	objPopup.document.write('<html>\n')
}