/* Use this file to hold the functions associated with the site.
 * This file is to be replaced only by qualified individuals.
 * Last update: 06.22.2005
 */

      //  var imgArray = new Array();       // an array of images; used for preloading the rollover images
       // var spacerArray = new Array();  // an array of images; used for preloading the news headlines
        var imageToggle = new Array();    // a boolean (true/false) array; used to determine if an image can change onMouseOut
		var beforeArray = new Array();
		var afterArray = new Array();
		var buyArray = new Array();
		var sellArray = new Array();

        // allows all images to change onMouseOut
        function toggleAllOn(){
           for (i=1; i<11; i++){
              imageToggle[i] = true;
           }
        }

        // allows imgArray[i] to change onMouseOut
        function toggleOn(i) {
           imageToggle[i] = true;
        }

        // prevents imgArray[i] from changing onMouseOut
        function toggleOff(i) {
           imageToggle[i] = false;
        }
        
        // turns off all images that do not fall into category x
        function filterAllBut(x) {
           resetAll();                        // first change all images back to default

           for (i=1; i<20; i++)               // iterate through all images. where 'i<y' change y
           {                                  //     to corrospond to the number of images in site
              if(imageType[i] != x) {        // if image is not of type x, 'turn it off'
                 swapNewsphotoIn(i);
                 toggleOff(i);
              }
           }
        }
		
		function opacity(id, opacStart, opacEnd, millisec) {
			//speed for each frame
			var speed = Math.round(millisec / 100);
			var timer = 0;
			if (secondTime == true) {
				clearInterval(afterInt);
			}
			secondTime = true;
			
			if(opacStart < opacEnd) {
				for(i = opacStart; i <= opacEnd; i++) {
					
					//if (secondTime == false) {
						//theOne = beforeArray[id];
						//if (i >= opacEnd) {
						//	secondTime = true;
						//	opacity("after", opacStart, opacEnd, millisec);
						//}
						setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
						timer++;
						
					//} else if (secondTime == true) {
						//theOne = afterArray[id];
						//setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
						
						//timer++;
						//if (i <= opacEnd && secondTime == true) {
						//	secondTime = false;
						//	opacity("after", opacStart, opacEnd, millisec);
						//}
					//}
				}
			}
		}
		
		function initImage() {
		  imageId1 = 'before';
		  image1 = document.getElementById(imageId1);
		  setOpacity(image1, 0);
		  image1.style.visibility = 'visible';
		  imageId2 = 'after';
		  image2 = document.getElementById(imageId2);
		  setOpacity(image2, 0);
		  image2.style.visibility = 'visible';
		//  fadeIn(imageId,0);
		}
		function initSellImage() {
		  imageId3 = 'sell';
		  image3 = document.getElementById(imageId3);
		  setOpacity(image3, 0);
		  image3.style.visibility = 'visible';
		}
		function initBuyImage() {
		  imageId4 = 'buy';
		  image4 = document.getElementById(imageId4);
		  setOpacity(image4, 0);
		  image4.style.visibility = 'visible';
		}
		
		function killIt() {
			clearInterval(afterInt);	
		}
		
		function setOpacity(obj, opacity) {
			  opacity = (opacity == 100)?99.999:opacity;
			  
			  // IE/Win
			  obj.style.filter = "alpha(opacity:"+opacity+")";
			  
			  // Safari<1.2, Konqueror
			  obj.style.KHTMLOpacity = opacity/100;
			  
			  // Older Mozilla and Firefox
			  obj.style.MozOpacity = opacity/100;
			  
			  // Safari 1.2, newer Firefox and Mozilla, CSS3
			  obj.style.opacity = opacity/100;
			}
			
			function fadeIn(objId,opacity) {
				if (objId == "after") {
					clearInterval(afterInt);	
				} else if (objId == "logosplash") {
					clearInterval(logoInt);	
				}
	 	   	    if (document.getElementById) {
				  obj = document.getElementById(objId);
				  if (opacity <= 100) {
				    setOpacity(obj, opacity);
				    opacity += 5;
				    window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 50);
				  //} else if (opacity == 100) {
				    //	fadeIn(imageId2, 0);	
				  }
			    }
			 }
		
		//change the opacity for different browsers
		function changeOpac(opacity, id) {
			var object = document.getElementById(id).style;
			object.opacity = (opacity / 100);
			//object.style.opacity = (opacity / 100);
			object.MozOpacity = (opacity / 100);
			object.KhtmlOpacity = (opacity / 100);
			object.filter = "alpha(opacity=" + opacity + ")";
		} 

        // simple preload images function. improves rollover time
        function preloadImages(k){
			if (k == 0) {
				initImage();
			} else if (k == 1) {
				initSellImage();
			} else if (k == 2) {
				initBuyImage();
			}
			
           for (i=1; i<12; i++){
            // imgArray[i] = new Image();
           //  imgArray[i].src = "images/newsphoto" + i + "_rollover.jpg";
           //  spacerArray[i] = new Image();
           //  spacerArray[i].src = "images/headline" + i + ".gif";
		 	 beforeArray[i] = new Image();
             beforeArray[i].src = "images/before/" + i + "before.jpg";
			 afterArray[i] = new Image();
             afterArray[i].src = "images/after/" + i + "after.jpg";
           }
		   for (m=1; m<22; m++){
			 sellArray[m] = new Image();
           	 sellArray[m].src = "images/sellimages/sell" + m + ".jpg";
		   }
		   for (n=1; n<2; n++){
			 buyArray[n] = new Image();
           	 buyArray[n].src = "images/buyimages/buy" + n + ".jpg";
		   }
		   if (k == 0) {
				//initImage();
				swapImages(1);
			} else if (k == 1) {
				//initSellImage();
				swapSellImages(1);
			} else if (k == 2) {
				//initBuyImage();
				swapBuyImages(1);
			}
        }
		
		function fadeLogoSplash() {
			changeOpac(0, "logosplash");
		   //fadeIn("before", 0);
		   logoInt = setInterval(fadeIn, 8000, "logosplash", 0);
		}

        // image swap onMouseOver
        function swapNewsphotoIn(x) {
           if (imageToggle[x]) {
              document["photo" + x].src = imgArray[x].src;
              document["headlinespacer"].src = spacerArray[x].src;
           }
        }

       // image swap onMouseOut
       function swapNewsphotoOut(x) {
		  if (imageToggle[x]) {
              document["photo" + x].src = "images/newsphoto" + x + ".jpg";
              document["headlinespacer"].src = "images/spacer.gif";
           }
        }

        // resets all images to page default
        function resetAll() {
			//clearInterval(afterInt);
           for (i=1; i<12; i++) {
              toggleOn(i);
			 // swapNewsphotoOut(i);
		   }
        }
        
        // use to swap the text in the news table; requires articles array
        function swapNewsText(x) {
           news.innerHTML = articles[x];
        }
		
		function ieInterval() {
			clearInterval(afterInt);
			fadeIn("after", 0);
		}
		
		// use to swap the text in the news table; requires articles array
        function swapImages(x) {
			//clearInterval(afterInt);
           //news.innerHTML = articles[x];
		   document["before"].src = beforeArray[x].src;
		   document["after"].src = afterArray[x].src;
		   changeOpac(0, "before");
		   changeOpac(0, "after");
		   fadeIn("before", 0);
		   afterInt = setInterval("ieInterval()", 1000);
		  // opacity("before", 0, 100, 1000);
		 //  afterInt = setInterval(opacity, 1100, "after", 0, 100, 1000);
        }
		function swapSellImages(x) {
			//clearInterval(afterInt);
           //news.innerHTML = articles[x];
		   document["sell"].src = sellArray[x].src;
		   changeOpac(0, "sell");
		   fadeIn("sell", 0);
		  // opacity("before", 0, 100, 1000);
		 //  afterInt = setInterval(opacity, 1100, "after", 0, 100, 1000);
        }
		function swapBuyImages(x) {
			//clearInterval(afterInt);
           //news.innerHTML = articles[x];
		   document["buy"].src = buyArray[x].src;
		   changeOpac(0, "buy");
		   fadeIn("buy", 0);
		  // opacity("before", 0, 100, 1000);
		 //  afterInt = setInterval(opacity, 1100, "after", 0, 100, 1000);
        }
        
        // used to clean up the onClick event for the images. Doesn't really do anything special.
        function imageClick(x) {
			//clearInterval(afterInt);
			secondTime = false;
		   resetAll();
		  // killIt();
          // swapNewsphotoIn(x);
		 // opacity(x, 0, 100, 1000, "before")
           toggleOff(x);
          // swapNewsText(x);
		  swapImages(x);
        }
		function imageSellClick(x) {
			//clearInterval(afterInt);
			//secondTime = false;
		  // resetAll();
		  // killIt();
          // swapNewsphotoIn(x);
		 // opacity(x, 0, 100, 1000, "before")
           //toggleOff(x);
          // swapNewsText(x);
		  swapSellImages(x);
        }
		function imageBuyClick(x) {
			//clearInterval(afterInt);
			//secondTime = false;
		 //  resetAll();
		  // killIt();
          // swapNewsphotoIn(x);
		 // opacity(x, 0, 100, 1000, "before")
          // toggleOff(x);
          // swapNewsText(x);
		  swapBuyImages(x);
        }


//begin jonny functions
function init() {
	initDHTMLAPI();
	loadFlag=true;
}
