<!-- ----------------------------------------------------------------------------------
//
// Description:	Application main view and framework routines.
//		  
// Maintenance:
//
// Date			Developer			Modification
// --------   	------------------	----------------------------------------------
// 10/01/06		Dan Carlson			Initial version created.
//
//--------------------------------------------------------------------------------- -->

//-------------------------------------------------------------------------------------
// Attach events.
//-------------------------------------------------------------------------------------
window.onload = InitPage;
window.onresize = SetMainLayout;
window.onbeforeunload = DBCloseUserSession;

//-------------------------------------------------------------------------------------
// Module level variables.
//-------------------------------------------------------------------------------------
var moMain = top.frames;
var msSessionID = "" + new Date().getTime();
var miLastMenu = -1;
var msUserSessionID = null;

//Variables for swapping/scrolling banner images.
var maBannerPics = new Array("River", "Lake", "MtnLake", "Mountain");
var miBannerPicIndex = 0;
var miBannerPicPos = 3; 
var miShortPause = 5000;
var miLongPause = 10000;
var miBannerPicTimer;

//Load sorting module here, so sorting can be non-asynchronous.
var moXSL = new ActiveXObject("Microsoft.XMLDOM");
moXSL.async = true;
moXSL.load("stylesheets/Sort.xsl");

//-------------------------------------------------------------------------------------
// Populates the menu bar for the specified view.
//-------------------------------------------------------------------------------------
function BuildMenuBarItems(bLevel2) {
		
	var aItems = new Array("Home", "Gallery", "Products", "Press Releases", "About Us", "Contact Us", "Member Login");


//****TESTING *****
bLevel2 = true;




	if (bLevel2) aItems.push("Admin");
	
	//Clear existing items on the Menu Bar.
	MenuBar.innerHTML = "";

	//Create a menu bar item for each reporting system.
	for (i=0; i<aItems.length; i++) {
		oLabel = document.createElement("LABEL");	
		MenuBar.insertAdjacentElement("beforeEnd",oLabel);
		oLabel.className = "MenuBarItem";
		oLabel.innerText = aItems[i];
		oLabel.id = aItems[i];
		oLabel.style.top = "+8";
		oLabel.style.height = 22;
		oLabel.setAttribute("Index", i);

		if (i==0) {
			oLabel.style.left = 10;
		}
		else {
			//Give every item a consistant gap.
			with (MenuBar.children(aItems[i-1])) {
				oLabel.style.left = offsetLeft + offsetWidth + 30;
			}	
			
            //Create separator between menu items.
		    oSpan = document.createElement("SPAN");	
	    	oSpan.className = "MenuBarSeparator";
		    oSpan.style.left = oLabel.offsetLeft - 15;
		    MenuBar.insertAdjacentElement("beforeEnd",oSpan);
		}
			
		oLabel.attachEvent('onmouseover', SetMenuHilite);
		oLabel.attachEvent('onmouseout', SetMenuHilite);
		oLabel.attachEvent('onclick', DoMenuItem);
	}
}

//-------------------------------------------------------------------------------------
// Displays the shopping cart. 
//-------------------------------------------------------------------------------------
function CartPopupShow() {
			
	CartPopup.Show(160, 100, 740, 370); 	
}

//-------------------------------------------------------------------------------------
// Submits SQL statement to db.asp to close user's session.
//-------------------------------------------------------------------------------------
function DBCloseUserSession() {

//This function disabled for now!
return;



	var fa = db.document.all;
	
	//User never got logged in.
	if (db.DataNodeList().length == 0) return;
	
	//Build SQL statement.
	var oSQL = new SQL();
	oSQL.stmt = "qryUserSessionClose";
	BuildSQL(oSQL, "@SessionID", msSessionID); 
	BuildSQL(oSQL, "@UserID", GetAttribute("UserID"), true); 
	
	//Trim the last comma from the SQL code string.
	oSQL.stmt = oSQL.stmt.substr(0,oSQL.stmt.length - 2); 
		
	//Submit SQL statement to the server.
	fa.Database.value = "";
	fa.ReturnData.value = "N";
	fa.Callback.value = "";
	fa.SQL.value = oSQL.stmt;
	fa.ErrorMsg.value = "Unable to close user session.";
	fa.frmDB.submit();

    //Create a slight delay, allowing time for SQL stmt to finish executing.
	for (var i=0; i<Math.pow(2, 17); i++) {
	}
}

