/**
 * @author slane
 */

/* --------------------------------------------
ObjBannerRotator()
Constructors: requires 1 args:
	0 - (String) strInstanceNameIn: name of the instance of the object created in the page.
				 Used to write calls to methods of the instance from the generated code.
 -------------------------------------------- */
 function ObjBannerRotator() {
	// object stuff
	this.objWindow = null;
	this.tmpImg = null;

	// banners
 	this.arrBanners = new Array();
	this.bannerHeight = 226;
	this.bannerWidth = 840;
	this.bannerTargetImg = null;
	this.bannerTargetImgName = "";
	this.bannerTargetImgClassname = "";
	
	// ads
	this.arrAds = new Array();
	
	this.setBannerSize = function(intHeight, intWidth){
		this.bannerHeight = intHeight;
		this.bannerWidth = intWidth;
	}

	this.setBannerClassname = function(strIn) {
		this.bannerTargetImgClassname = strIn;   
	}
	
	this.addBanner = function(objIn) {
		if (objIn.type == void(0) || objIn.type != "objbanneritem") return;
		// add to array
		this.arrBanners[this.arrBanners.length] = objIn;
		// add images to document.images array.
		if (objIn.src != null) {
			objIn.docArrayPos = document.images.length;
			this.tmpImg = new Image(this.bannerWidth, this.bannerHeight);
			this.tmpImg.src = objIn.src;
			this.tmpImg.name = objIn.name;
			this.objWindow.document.images[this.objWindow.document.images.length] = this.tmpImg;
		}
	}		
	
	this.addAd = function(objIn) {
		if (objIn.type == void(0) || objIn.type != "objaditem") return;
		// add to array
		this.arrAds[this.arrAds.length] = objIn;
		// add images to document.images array.
		if (objIn.src != null) {
			objIn.docArrayPos = document.images.length;
			this.tmpImg = new Image();
			this.tmpImg.src = objIn.src;
			this.tmpImg.name = objIn.name;
			this.objWindow.document.images[this.objWindow.document.images.length] = this.tmpImg;
		}
	}		

	this.getBanner = function() {
		var intCursor = Math.round( Math.random()*this.arrBanners.length );
		if (intCursor == this.arrBanners.length) intCursor = intCursor-1;
		var thisBanner = this.arrBanners[intCursor];
		var thisTarget = findDOM(this.bannerTargetImgName);
		if (thisBanner.href.length == 0) {
			thisTarget.innerHTML ="<img src=\""+ thisBanner.src + "\" class=\"" + this.bannerTargetImgClassname + "\" style=\"width: " + this.bannerWidth + "px; height: + this.bannerHeight + px;\"></a>";
//			if (this.bannerTargetImg == null) this.bannerTargetImg = this.objWindow.document[this.bannerTargetImgName];
//			this.bannerTargetImg.src = thisBanner.src;
		} else {
			thisTarget.innerHTML ="<a href=\"" + thisBanner.href + "\" target=\"_new\" class=\"" + this.bannerTargetImgClassname + "\" style=\"margin: 0px; padding: 0px;\"><img src=\""+ thisBanner.src + "\" style=\"width: " + this.bannerWidth + "px; height: + this.bannerHeight + px;\"></a>";
		}
	}
	
	// Constructor setup & Validation
	if (arguments[0] == void(0) ) {
		alert("Must pass in reference to main window object");
		return;
	} else {
		this.objWindow = arguments[0];
	}

	// set banner rotation target 	 
	if (arguments[1] != void(0) && arguments[1].length > 0) {
		this.bannerTargetImgName = arguments[1];
	}
 }

/* -------------------------------------------
ObjBannerItem()
Constructors: requires 1 args:
	0 - (String) strInstanceNameIn: name of the instance of the object created in the page.
				 Used to write calls to methods of the instance from the generated code.
 -------------------------------------------- */
