// JavaScript Document

function newImage(arg) {
	// Load a new image into memory
	if (document.images) {
		var rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function init_nwls(this_el) {
	this_el.onclick = function() {
		window.open(this_el.href);
		return false;
	};
}

function changeImage(arg, arg2) {
	if (document.images && (preloadFlag == true)) {
		var img = arg;
		var new_img = arg2;
		// Call the changeImages function
		try {
			document.getElementById(img).src = new_img;
		}
		catch(err) {
			errorTxt="There was an error on this page.\n\n";
			errorTxt+="Error description: " + err.description + "\n\n";
			errorTxt+="img = " + img.id + "\n";
			errorTxt+="new_img = " + new_img + "\n\n";
			errorTxt+="Click OK to continue.\n\n";
			alert(errorTxt);
		}
	}
}

function addChangeCurrentImgFaded(id, id2, id3, id4, arg, el) {
	// Change an image src when an image is clicked.	
	// Requires global imgThumbs array and global currentImg.
	var src = arg;
	var thumbs = el; 
	try {
		var clicked_img = document.getElementById(id); //image clicked on
		var change_img = document.getElementById(id2); //image to change
		var fade_img = document.getElementById(id3); //image that fades out to reveal new image
		var load_img = document.getElementById(id4); //loading animation image
	}
	catch(err) {
		errorTxt="There was an error on this page.\n\n";
		errorTxt+="Error description: " + err.description + "\n\n";
		errorTxt+="Click OK to continue.\n\n";
		alert(errorTxt);
	}	
	if (clicked_img && change_img) {
		var parent = clicked_img.parentNode; // parent node of clicked_img
		
		// Check if parent node is a <a> tag
		if ((parent.nodeType == 1) && (parent.nodeName == 'A')) {
			var this_element = parent;
		}
		else {
			// Apply onclick to the image itself
			var this_element = clicked_img;
		}
		
		this_element.onclick = function() {
			//alert('clicked_img.id = ' + clicked_img.id + "\nimgThumbs[currentImg][0] = " + imgThumbs[currentImg][0]);
			if (clicked_img.id != ('i_img' + imgThumbs[currentImg][0])) { //only do changes if a change is needed!
			  //alert('fade_img.src = ' + fade_img.src + "\nchange_img.src = " + change_img.src);
			  
			  
			  doOpacChangeMem(fade_img,100,100,1,1,1); // change opacity of fade_img to 100% - No animation
			  doOpacChangeMem(load_img,100,100,1,1,1); // change opacity of load_img to 100% - No animation	
			  changeImage(fade_img.id, change_img.src);
			  
			  change_img.onload = function() {
				  doOpacChangeMem(load_img,0,0,1,1,1); // change opacity of load_img to 0% - No animation
				  doOpacChangeMem(fade_img,100,0,20,10,1); // initialize fading out animation for fade_img
				  
				  //this.onload = ''; // clear onload function
			  };
			  
			  changeImage(change_img.id, src); 
			  
			  var new_current_id = clicked_img.id.slice(5); // Remove the first five characters:
			  for (i = 0; i < imgThumbs.length; i++) {
				  if (imgThumbs[i][0] == new_current_id) {
					  // 0 in the array is either 'i_img_id' or 'p_img_id'
					  currentImg = i;
					  initThumbs(thumbs);
				  }
			  }
			}
		};
		
		
	}	
}

function initThumbs(array, id, id2, id3) {
		var thumbs = array;
		
		var large_img_id = id; // holds the current and final image
		var fade_img_id = id2; // temporarily holds the previous image. once the new image loads this image fades out
		var loading_img_id = id3; // animation for loading the image
		
		var num_thumbs = thumbs.length;
		for (i = 0; i < num_thumbs; i++) {
			if (i != currentImg) {
				// This is not the current thumbnail image. Apply mouse over
				addMouseOverToImg(thumbs[i].id, imagesPath + imgThumbs[i]['img_thumb'], imagesPath + imgThumbs[i]['img_thumb_on']);
			}
			else {
				// This is the current thumbnail image. Keep img as 'on'
				addMouseOverToImg(thumbs[i].id, imagesPath + imgThumbs[i]['img_thumb_on'], imagesPath + imgThumbs[i]['img_thumb_on']);
				// turn 'off' other thumbnail images
				for (j = 0; j < num_thumbs; j++) {
					if (j == currentImg) {
						thumbs[j].src = imagesPath + imgThumbs[j]['img_thumb_on'];
					}
					else {
						thumbs[j].src = imagesPath + imgThumbs[j]['img_thumb'];
					}
				}
				
			}
			// Apply on click event
			addChangeCurrentImgFaded(thumbs[i].id, large_img_id, fade_img_id, loading_img_id, imagesPath + imgThumbs[i]['img'], thumbs);
		}
	}


function addMouseOverToImg(id, arg1, arg2) {
	// Apply mouse events to an image within an "a" node.
	var img_id = id;
	var s = arg1; // source for img
	var o = arg2; // source for img over
	try {
		var img = document.getElementById(img_id);
	}
	catch(err) {
		errorTxt="There was an error on this page.\n\n";
		errorTxt+="Error description: " + err.description + "\n\n";
		errorTxt+="Click OK to continue.\n\n";
		alert(errorTxt);
	}
	
	
	if (img) {
		var parent = img.parentNode; // parent node
		
		// Check if parent node is a <a> tag
		if ((parent.nodeType == 1) && (parent.nodeName == 'A')) {
			parent.onmouseover = function() { 
				changeImage(img_id, o); 
			};
			parent.onmouseout = function() { 
				changeImage(img_id, s); 
			};
		}
		else {
			// Apply the mouse over directly to the image
			img.onmouseover = function() { 
				changeImage(img_id, o); 
				img.style.cursor = 'pointer';
			};
			img.onmouseout = function() { 
				changeImage(img_id, s); 
				img.style.cursor = 'default';
			};
		}
	}
}

function setMyClass(el, myClass)  {
	setMyAttribute(el, 'className', myClass); //for IE
	setMyAttribute(el, 'class', myClass); //for Mozilla/Firefox
}

function setMyAttribute (el, att, attV) {
	//allows for error checking
	try {
		el.setAttribute(att, attV);
	}
	catch(err) 
	{
		errorTxt="There was an error on this page.\n\n";
		errorTxt+="Error description: " + err.description + "\n\n";
		errorTxt+="Click OK to continue.\n\n";
		alert(errorTxt);
	}
}

function addMouseOverToProd(arg, arg2, arg3, arg4) {
	// Apply mouse events for a product category image
	
	//if ((getInternetExplorerVersion() != -1) && (getInternetExplorerVersion() <= 7)) return; //detected IE7 or earlier so end function (IE7 and earlier display the z-index incorrectly)
	try { 
		var my_img = document.getElementById(arg);
		var my_table = document.getElementById(arg2);
	}
	catch(err) {
		errorTxt="There was an error on this page.\n\n";
		errorTxt+="Error description: " + err.description + "\n\n";
		errorTxt+="Click OK to continue.\n\n";
		alert(errorTxt);
	}
	var src = arg3;
	var name = arg4;
	var my_a;
	try {
		var my_div_array = my_table.getElementsByTagName('DIV');
		var my_div = my_div_array[0];
	}
	catch(err) {
		errorTxt="There was an error on this page.\n\n";
		errorTxt+="Error description: " + err.description + "\n\n";
		errorTxt+="Click OK to continue.\n\n";
		alert(errorTxt);
	}
	if (my_img) {
		my_a = my_img.parentNode; 
		// Check if parent node is a <a> tag
		if ((my_a.nodeType == 1) && (my_a.nodeName == 'A')) {
			my_a.onmouseover = function() { 
				doOpacChangeMem(my_table,0,100,10,10,1);
				//my_table.style.visibility = 'visible';
			};
			my_a.onmouseout = function() { 
				//doOpacChangeMem(my_div,100,0,15,10,1);
				doOpacChangeMem(my_table,0,0,1,1,1);
			};
		}
	}
}

function initToolTip(tooltip) {
	// initialize tooltip animations
	try {
		var spanNodes = tooltip.getElementsByTagName('span');
		var spanNode = spanNodes[0];
	}
	catch(err) {
		alert("There is an error on this page\n"+err);
	}
	
	
	tooltip.onfocus = function() {
		// remove focus to prevent dotted lines
		this.blur();
	};
	tooltip.onmouseover = function() {
		// fade in span node
		doOpacChangeMem(spanNode,0,100,10,10,1);
	};
	tooltip.onclick = tooltip.onmouseout = function() {
		// hide span node
		doOpacChangeMem(spanNode,0,0,1,1,1);
	};
}


function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			alert(alerttxt);
			return false;
		}
		else {return true;}
	}
}

function admin_submit(my_field) {
	//redirect page to new url specified as the value of the form field
	$url = my_field.value;
	if ($url != '') window.location = $url;
}



function toggle_class(id, str1, str2) {
	var class1 = str1;
	var class2 = str2;
	
	try {
		var el = document.getElementById(id); //image clicked on
	}
	catch(err) {
		errorTxt="There was an error on this page.\n\n";
		errorTxt+="Error description: " + err.description + "\n\n";
		errorTxt+="Click OK to continue.\n\n";
		alert(errorTxt);
	}	
	if (el) {
		el.className = (el.className==class1) ? class2 : class1;
	}
	
}



function toggle_display(id, str) {
	if (str) {
		var display_type = str; 
	}
	else {
		var display_type = 'block';
	}
	
	try {
		var el = document.getElementById(id); //image clicked on
	}
	catch(err) {
		errorTxt="There was an error on this page.\n\n";
		errorTxt+="Error description: " + err.description + "\n\n";
		errorTxt+="Click OK to continue.\n\n";
		alert(errorTxt);
	}	
	if (el) {
		if ( el.style.display != display_type ) {
			el.style.display = display_type;
		}
		else {
			el.style.display = 'none';
		}
	}
	
}
