/*
incPhotoWall.js
Javascript Functionality for PhotoWallPhoto.asp
-scott 12/11/06
    - 12/15/09 -christina WO71791 removed fixed height of 366 pixels
*/

// Global Gallery Variables
var objScrollTimer, gObjSlideShowTimer; // Timer variables for setTimout
var nTimesScrolled = 0; // Numbuer of times scrolled for hesitation in mousedown scrolling
var bManualScroll = false; // Variable to keep track if we had to allow for manual scrolling

var bGalleryLoaded = false; // Has the Scrolling Thumbnails iFrame Loaded?
var bMPUFrameLoaded = false; // Has the MPU iFrame Loaded?
var bMPUActive = false; // Used to prevent User from "skipping" MPU by clicking another photo

var bActiveSlideShow = false; // Are we in Slide-show mode?

// Slide show and MPU Ad Variables
var nSlideShowTime = 15;
var nMpuPerImages = 5;
var nImagesDisplayed = 0;

var nPhotoWallID = 0;


// Function to Get the Current URL for Non-Support Redirect
function funGetGelleryPhotoURL() {
	return "PhotoWallPhoto.asp?wallid=" + nPhotoWallID + "&photoid=" + nCurrentPhotoID + "&id=" + nCurrentPhotoID + "&SearchString=" + sSearchString;
}

// Sends the correct URL to the client
function funSendToFriend() {
	var sReferrer = "http://" + document.location.hostname + funGetGelleryPhotoURL();
	if (encodeURI) sReferrer = encodeURI(sReferrer);
	else if (escape) sReferrer = escape(sReferrer);
	sReferrer = sReferrer.replace(/&/g,"%26");
	document.location.href = "/tellafriend.asp?t=the%20Galleries&r=" + sReferrer;
}


// Get the Data for a new selected Photo
function funShowPhoto(nMediaFileID, bMPUViewed) {
	if (nMediaFileID == null) nMediaFileID = nCurrentPhotoID;
	if (bMPUViewed == null) bMPUViewed = false;

	nCurrentPhotoID = nMediaFileID;
	
	var bBrowserCompatible = (document.body.innerHTML) ? true : false
	
	if (bBrowserCompatible) {
		// Temporarily Change the Caption Text to "Loading"
		gfunDynamicTextUpdate("galleryPhotoName", "");
		g_getRawObject("galleryPhotoCaption").innerHTML = "Loading...";

		// Call AJAX Request Function [gbinc/jsfiles/incAJAX.js]
		bBrowserCompatible = gfunSendRequest("/PhotoWallAJAX.asp?wallid=" + nPhotoWallID + "&photoid=" + nMediaFileID, "get", null, null, "funDynamicPhotoUpdate", false, null);	
	}
	
	if (!bBrowserCompatible) {
		document.location.href = funGetGelleryPhotoURL();
	}
}

// Take AJAX Data and Display New Photo Details
function funDynamicPhotoUpdate(sAJAXString) {
	
	// Split the Data String into an Array of Properties
	var aryData = sAJAXString.split("\r\n");
    var nWidth = aryData[2];
	var nHeight= aryData[3];
	
    if (nWidth > 660){
       // nHeight = Math.round(nHeight / (nWidth / 550));
	    nWidth = 660;
    }
     
	// Temporarily load a trans.gif in the image slot
	gfunSwapImage("galleryPhotoImage", "/images/trans.gif");

	//Data: PhotoName 0, ImageURL 1, Width 2, Height 3, Caption 4, CurrentIndex 5, nPreviousFileID 6, nNextFileID 7, nMediaFileID 8
	gfunSwapImage("galleryPhotoImage", aryData[1]);
	funSetImageDimension("galleryPhotoImage", aryData[0], nWidth, nHeight);
	g_getRawObject("galleryPhotoName").innerHTML = aryData[0];
	g_getRawObject("galleryPhotoCaption").innerHTML = aryData[4];
	if (nPhotoWallID > 0) {
		gfunDynamicTextUpdate("galleryPhotoIndex", aryData[5]);
		nPreviousPhotoID = parseInt(aryData[6]);
		nNextPhotoID = parseInt(aryData[7]);
	}
	else {
		// Get these Values from the Scrolling iFrame
		var objIFrame = g_getRawObject("GalleryThumbnailsFrame");
		if (objIFrame) {
			if (objIFrame.contentWindow) objIFrame = objIFrame.contentWindow;
			if (objIFrame.funSetTopIndexes) {
				objIFrame.funSetTopIndexes();
				gfunDynamicTextUpdate("galleryPhotoIndex", nCurrentIndex.toString());
			}
		}
	}
	
	// Hide the MPU Ad and Show the Proper Iterating Buttons
	funHideMPUAd();
	funSetGalleryButtons();

	// Record Hit
	funRecordHit("Photo Gallery (" + aryData[8] + ")");
	nImagesDisplayed++;
}