//-------------------------------------------------------------------------------------
// Submits SQL statement to db.asp to retrieve user profile from database and loads 
// results into an XML document.
//-------------------------------------------------------------------------------------
function DBLoadUser(sCallback) {
	
	var fa = db.document.all;
	
	//Build SQL statement.
	var oSQL = new SQL();
	oSQL.stmt = "qryUserSel";
	BuildSQL(oSQL, "@NT_UserName", txtUserName.value); 
	BuildSQL(oSQL, "@SessionID", msSessionID); 
	BuildSQL(oSQL, "@BrowserVersion", clientInformation.appVersion); 
	BuildSQL(oSQL, "@BrowserMinorVersion", clientInformation.appMinorVersion); 
	BuildSQL(oSQL, "@ScreenSize", screen.width + " by " + screen.height); 
	BuildSQL(oSQL, "@CookiesOnFlag", (clientInformation.cookieEnabled) ?1 :0, true); 
	BuildSQL(oSQL, "@CPU", clientInformation.cpuClass); 

	//Trim the last comma from the SQL code string.
	oSQL.stmt = oSQL.stmt.substr(0,oSQL.stmt.length - 2); 
		
	//Submit SQL statement to the server.
	fa.Database.value = "";
	fa.ReturnData.value = "Y";
	fa.Callback.value = sCallback;
	fa.SQL.value = oSQL.stmt;
	fa.ErrorMsg.value = "Unable to retrieve user profile.";
	fa.frmDB.submit();
}

//-------------------------------------------------------------------------------------
// Submits SQL statement to db.asp to retrieve a unique session ID.
//-------------------------------------------------------------------------------------
function DBCreateUserSession(sCallback) {
	
	var fa = db.document.all;
	
	//Build SQL statement.
	var oSQL = new SQL();
	oSQL.stmt = "qryUserSessionCreate";

	//Trim the last comma from the SQL code string.
	//oSQL.stmt = oSQL.stmt.substr(0,oSQL.stmt.length - 2); 
		
	//Submit SQL statement to the server.
	fa.UseCustomCOM.value = "N";
	fa.Database.value = "";
	fa.ReturnData.value = "Y";
	fa.Callback.value = "parent.OnUserSessionCreated();"
	fa.SQL.value = oSQL.stmt;
	fa.ErrorMsg.value = "Unable to create session.";
	fa.frmDB.submit();
}

//-------------------------------------------------------------------------------------
// Process a mouse click on a menu bar item.
//-------------------------------------------------------------------------------------
function DoMenuItem() {

	var oMenu = event.srcElement;
    
    switch (oMenu.innerText) {
        case "Home":
			ContentFrame.location = "Home.asp";
            break;
        case "Gallery":
			ContentFrame.location = "Galleries.asp";
            break;
        case "Products":
			ContentFrame.location = "Products.asp";
            break;
        case "Press Releases":
			ContentFrame.location = "Press.asp";
            break;
        case "Our Clients":
			alert("Coming Soon!");
            break;
        case "About Us":
			ContentFrame.location = "AboutUs.asp";
            break;
        case "Contact Us":
			ContentFrame.location = "ContactUs.asp";
            break;
        case "Member Login":
			DBCreateUserSession();
            break;
        case "Admin":
			var sURL = "://";
			if (window.location.hostname.indexOf("www") >= 0) {
				sURL += "www.grand-majestic-images.com";
				if (window.location.pathname.indexOf("/test/") >= 0) sURL += "/test"; 
			}
			else {
				sURL += window.location.hostname + "/MajesticImages"; //Local server
			}
			var sProtocol = "https";
			
			//***TESTING***
			sProtocol = "http";
			
			var sPage = "Memli.asp";
			ContentFrame.location = sProtocol + sURL + "/" + sPage + "?l=2&t=email";
			break;
        default:
			break;
    }
}


//-------------------------------------------------------------------------------------
// Gets the specifed user attribute for the current client.
//-------------------------------------------------------------------------------------
function GetAttribute(sAttribute) {

	//Filter by client, if desired.
	var oNode = db.GetRow("ClientText", msClientText);

    //Return specified attribute.
    if (oNode == null) {
        return null;
    }
    else {
	    return oNode.getAttribute(sAttribute);
    }	    
}

//-------------------------------------------------------------------------------------
// Makes sure Popup Menu and Popup Submenu are both closed.
//-------------------------------------------------------------------------------------
function HideMenus() {

	//Popup menu's not used at this time.
	return;


	try {
		document.all.PopupMenu.style.visibility = "hidden";
		document.all.PopupSubMenu.style.visibility = "hidden";
	}
	catch(e) {
		//ignore error
	}
}

