﻿function openWindow(URL, windowName, windowFeatures)
{
    newWindow = window.open(URL, windowName, windowFeatures)
}

function ShowHide(eID)
{
    var el = document.getElementById(eID);
    el.style.display = ('none' == el.style.display)? '':'none';
}

function HideDiv(eID)
{
    var el = document.getElementById(eID);
    el.style.display = 'none';
}

function ShowDiv(eID)
{
    var el = document.getElementById(eID);
    el.style.display = '';
}

function HideParentDiv(eID)
{
    var el = parent.document.getElementById(eID);
    el.style.display = 'none';
}

function ShowParentDiv(eID)
{
    var el = parent.document.getElementById(eID);
    el.style.display = '';
}

function LoadIframe(eID, strPath)
{
    var el = document.getElementById(eID);
    
    el.src = strPath;
}

function LoadParentIframe(eID, strPath)
{
    var el = parent.document.getElementById(eID);
    
    el.src = strPath;
}

function SetParentIFrameHeight(eID)
{
    var el = parent.document.getElementById(eID);
       
    //alert('document.body.offsetHeight');
    //alert(document.body.offsetHeight);
    //alert(el.style.height);
    el.style.height = document.body.offsetHeight + "px";

}

function SetParentElementValue(eID, strText)
{
    //get parent element
    var el = parent.document.getElementById(eID);
    
    //set parent element value
    el.value = strText;
}

function SetElementValue(eID, strText)
{
    //get parent element
    var el = document.getElementById(eID);
    
    //set parent element value
    el.value = strText;
}

function AppendToParentElementValue(eID, strText)
{
    //get parent element
    var el = parent.document.getElementById(eID);
    
    //set parent element value
    el.value += strText;
}

function TextCounter(field, countfield, maxlimit) 
{
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    else // otherwise, update 'characters left' counter
     countfield.value = maxlimit - field.value.length;
}

function ChangeButtonText(elID_button, new_text)
{           
    //get button by id
    var el = document.getElementById(elID_button);
                
    //change button text to "Saving Changes ..."            
    el.value = new_text;
}

function SetInnerHTML(eID, strText)
{           
    //get element
    var el = document.getElementById(eID);
                
    //set innerHTML           
    el.innerHTML = strText;
}

function SetParentInnerHTML(eID, strText)
{           
    //get element
    var el = parent.document.getElementById(eID);
                
    //set innerHTML           
    el.innerHTML = strText;
}

function FadeDiv(strElementID, opacStart, opacEnd, millisec)
{
    //speed for each frame
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            
            if(i == 0)
            {
                HideDiv(strElementID);
            }
            else
            {
                ShowDiv(strElementID);
            }
            
            setTimeout("changeOpac2(" + i + ",'" + strElementID + "')",(timer * speed)); 
            timer++; 
        }        
    } else if(opacStart < opacEnd) { 
        
        //set display to ''
        var el = document.getElementById(strElementID);
        el.style.display = '';
        changeOpac2(0,strElementID);
        
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            
            if(i == 0)
            {
                HideDiv(strElementID);
            }
            else
            {
                ShowDiv(strElementID);
            }
            
            setTimeout("changeOpac2(" + i + ",'" + strElementID + "')",(timer * speed)); 
            timer++; 
        } 
    }
}

//change the opacity for different browsers 
function changeOpac2(opacity, strElementID) { 
    
    var el = document.getElementById(strElementID);
    
    el.style.opacity = (opacity / 100);
    el.style.MozOpacity = (opacity / 100);
    el.style.KhtmlOpacity = (opacity / 100);
    el.style.filter = "alpha(opacity=" + opacity + ")"; 
} 

function FadeOutAlertDiv(eID, strRedirectTo)
{              
    //show div              
    ShowDiv(eID);
    
    //fade out div
    setTimeout("FadeOutDiv('" + eID + "',100,0,2000,'" + strRedirectTo + "')",2000);
}

function FadeOutDiv(strElementID, opacStart, opacEnd, millisec, strRedirectTo) {
   
    //speed for each frame
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + strElementID + "','" + strRedirectTo + "')",(timer * speed)); 
            timer++; 
        }        
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + strElementID + "','" + strRedirectTo + "')",(timer * speed)); 
            timer++; 
        } 
    }
} 

//change the opacity for different browsers 
function changeOpac(opacity, strElementID, strRedirectTo) { 
    
    var el = document.getElementById(strElementID);
    
    el.style.opacity = (opacity / 100);
    el.style.MozOpacity = (opacity / 100);
    el.style.KhtmlOpacity = (opacity / 100);
    el.style.filter = "alpha(opacity=" + opacity + ")"; 
    
    if(opacity == 0)
    {
        HideDiv(strElementID);
        
        if (strRedirectTo != '')
            window.location = strRedirectTo;
    }
}

