﻿// specific to custom cover books
var wasInnerPage = 0;

function setCustomCover()
{
    bookDivName = "customCover";
    bookCoverSrc = "photoCover";
    bookSpreadDiv = "customSpread";
    bookSpreadSrc1 = "photoLeft";
    bookSpreadSrc2 = "photoRight";
    shadowPageDiv = "shadowPage";
    shadowPageInner = "shadowInnerPage";
    bookZoomDiv = "zoomPhoto";
    bookZoomInner = "zoomInnerDiv";
    logoButtonLeftFF = "800px";
    logoButtonLeftIE6 = "893px";
    logoButtonLeftMinIE6 = "344px";
    logoButtonTopIE6 = "-25px";
    logoButtonTopMinIE6 = "0px";
    spineBackLeft = "374px";
    spineFrontLeft = "48px";
    bookZoomWidth = 787;
    bookZoomHeight = 619;
    intCurLeft = 0;
    intCurRight = 1;
    curZoomState = 0;
    isCoverPage = 1;
    wasInnerPage = 0;
    getBookPage(0, 0, bookDivName);
    getZoomedPhoto(0, 0);
    getBookPage(1, preLoadPhotos, bookSpreadDiv);
    getBookPage(albumCount - 1, 0, bookDivName);
    if (document.getElementById(shadowPageDiv)) 
        document.getElementById(shadowPageDiv).style.visibility="hidden";
    if (document.getElementById(bookDivName)) 
        document.getElementById(bookDivName).style.visibility="visible";
    if (document.getElementById(bookSpreadDiv)) 
        document.getElementById(bookSpreadDiv).style.visibility="hidden";
    if (document.getElementById("ButtonView")) 
        document.getElementById("ButtonView").style.visibility="visible";
    if (document.getElementById("AgainButtonView")) 
        document.getElementById("AgainButtonView").style.visibility="hidden";
    if (document.getElementById(bookZoomDiv)) 
        document.getElementById(bookZoomDiv).style.visibility="hidden";
    if (document.getElementById("zoomedSpine")) 
        document.getElementById("zoomedSpine").style.visibility="hidden";
    if (document.getElementById("pageNumbers")) 
        document.getElementById("pageNumbers").style.visibility="hidden";
    if (document.getElementById(shadowPageInner)) 
       document.getElementById(shadowPageInner).innerHTML = 
          "<img id=\"shadowBook\" alt=\"\" src=\"images/webview-shadow.png\" style=\"width: 922px; height: 374px;\" />";
    var pageLoc = document.getElementById(bookCoverSrc);
    pageLoc.photoIndex = 0;
	
	if (chk.getBrowser() == "Internet Explorer" && chk.getOS() != "Macintosh" ) 
	{
	    if (chk.needsPngFix())
	    {
	        correctPNG();
	    }
    }
    
    setCustomPositions();
}