function ObjBannerItem() {
	this.name="";
	this.src = "";
	this.href = "";
	this.type = "objbanneritem";
			
	// constructor, kind of
	if (arguments[0] != void(0) ) {
		this.name = arguments[0];
	}
	if (arguments[1] != void(0) && arguments[1].length > 0) {
		this.src = arguments[1];
	}

	if (arguments[2] != void(0) && arguments[2].length > 0) {
		this.href = arguments[2];
	}

}
 

 
 
function ObjBanners(arrBannersIn,ObjWindowIn) {
	this.arrBanners = new Array();
	this.Window;
	
	this.add = function(strImgPath) {
		this.Window.document.images[document.images.length] = new Image();
		this.Window.document.images[ (document.images.length-1) ].src = strImgPath;
		this.arrBanners[this.arrBanners.length] = document.images[ (document.images.length-1) ];
	}
	this.show = function() {
		var intCursor = Math.round( Math.random()*this.arrBanners.length );
		this.bannerImg.src = document.images[intCursor].src;
	}
	this.setBanner = function(objBannerImgIn){
		this.bannerImg = objBannerImgIn;
	}
	
	//constructor-ish stuff
	if (arguments.length > 0) {
		this.Window = ObjWindowIn;
		this.arrBanners = arrBannersIn;	
		for (var i=0; i< this.arrBanners.length; i++) {
			this.add(this.arrBanners[i]);
		}
	}
}
 
 
/* --------------------------------------------
ObjMainMenu()
Constructors: requires 1 args:
	0 - (String) strInstanceNameIn: name of the instance of the object created in the page.
				 Used to write calls to methods of the instance from the generated code.
 -------------------------------------------- */
function ObjMainMenu(strInstanceNameIn) {
	if (arguments.length < 1) {
		alert("Specify instance name in ObjMainMenu constuctor");
		return;
	}
	
	this.type = "objmainmenu";
	this.strInstanceName = strInstanceNameIn;
	this.arrMenuItems = new Array();
	this.intCurrentOffset = 0;
	this.currentMenuItemNumber = -1;

	this.add = function(objIn, self) {
		if (objIn.type == void(0)) return;
		
		if (objIn.type == "objmainmenuitem") {
			// set parent reference on incoming item
			objIn.objMenu = this;
			// add to array
			this.arrMenuItems[objIn.orderNum] = objIn;
			// add images to document.images array.
			if (objIn.img != null) document.images[document.images.length] = objIn.img;
			if (objIn.imgOver != null) document.images[document.images.length] = objIn.imgOver;
			if (objIn.imgSelected != null) document.images[document.images.length] = objIn.imgSelected;
		}

	}		
	
	this.setCurrentOffset = function() {
		// find left edge of content table
		this.intCurrentOffset = findLeft("mainArea");
	}
	
	this.swapOn = function(intImgNum, objImg) {
		this.currentMenuItemNumber = intImgNum;
		this.arrMenuItems[this.currentMenuItemNumber].swapImage(objImg);
	}		
	
	this.swapOff = function(intImgNum, objImg) {
		this.arrMenuItems[this.currentMenuItemNumber].swapImage(objImg);
	}

	this.subMenuOn = function() {
		this.arrMenuItems[this.currentMenuItemNumber].subMenuOver();
	}
	
	this.subMenuOff = function() {
		this.arrMenuItems[this.currentMenuItemNumber].subMenuOut();
	}
	
	this.setCurrentOffset = function() {
		this.intCurrentOffset = findLeft("mainArea");
	}
	
	this.writeMenu = function(strTargetIdIn) {
		var objTarget = findDOM(strTargetIdIn, false);
		if (objTarget == null) {
			alert("target not found with id " + strTargetIdIn);
		} else {
			var strMenuItems = "";
			var strTmp = "";
			var isCurrentPage = false;
			for (var i=0; i<this.arrMenuItems.length; i++) {
				if (this.arrMenuItems[i] == null) continue;
				// determine if current page is showing.
				if ((this.arrMenuItems[i].href !="") && (window.location.pathname.indexOf(this.arrMenuItems[i].href) != -1)) isCurrentPage=true;
				else isCurrentPage = false;
				
				if (this.arrMenuItems[i].type == "objmainmenuitem") {
					// get image source
					strTmp = "<img src=\"";
					if (isCurrentPage && this.arrMenuItems[i].imgSelected != null) {
						strTmp += this.arrMenuItems[i].imgSelected.src;
					} else {
						strTmp += this.arrMenuItems[i].img.src;
					}
					strTmp += "\" id=\"mm" + i + "\" class=\"MenuTile\" ";
					
					// add mouseover, mouseout if rollover exists.
					if (this.arrMenuItems[i].imgOver != null && !isCurrentPage) {
						strTmp += "onmouseover=\"" + this.strInstanceName + ".swapOn(" + i + ", this)\"";
						strTmp += " onmouseout=\"" + this.strInstanceName + ".swapOff(" + i + ", this)\"";
					}
					strTmp += ">"; 
					//alert(strTmp);
					
					// if menuItem has href, format link. (Need to add popup code)
					if (this.arrMenuItems[i].href.length > 0 && !isCurrentPage) {
						strTmp = "<a href=\"" + this.arrMenuItems[i].href + "\">" + strTmp + "</a>";
					}
					strMenuItems += strTmp;
					strTmp = "";
				}
				
			}
			//alert(strMenuItems);
			objTarget.innerHTML = strMenuItems;
		} 
		return;
	}
	
}