function ViewAlbum_MouseOver(Identifier)
{
    //set opacity to half
    changeOpac2(40,'divMain' + Identifier);
    
    //hide title
    //HideDiv('divTitle' + Identifier);
    
    //hide date
    HideDiv('divDate' + Identifier);
    
    //show top div
    ShowDiv('divTop' + Identifier);
}

function ViewAlbum_MouseOut(Identifier)
{
    //set opacity to full
    changeOpac2(100,'divMain' + Identifier);
    
    //show title
    //ShowDiv('divTitle' + Identifier);
    
    //show date
    ShowDiv('divDate' + Identifier);
    
    //hide top div
    HideDiv('divTop' + Identifier);
}

function ViewAlbumCover_MouseOver(Identifier)
{
    //set opacity to half
    changeOpac2(40,'divMain' + Identifier);
    
    //hide title
    //HideDiv('divTitle' + Identifier);
    
    //hide date
    HideDiv('divDate' + Identifier);
    
    //show top div
    ShowDiv('divTop' + Identifier);
}

function ViewAlbumCover_MouseOut(Identifier)
{
    //set opacity to full
    changeOpac2(100,'divMain' + Identifier);
    
    //show title
    //ShowDiv('divTitle' + Identifier);
    
    //show date
    ShowDiv('divDate' + Identifier);
    
    //hide top div
    HideDiv('divTop' + Identifier);
}

function ShowSelectedTab(strElementID)
{
    //set all tabs to unselected
    document.getElementById('divHome').className = 'top_nav';
    document.getElementById('divMyAlbums').className = 'top_nav';
    document.getElementById('divUploadPhotos').className = 'top_nav';
    document.getElementById('divMyAccount').className = 'top_nav';
    
    //set selected tab
    document.getElementById(strElementID).className = 'top_nav_selected';
}

function LoadImage2(strUserID,strImageID,strImageAlbumID,strPage)
{
    LoadParentIframe('ifPictureViewer', 'iframes/ifPictureViewer.aspx?UserID=' + strUserID + '&ImageID=' + strImageID + '&ImageAlbumID=' + strImageAlbumID + '&page=' + strPage);
    HideDiv('divShowifPictureViewer');
    ShowDiv('divLoadingifPictureViewer');
}

function RightLeftKeyCheck(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   switch(KeyID)
   {
      case 37: //left arrow pressed
      {       
        //get prev index
        var strPrevIndex = document.getElementById('txtPrevIndex').value;
        
        if (strPrevIndex != '')
        {
            //load prev image
            ImageSelected_MainWindow(strPrevIndex);        
        }
      }
      break;       
      case 39: //right arrow pressed
      {
        //get next index
        var strNextIndex = document.getElementById('txtNextIndex').value;

        if (strNextIndex != '')
        {
            //load next image
            ImageSelected_MainWindow(strNextIndex);
        }
      }
      break;      
    }
}

function RightLeftKeyCheck2(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   switch(KeyID)
   {
      case 37: //left arrow pressed
      {       
        //get prev index
        var strPrevIndex = parent.document.getElementById('txtPrevIndex').value;
        
        if (strPrevIndex != '')
        {
            //load prev image
            ImageSelected_IFPictureList(strPrevIndex);        
        }
      }
      break;       
      case 39: //right arrow pressed
      {
        //get next index
        var strNextIndex = parent.document.getElementById('txtNextIndex').value;

        if (strNextIndex != '')
        {
            //load next image
            ImageSelected_IFPictureList(strNextIndex);
        }
      }
      break;      
    }
}

function IsImageLoaded(strImageName, strDivToHide, strDivToShow)
{
    alert('IsImageLoaded(' + strImageName + ',' + strDivToHide + ',' + strDivToShow + ')');

    var img = document.getElementById(strImageName);
    
    alert(img.clientWidth);

    if (img.width > 0 && img.height > 0)
    {
        ShowParentDiv(strDivToShow);
        HideParentDiv(strDivToHide);
    }
    else
    {
        setTimeout("IsImageLoaded('" + strImageName + "','" + strDivToHide + "','" + strDivToShow + "')",3000);
    }
}

