/*
	(c) 2006- Elin Tjerngren, Artopod http://artopod.se
*/

// general function for changing images (DOM element) (often called from within html - so keep att top for fast loading)
function domChP(el, src) {
	if (navigator.userAgent.indexOf("MSIE 6")!=-1 && src.indexOf("png")!=-1) {
        el.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='image')";
	}
	else if (document.getElementById && (el.tagName.toUpperCase()=="IMG" || el.tagName.toUpperCase()=="INPUT")) {
        var image = new Image();
        image.src = src;
		el.src = src;
	}
}

function urchinTracker() { /* gets overwriten by urchin script, please leave the order of the scripts intact, this is only to be able to use the scripts locally without internet connection */ }

// called after load at every page 
function init_haggar() {
	// scroll bars initialized
	if (document.getElementById("scroll")) {
		CSBfleXcroll('scroll');
		document.getElementById("scroll").onclick=new Function("this.blur()");
	}
	if (document.getElementById("scroll2")) {
		CSBfleXcroll('scroll2');
		document.getElementById("scroll2").onclick=new Function("this.blur()");
	}	
	// Google analytics
	_uacct = "UA-32240-13";
	urchinTracker();
	// check through all the clsassnames and look for mouseover - to set function
	var el=document.getElementsByClassName("mouseover");
	var reg=/show_(\d+)/;
	for(var i=0;i<el.length;i++) {
		var ar=el[i].className.match(reg);
		if (ar && layer[ar[1]]) { // has matched and layer array exists and has that index (from regex)
			el[i].onmouseover=new Function("chVis(layer,"+ar[1]+")");
			el[i].onmouseout=new Function("chVis(layer,"+ar[1]+")");
		}
	}
	// check for click
	el=document.getElementsByClassName("click");
	reg=/show_(\d+)/;
	for(var i=0;i<el.length;i++) {
		ar=el[i].className.match(reg);
		if (ar && layer[ar[1]]) { // has matched and layer array exists and has that index (from regex)
			el[i].onclick=new Function("chVis(layer,"+ar[1]+");return false;");
		}
	}

	// home button gets a rollover	
	if (document.getElementById("home")) {
		document.getElementById("home").onmouseover=new Function("domChP(document.getElementById('home').firstChild, '../img/home.gif')");
		document.getElementById("home").onmouseout=new Function("domChP(document.getElementById('home').firstChild, '../img/haggar.gif')");
	}	

	// set classname for png on IE 6 (Alpha tranparency)
	if (navigator.userAgent.indexOf("MSIE 6")!=-1) {
		var imgs=document.getElementsByTagName("img");
		for(var i=0;i<imgs.length;i++) {
			reg=/.png$/;
			if (imgs[i].src.match(reg)) {
				imgs[i].className+=" png";
			}
		}
	}

	// Haggar.com product pages - swatches for clothes, set mouseover function
	var el=document.getElementsByClassName("swatches");
	var cursrc, bigsrc;
	var newdiv,newimg;
	var count=1;
	for(var i=0;i<el.length;i++) {
		if (el[i].tagName.toUpperCase()=="DL") { 
			var sw=el[i].getElementsByTagName("DD");
			for(var x=0;x<sw.length;x++) {
				swimg=sw[x].getElementsByTagName("img")[0];
				if (swimg.tagName.toUpperCase()=="IMG") { 
					cursrc=swimg.src;
					bigsrc=cursrc.replace("sw-small","sw-big");
				
					sw[x].style.zIndex=70+count;

					newdiv=document.createElement('div');
					// outside the dl, place this: <div><img ...></div>
					newdiv.id="swatchno_"+count;
					newdiv.style.height="68px";
					newdiv.style.width="77px";
					newdiv.style.position="absolute";

					newbg=document.createElement('img');
					newbg.setAttribute("src",'../img/swatchbg.png');
					newbg.className="png";
					newbg.style.position="absolute";

					newimg=document.createElement('img');
					newimg.setAttribute("src",bigsrc);
					newimg.setAttribute("border",0);
					newimg.style.position="absolute";
					newimg.style.left="2px";
					newimg.style.top="2px";

					l = sw[x].offsetLeft + + el[i].offsetLeft; 
					t = sw[x].offsetTop + el[i].offsetTop;
					newdiv.style.left=(l-26)+"px";
					newdiv.style.top=(t-67)+"px";

					newdiv.style.display="none";
					newdiv.onmouseout=new Function("hideSwatch('swatchno_"+count+"')");

					newdiv.appendChild(newbg)
					newdiv.appendChild(newimg)
					el[i].parentNode.appendChild(newdiv);

					sw[x].onmouseover=new Function("showSwatch('swatchno_"+count+"')");
					sw[x].onmouseout=new Function("hideSwatch('swatchno_"+count+"')"); // <-- kan behövas
					// increase count for neext divs and images...
					count++;
					//sw[x].onmouseout=new Function("hideSwatch(this,'"+cursrc+"')");
				}
			}
		}
	}
	ld=true;
}
window.onload=init_haggar;