/* --------------------------------------------
ObjMainMenuItem()
Constructors: will accept 0-6 args, adding them in order of:
	0 - (String) img: source or "at rest" image
	1 - (String) imgOver: source of "over" image
	2 - (String) imgSelected: source of "selected" image
	3 - (int) orderNum: order number of appearance in menu
	4 - (String) href: where you wnat link to go
	5 - (bool) isPopup: should href target a popup window?
	6 - (String) subMenuId:  Id of item that contains submenu links.
 -------------------------------------------- */
function ObjMainMenuItem() {
	this.type = "objmainmenuitem";
	this.orderNum = 0;	

	// images
	this.targetImage	= null; // Image on Page
	this.img 			= null;	// Image Object - at rest
	this.imgOver 		= null;	// Image Object - hover
	this.imgSelected 	= null;	// Image Object - selected
	
	this.imgLeft 		= 0;	// Left position of target image
	
	// link target
	this.href="";
	this.isPopup = false;
	// Parent and Child object references - will be set when loaded.
	this.objMenu = null;		// the ObjMainMenu that holds it
	this.objSubMenu = new ObjSubMenu();		// the ObjSubMenu to open onMouseOver (optional)
	this.objSubMenu.objMenuItem = this; 	// set reference on child object to parent.

	// constructor, kind of
	if (arguments[0] != void(0) && arguments[0].length > 0) {
		this.img = new Image();
		this.img.src = arguments[0];
	}
	if (arguments[1] != void(0) && arguments[1].length > 0) {
		this.imgOver = new Image();
		this.imgOver.src = arguments[1];
	}
	if (arguments[2] != void(0) && arguments[2].length > 0) {
		this.imgSelected = new Image();
		this.imgSelected.src = arguments[2];
	}
	if (arguments[3] != void(0)) this.orderNum = arguments[3];
	if (arguments[4] != void(0)) this.href = arguments[4];
	if (arguments[5] != void(0)) this.isPopup = arguments[5];
	// if a Submenu DIV id is supplied, set it on the blank ObjSubMenu thias is a child fo the MenuItem
	if (arguments[6] != void(0)) this.objSubMenu.divId = arguments[6];

	
	this.swapImage = function(targetImageIn) {
		// init target image
		if (this.targetImage == null) this.setTargetImage(targetImageIn);
	
	
		// if targetImage Source is same as overState src, change back to inactive state source 
		if (this.targetImage.src == this.img.src) {
			// onMouseOver state - mouse has just entered image area.  Put submenu code in here
			this.targetImage.src = this.imgOver.src;

			if (this.objSubMenu.divId != "") {
				// init SubMenuItem
				this.objSubMenu.init();
				// reset offset for left margin
				this.objMenu.setCurrentOffset();
				this.objSubMenu.mouseOver();
			}		
		} else {
			// onMouseOut state - image has already flopped, switch back
			this.targetImage.src = this.img.src;
			this.objSubMenu.mouseOut();
		}
	}
	
	this.subMenuOver = function(){
		// show selected submenu, if applicable.
		this.objSubMenu.mouseOver();
	}
		
	this.subMenuOut = function(){
		this.objSubMenu.mouseOut();
	}
		
	this.setTargetImage = function(objImageIn){
		this.targetImage = objImageIn;
		// find left position of target image
		this.imgLeft = findLeft(this.targetImage.id);
	}

/*
		objImg.src = this.arrMenuItems[intImgNum].imgOver.src;
		// find left edge of calling image.
		this.imgLeft = findLeft(objImg.id);
		var intTotalOffset = this.imgLeft + intOffset;
*/

}