function SetSelectedPhotoBorder(intIndex)
{   
    //first clear any highlighted thumbs
    var els = document.getElementsByTagName("div");
    for(i=0; i<els.length; ++i)
    {
        if (els[i].id != "")
            els[i].style.border = "1px solid #333333";
    }        
    
    //next, highlight selected thumb
    var img = document.getElementById('div' + intIndex);
    img.style.border = "1px solid #00bfff";
}

function SetSelectedPhotoBorder2(intIndex)
{   
    //get picture list iframe   
    var ifPictureList = ReturnIFrameDocument('ifPictureList');
    
    //first clear any highlighted thumbs
    var els = ifPictureList.getElementsByTagName("div");

    for(i=0; i<els.length; ++i)
    {
        if (els[i].id != "")
            els[i].style.border = "1px solid #333333";
    }        
    
    //next, highlight selected thumb
    var img = ifPictureList.getElementById('div' + intIndex);
    img.style.border = "1px solid #00bfff";
}

function ReturnIFrameDocument(strIFrameID)
{    
    //get iframe document
    var ifrm = window.frames[strIFrameID].document;
    
    return ifrm;
}

function ReturnParentIFrameDocument(strIFrameID)
{    
    //get iframe document
    var ifrm = parent.window.frames[strIFrameID].document;
    
    return ifrm;
}

function ImageSelected_MainWindow(strIndex)
{
    //this function gets fired off when either the next or previous buttons are clicked
    
    //vars
    var strImageID = "";
    var strPrevIndex = "";
    var strNextIndex = "";
 
    //reset previous borders and set new selected border
    SetSelectedPhotoBorder2(strIndex);

    //show loading animation for image to be loaded
    ShowDiv('divLoadingifPictureViewer');
    HideDiv('divShowifPictureViewer');
    
    //reset for image details
    HideDiv('divShowPictureDetails');
    
    //get value of txtIndexList
    //var strFullIndexList = "";
    var strFullIndexList = ReturnIFrameDocument('ifPictureList').getElementById('txtIndexList').value;
    
    //split txtIndexList into array
    var saFullIndexArray = strFullIndexList.split("_");
    
    //loop through array and convert each section into another array
    var i = 0;
    for(i=0;i<saFullIndexArray.length;i++)
    {
        //split smaller index into array and check to 
        var saChildIndexArray = saFullIndexArray[i].split(".");
        
        //loop through array and convert each section into another array
        var j = 0;
        for(j=0;j<saChildIndexArray.length;j++)
        {
            //split smaller index into array and check to see if the passed index matches the current one
            //saChildIndexArray[0] = index
            //saChildIndexArray[1] = imageid
            
            //if index matches
            if (saChildIndexArray[0] == strIndex)
            {
                //indexes match! this is the selected index, so we can now read the selected imageid
                strImageID = saChildIndexArray[1];
                
                //now we need to check to see if there are imageid's on either side of the selected imageid
                
                //prev image
                var intPrevIndex = parseInt(i) - parseInt(1);
                if (saFullIndexArray[intPrevIndex])
                {
                    //element exists?? not sure if this will work
                    //prev image exists so set it
                    saPrevImageArray = saFullIndexArray[intPrevIndex].split('.');
                    strPrevIndex = saPrevImageArray[0];
                }
                
                //next image
                var intNextIndex = parseInt(i) + parseInt(1);
                if (saFullIndexArray[intNextIndex])
                {
                    //element exists?? not sure if this will work
                    //next image exists so set it
                    saNextImageArray = saFullIndexArray[intNextIndex].split('.');
                    strNextIndex = saNextImageArray[0];
                }
            }
        }
    }
    
    //load selected photo
    LoadIframe('ifPictureViewer', 'iframes/ifPictureViewer.aspx?ImageID=' + strImageID);
    LoadIframe('ifPictureDetails', 'iframes/ifPictureDetails.aspx?ImageID=' + strImageID);
    
    //set next and prev links on parent
    var txtPrevIndex = document.getElementById('txtPrevIndex');
    var txtNextIndex = document.getElementById('txtNextIndex');
    
    //prev
    if (strPrevIndex != "")
    {   
        //prev index
        //set prev link
        SetInnerHTML("divPrev", "<a href=javascript:ImageSelected_MainWindow('" + strPrevIndex + "');><img src='images/viewer_nav/large_arrow_left.gif' style='border:none'></a>");
    
        //prev arrow action
        txtPrevIndex.value = strPrevIndex;
    }
    else
    {
        //clear prev link
        SetInnerHTML("divPrev", "<img src='images/viewer_nav/large_arrow_left_no.gif'>");
        
        //clear prev arrow action
        txtPrevIndex.value = "";
    }
    
    //next
    if (strNextIndex != "")
    {
        //next index   
        //set next link
        SetInnerHTML("divNext", "<a href=javascript:ImageSelected_MainWindow('" + strNextIndex + "');><img src='images/viewer_nav/large_arrow_right.gif' style='border:none'></a>");
    
        //next arrow action
        txtNextIndex.value = strNextIndex;
    }
    else
    {
        //clear next link
        SetInnerHTML("divNext", "<img src='images/viewer_nav/large_arrow_right_no.gif'>");
        
        //clear next arrow action
        txtNextIndex.value = "";
    }
}

