// Javascript class to hold a slideshow photo object
function slidePhoto( sPht, sCap )
{
    this.Photo = sPht;
    this.Caption = sCap;
    this.Image = new Image();
    this.Image.src = sPht;

	// IMM 06.19.08: Store filename only
	aPhtEle = sPht.split("/");
	this.FileName = aPhtEle[aPhtEle.length-1];
}

// Location of slideshow control images
g_sConPth = "/imgs/dhp032008/dscon/";

g_iSldPos = 0;

g_iPlyHdl = 0;      // Handle to play-pause interval object
g_iFrmCnt = 0;      // Animation frame counter
g_iAnmHdl = 0;      // Handle to animation interval object
g_iCurSld = 0;      // Current slide being viewed

g_iAnmStp = 0;      // Number of pixels to move during animation step
g_iAnmTot = 10;     // Total number of animation steps during movement
g_iAnmSpd = 10;     // Number of animation steps
g_iSldInt = 3000;	// Number of seconds between slide transitions

g_iPhtWdt = 0;      // Width of individual photo
g_Sliding = false;	// When true, slide is transitioning

function loadSlides()
{
    // Store width of individual photo
    g_iPhtWdt = g_aShots[0].Image.width;

	// Set caption width to be 6 less than photo width
	i_CapWdt = g_iPhtWdt - 6;

    // Assign number of pixels to move based on width of first image stored
    g_iAnmStp = g_aShots[0].Image.width / g_iAnmSpd;

    // Now load up the slide container
    gSldCnt = document.getElementById("ShwLst");
    
    iLftPos = 0;
    for (iSld = 0; iSld < g_aShots.length; iSld++)
    {
        // Style for slide holder
        var sSldHld = "float:left; width:" + g_iPhtWdt + "px;";

        // Style for image holder
        var sImgHld = "width:" + g_iPhtWdt + "px;";

        // Style for caption holder		
		// IMM 05.08.09: Force caption width (Firefox bug fix)
        var sCapHld = "font:normal 12px/14px Arial; color:#333333; width:" + i_CapWdt + "px; text-align:left; padding-left:2px; padding-right:2px;"

        // Store image source
        var sImgSrc = g_aShots[iSld].Image.src;

        var oSldHld = undefined;    // Declare slide holder
        var oImgHld = undefined;    // Declare image holder
        var oCapHld = undefined;    // Declare caption holder

        var oImgObj = undefined;
        var oCapTxt = undefined;

        if (BrowserDetect.browser == "Explorer")
        {
            oSldHld = document.createElement("<div id=\"SldHld\" style=\"" + sSldHld + "\">");
            oImgHld = document.createElement("<div id=\"ImgHld\" style=\"" + sImgHld + "\">");
            oCapHld = document.createElement("<div id=\"CapHld\" style=\"" + sCapHld + "\">");

            oImgObj = document.createElement("<img id=\"SldPht\" src=\"" + sImgSrc + "\" style=\"" + sImgHld + "\">");
        }
        else
        {
            oImgObj = document.createElement("img");
            oImgObj.setAttribute("id", "SldPht");
            oImgObj.setAttribute("src", sImgSrc);
            oImgObj.setAttribute("style", sImgHld);

            oSldHld = document.createElement("div");
            oSldHld.setAttribute("id", "SldHld");
            oSldHld.setAttribute("style", sSldHld );

            oImgHld = document.createElement("div");
            oImgHld.setAttribute("id", "ImgHld");
            oImgHld.setAttribute("style", sImgHld );

            oCapHld = document.createElement("div");
            oCapHld.setAttribute("id", "CapHld");
            oCapHld.setAttribute("style",sCapHld);
        }

        // Create text node for caption
        oCapTxt = document.createTextNode( g_aShots[iSld].Caption + " - (" + (iSld + 1) + " of " + g_aShots.length + ")" );

        // Bind the text node to caption container
        oCapHld.appendChild( oCapTxt );

        // Bind image to slide container
        oImgHld.appendChild( oImgObj );

        // Bind caption to slide container
        oSldHld.appendChild( oImgHld );
        oSldHld.appendChild( oCapHld );

        // Bind slide container to slideshow container
        gSldCnt.appendChild( oSldHld );

        // Advance image placement
        iLftPos += g_aShots[iSld].Image.width;
    }

    oShwCnt = document.getElementById("ShwCnt");
    oShwLst = document.getElementById("ShwLst");

    if (BrowserDetect.browser == "Explorer")
    {
        oShwCnt.style.width = g_iPhtWdt + "px";
        oShwLst.style.width = (g_iPhtWdt * g_aShots.length) + "px";
    }
    else
    {
        oShwCnt.setAttribute("style", "width: " + g_iPhtWdt + "px; overflow:hidden;" );
        oShwLst.setAttribute("style", "width: " + (g_iPhtWdt * g_aShots.length) + "px; position:relative; left:0px; " );
    }
}

