var iIndex = 1;
var iBIndex = 1;
var iINTERVAL = 5000;
var iBLOCK=4;
var iMAX=0;
var iOffset=0;
var iROff=0;
var iOff=0;
var sPRE='ctl00_mainContent_Gallery1_img';
var sMAIN = 'ctl00_mainContent_Gallery1_imgMain';
var sXML='gallery';
var sXSL='gallery';
var bPlay=false;
var iImgSlot=0;
var iLastImgSlot=1;
var sImgDir='';
var sHOST=location.host;
var bSet=false;
var oTimer;
var jsonResp=null;
var bL=false;
var iRev = 0;
var iDEFORD = 0;
var iFBDesc = -1;
var iFBTitle = -1;
var iFBTI = -1;
var oMetaTags;
var sGRT, sTWT;

var sTestHref = location.href;
var sTemp = new Array();
sTemp = sTestHref.split('/');
var sDIR = sTemp[3];

function fn0()
{
    sImgDir = document.getElementById('ctl00_mainContent_Gallery1_idGal').value;
    iMAX = parseInt(document.getElementById('ctl00_mainContent_Gallery1_idMax').value, 0);
    iIndex = parseInt(document.getElementById('ctl00_mainContent_Gallery1_idImg').value, 0);
    iRev = parseInt(document.getElementById('ctl00_mainContent_Gallery1_idRev').value, 0);
    sGRT = document.getElementById('ctl00_mainContent_Gallery1_idShare1').value
    sTWT = document.getElementById('ctl00_mainContent_Gallery1_idShare2').value

    if (iRev == iDEFORD)
    {
        iBIndex = iIndex + ((iMAX - iIndex) % iBLOCK);
        iImgSlot = iBIndex - iIndex + 1;
    }
    else
    {
        iBIndex = iIndex - ((iIndex - 1) % iBLOCK);
        iImgSlot = iIndex - iBIndex + 1;
    }

    iLastImgSlot = iImgSlot;
    
	var t,s;

    if (iMAX < iBLOCK)
        iBLOCK=iMAX;
        
    iROff = iMAX%iBLOCK;

    // If loading page at a split Thumbnail transition then set Thumbnail offset

    var iDiff;
    if (iRev == iDEFORD)
        iDiff = iBIndex;
    else
        iDiff = iMAX - iBIndex + 1;

    if (iDiff < iBLOCK)
        iOffset = iBLOCK - iDiff;
        
    var sHost = location.host;   
    fnAJAX("http://" + sHost + "/" + sDIR + "/json/" + sImgDir + "/gallery.txt", iIndex); 
    bL = true;

    fnXIn();
    fnLoadCJL();
}

function fn1()
{
  
   setTimeout("fn2()", iINTERVAL);
}

function fnWid()
{
   var sAction;
   var sStyle;
   
   if (bL) 
   {
       if (!bPlay)
       {
	        bPlay=true;
            sAction="stop";

            if (iRev == iDEFORD) {
                var iTest = iBIndex - iIndex+1;
                
                if (iLastImgSlot <= iTest)
                    iOff = (iBLOCK - (iMAX - iBIndex)%iBLOCK)%iBLOCK;
                else
                    iOff = iTest;
            }
            else {
                var iTest = (iMAX - iBIndex) + 1;
                if (iLastImgSlot <= iTest)
                    iOff = (iBLOCK - (iBIndex - 1) % iBLOCK) % iBLOCK;
                else
                    iOff = iTest;
            }
            
     	    fn2();
       }
       else
       {
	        bPlay=false;
	        sAction = "play";
	        clearTimeout(oTimer);
       }
       document.getElementById("dvPlay").className=sAction;
       document.getElementById("dvPlay1").className=sAction;
   }
   
}

function fnInc() {

    iBIndex = (iBIndex + iBLOCK) % iMAX;
    if (iBIndex == 0)
        iBIndex = iMAX; 
}

function fnDec() {

    iBIndex = (iBIndex - iBLOCK) % iMAX;
    if (iBIndex <= 0)
        iBIndex = iBIndex + iMAX;
}