function setCustomPositions()
{
    var windowWidth = getWindowWidth(top);
    
    if (chk.getBrowser() == "Internet Explorer")
    {       
        if (chk.needsPngFix())
        {
            if (document.getElementById("customShadow"))
                document.getElementById("customShadow").style.top = "-299px";
            if (document.getElementById("customSpine"))
                document.getElementById("customSpine").style.top = "-286px";
            if (document.getElementById("customSpineBack"))
                document.getElementById("customSpineBack").style.top = "-599px";
        }   
        
        if (windowWidth < 916)
        {
            bookZoomWidth = 787;
            bookZoomHeight = 619;
            document.getElementById(shadowPageDiv).style.left = "0px";
            document.getElementById(bookZoomDiv).style.left = "50px";
            document.getElementById("MainOwner").style.left = "3px";            
            document.getElementById(bookDivName).style.left = "219px";
            document.getElementById("customSpine").style.left = spineFrontLeft;
            document.getElementById("customSpineBack").style.left = spineBackLeft;
            document.getElementById("customShadow").style.left = "-4px";
            document.getElementById(bookSpreadDiv).style.left = "-3px";
            document.getElementById("ButtonView").style.left = "362px";
            document.getElementById("AgainButtonView").style.left = "275px";
            document.getElementById("pageNumbers").style.left = "240px";
            clickZoomSpreadLeft = "805px";
            clickZoomLeft = "644px";
            
            if (windowWidth < 675)
            {
                  document.getElementById(bookDivName).style.overflow = "visible";
            }
            else
            {
                  document.getElementById(bookDivName).style.overflow = "hidden";
            }
        }
        else
        {
            var windowHeight = getWindowHeight(top);
            // ***** figure out zoom scale
            bookZoomWidth = 787;
            bookZoomHeight = 619;
            windowWidth = windowWidth / 2;
            document.getElementById(shadowPageDiv).style.left = (windowWidth - 460) + "px";
            document.getElementById(bookZoomDiv).style.left = (windowWidth - 400) + "px";
            document.getElementById("MainOwner").style.left = (windowWidth - 449) + "px";
            document.getElementById(bookDivName).style.left = (windowWidth - 234) + "px";
            document.getElementById("customSpine").style.left = spineFrontLeft;
            document.getElementById("customSpineBack").style.left = spineBackLeft;
            document.getElementById("customShadow").style.left = "-4px";
            document.getElementById(bookSpreadDiv).style.left = (windowWidth - 464) + "px"; // "18px";
            document.getElementById("ButtonView").style.left = (windowWidth - 92) + "px";
            document.getElementById("AgainButtonView").style.left = (windowWidth - 180) + "px";
            document.getElementById("pageNumbers").style.left = (windowWidth - 212) + "px";
            clickZoomSpreadLeft = (windowWidth + 352) + "px";
            clickZoomLeft = (windowWidth + 190) + "px";
            document.getElementById(bookDivName).style.overflow = "hidden";
        }
    }
    else   // Firefox
    {
        document.getElementById("MainOwner").style.top = "40px";
        document.getElementById(shadowPageDiv).style.top = "86px";
        document.getElementById(bookDivName).style.top = "110px";
        document.getElementById("customPhoto").style.left = "0px";
        document.getElementById("customSpine").style.top = "-283px";
        document.getElementById("customSpineBack").style.top = "-596px";
        document.getElementById("customShadow").style.top = "-295px";
        document.getElementById(bookSpreadDiv).style.top = "85px";
        spineFrontLeft = "-161px";
        spineBackLeft = "162px";
       
        if (windowWidth < 930)
        {
            document.getElementById("MainOwner").style.left = "3px";
            document.getElementById(shadowPageDiv).style.left = "-4px";
            document.getElementById(bookDivName).style.left = "215px";
            document.getElementById("customSpine").style.left = spineFrontLeft;
            document.getElementById("customSpineBack").style.left = spineBackLeft;
            document.getElementById("customShadow").style.left = "-4px";
            document.getElementById(bookSpreadDiv).style.left = "-5px";
            document.getElementById("ButtonView").style.left = "360px";
            document.getElementById("AgainButtonView").style.left = "275px";
            document.getElementById("pageNumbers").style.left = "240px";
            document.getElementById(bookZoomDiv).style.left = "50px";
            clickZoomSpreadLeft = "805px";
            clickZoomLeft = "642px";
            
            if (windowWidth < 675)
            {
                  document.getElementById(bookDivName).style.overflow = "visible";
            }
        }
        else
        {
            windowWidth = windowWidth / 2;
            document.getElementById("MainOwner").style.left = (windowWidth - 460) + "px";
            document.getElementById(shadowPageDiv).style.left = (windowWidth - 469) + "px";
            document.getElementById(bookDivName).style.left = (windowWidth - 242) + "px";
            document.getElementById("customSpine").style.left = spineFrontLeft;
            document.getElementById("customSpineBack").style.left = spineBackLeft;
            document.getElementById("customShadow").style.left = "-4px";
            document.getElementById(bookSpreadDiv).style.left = (windowWidth - 470) + "px";
            document.getElementById("ButtonView").style.left = (windowWidth - 100) + "px";
            document.getElementById("AgainButtonView").style.left = (windowWidth - 190) + "px";
            document.getElementById("pageNumbers").style.left = (windowWidth - 221) + "px";
            document.getElementById(bookZoomDiv).style.left = (windowWidth - 415) + "px";
            clickZoomSpreadLeft = (windowWidth + 337) + "px";
            clickZoomLeft = (windowWidth + 182) + "px";
        }
    }
    
    document.getElementById("clickToZoomDiv").style.left = curPageType == INSIDE ? clickZoomSpreadLeft : clickZoomLeft;   	        
    setHeadAndFootPosition();
}

function setNextCustomPage()
{
    if (isCoverPage == 1)
    {
        isCoverPage = 0;
        intCurLeft = 1;
        intCurRight = 2;
        wasInnerPage = 1;
       
        if (chk.getBrowser() == "Internet Explorer")
        {
            document.getElementById("customShadow").style.top = "-302px";
        }
        else
        {
            document.getElementById("customShadow").style.top = "-296px";
        }
	    
        getBookPage(intCurLeft, preLoadPhotos, bookSpreadDiv, showCustomPhoto);
        getZoomedPhoto(intCurLeft, preLoadZoomedPhotos);
        showCustomPhoto(1);
    }
    else
    {
        nextCustomPhoto();
    }
}