// initialize stores function 
var storesarray=new Array();
var category=new Array();
function init_stores() {
	// check for click
	var el=document.getElementsByClassName("stores");
	var reg=/show_(\d+)/;
	var ar=new Array();
	for(var i=0;i<el.length;i++) {
		ar=el[i].className.match(reg);
		if (ar) { // has matched and layer array exists and has that index (from regex)
			if (document.getElementById("store_"+ar[1])) {
				// create layer dependencies
				storesarray[ar[1]]=new layObj("store_"+ar[1],"#",false);
				document.getElementById("store_"+ar[1]).style.display="none";
				// behaviour, display address and alter text
				el[i].onclick=new Function("chDisplay(storesarray,"+ar[1]+",false);changeText(this);return false;");
			}
		}
	}
	
	el=document.getElementsByClassName("category");
	reg=/cat_(\d+)/;
	for(var i=0;i<el.length;i++) {
		ar=el[i].id.match(reg);
		if (ar) {
			// make the layer object of all types of stores
			category[ar[1]]=new layObj("cat_"+ar[1],"#",false);
			// hide them, not the first though!
			if (el[i].className.indexOf("default")==-1) {
				document.getElementById("cat_"+ar[1]).style.display="none";
			}
		}
	}
	document.getElementById("category_motor").onchange=new Function("chDisplay(category,this.options[this.selectedIndex].value,true);");
	ld=true;
}
// store popup for adresses
function changeText(el) {
	if (el.innerHTML=="hide details") {
		el.innerHTML="show details"
	} else {
		el.innerHTML="hide details"
	}
}

// Haggar.com pdp pages - swatches for clothes
function showSwatch(swid) {
	document.getElementById(swid).style.zIndex="100";
	document.getElementById(swid).style.display="block";
}
function hideSwatch(swid) {
	document.getElementById(swid).style.display="none";
}

// general layer Object, used in visible/hidden situations
if (parseFloat(navigator.appVersion)<5 && navigator.appName=="Netscape") {
	var ns=true;
} else if (document.getElementById) {
	var dom=true;
} else if (document.all) {
	var ie=true;
}

var ld=false;
var obj=false;
if (ns) {
	var visible="show";
	var hidden="hide";
} else {
	var visible="visible";
	var hidden="hidden";
}
var containers=new Array();

function layObj(id,loc,isVisible) {
	this.id=id;
	this.loc=loc;
	this.isVisible=isVisible;
}