function fnSel(iSlot){

if (bL)
{
    clearTimeout(oTimer);
    //clearInterval(isf.clock);
    if (iSlot <= iMAX)
    {
           
            bPlay=false; //Stop the slide show
            var sAction="play";
            document.getElementById("dvPlay").className=sAction;
            document.getElementById("dvPlay1").className=sAction;

            fnOff();
            if (iRev == iDEFORD) {
                var iValue = iBIndex - (iSlot - 1);
                var iInd = iMAX - iValue;
                if (iInd < iMAX)
                    iIndex = iValue;
                else if (iInd == iMAX)
                    iIndex = iMAX;
                else
                    iIndex = iMAX + iValue;
            }
            else 
            {
                var iValue = iBIndex + iSlot - 1;
                var iInd = iMAX - iValue;
                if (iInd > 0)
                    iIndex = iValue;
                else if (iInd == 0)
                    iIndex = iMAX;
                else
                    iIndex = iInd * -1;
            }

            fnSwap(iSlot);
            fnSet(iSlot, true); 
            fnRenInfo(iIndex);      
           
    }
}

}

function fnRenInfo(iIndex)
{

     var sId = sImgDir + "," + String(iIndex);
     document.getElementById('dvText2').innerHTML=jsonResp.gallery[iIndex-1].html;
     fnDo();
     //  document.getElementById('dvText2').innerHTML = sHTML;
    
        
}

function fnRenLinks()
{

var sHREF;
var sTitle = document.getElementById('spTitle').innerHTML;
var sTemp = document.getElementById('ctl00_mainContent_Gallery1_idHead').innerHTML;
var iInd = sTemp.indexOf("::")
var sCat = sTemp.substring(0, iInd-1);

var sSubject= sHOST + ": " + sTitle;
var sBody = sGRT;
var sT1 = sTitle.toUpperCase();
sBody += escape('\n\n')
sBody += sT1;
sBody += escape('\n')
sBody += escape('"') + document.getElementById('pCap').innerHTML + escape('"');
sBody += escape('\n');
var sU = "http://" + sHOST + "/" + sDIR + "/gallery.aspx?id=" + String(sImgDir) + escape('&') + "img=" + String(iIndex);
sBody += sU;

sHREF = '<div id="li1"><a title="Email a Friend" href="';
sHREF += 'mailto:?Subject=' + sSubject + '&body=' + sBody;
sHREF += '"></a></div>';
sHREF += '<div id="li2"><a title="Tweet this Image" href="javascript:fnOpenNew(' + escape('"') ;
sHREF += 'http://twitter.com/home?status=' + sTWT + ' - ' + escape(sTitle) + ' - ' + escape(sU) + escape('"') + ')';
sHREF += '"></a></div>';
sHREF += '<div id="li3"><a title="Share on Facebook" href="javascript:fnOpenNew(' + escape('"');
sHREF += 'http://www.facebook.com/sharer.php?t=' + escape(sTitle) + '&u=' + escape(sU) + escape('"') + ')';
sHREF += '"></a></div>';
sHREF += '<div id="li4"><a title=';
sHREF += '"Subscribe to the ' + sCat + ' RSS Feed"';
sHREF += ' href="rss/' + sCat.toLowerCase() + '/feed.xml"></a></div>';

var sBlog = jsonResp.gallery[iIndex-1].blog;
if (sBlog != "")
{
    sHREF += '<div id="li5"><a title="Read the Blog" href="http://';
    sHREF += sHOST + "/" + sDIR + "/blog.aspx?cat=" + sBlog;
    sHREF += '"></a></div>';
}

document.getElementById('dvLinks').innerHTML = sHREF;

var sTags = jsonResp.gallery[iIndex-1].tags;
var sT = "";

if (sTags.length > 0) 
   sT="Tags: ";
    
while (sTags.length > 0) 
{
    var iSep = sTags.lastIndexOf(",");
    sTag = sTags;
    if (iSep > 0)
       sTag = sTags.substr(iSep + 2, sTags.length - iSep - 2);
    
    sT += '<a href="gallery.aspx?ID=' + sTag + '">' + sTag + '</a>';
               
    if (sTag.length < sTags.length)
    {
        sTags = sTags.substr(0, iSep);
        sT += ", ";
    }
    else
        sTags = "";
}

document.getElementById('spTags').innerHTML = sT;

var sURL = "http://" + sHOST + "/" + sDIR + "/gallery.aspx?id=" + sImgDir;
var sIconPath = "http://" + sHOST + "/" + sDIR + "/img/gallery.ico";
fnAddToJumpList(sTemp, sURL, sIconPath);

}

