
// -------------------------------------------------------------------------
// frontpage.js -- Global Javascript functions for runawaytractor.com
// -------------------------------------------------------------------------

// Image preload
   var imgs = new Array('bg','runaway','tractor','logo','main-left','main-right','r1c1','r2c1','r2c2a','top-bar',
                        'r3c2front','r4c2front','specials-top','specials-bot','r4c1','r4c2','bot-left','bot-right','r2c2front');
   var ovrs = new Array('equipment','specials','about','links','contact','home');
   var objName,objOver,imgName,overName;
   for (i=0;i<imgs.length;i++) {
      objName = imgs[i];
      imgName = '\/images\/' + imgs[i] + '.gif';
      objName = new Image(); objName.src=imgName;
   }
   for (i=0;i<ovrs.length;i++) {
      objName  = ovrs[i];
      objOver  = ovrs[i] + 'b';
      imgName  = '\/images\/' + imgs[i] + '.gif';
      overName = '\/images\/' + imgs[i] + 'over.gif';
      objName = new Image(); objName.src=imgName;
      objOver = new Image(); objOver.src=overName;
   }
   
   
//global variables
   var layerRef=""; 
   animObjectArray= new Array ("logo","runaway","tractor");
   var topCoords  = new Array(30,15,-45);    // If animation is supported, these are the coords
   var leftCoords = new Array(-40,-390,15);  // the initial animated objects are moved to - off page
   var logoCoords = setPath(-295,8,38);       // Below: Set animation paths, define arrays of coords for all three
   logoArray      = new Array();
   logoArray      = attempt_center(logoCoords);
   var runCoords  = setPath(-460,28,31);
   runArray       = new Array();
   runArray       = attempt_center(runCoords);
   var tractorCoords="30,-40,30,-30,30,0,30,10,30,30,30,35,30,40,30,45,30,50,30,55,30,60,30,62,30,64,30,66,30,68,30,70";
   tractorArray   = new Array();
   tractorArray   = attempt_center(tractorCoords);   // Define all three animated opbjects
   logo     = new animatedObject('logo','logo','no',20,0,'0',logoArray);
   runaway  = new animatedObject('runaway','runaway','no',100,0,'0',runArray);
   tractor  = new animatedObject('tractor','tractor','no',20,0,'0',tractorArray);

// INITIALIZE --------------------------------------------
// If the required objects exist in the browser for animation, 
// Define variables and ultimately the animated objects and begin the play.

function init(){

    if (document.getElementById) {
        for (i=0;i<animObjectArray.length;i++) {
             var elm = document.getElementById(animObjectArray[i]);
              if (elm.style) {
                  if (typeof(elm.style.left) == 'number') {
                       // Opera 5/6 do not implement the standard correctly
                       // and assume that elm.style.left and similar properties
                       // are numbers.
                       elm.style.top  = topCoords[i];
                       elm.style.left = leftCoords[i];
                  }
                  else {
                       elm.style.top  = topCoords[i] + 'px';
                       elm.style.left = leftCoords[i] + 'px';
                  }
            }
        }
    }
    else { return false; }
}