//-------------------------------------------------------------------------------------
// Initializes the page, fires immediately after the browser loads page. 
//-------------------------------------------------------------------------------------
function InitPage() {

	//Initialize view.
	SetMainLayout();

	//Load image information.
	PicsLoadFromDB();

	//Initiate banner pic swapping. 
	//moBannerPicTimer = window.setInterval("SwapBannerPic()",5000);
	
	//Build top-level menus.
	BuildMenuBarItems();

	//Get user information.
	//DBLoadUser("window.parent.OnUserLoaded()");
}

//-------------------------------------------------------------------------------------
// Called after the user session has been created.
//-------------------------------------------------------------------------------------
function OnUserSessionCreated() {

	//Check for error.
	if (db.Error()) {
		return;
	}

	//Get reference to XMLDOMNodeList.
	var oResults = db.DataNodeList();
    
    //If no data, get outta here.
	if (oResults.length == 0) {
		msUserSessionID = "alsjfdjasd34fsak343flja44lkjasdf";
		//return;
	}
	else {
		//Get new/modified Email ID.
		msUserSessionID = oResults.item(0).getAttribute("UserSessionID");
	}

	//Load login page.
	var sURL = "://";
	if (window.location.hostname.indexOf("www") >= 0) {
		sURL += "www.grand-majestic-images.com";
		if (window.location.pathname.indexOf("/test/") >= 0) sURL += "/test"; 
	}
	else {
		sURL += window.location.hostname + "/MajesticImages"; //Local server
	}
	var sProtocol = "https";
			
			//***TESTING***
			sProtocol = "http";
			
	var sPage = "Memli.asp";
	ContentFrame.location = sProtocol + sURL + "/" + sPage + "?l=1&u=" + msSessionID + "&s=" + msUserSessionID.substr(1,36);
}

//-------------------------------------------------------------------------------------
// Adjusts the size of the popup to fit the image.
//-------------------------------------------------------------------------------------
function PicPopupAdjust() {

	//alert("Image width = " + ImageStaging.offsetWidth + ", height = " + ImageStaging.offsetHeight);

	PicPopup.style.width  = ImageStaging.offsetWidth + 32;
	PicPopup.style.height = ImageStaging.offsetHeight + 40;
	PicPopupImage.src = ImageStaging.src;
	PicPopupImage.style.width = ImageStaging.offsetWidth;
	PicPopupImage.style.height = ImageStaging.offsetHeight;
	
	PicPopupLoad.style.visibility = "hidden";
}

//-------------------------------------------------------------------------------------
// Displays a larger version of a pic in a popup.
//-------------------------------------------------------------------------------------
function PicPopupShow(bSwitch, sPicURL, sTitle, sLeft, sTop, sWidth, sHeight) {

	if (bSwitch == true) {
		if (ImageStaging.getAttribute("PicURL") == sPicURL) {
			PicPopup.style.width  = ImageStaging.offsetWidth + 32;
			PicPopup.style.height = ImageStaging.offsetHeight + 40;
			PicPopupImage.src = ImageStaging.src;
			PicPopupImage.style.width = ImageStaging.offsetWidth;
			PicPopupImage.style.height = ImageStaging.offsetHeight;
		}
		else {
			ImageStaging.src = sPicURL;
			ImageStaging.setAttribute("PicURL", sPicURL);
			//PicPopupImage.src = sPicURL;
			PicPopupTitle.innerText = sTitle;
			PicPopup.style.width = sWidth;
			PicPopup.style.height = sHeight;
			PicPopup.style.left = sLeft;
			PicPopup.style.top = sTop;
		}
		
		//PicPopup.style.display = "inline";
		
		PicPopup.style.visibility = "visible";
		PicPopupLoad.style.visibility = "visible";
		
		//PicPopupImage.style.width = sWidth - 32;
		//PicPopupImage.style.height = sHeight - 40;
	}
	else {
		PicPopupImage.src = "images/WhiteBox.gif";
		PicPopup.style.visibility = "hidden";
		//PicPopup.style.display = "none";
	}
}

