/**
* 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)
*/

//Commenting this out. Use the custom tag for applying IE transparency filter.

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', 'http://www.herbalessences.com/us/images/spacer.png');
            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);

/*
* fix alpha pngs on a page (turn on transparency for IE)
*/

//Commenting this out. Use the custom tag for applying IE transparency filter.

function fixAlphaPngById(imageId) {
    var src     = '';
    var ua      = '';
    var width   = '';
    var height  = '';
    var rpng    = new RegExp('\.(png|jpg)$');
    var rmsie   = new RegExp('msie');
    var rmac    = new RegExp('mac');

	src     = document.getElementById(imageId).getAttribute('src');
	ua      = navigator.userAgent.toLowerCase();
	width   = document.getElementById(imageId).getAttribute('width');
	height  = document.getElementById(imageId).getAttribute('height');

    if (src.match(rpng) && ua.match(rmsie) && !ua.match(rmac)) {
    	document.getElementById(imageId).setAttribute('src', 'http://www.herbalessences.com/us/images/spacer.png');
        document.getElementById(imageId).style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\', sizingMethod=scale)';
       	document.getElementById(imageId).style.width = width + 'px';
        document.getElementById(imageId).style.height = height + 'px';
    }
}

/*
Nav Fix for IE hover
*/
navArray = Array('nav');
startList = function() {
	if (document.all&&document.getElementById) {
		//Top Navs
		for (m=0; m<navArray.length; m++) {
			if (document.getElementById(navArray[m])) {
				topNavRoot = document.getElementById(navArray[m]);
				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 ", "");
										}
									}
								}
							}
						}
						//alert(" m = "+m+"\n i = "+i+"\n j = "+j+"\n k = "+k);
						var nav = i+1;
						node.filterroll = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="http://espanol.herbalessences.com/us/images/nav/btn'+nav+'_roll.png", sizingMethod="crop")';
						node.filterrest = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="http://espanol.herbalessences.com/us/images/nav/btn'+nav+'_rest.png", sizingMethod="crop")';
						node.filtersel = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="http://espanol.herbalessences.com/us/images/nav/btn'+nav+'_sel.png", sizingMethod="crop")';

						node.onmouseover=function() {
							this.className="over "+this.className;
							this.style.filter=this.filterroll;
						}
						node.onmouseout=function() {
							this.className=this.className.replace("over ", "");
							if (this.className=="on") {
								this.style.filter=this.filtersel;
							} else {
								this.style.filter=this.filterrest;
							}
						}
					}
				}
			}
		}
	}
}
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 = ""
}


/*
pop up new scrollable, resizable window
*/
function popup(url,w,h) {
	if (!w) {w='270';}
	if (!h) {h='270';}
	newPopup = window.open (url, 'herbalpop', 'resizable,scrollbars,width='+w+',height='+h);
	newPopup.focus();
}

/*
pop up new non-scrollable, non-resizable window
*/
function kiosk(url,w,h) {
	//resize Mood River popup
	if (url.indexOf('/xxx/') != -1) {
		w = 720;
	}
	newKiosk = window.open (url, 'herbalkiosk', 'width='+w+',height='+h);
	newKiosk.focus();
}

/*
switch product pages within a collection
*/
function productSelect(url) {
	window.location = url;
}

/*
switch collection image on collection landing page
*/
function swapImage(id, imageName) {
	document.getElementById(id).setAttribute('src', imageName);
	fixAlphaPngById(id);
}

/*

*/
function preloadCollectionImages() {
	var collectionImg = document.getElementById('collection_img');

	// preload images
	collectionImg.body_envy = new Image();
	collectionImg.body_envy.src = "http://www.herbalessences.com/us/images/collections/bottles_body_envy.png";
	collectionImg.breaks_over = new Image();
	collectionImg.breaks_over.src = "http://www.herbalessences.com/us/images/collections/bottles_breaks_over.png";
	collectionImg.color_me_happy = new Image();
	collectionImg.color_me_happy.src = "http://www.herbalessences.com/us/images/collections/bottles_color_me_happy.png";
	collectionImg.dangerously_straight = new Image();
	collectionImg.dangerously_straight.src = "http://www.herbalessences.com/us/images/collections/bottles_dangerously_straight.png";
	collectionImg.default_img = new Image();
	collectionImg.default_img.src = "http://www.herbalessences.com/us/images/collections/bottles_default.png";
	collectionImg.drama_clean = new Image();
	collectionImg.drama_clean.src = "http://www.herbalessences.com/us/images/collections/bottles_drama_clean.png";
	collectionImg.hello_hydration = new Image();
	collectionImg.hello_hydration.src = "http://www.herbalessences.com/us/images/collections/bottles_hello_hydration.png";
	collectionImg.long_term_relationship = new Image();
	collectionImg.long_term_relationship.src = "http://www.herbalessences.com/us/images/collections/bottles_long_term_relationship.png";
	collectionImg.no_flakin_way = new Image();
	collectionImg.no_flakin_way.src = "http://www.herbalessences.com/us/images/collections/bottles_no_flakin_way.png";
	collectionImg.none_of_your_frizzness = new Image();
	collectionImg.none_of_your_frizzness.src = "http://www.herbalessences.com/us/images/collections/bottles_none_of_your_frizzness.png";
	collectionImg.set_me_up = new Image();
	collectionImg.set_me_up.src = "http://www.herbalessences.com/us/images/collections/bottles_set_me_up.png";
	collectionImg.totally_twisted = new Image();
	collectionImg.totally_twisted.src = "http://www.herbalessences.com/us/images/collections/bottles_totally_twisted.png";
	collectionImg.shimmery_nights = new Image();
	collectionImg.shimmery_nights.src = "http://www.herbalessences.com/us/images/collections/bottles_shimmery_nights.png";
}

/*
switch collection image on collection landing page
*/
function swapImageViaObject(id, imageObject) {
	document.getElementById(id).setAttribute('src', imageObject.src);
	fixAlphaPngById(id);
}