// Set the Image Width and Height
function funSetImageDimension(sImage, sImageName, nWidth, nHeight) {
	var objImage = g_getRawObject(sImage);
	
	if (objImage.complete != null) {
		if (!objImage.complete) {
			objPhotoLoadTimer = setTimeout("funSetImageDimension('" + sImage + "', '" + sImageName + "', , );", 500);
		}
		else {	
			if (objImage.width) objImage.width = nWidth;
			if (objImage.height) objImage.height = nHeight;
			if (objImage.alt) objImage.alt = sImageName;
		}
	}
	else {
		if (objImage.width) objImage.width = nWidth;
		if (objImage.height) objImage.height = nHeight;
		if (objImage.alt) objImage.alt = sImageName;
	}
}

// Show the Previous Photo in the Gallery
function funPrevPhoto() {

	if (nPreviousPhotoID > 0) {
		funUpdateScroller(nPreviousPhotoID, "prev");
	//	funShowPhoto(nPreviousPhotoID);
	}

}
// Show the Next Photo in the Gallery
function funNextPhoto() {
    //for(i=0;i<8;i++){
	if (nNextPhotoID > 0) {
		funUpdateScroller(nNextPhotoID, "next");
		
				
		//funShowPhoto(nNextPhotoID);
		//if (bActiveSlideShow) {
		//	gObjSlideShowTimer = setTimeout("funNextPhoto();", nSlideShowTime * 1000);
		//}
	}
	//}
}

// Show the Previous Photo in the Gallery
function funPrevPhoto7() {
    for(i=0;i<7;i++){
	if (nPreviousPhotoID > 0) {
		funUpdateScroller(nPreviousPhotoID, "prev");
		//funShowPhoto(nPreviousPhotoID);
	}
  }
}
// Show the Next Photo in the Gallery
function funNextPhoto7() {
    for(i=0;i<7;i++){
	if (nNextPhotoID > 0) {
		funUpdateScroller(nNextPhotoID, "next");		
		//funShowPhoto(nNextPhotoID);		
	    }
	}
}

// Update the Selected Photo in the Thumbnail iFrame
function funUpdateScroller(nPhotoID, sIndexChange) {
	var objIFrame = g_getRawObject("GalleryThumbnailsFrame");
	
	if (objIFrame) {
		if (objIFrame.contentWindow) objIFrame = objIFrame.contentWindow;
	
		if (objIFrame.funSetCurPhoto) objIFrame.funSetCurPhoto(nPhotoID, sIndexChange);
	}
}

// Show or Hide the Prev and Next Buttons [gfunHide and gfunShow in gbinc/jsfiles/incPositioningAPI.js]
function funSetGalleryButtons() {
	if (nPreviousPhotoID == 0) gfunHide("btnGalleryPhotoPrev"); 
	else gfunShow("btnGalleryPhotoPrev");
	if (nNextPhotoID == 0) gfunHide("btnGalleryPhotoNext");
	else gfunShow("btnGalleryPhotoNext");
}

/************************************************************
	SCROLLING THUMBNAIL BUTTONS FUNCTIONS
*************************************************************/
/*
Scrolling Thumbnails are in an iframe called: GalleryThumbnailsFrame
Each photo is in a Square Cell followed by a 2 pixel spacer
*/

// Scroll the Thumbnail iFrame "Left" or "Right"
function funStartPhotoScroll(sDir) {
	var objIFrame = g_getRawObject("GalleryThumbnailsFrame");
	
	if (objIFrame) {
		if (objIFrame.contentWindow) objIFrame = objIFrame.contentWindow;
	
		try {
			// Call the function in the iFrame which returns true if more room to scroll.
			if (objIFrame.funScroll(sDir)) {
				// Still can scroll that direction
				var nTimer;
				// Ramp up the Speed of Scrolling
				if (nTimesScrolled == 0) nTimer = 0;
				//else if (nTimesScrolled < 5) nTimer = 200;
				//else if (nTimesScrolled < 8) nTimer = 150;
				//else if (nTimesScrolled < 10) nTimer = 100;
				//else nTimer = 80;
				
				objScrollTimer = setTimeout("funStartPhotoScroll('" + sDir + "');", 0);
				nTimesScrolled++;
			}
			//else {
			//	funStopPhotoScroll();
			//}
		}
		catch(e) {
			// Error, so let the client scroll manually
			if (!bManualScroll) {
				var objIFrame = g_getRawObject("GalleryThumbnailsFrame");

				// Increase the height and show the browser scrollbar
				if (objIFrame.height) objIFrame.height = 87;
				if (objIFrame.scrolling) objIFrame.scrolling = "yes";
				bManualScroll = true;
			}
		}
	}
}