function fnOpenNew(sHRef) {
    window.open(sHRef, "newWin", "width=800,height=600,status=1,resizeable=1,scrollbars=1");
}


function fnSwap(iSlot)
{
	    var sId = sPRE + String(iSlot);
        var sSrc = document.getElementById(sId).src;
        document.getElementById(sMAIN).src = sSrc;
}

function fnOff()
{

 	iImgSlot=iLastImgSlot;
    fnSet(iImgSlot, false);

}

function fnNav(iInd){

if (bL) {
        bPlay = false;
        var sAction = "play";
        document.getElementById("dvPlay").className = sAction;
        document.getElementById("dvPlay1").className = sAction;
        fnOff();

        if (iRev == iDEFORD) {
            if (iInd == 0)
                fnInc();
            else
                fnDec();
        }
        else {
            if (iInd == 0)
                fnDec();
            else
                fnInc();
        }
        
        iIndex = iBIndex;
        fnRender();
        fnSwap(1);
        fnRenInfo(iIndex);
        fnSet(1, true);
}

}

function fn2()
{

var sHTML;
var sSPEED='1';
var sIndex;

if (bPlay) {

    if (iRev == iDEFORD) {
        iIndex = iIndex - 1;
        if (iIndex == 0) {
            iIndex = iMAX;
        }
    }
    else
        iIndex = (iIndex % iMAX) + 1;
    
sIndex = String(iIndex);

fn3(); // Render the Thumb-Nail Gallery

// sHTML = "img/" + sImgDir + "/img" + sIndex + ".jpg";

sHTML = fnImgPath(iIndex);

swapfade(document.getElementById(sMAIN), sHTML, sSPEED, sIndex)  // Fade out and fade in the new image

oTimer = setTimeout("fn2()", iINTERVAL);

// fnRenInfo(iIndex);

}

}

function fnImgPath(iImgInd)
{

var sImgPath = jsonResp.gallery[iImgInd-1].path;

var iDelim = sImgPath.indexOf("/")
var sGalID = sImgPath.substring(0, iDelim);
var sImgID = sImgPath.substring(iDelim+1, sImgPath.length);

var sRetPath = "img/" + sGalID + "/img" + sImgID + ".jpg";

return (sRetPath);

}

function fn3(){

// Render the Thumb Nail Gallery
if (fnTransition()) {

    if (iRev == iDEFORD)
        iImgSlot = (iMAX - iIndex + iOff) % iBLOCK;
    else
        iImgSlot = (iIndex + iOff - 1) % iBLOCK;

	if (iImgSlot == 0)
	{
	    //Thumbnail Turnover

	    if (iRev == iDEFORD) {
	        if (iOffset == 0)  // Full complement rendered
	            iBIndex -= iBLOCK;
	        else
	            iBIndex = iIndex;
	    }
	    else {
	        if (iOffset == 0)  // Full complement rendered
	            iBIndex += iBLOCK;
	        else
	            iBIndex = iIndex;
	    }      /// Starting part way thru
     
        bSet=false;
		fnRender();
		fnSet(iBLOCK, false);
        fnSet(1, true);
	}
	else
	{
	    // Thumbnail mid-stream
		fnSet(iImgSlot, false);

		iImgSlot++;
		iImgSlot%=iBLOCK;
		if (iImgSlot==0)
    		iImgSlot=iBLOCK;

		fnSet(iImgSlot, true);
	}
}
else
{
   //This means the images are starting to replay from the 1st one. iIndex = 1

   iOff=(iOff+iROff)%iBLOCK;
   bSet = true;

   if (iOff == 0)
   {
    // Thumbnail Turnover

    bSet = false;
    if (iRev == iDEFORD)
        iBIndex = iMAX;
    else
        iBIndex = 1;
    fnRender();
    fnSet(iBLOCK, false);
    fnSet(1, true);
   }
   else
   {
    // Thumbnail Mid-stream
    
        // Resetting the block start index by offset amount
        iImgSlot = iOff;
        fnSet(iImgSlot, false);
        fnSet(iImgSlot+1, true);
   }	
}

}