//-------------------------------------------------------------------------------------
// Displays image pricing in a popup.
//-------------------------------------------------------------------------------------
function PricePopupShow(bSwitch, sPicID, sTitle, iLeft, iTop) {

	if (bSwitch == true) {
		PricePopup.style.visibility = "visible";
		PricePopupTitle.innerText = "Prices: " + sTitle;
		
		//Initialize popup height;
		PricePopup.style.height = "400px";
		
		BuildPriceList(sPicID);
	
		//Set the horizontal location of the popup. If popup is cutoff on the right side, 
		//change the position of the popup so that it is located (and tracks) to the left 
		//of the mouse pointer.
		iLeft -= 5;
		if ((iLeft + PricePopup.offsetWidth) > (moMain.ContentBox.offsetWidth)) {
			iLeft = iLeft - moMain.PricePopup.offsetWidth - 5;
		}
			
		//Set the top position for the popup. If the bottom portion of the popup is not 
		//displayed, move the popup to a higher position.
		//iTop -= (PricePopup.offsetHeight + 20);
		iTop -= PricePopup.offsetHeight;
		if ((iTop + PricePopup.offsetHeight) > (MenuBar.offsetTop + ContentBox.offsetHeight)) {
			iTop -= ((iTop + PricePopup.offsetHeight) - (MenuBar.offsetTop + ContentBox.offsetHeight));
		}
		if (iTop < 0) iTop = 10;
		
		PricePopup.style.left = iLeft;
		PricePopup.style.top = iTop;
		PricePopup.style.visibility = "visible";
	}
	else {
		PricePopup.style.visibility = "hidden";
	}
}

//-------------------------------------------------------------------------------------
// Process the keydown event. Handles all relevant keystrokes.
//-------------------------------------------------------------------------------------
function ProcessKey() {
	
	if (event == null) return;
	
	//SHIFT+ first letter of menu will invoke the menu frame.
	if (event.shiftKey) {
		for(i=0; i<MenuBar.children.length; i++) {
			if (event.keyCode == MenuBar.children(i).innerText.charCodeAt(0)) {
				MenuBar.children(i).fireEvent("onmouseover");		
				return true;
			}		
		}
	}

    return;
    
	//Process arrow keys.
	if (document.all.PopupMenu.style.visibility == "visible") {
		//Get the index of the menu that is currently visible.
		i = miLastMenu;
	
		switch (event.keyCode) {
			case giKeyRight:
				MenuBar.children(i).fireEvent("onmouseout");
				i = (i == (MenuBar.children.length-1)) ? 0 : i+1;
				MenuBar.children(i).fireEvent("onmouseover");
				break;
			case top.giKeyLeft:
				MenuBar.children(i).fireEvent("onmouseout");
				i = (i == 0) ? MenuBar.children.length-1 : i-1;
				MenuBar.children(i).fireEvent("onmouseover");
				break;
		}
	}	
}

//-------------------------------------------------------------------------------------
// Returns user session identifier, used to cancel counts.
//-------------------------------------------------------------------------------------
function SessionID() {

	return msSessionID;
}

//-------------------------------------------------------------------------------------
// Turn content box left and right borders on or off.
//-------------------------------------------------------------------------------------
function SetContentBorders(bSwitch) {

	if (bSwitch) {
		ContentBox.style.borderColor = "rgb(230,233,233)";
		//ContentBox.style.borderStyle = "solid";
		//ContentBox.style.borderLeftWidth = "2px";
		//ContentBox.style.borderRightWidth = "2px";
	}
	else {
		ContentBox.style.borderColor = "white";
		//document.all.ContentBox.style.borderStyle = "none";
		//document.all.ContentBox.style.borderLeftWidth = "0px";
		//document.all.ContentBox.style.borderRightWidth = "0px";
	}
}

//-------------------------------------------------------------------------------------
// Changes background color of textboxes when they receive or lose focus.
//-------------------------------------------------------------------------------------
function SetInputFocus(oDoc) {

	var oElements;
	var iLength;

	//Single-line text-entry fields.
	oElements = oDoc.all.tags("INPUT");
	iLength = oElements.length;
	
	for (i=0; i<iLength; i++) {
		with (oElements.item(i)) {
			if (type == "text" && tabIndex >= 0) addBehavior("scripts/common/InputFocus.htc");
		}
	}	

	//Multi-line text entry controls. 
	oElements = oDoc.all.tags("TEXTAREA");
	iLength = oElements.length;
	
	for (i=0; i<iLength; i++) {
		with (oElements.item(i)) {
		    addBehavior("scripts/common/InputFocus.htc");
		}
	}	
	
	//Multi-line text entry controls. 
	oElements = oDoc.all.tags("SELECT");
	iLength = oElements.length;
	
	for (i=0; i<iLength; i++) {
		with (oElements.item(i)) {
		    addBehavior("scripts/common/InputFocus.htc");
		}
	}		
}