// Stop Scrolling the Thumbnails iFrame
function funStopPhotoScroll() {
	window.clearTimeout(objScrollTimer);
	nTimesScrolled = 0;
	return true;
}


/************************************************************
	MPU AD FUNCTIONS
*************************************************************/

// Called onload of MPU iFrame Document
function funMPULoaded() {
	bMPUFrameLoaded = true;
	//funShowMPUAd();
}

// Updates the src of the MPU Ad iFrame
var sSrc = "";
function funGetNewMPUAd(bSameAsLast) {
	if (bSameAsLast == null) bSameAsLast = false;
	var objIFrame = g_getRawObject("frameMPU");
	if (objIFrame) {
		if (!bSameAsLast) {
			var dDate = new Date();
			sSrc = "/PhotoWallMPU.asp?" + dDate.getTime()
		}
		objIFrame.src = sSrc;

		bMPUFrameLoaded = false;
	}
}

// Displays an MPU Ad for a specified time before showing the next Photo
var bMPUTryingToLoad = 0;
function funShowMPUAd() {
		
	// Now, Show the MPU Ad and Hide the Image	
	var objIFrame = g_getRawObject("frameMPU");

	if (objIFrame) {
		// Start MPU Ad Over Again
		if (bMPUTryingToLoad == 0) funGetNewMPUAd(true);
		bMPUActive = true;

		// Hide the Image and other Image related items
		var objImage = g_getRawObject("galleryPhotoImage");
		objImage.height = 1;
		objImage.width = 1;
		gfunCollapse("galleryPhotoImage"); // [gbinc/ jsfiles/ incPositioning.js]

		// Show the Loading Message
		gfunDynamicTextUpdate("galleryPhotoName", ""); // [gbinc/ jsfiles/ incCommon.js]
		gfunDynamicTextUpdate("galleryPhotoIndex", ""); // [gbinc/ jsfiles/ incCommon.js]
		gfunHide("galleryPhotoIndexTotal"); // [gbinc/ jsfiles/ incPositioning.js]
		gfunDisplay("MPULoadingMessage"); // [gbinc/ jsfiles/ incPositioning.js]
		g_getRawObject("galleryPhotoCaption").innerHTML = "";
		//g_getRawObject("galleryPhotoCaption").innerHTML = "<div align=\"center\"><p>Your content is loading.</p></div>";
		gfunHide("btnGalleryPhotoPrev"); // [gbinc/ jsfiles/ incPositioning.js]
		gfunHide("btnGalleryPhotoNext"); // [gbinc/ jsfiles/ incPositioning.js]

		// Check to see if the MPU is Loading
		if (!bMPUFrameLoaded) {
			if (bMPUTryingToLoad < 30) {
				bMPUTryingToLoad++;
				setTimeout("funShowMPUAd();",500);
				return false;
			}
			else {
				// Time limit is Up!
				funHideMPUAd(true);
				funShowPhoto(null, true);
				return true;
			}
		}

		// Expand and Show the MPU IFrame
		objIFrame.height = 263;
		objIFrame.width = 350;
		gfunDisplay("frameMPU"); // [gbinc/ jsfiles/ incPositioning.js]

		// Set the Timout for Showing the Next Photo
		gObjSlideShowTimer = setTimeout("funShowPhoto(null, true);", nSlideShowTime * 1000);

		// Reset our Safety Valve
		bMPUTryingToLoad = 0;
	}
	else {
		document.location.href = funGetGelleryPhotoURL() + "&MPU=yes";
	}
}

// Hide the MPU Ad iFrame
function funHideMPUAd(bForceHide) {
	if (bForceHide == null) bForceHide = false;
	var objIFrame = g_getRawObject("frameMPU");

	if (objIFrame) {
		bMPUActive = false;
		objIFrame.src = "PhotoWallMPU.asp?hide=yes";
		if (objIFrame.height > 1 || bForceHide) {
			// Hide the MPU iFrame
			objIFrame.height = 1;
			objIFrame.width = 1;
			gfunCollapse("frameMPU"); // [gbinc/ jsfiles/ incPositioning.js]
			gfunCollapse("MPULoadingMessage"); // [gbinc/ jsfiles/ incPositioning.js]
			// Show the Image and the Photo Index
			gfunDisplay("galleryPhotoImage"); // [gbinc/ jsfiles/ incPositioning.js]
			gfunShow("galleryPhotoIndexTotal"); // [gbinc/ jsfiles/ incPositioning.js]
		}
	}
}

/**************************************
	WEBSIDESTORY HIT BOX CODE
***************************************/

// Code taken from incPostBlock.asp to generate Page View on new Image displayed
function funRecordHit(sPageName) {
	
}
