// JavaScript Document

/*******************************************************************************************************
LOGIC FOR MOUSEOVER, CLICK, and CLICKNEW
PRELOAD
-------
1. Create and set variables for each size to true

MOUSEOVER and MOUSEOUT
----------------------
1. The onMouseOver event calls MM_swapImage('[name of image to be swapped]','','[file to swap in]',1)
2. OnMouseOut, call MM_swapImageRestore([size value])
	2a. MM_swapImageRestore evaluates the size var passed in against the list of Restore variables.
	2b. When it finds a match, it checks the state of that restore variable. if it is true, it swaps the image back to its original image.
	2c. If the var has been set to false, it does not swap the image back (the click event changes it to false).

ONCLICK
-------
1. Click event calls noRestore(size) to flip Restore variable to false.
2. noRestore first calls JG_SwitchBack() which uses a series of If statements to evaluate if any of the Restore variables are false (they were clicked before).
3. For each variable, if it is False, it calls JG_swapImage('[name of previous selection]','','[original load file]',1) to change the previously 
   clicked image back to unclicked.
4. It then sets the Restore variable for that size back to true (able to be flipped).
5. For all other variables that are not false, it ignores them since they are already true.
6. Program then returns to noRestore and compares the size var passed in to a switch case for each size.
7. If the size matches, it changes the corresponding restore variable to false, and then calls JG_SwapImage([vars]) to switch the image to the selected image. 

********************************************************************************************************
*/

//TODO: function to change images onmouseover
//TODO: function to change header image, order box image onclick

// By default, restore is enabled for each button


var Restore11 = true;
var Restore12 = true;
var Restore13 = true;



function noRestore (size) {
//Evaluate all switches first. if any switch is false, call the restore function for that image
JG_SwitchBack ();

	switch(size){
		
	case 11:
		Restore11 = false; //flip the flag to false
		JG_swapImage('TL_orderST_06','','images/adj_collar/size_sm.gif',1);  // add to order box
		break;
	case 12:
		Restore12 = false; //flip the flag to false
		JG_swapImage('TL_orderST_06','','images/adj_collar/size_md.gif',1);  // add to order box
		break;
	case 13:
		Restore13 = false; //flip the flag to false
		JG_swapImage('TL_orderST_06','','images/adj_collar/size_lg.gif',1);  // add to order box
		break;
		
	}
}

function JG_SwitchBack () { //this function should call swapImage to set an image back to its original state if it is currently set to false.
//logic here is to evaluate if any of the switches are flipped. if false, then flip to true.

	if (Restore11 == false) {
		JG_swapImage('TL_products_size_bar_03','','images/TL_products_size_bar_03.gif',1);
		Restore11 = true;} //reset the flag}
	if (Restore12 == false) {
		JG_swapImage('TL_products_size_bar_04','','images/TL_products_size_bar_04.gif',1);
		Restore12 = true;} //reset the flag}
	if (Restore13 == false) {
		JG_swapImage('TL_products_size_bar_05','','images/TL_products_size_bar_04.gif',1);
		Restore13 = true;} //reset the flag}
		
	
}

function JG_swapImage() { //v3.0
  var i,j=0,x,a=JG_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var swapImage = true;
/*
function MM_preloadImages() { //v3.0
  //JG_TEST();
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
*/
function JG_restoreImage (size) {
	//window.alert ("swapimgrestore with size var "+size)

	switch (size) {
	
	case 11 : 
	//window.alert("restore11 evaluated");
	if (Restore11 == true) {
	//window.alert("Restore11 is "+Restore11);
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;}
	//else {window.alert("Restore11 = "+Restore11)};
	break;
	case 12 : 
	//window.alert("restore12 evaluated");
	if (Restore12 == true) {
	//window.alert("Restore12 is "+Restore12);
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;}
	//else {window.alert("Restore12 = "+Restore12)};
	break;
	case 13 : 
	//window.alert("restore13 evaluated");
	if (Restore13 == true) {
	//window.alert("Restore13 is "+Restore13);
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;}
	//else {window.alert("Restore13 = "+Restore13)};
	break;
	}
}


function TurnOnRestore(size) {  //This function flips the bits of all but the passed in var
/* if the name of the var passed in does not equal the name of the size below, flip the bit to true.
*/
// array mapped to the global vars for restore on and off
var sizeVariableArray = new Array(3)
	sizeVariableArray[0]=Restore11;
	sizeVariableArray[1]=Restore12;
	sizeVariableArray[2]=Restore13;
//set all flags to true (go ahead and restore)
	var x=0;
	for (x=0; x<10; x++) {
	sizeVariableArray[x] = true;
	};
// now flip the switch on the the one that was passed in
	window.alert(size);
	switch (size) {
	case 11 : 
		Restore11 = false;
		break;
	case 12 : 
		Restore12 = false;
		break;
	case 13 : 
		Restore13 = false;
		break;
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/*function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3)
  //window.alert (swapImage);
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
*/
function JG_swapSizeImage() { //v3.0
  var i,j=0,x,a=JG_swapSizeImage.arguments; 
  document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3)
  //window.alert (swapImage);
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//function JG_restoreImage() { //v3.0
//  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
//}