function nextCustomPhoto()
{ 
	photoShown[intCurLeft] = 0;
	photoShown[intCurRight] = 0;
	intCurLeft += 2;
	wasInnerPage = 1;
	
	if (intCurLeft >= albumCount)
	{
	     intCurLeft = 0;
	}
	
	intCurRight = intCurLeft + 1;
	
    if (intCurLeft > 0 && intCurLeft < albumCount - 1)
    {
        getBookPage(intCurLeft, preLoadPhotos, bookSpreadDiv, showCustomPhoto);
	    getZoomedPhoto(intCurLeft, preLoadZoomedPhotos);
	    showCustomPhoto(intCurLeft);
	}
	else if (intCurLeft == 0 && (albumCount % 2 != 0))
    {
        getBookPage(intCurLeft, 0, bookDivName, showCover);
        getZoomedPhoto(intCurLeft, 0);
        showCover(intCurLeft);
    }
    else
    {
        if (intCurLeft == 0) intCurLeft = albumCount - 1;
        getBookPage(intCurLeft, 0, bookDivName, showBack);
        getZoomedPhoto(intCurLeft, 0);
        showBack(intCurLeft);
    }

	lPreview(originalInstanceGUID, instanceVersionGUID, 1);
	cookiePreview.lastPage = intCurLeft;
	cookiePreview.store();	
}

function prevCustomPhoto()
{
	photoShown[intCurLeft] = 0;
	photoShown[intCurRight] = 0;

    // jh 1-27-09 fix going from spread 2,3 to spread blank,1 instead of cover	
	if(intCurLeft == 2)
    	intCurLeft -= 1;
    else
    	intCurLeft -= 2;
	
	if (intCurLeft <= 0 && wasInnerPage == 1)
	{
	    intCurLeft = 0;
        getBookPage(intCurLeft, 1, bookDivName, showCover);
        getZoomedPhoto(intCurLeft, 1);
        showCover(intCurLeft);
	}
	else
	{
        if (intCurLeft > 0 && intCurLeft < albumCount - 1)
        {
            intCurRight = intCurLeft + 1;
            getBookPage(intCurLeft, preLoadPhotos, bookSpreadDiv, showCustomPhoto);
	        getZoomedPhoto(intCurLeft, preLoadZoomedPhotos);
	        showCustomPhoto(intCurLeft);
	    }
	    else if (intCurLeft == 0)
        {
            intCurRight = intCurLeft + 1;
            getBookPage(intCurLeft, 1, bookDivName, showCover);
            getZoomedPhoto(intCurLeft, 1);
            showCover(intCurLeft);
        }
        else
        {
            intCurLeft = albumCount - 1;
            intCurRight = intCurLeft + 1;
            getBookPage(intCurLeft, 1, bookDivName, showBack);
            getZoomedPhoto(intCurLeft, 1);
            showBack(intCurLeft);
        }

        lPreview(originalInstanceGUID, instanceVersionGUID, -1);
        cookiePreview.lastPage = intCurLeft;
        cookiePreview.store();		
    }
}

function showCustomPhoto(curPhoto)
{
    if (curPhoto != 1 && curPhoto < albumCount - 2) 
    {
	    showSingleCustomPhoto(curPhoto, bookSpreadSrc1, "pageNumberLeft");
	    showSingleCustomPhoto(curPhoto+1, bookSpreadSrc2, "pageNumberRight");
    } 
    else if (curPhoto == 1)
    {
        showEmptyPage(curPhoto-1, bookSpreadSrc1, "pageNumberLeft");
	    showSingleCustomPhoto(curPhoto, bookSpreadSrc2, "pageNumberRight");
	    intCurLeft--;
    }
    else
    {
	    showSingleCustomPhoto(curPhoto, bookSpreadSrc1, "pageNumberLeft");
        showEmptyPage(curPhoto+1, bookSpreadSrc2, "pageNumberRight");
    }
	
	if (chk.getBrowser() == "Internet Explorer" && chk.getOS() != "Macintosh" ) 
	{
	    if (chk.needsPngFix())
	    {
	        correctPNG();
	    }
    }
}