function showLayer(layerName){
       if ((document.getElementById) || (document.all) || (document.layers)) {
            if (document.getElementById) {
                 layerRef = document.getElementById(layerName);
                 layerRef.style.visibility="visible";
            }
  	        else { eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"'); }
       }
       else { return false; }
}


//ANIMATED OBJECT
function animatedObject (objectName,layerName,loop,speed,selectedIndex,endRoutines,route){
	this.objectName=objectName;
	this.layerName=layerName;
	this.loop=loop;
	this.speed=speed;
	this.selectedIndex=selectedIndex;
	this.endRoutines=endRoutines;
	this.route=route;
 }

// ANIMATION PATH -- create an array of x-y coordinates, z=how far lt
function setPath(x,y,z) {
    //decrease i to move the end to the left
    var workPath = "", path = "";
        for (i = z; i > 1; i--) {
           x =  x + i;
           var tmp = x + ',' + y + ',';
           workPath = workPath + tmp;
        }
        //kill the last comma
        for ( j = 0; j <= (workPath.length - 2); j++) {
            path = path + workPath.charAt(j);
        }
    return path;
}


// Attempt to center the horizontal coordinated based on screen width
function attempt_center(arr) {
  var odd = 0;
  tmpArr = new Array();
  tmpArr = arr.split(',');
  var d = arr;
  if (screen.width) {
     var baseWidth = screen.width;
     if (baseWidth > 1280) { baseWidth = 1280; }      // Large two-monitor screen object.
     var mainWidth = parseInt(baseWidth * .95  - 36); //main div, see css
     var leftEdge = (baseWidth/2) - (mainWidth/2);
	 for (i=0;i<tmpArr.length;i++) {
	   odd=(odd==0)?1:0;
	   if (odd==1) { tmpArr[i] = parseInt(((tmpArr[i]*100)+(leftEdge*100))/100); }
     }
  }
  return tmpArr;
}


//TIMELINE
	var totalTime=10;
	var currTime=0;
	var timeline=new Array ();
	timeline[1]='doAnimation("logo")';
	timeline[2]='doAnimation("runaway")';
	timeline[3]='doAnimation("tractor")';

function stopTimeline()  {  currTime=totalTime+1;                }

//START TIMELINE
function startTimeline() {  currTime=0;  timelineController();   }

//TIMELINE CONTROLLER
function timelineController(){
    if (currTime <= totalTime){
	currTime++;
	if (timeline[currTime] != null){
		eval(timeline[currTime]);
	}
	setTimeout("timelineController()",1000);
    }
}

//MOVE LAYER TO
function moveLayerTo(layerName,left,top){
   if (document.getElementById) {
     top += 'px';
	 left += 'px';
     layerRef = document.getElementById(layerName);
     layerRef.style.top=top;
     layerRef.style.left=left;
   }
   else { return false; }
}

//DO ANIMATION
function doAnimation(objName){
    showLayer(objName);
    if (eval(objName + '.selectedIndex <' + objName + '.route.length')){
	    eval('moveLayerTo(' +objName+ '.layerName,' + objName + '.route[' + objName +
        '.selectedIndex],' + objName + '.route[' +objName+ '.selectedIndex+1])');
	    eval (objName+'.selectedIndex+=2');
	    setTimeout('doAnimation("'+objName+'")',eval(objName+'.speed'));
    }
    else  {
	    var commandString=eval(objName+'.endRoutines');
	    eval (commandString);
	    eval (objName + '.selectedIndex=0');
	    if (eval(objName+'.loop=="yes"')){
	        eval('doAnimation("'+objName+'")',eval(objName+'.speed'));
	    }
    }
}



// FORM CHECKS  ------------------------------------------------------------

function checkForm(form) {

    var passChecks = true;
    if ((form.Name.value == "") || (form.Email.value == "")) {
        var msg = "In order for our service representatives to\n" +
                  "serve you, you need to give us your name and\n" +
                  "your email address.";
        alert(msg);
        passChecks = false;

    }
    else if (form.Email.value != "") {
        var found = false;
        var emailString = form.Email.value;
        for (j=0; j < emailString.length; j++) {
            if (emailString.charAt(j) == "@") { found = true; }
        }
        if (found == false) {
            var mailMsg = 'Is that your true email address?\n' +
                          'Please enter your correct email address.\n' +
                          'We will not place you on any mailing lists!\n';
            alert(mailMsg);
            passChecks = false;
        }

    }

    if (passChecks == true) { form.submit(); }

}


function newWin(url,img,title,w,h) {

	   if (! (url) && ! (img)) { return; }
	   var day= new Date();
	   var id = day.getTime();
	   var ww = w+75;
	   var wh = h+125;
	   if ((screen.height) && (wh > screen.height-100)) { wh = screen.height-100; }
	   var params = 'width='+ww+',height='+wh+',scrollbars,resizable';
	   var t = (title != '')?title:img;
	   if (url=='') {
	      var msg='<!DOCTYPE HTML PUBLIC "-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN" "http:\/\/www.w3.org/TR\/html4\/loose.dtd">\n'+
		          '<meta http-equiv="Content-Type" content="text\/html; charset=iso-8859-1">\n'+
		          '<html><head><title>'+t+'<\/title><\/head>'+
		          '<style type="text\/css">\n'+
				  'html,body { font-family: Arial,Helvetica,Sans-Serif; }\n'+
				  'h3 { text-align: center; }\n'+
				  'td { text-align: center; }\n'+
				  '<\/style><body>\n'+
				  '<h3>'+t+'<\/h3>\n <table align="center" border="0"><tr><td><img src="'+img+'" width="'+w+'" height="'+h+'" border="0" alt="'+title+'"></td></tr>\n'+
				  '<tr><td><hr width="100%" size="1"><form><input type="button" onClick="javascript:window.close();" value="Close Window">\n'+
				  '<hr width="100%" size="1"><\/form><\/td><\/tr><\/table><\/body><\/html>\n';
	      var win = open('',id,params);
		  win.document.write(msg);
		  win.document.close();
	   }
	   else { var win = open(url,id,params); } 
}