function fnTransition() {

var bRes = false;

if (iRev != iDEFORD) {
    if (iIndex > 1)
        bRes = true;
}
else {
    if (iIndex < iMAX)
        bRes = true;
}

return (bRes);
}

function fnSet(iInd, bSet)
{
// Set the Thumb-Nail Selection
var sTag;

sTag = sPRE+String(iInd);
var oImg = document.getElementById(sTag);

if (bSet)
{
 oImg.className="spImg1Hi";
 iLastImgSlot = iInd;
}
else
 oImg.className="spImg1";

}

function fnRender()
{
// Render the Thumb-Nail Gallery
	var iInd;
    var iInd2;
    var sTag='';
    var iLim;
	var sImg='';

	iLim =iBLOCK;

	if (iRev == iDEFORD) {
	    if (iIndex < iBLOCK)
	        iLim = iIndex;
	}
	else {
	    if ((iMAX - iIndex) < iBLOCK)
	        iLim = (iMAX - iIndex) + 1;
	}

    for (iInd=0;iInd<iLim;iInd++) {

        if (iRev == iDEFORD)
            iInd2 = iBIndex - iInd;
        else
            iInd2 = iBIndex + iInd;
        sImg = fnImgPath(iInd2);
        sTag=sPRE + String(iInd+1);
        document.getElementById(sTag).src = sImg;
    }
    if (iRev == iDEFORD)
        iInd2 = iMAX+1;
    else
        iInd2=0;
 	for (;iInd<iBLOCK;iInd++) {

 	    if (iRev == iDEFORD)
 	        iInd2--;
 	    else
 	        iInd2++; 
	    sImg = fnImgPath(iInd2);
        sTag=sPRE + String(iInd+1);
        document.getElementById(sTag).src = sImg;
    }

    if (iRev == iDEFORD)
        iOffset = iMAX - iInd2+1;
    else
        iOffset = iInd2;
}

function createXMLHttpRequest() {
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   alert("XMLHttpRequest not supported");
   return null;
}
 
function fnAJAX(sFile, iIndex)
{

  var oXML="";
  var xhReq = createXMLHttpRequest();
  if (window.ActiveXObject)
    {   
         xhReq.open("GET", sFile, false);
         xhReq.send(null);
         var oXML = xhReq.responseText;  
         jsonResp = eval("(" + oXML + ")");
         fnRenInfo(iIndex);
    }
    else 
    {
    // Need to handle the AJAX call asynchronously for Mozilla
    
         xhReq.open("GET", sFile, true); // Server stuck in a loop.
         var requestTimer = setTimeout(function() {
         xhReq.abort();
       // Handle timeout situation, e.g. Retry or inform user.
         }, 10000); 
         xhReq.onreadystatechange = function() {
         if (xhReq.readyState != 4)  { return; }
        
        if (xhReq.status != 200)  {
       // Handle error, e.g. Display error message on page
          return;
        }
          oXML = xhReq.responseText;
          var oXML = xhReq.responseText;  
          jsonResp = eval("(" + oXML + ")");
          fnRenInfo(iIndex);
          clearTimeout(requestTimer); 
         }
        xhReq.send(null);
        
     }  
}

function fnDo() {

        var sF = document.getElementById('spF').innerHTML;
        
        if (sF == "L")
            document.getElementById('dvMain').className = "ls";
        else
             document.getElementById('dvMain').className = "pt";
             
        if (window.ActiveXObject)
        {
            document.getElementById(sMAIN).alt = document.getElementById('spTitle').innerHTML;   
        }
        document.getElementById(sMAIN).title = document.getElementById('spTitle').innerHTML;
        
        fnRenLinks();
}