function ImageSelected_IFPictureList(strIndex)
{
    //this function gets fired off when an image is clicked on INSIDE of the ifpicturelist iframe
    
    //vars
    var strImageID = "";
    var strPrevIndex = "";
    var strNextIndex = "";
    
    //get value of txtIndexList
    //var strFullIndexList = "";
    var strFullIndexList = ReturnParentIFrameDocument('ifPictureList').getElementById('txtIndexList').value;
    
    //split txtIndexList into array
    var saFullIndexArray = strFullIndexList.split("_");
    
    //loop through array and convert each section into another array
    var i = 0;
    for(i=0;i<saFullIndexArray.length;i++)
    {
        //split smaller index into array and check to 
        var saChildIndexArray = saFullIndexArray[i].split(".");
        
        //loop through array and convert each section into another array
        var j = 0;
        for(j=0;j<saChildIndexArray.length;j++)
        {
            //split smaller index into array and check to see if the passed index matches the current one
            //saChildIndexArray[0] = index
            //saChildIndexArray[1] = imageid
            
            //if index matches
            if (saChildIndexArray[0] == strIndex)
            {
                //indexes match! this is the selected index, so we can now read the selected imageid
                strImageID = saChildIndexArray[1];
                
                //now we need to check to see if there are imageid's on either side of the selected imageid
                
                //prev image
                var intPrevIndex = parseInt(i) - parseInt(1);
                if (saFullIndexArray[intPrevIndex])
                {
                    //element exists?? not sure if this will work
                    //prev image exists so set it
                    saPrevImageArray = saFullIndexArray[intPrevIndex].split('.');
                    strPrevIndex = saPrevImageArray[0];
                }
                
                //next image
                var intNextIndex = parseInt(i) + parseInt(1);
                if (saFullIndexArray[intNextIndex])
                {
                    //element exists?? not sure if this will work
                    //prev image exists so set it
                    saNextImageArray = saFullIndexArray[intNextIndex].split('.');
                    strNextIndex = saNextImageArray[0];
                }
            }
        }
    }
    
    //reset previous borders and set new selected border
    SetSelectedPhotoBorder(strIndex);
    
    //show loading animation for image to be loaded
    ShowParentDiv('divLoadingifPictureViewer');
    HideParentDiv('divShowifPictureViewer');
    
    //reset for image details
    HideParentDiv('divShowPictureDetails');
    
    //load selected photo
    LoadParentIframe('ifPictureViewer', 'iframes/ifPictureViewer.aspx?ImageID=' + strImageID);
    LoadParentIframe('ifPictureDetails', 'iframes/ifPictureDetails.aspx?ImageID=' + strImageID);
    
    //set next and prev links on parent
    var txtPrevIndex = parent.document.getElementById('txtPrevIndex');
    var txtNextIndex = parent.document.getElementById('txtNextIndex');
    
    //prev
    if (strPrevIndex != "")
    {
        //set prev link
        SetParentInnerHTML("divPrev", "<a href=javascript:ImageSelected_MainWindow('" + strPrevIndex + "');><img src='images/viewer_nav/large_arrow_left.gif' style='border:none'></a>");
    
        //prev arrow action
        txtPrevIndex.value = strPrevIndex;
    }
    else
    {
        //clear prev link
        SetParentInnerHTML("divPrev", "<img src='images/viewer_nav/large_arrow_left_no.gif'>");
        
        //clear prev arrow action
        txtPrevIndex.value = "";
    }
    
    //next
    if (strNextIndex != "")
    {
        //set next link
        SetParentInnerHTML("divNext", "<a href=javascript:ImageSelected_MainWindow('" + strNextIndex + "');><img src='images/viewer_nav/large_arrow_right.gif' style='border:none'></a>");
    
        //next arrow action
        txtNextIndex.value = strNextIndex;
    }
    else
    {
        //clear next link
        SetParentInnerHTML("divNext", "<img src='images/viewer_nav/large_arrow_right_no.gif'>");
        
        //clear next arrow action
        txtNextIndex.value = "";
    }
}