function getObj(divID) {
	var locobj;
	if (ns) (containers[divID]) ? eval("locobj="+containers[divID]+"document.layers."+divID) : locobj=document.layers[divID];
	else if (ie) locobj=document.all[divID];
	else if (dom) locobj=document.getElementById(divID);
	return locobj;
}
function chVis(ar,i,forceVisible) {
	if (ld) {
		if ((ns)||(ie)||(dom)) {
			var status;
			for (var x=0; x<ar.length; x++) {
				if (x==i) {
					if (forceVisible==true) { // force current no to visible
						ar[i].isVisible=true;
						status=visible;
					} else { // toggle
						ar[i].isVisible=!ar[i].isVisible;
						if (ar[i].isVisible) status=visible;
						else status=hidden;
					}
				} else {
					ar[x].isVisible=false;
					status=hidden;
				}
				var obj=getObj(ar[x].id);
				if (obj) {
					if (ns) {
						obj.visibility=status;
					} else if (ie) {
						obj.style.visibility=status;
						event.cancelBubble=true;
					} else {
						obj.style.visibility=status;
					} 
				}
			}
		} else {
			if (ar[i].loc) location=ar[i].loc;
		}
	}
}
// Store locator at Haggar.com, + ads page
// alternative for visibility hidden - uses display=none instead
function chDisplay(ar,i,forceVisible) {
	if (ld) {
		if ((ns)||(ie)||(dom)) {
			var status;
			for (var x=0; x<ar.length; x++) {
				if (x==i) {
					if (forceVisible) {
						ar[i].isVisible=true;
						status="block";
					} else {
						ar[i].isVisible=!ar[i].isVisible;
						if (ar[i].isVisible) status="block";
						else status="none";
					}
				} else {
					ar[x].isVisible=false;
					status="none";
				}
				var obj=getObj(ar[x].id);
				if (obj) {
					obj.style.display=status;
					if (ie) event.cancelBubble=true;
				}
			}
		} else {
			if (ar[i].loc) location=ar[i].loc;
		}
	}
}
// general (used in mutlimedia page)
function emptyNode(el) {
	while (el.firstChild)
		el.removeChild(el.firstChild);
}

// check through all the classnames and look for mouseover - to set function
// classname should be "activstate" on img tag and script needs id of the same element.
// warning - veeeery haggar specific code. (see changeTo)
// used on multimedia page
var actives=new Array();
var curActiveId=-1;
function init_activestate() {
	var el=document.getElementsByClassName("activestate");
	for(var i=0;i<el.length;i++) {
		if (el[i].id) { // need an id to work
			actives[i]=el[i].id;
			el[i].onclick=new Function("activate(this)");
			el[i].onmouseover=new Function("changeTo(true,this)");
			el[i].onmouseout=new Function("changeTo(false,this)");
		}
	}
}
function activate(el) {
	curActiveId=el.id;
	for (var i=0;i<actives.length;i++) {
		if (actives[i]==el.id)
			changeTo(true,el)
		else
			changeTo(false,document.getElementById(actives[i]))
	}
}
function isActive(el) {
	if (el.src.indexOf("1")!=-1) return true;
	else return false;
}
// type of img-names used: (not png!) imagename.gif (inactive) imagename1.gif (active)
function changeTo(active,el) {
	if (active) {
		if (!isActive(el))
			el.src=el.src.replace(/.(gif|jpg)/i,"1.$1")
		el.blur();
	} else if (isActive(el) && (curActiveId!=el.id)) {
		el.src=el.src.replace(/1.(gif|jpg)/i,".$1")
	}
}

/* open popup window functions */

function openWindow(url,winname,w,h) {
	return window.open(url, winname, 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,dependent=no,width='+w+',height='+h+'')
}
function openWindowMaxH(url,winname,w) {
	var h=window.screen.availHeight;
	var w=window.open(url, winname, 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,dependent=no,width='+w+',height='+h+'')
	w.moveTo(0,0);
	w.focus();
}

/* form validation functions */

function checkEmail(f,ar) {
	var failure=false;
	for (var i=0;i<ar.length;i++) {
		if (f.elements[ar[i]].value.indexOf("@")==-1) {
			failure=true;
		}
	}
	return returnForm(failure);
}
function checkOneTrue(f,ar) {
	var failure=true;
	for (var i=0;i<ar.length;i++) {
		if (f.elements[ar[i]].value) {
			failure=false;
		}
	}
	return returnForm(failure);
}
function checkNotEmpty(f,ar) {
	var failure=false;
	for (var i=0;i<ar.length;i++) {
		if (f.elements[ar[i]].value=="") {
			failure=true;
		}
	}
	return returnForm(failure);
}
function returnForm(failure) {
	if (failure) {
		alert("We are missing some information from you to be able to complete your submission. \n\nPlease enter the information in the fields marked with *");
		return false;
	} else {
		return true;
	}

}

/* Thanks to Jeremy Keith @media 2006 */
document.getElementsByClassName = function(name) {
  var results = new Array();
  var elems = document.getElementsByTagName("*");
  for (var i=0; i<elems.length; i++) {
    if (elems[i].className.indexOf(name) != -1) {
      results[results.length] = elems[i];
    }
  }
  return results;
};