function showCover(curPhoto)
{
    showCustomDiv(FRONT);
    isCoverPage = 1;
    wasInnerPage = 0;
	intCurLeft = 0;
	intCurRight = 1;
	
    var olObject = document.getElementById(bookCoverSrc);
    
	if (!photoCache[curPhoto] && curPhoto < albumCount) 
	{
		olObject.src = imgLoading;
		return;
	}
	if (photoCache[curPhoto] && photoCache[curPhoto].src.slice(-1*imgError.length) == imgError) 
	{
		olObject.src = photoCache[curPhoto].src;
		return;
	}
	if (photoShown[curPhoto] == 1 && olObject.src == photoList[curPhoto] && photoList[curPhoto] != imgDummy) return;
	
	photoShown[curPhoto] = 1;
	olObject.src = photoList[curPhoto];
    olObject.photoIndex = curPhoto;
}

function showBack(curPhoto)
{
    showCustomDiv(BACK);	
    var olObject = document.getElementById(bookCoverSrc);
    
	if (!photoCache[curPhoto] && curPhoto < albumCount) 
	{
		olObject.src = imgLoading;
		return;
	}
	if (photoCache[curPhoto] && photoCache[curPhoto].src.slice(-1*imgError.length) == imgError) 
	{
		olObject.src = photoCache[curPhoto].src;
		return;
	}
	if (photoShown[curPhoto] == 1 && olObject.src == photoList[curPhoto] && photoList[curPhoto] != imgDummy) return;
	
	photoShown[curPhoto] = 1;
	olObject.src = photoList[curPhoto];
    olObject.photoIndex = curPhoto;
}

function ViewCustomAgain()
{
    photoShown[intCurLeft] = 0;
    photoShown[intCurRight] = 0;
    intCurLeft = 0;
    intCurRight = 1;
    curZoomState = 0;
    isCoverPage = 1;
    getBookPage(0, 0, bookDivName, showCover);
    getZoomedPhoto(0, 0);
    getBookPage(1, preLoadPhotos, bookSpreadDiv);
    showCover(intCurLeft);
}

function showEmptyPage(curPhoto, imgName, pageNumberName)
{
    var olObject = document.getElementById(imgName);
    showCustomPageNumber(pageNumberName, curPhoto);
    olObject.src = imgDummy;
    return;
}

function showSingleCustomPhoto(curPhoto, imgName, pageNumberName)
{
	if (curPhoto != intCurLeft && intCurRight != curPhoto) return;

    // Get photo img element from html 
    var olObject = document.getElementById(imgName);

	if (!photoCache[curPhoto] && curPhoto < albumCount) 
	{
		showCustomPageNumber(pageNumberName, curPhoto);
		olObject.src = imgLoading;
		return;
	}

	if (photoCache[curPhoto] && photoCache[curPhoto].src.slice(-1*imgError.length) == imgError) 
	{
	    showCustomPageNumber(pageNumberName, curPhoto);
		olObject.src = photoCache[curPhoto].src;
		return;
	}

	if (photoShown[curPhoto] == 1 && olObject.src == photoList[curPhoto] && photoList[curPhoto] != imgDummy) return;

	photoShown[curPhoto] = 1;
    olObject.photoIndex = curPhoto;

	showCustomPageNumber(pageNumberName, curPhoto);

    var photoToShow;
    if (curPhoto >= albumCount) 
    {
		photoToShow = imgBlank;
	} 
	else 
	{
		if (photoCache[curPhoto]) 
		{
			photoToShow = photoList[curPhoto];
		}  
		else 
		{
			photoToShow = imgLoading;
		}
	}

	if (photoToShow != imgLoading) ApplyTransition(imgName, photoToShow, getTransition());
}

function showCustomPageNumber(pageNumberName, curPhoto)
{    
    var nPageNumber = curPhoto;
	document.getElementById(pageNumberName).innerHTML= "<FONT face=\"Arial\" size=3 color=\"gray\"><STRONG>" + ((nPageNumber == 0 || curPhoto >= albumCount) ? " " : nPageNumber) + "</strong></font>";
	showCustomDiv(INSIDE);
}