//-------------------------------------------------------------------------------------
// Hilites an menu bar item when user does a mouseover, and displays the menu.
//-------------------------------------------------------------------------------------
function SetMenuHilite() {
	
    if (event.type == "mouseover") {
        //Hilite menu item.
        event.srcElement.style.color = top.gsColorHilite;
    			
    	//Save index of menu being shown for keyboard support.
    	miLastMenu = event.srcElement.getAttribute("Index");
    			
    	//Let the current content frame handle the menu display.
    	try {
    	    eval("ContentFrame.ShowMenu('" + event.srcElement.innerText + "')");
    	}
    	catch(e) {
    	}    
    }
    else {
        //Remove hilite.
       	event.srcElement.style.color = top.gsColorMenuItem;
       	
    	//Hide menu if user is going across menu bar items.
    	if (event.y < 98) HideMenus();
    }
}

//-------------------------------------------------------------------------------------
// Applies or removes hilite on the shopping cart objects located on the menu bar.
//-------------------------------------------------------------------------------------
function SetMenuCartHilite() {

	if (event.type == "mouseover") {
		MenuCartItems.style.color = top.gsColorHilite;
		MenuCartHdr.style.color = top.gsColorHilite;
	}
	else {
		MenuCartItems.style.color = top.gsColorMenuItem;
		MenuCartHdr.style.color = top.gsColorMenuItem;
	}
}

//-------------------------------------------------------------------------------------
// Applies or removes hilite on popup's CLOSE image.
//-------------------------------------------------------------------------------------
function SetPopupCloseHilite() {

	var oSrc = event.srcElement;
	
    if (event.type == "mouseover") {
		oSrc.src = "images/PopupCloseOn.jpg";
    }
    else {
		oSrc.src = "images/PopupCloseOff.jpg";
 	}
}

//-------------------------------------------------------------------------------------
// Sets content visual layout.
//-------------------------------------------------------------------------------------
function SetMainLayout() {

	var da = document.all;
	var db = document.body;

	gel("UserName").style.left = parseInt(db.offsetWidth) - 230;  
	gel("UserName").style.top  = parseInt(da.MenuBar.offsetTop) + 6;

	gel("ContentBox").style.height = parseInt(db.offsetHeight)- 230;

	gel("ContentFrame").style.height = parseInt(da.ContentBox.offsetHeight);
	
	gel("MenuBar").style.width = "1009px";
	gel("MenuBarLine").style.width = "1009px";
	
	//Set footer layout, if footer is available.
	try {
		gel("ContentFrame").document.frames(0).document.frames("FooterFrame").SetFooterLayout();
	}
	catch (e) {}
}

//-------------------------------------------------------------------------------------
// Helper function to return reference to an object/element using getElementById.
//-------------------------------------------------------------------------------------
function gel(sID, oParent) {

	try {
		return (oParent) ? oParent.getElementById(sID) : document.getElementById(sID);
	}
	catch(e) {
		return null;
	}
}	

//-------------------------------------------------------------------------------------
// Displays text in the page header.
//-------------------------------------------------------------------------------------
function SetPageHdr(oLabel, bAction, sText) {

	with (oLabel) {
		oLabel.innerText = sText;
		oLabel.style.color = (bAction==true) ? "yellow" : top.gsColorPageHdr;
	}
}

//-------------------------------------------------------------------------------------
// Displays the slide show object.
//-------------------------------------------------------------------------------------
function StartSlideShow(sPicURL) {

	SlideShowBox.style.visibility = "visible";
	SlideShowBox.style.display = "inline";
}

//-------------------------------------------------------------------------------------
// Swaps the banner image displayed.
//-------------------------------------------------------------------------------------
function SwapBannerPic() {
	
	//Cancel the timer.
	window.clearInterval(moBannerPicTimer);

	//Increment banner pic counter.
	miBannerPicPos += 1;
	
	//Change image theme if the last pic in the theme is displayed.
	if (miBannerPicPos > 3) {
		miBannerPicPos = 1;
		miBannerPicIndex += 1;
		if (miBannerPicIndex >= maBannerPics.length) miBannerPicIndex = 0;
	}
	
	//Display the appropriate image.
	var sObject = "BannerPic" + miBannerPicPos;
	var sImage = "images/" + maBannerPics[miBannerPicIndex] + miBannerPicPos + ".jpg";
	eval("var oImage = " + sObject);
	oImage.style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=3)";
    oImage.filters.item(0).Apply();
	oImage.src = sImage;
    oImage.filters.item(0).Play()	 
	
	//Initiate timer.
	var iPauseTime = (miBannerPicPos == 3) ? 10000 : 4000;
	moBannerPicTimer = window.setInterval("SwapBannerPic()",iPauseTime);
}

-->