function ObjSubMenu (divIdIn, objMenuItemIn) {
	this.type = "objsubmenu";
	this.onStyleClassname = "subMenu";
	this.offStyleClassname = "subMenuOff";
	this.isOpen = false;
	
	// the DIV in the page
	this.divId = "";			// if supplied, set when constructed. If it is blank, then no submenu
	this.divObj = null;
	this.divLeft = 0;
	
	// object references
	this.objMainMenu = null;
	this.objMenuItem = null;	// set when constructed
	
	// methods
	this.show = function() {
		if(this.divObj != null) {
			this.divObj.className = this.onStyleClassname;
			this.setLeft();
			this.isOpen=true;
		}
	}
	
	this.hide = function() {
//		this.writeNote("hide");
		if(this.divObj != null) {
			this.divObj.className = this.offStyleClassname;
			this.isOpen=false;
		}
	}
	
/*
	this.strNote = this.divId + "<BR>";
	this.writeNote = function(strIn) {
		this.strNote = strIn + "<BR>" + this.strNote;
		var noteDiv = findDOM("note", false);
		noteDiv.innerHTML = this.strNote;		
	}
*/	

	this.mouseOver = function() {
		if (!this.isOpen) this.show();
		this.isOpen = true;
//		this.writeNote("over: " + this.isOpen);
	}
	
	this.mouseOut = function() {
//		if (this.isOpen) setTimeout("mMenu.arrMenuItems[mMenu.currentMenuItemNumber].objSubMenu.hide()", 1000);
		if (this.isOpen) this.hide();
		this.isOpen = false;
//		this.writeNote("out: " + this.isOpen);
	}

	this.setLeft = function() {
		setLeft(this.divObj, (this.objMainMenu.intCurrentOffset + this.divLeft) );
		//alert("objMainMenu.intCurrentOffset: " + this.objMainMenu.intCurrentOffset + "\nthis.divLeft: " + this.divLeft);
	}
	
	/* Constructor-ish stuff */
	this.init = function(){
		// check to see if already initalized
		if (this.objMainMenu != null) return;
		// set Object References
		this.objMainMenu = this.objMenuItem.objMenu;
		
		// Set this.divId
		// see if submenu DIV element exists
		this.divObj = findDOM(this.divId);
		if (this.divObj == null) return;
		
		// if DIV does exist, firgure out where corresponding image is and set left edge
		this.divLeft = this.objMenuItem.imgLeft;
	}

}
/* --------------------------------------------
Various window-level functions	
 -------------------------------------------- */
function openWin(strURL,width,height) {
	 var h = 500;
	 var w = 750; 
	 if (arguments[1] != void(0)) w = arguments[1]+20;
	 if (arguments[2] != void(0)) h = arguments[2]+20;
	 var strArgs = 'scrollbars=yes,width='+w+',height='+h+',left=50,top=50,resizable=yes,menubar=yes,scrollbars=yes,status=yes,toolbar=yes,location=yes'
	// alert(strArgs);
     var siteWin = window.open('', 'siteWin', strArgs);
     siteWin.location = strURL;
     siteWin.focus();
}

function openEmail() {
	window.location.href="mailto:jfflt@aol.com";	
}

function doOnUnload() {
}

function doOnLoad() {
	mMenu.writeMenu("mainMenu");
	objBanner.getBanner();
}