function showCustomDiv(type)
{
    if (type == curPageType) return;
    curPageType = type;
    
    if (type == INSIDE)
    {
        if (document.getElementById(bookSpreadDiv)) document.getElementById(bookSpreadDiv).style.visibility="visible";
        if (document.getElementById(bookDivName)) document.getElementById(bookDivName).style.visibility="hidden";
        if (document.getElementById("customSpine")) document.getElementById("customSpine").style.visibility="hidden";
        if (document.getElementById("customSpineBack")) document.getElementById("customSpineBack").style.visibility="hidden";
        if (document.getElementById(shadowPageDiv)) document.getElementById(shadowPageDiv).style.visibility="visible";
        if (document.getElementById("pageNumbers")) document.getElementById("pageNumbers").style.visibility="visible";
        if (document.getElementById("ButtonView")) 
        {
            document.getElementById("ButtonView").style.visibility="visible";
            document.getElementById("ButtonView").style.top = "463px";
        }
        if (document.getElementById("zoomSpineDiv"))
        {
            document.getElementById("zoomSpineDiv").style.visibility="hidden";
        }
        if (document.getElementById("clickToZoomDiv"))
        {
            document.getElementById("clickToZoomDiv").style.top = "463px";
            document.getElementById("clickToZoomDiv").style.left = clickZoomSpreadLeft;
        }
        if (document.getElementById("AgainButtonView")) document.getElementById("AgainButtonView").style.visibility="hidden";
        if (document.getElementById("BuyButton")) document.getElementById("BuyButton").style.visibility="visible";
        if (document.getElementById("footerMaster"))
            document.getElementById("footerMaster").style.top = "505px";
    }
    else if (type == BACK)
    {
        if (document.getElementById(bookSpreadDiv)) document.getElementById(bookSpreadDiv).style.visibility="hidden";
        if (document.getElementById(bookDivName)) document.getElementById(bookDivName).style.visibility="visible";
        if (document.getElementById("customSpine")) document.getElementById("customSpine").style.visibility="hidden";
        if (document.getElementById("customSpineBack")) document.getElementById("customSpineBack").style.visibility="visible";
        if (document.getElementById("zoomedSpine"))
        {
            document.getElementById("zoomedSpine").src="images/spine_shadow_overlay_back_cover.png";
            document.getElementById("zoomSpineDiv").style.left = spineZoomBackLeft;
        }
        if (document.getElementById(shadowPageDiv)) document.getElementById(shadowPageDiv).style.visibility="hidden";
        if (document.getElementById("pageNumbers")) document.getElementById("pageNumbers").style.visibility="hidden";
        if (document.getElementById("ButtonView")) document.getElementById("ButtonView").style.visibility="hidden";
        if (document.getElementById("clickToZoomDiv")) document.getElementById("clickToZoomDiv").style.left = clickZoomLeft;
        if (document.getElementById("clickToZoomDiv")) document.getElementById("clickToZoomDiv").style.top = "434px";
        if (document.getElementById("AgainButtonView")) document.getElementById("AgainButtonView").style.visibility="visible";
        if (document.getElementById("BuyButton")) document.getElementById("BuyButton").style.visibility="hidden";
        if (document.getElementById("footerMaster"))
            document.getElementById("footerMaster").style.top = "490px";
    }
    else
    {
        if (document.getElementById(bookSpreadDiv)) document.getElementById(bookSpreadDiv).style.visibility="hidden";
        if (document.getElementById(bookDivName)) document.getElementById(bookDivName).style.visibility="visible";
        if (document.getElementById("customSpine")) document.getElementById("customSpine").style.visibility="visible";
        if (document.getElementById("customSpineBack")) document.getElementById("customSpineBack").style.visibility="hidden";
        if (document.getElementById("zoomedSpine"))
        {
            document.getElementById("zoomedSpine").src="images/spine_shadow_overlay.png";
            document.getElementById("zoomSpineDiv").style.left = spineZoomFrontLeft;
        }
        if (document.getElementById(shadowPageDiv)) document.getElementById(shadowPageDiv).style.visibility="hidden";
        if (document.getElementById("pageNumbers")) document.getElementById("pageNumbers").style.visibility="hidden";
        if (document.getElementById("ButtonView")) 
        {
            document.getElementById("ButtonView").style.visibility = "visible";
            document.getElementById("ButtonView").style.top = "430px";
        }
        if (document.getElementById("clickToZoomDiv")) document.getElementById("clickToZoomDiv").style.top = "434px";
        if (document.getElementById("clickToZoomDiv")) document.getElementById("clickToZoomDiv").style.left = clickZoomLeft;
        if (document.getElementById("AgainButtonView")) document.getElementById("AgainButtonView").style.visibility="hidden";
        if (document.getElementById("BuyButton")) document.getElementById("BuyButton").style.visibility="visible";
        if (document.getElementById("footerMaster"))
            document.getElementById("footerMaster").style.top = "490px";
    }
}