function advanceSlide(iDir)
{
	if (!g_Sliding)
	{
		g_Sliding = true;

		if (iDir == -1)
			g_iCurSld++;
		else
			g_iCurSld--;

		// If we are at the end of the show, and 
		// user wants next photo, reverse to start
		if (g_iCurSld >= g_iSldMax)
		{
			g_iAnmStp = (g_iPhtWdt * (g_iSldMax - 1)) / 10;
			iDir = 1;
			g_iCurSld = 0;
		}
		else if  (g_iCurSld < 0)
		{
			g_iAnmStp = (g_iPhtWdt * (g_iSldMax - 1)) / 10;
			iDir = -1;
			g_iCurSld = g_iSldMax - 1;
		}

		g_iAnmHdl = setInterval( "moveSlide(" + iDir + ")", g_iAnmSpd );
	}
}

function moveSlide(iDir)
{
    gSldCnt = document.getElementById("ShwLst");

    var sCurPos = gSldCnt.style.left;
    var sStrPos = stripNonNum(sCurPos);
    var iCurPos = Number(sStrPos)
    var iNewPos = iCurPos + (g_iAnmStp * iDir);

    if (BrowserDetect.browser == "Explorer")
        gSldCnt.style.left = iNewPos;
    else
        gSldCnt.setAttribute("style","width: " + (g_iPhtWdt * g_aShots.length) + "px; position:relative; left:" + iNewPos + "px; " );

    g_iFrmCnt++;

    if (g_iFrmCnt >= g_iAnmTot)
    {
        clearInterval( g_iAnmHdl );
		g_Sliding = false;
        g_iFrmCnt = 0;
        g_iAnmStp = 28;

		// IMM 06.19.08: Push HitBox metrics for current slide
		var new_pn = "DEMOshots | " + g_aShots[g_iCurSld].FileName;
		_hbSet('n',new_pn);
		_hbSend();

		// 06-11-08: Resend QuantCast
		_qpixelsent = "";
		quantserve();
    }
}

function stripNonNum( sStr )
{
    sNew = "";
    for (var iPos = 0; iPos < sStr.length; iPos++)
    {
        if ("-0123456789".indexOf(sStr.substr(iPos,1)) > -1)
            sNew += sStr.substr(iPos,1);
    }
    return sNew;
}

function playPauseSlide()
{
    if (g_iPlyHdl != 0)
    {
        document.getElementById("PlyPseImg").src = g_sConPth + "plyphtclr.gif";
        clearInterval(g_iPlyHdl);
        g_iPlyHdl = 0;
    }
    else
    {
        advanceSlide(-1);
        document.getElementById("PlyPseImg").src = g_sConPth + "psephtclr.gif";
        g_iPlyHdl = setInterval( "advanceSlide(-1)", g_iSldInt );
    }
}

// Setup slideshow
$(document).ready(function(){loadSlides();})
