var wsCall = null;
var sortType = new String();
var albumsXML;
var popupClass = new String();
var closeGraphic = new String();
function GetUsersAlbums() {
    if(tabColor != null) {
        popupClass = "popup_win_" + tabColor;
        closeGraphic = "popup_close_" + tabColor + ".gif";
    } else {
        popupClass = "popup_win_red";
        closeGraphic = "popup_close_red.gif";
    }
    if (!shareID)
    {
        var shareID = 0;
    }
    wsCall = new wsObj();
    wsCall.functionName = "GetUsersAlbumsWithThumbnails";
    wsCall.callbackFunction = GetAlbumResponse;
    wsCall.addParam("UserID", shopperID);
    wsCall.addParam("shareID", shareID);
    wsCall.addParam("merchantID", merchantID);
    wsCall.sendRequest();
}
function GetAlbumResponse() {
    if(wsCall.goodResponse()) {
        var dom = wsCall.http.responseXML;
        var body = dom.getElementsByTagName("Body");
        if (body != null) {
            var usersAlbums = dom.getElementsByTagName("UsersAlbums");
            if(usersAlbums) {
                var parAlbumCtr = 0;
                var albums = dom.getElementsByTagName("album");
                albumsXML = albums;
                
                lpAlbums = new Array();
                for (i=0; i<albums.length; ++i) {    
                    if(albums[i].getAttribute("parent") == "0") {
                        parAlbumCtr++;
                    }
				    lpAlbums[i] = new AlbumData();
				    lpAlbums[i].name = albums[i].getAttribute("name");
				    lpAlbums[i].ownerID = albums[i].getAttribute("ownerID");
				    lpAlbums[i].date = albums[i].getAttribute("date");
				    try {
				        zeDate = formatDate(lpAlbums[i].date);
				        zeDate = zeDate.split('/');
				        zeSDate = zeDate[2] + " " + (zeDate[0].length == 1 ? "0" + zeDate[0] : zeDate[0]) + " " + (zeDate[1].length == 1 ? "0" + zeDate[1] : zeDate[1]);
				    } catch (e){
				        zeSDate = "";
				    }
				    lpAlbums[i].sortDate = zeSDate;
				    lpAlbums[i].numPics = albums[i].getAttribute("numImages");
				    lpAlbums[i].id = albums[i].getAttribute("id");
				    lpAlbums[i].plainID = albums[i].getAttribute("plainID");
				    lpAlbums[i].thumbnailPath = albums[i].getAttribute("path");
				    //Fix this retarded ass safari bug if it shows up
                    lpAlbums[i].thumbnailPath = lpAlbums[i].thumbnailPath.replace("&#38;maxSize", "&maxSize");
                    //End fix safari bug
				    lpAlbums[i].thumbnailOrientation = albums[i].getAttribute("orientation");
				    lpAlbums[i].parentAlbum = albums[i].getAttribute("parent");
				    lpAlbums[i].parentDec = albums[i].getAttribute("parentDec");
				    lpAlbums[i].hasChildren = albums[i].getAttribute("hasChildren");
                }
				lpAlbumsFull = lpAlbums;
				
				if(parAlbumCtr <= showNumAlbums) {
				    document.getElementById("showall").style.display = 'none';
				}
				
				// Get the sort order for this user
				var SortTypeNode = dom.getElementsByTagName("SortType")[0];
				if(SortTypeNode) {
				    switch(SortTypeNode.getAttribute("type")) {
				        case "title":
				            document.getElementById("sortDDL").selectedIndex = 0;
				            break;
				        case "titleRev":
				            document.getElementById("sortDDL").selectedIndex = 1;
				            break;
				        case "dateNewOld":
				            document.getElementById("sortDDL").selectedIndex = 2;
				            break;
				        case "dateOldNew":
				            document.getElementById("sortDDL").selectedIndex = 3;
				            break;
				    }
				}
				
				// Render the albums page
				var sortDDL = document.getElementById("sortDDL");
				RenderAlbums(sortDDL.value);
            }
        }
        
        if(advancedOptions) {
            showAdvanced();
        }
        
        document.body.style.cursor="default";
    }
}

// RenderAlbums() actually "draws" the page with the albums.
function RenderAlbums(sortType) {
    var displayType = document.getElementById("displayDDL");
    var rowCount = 0;
    var albumCounter = 0;
    var hasAlbums = false;
    switch(sortType) {
        case "1":
            lpAlbums.sort(sortNameAsc);
            break;
        case "2":
            lpAlbums.sort(sortNameDesc);
            break;
        case "3":
            lpAlbums.sort(sortDateDesc);
            break;
        case "4":
            lpAlbums.sort(sortDateAsc);
            break;
    }
    if(displayType.value == "1") {
        //Display thumbnail style
        var albumsTable = document.getElementById("thumbsTable");
        var tableRows = albumsTable.rows.length;
        if(lpAlbums.length == 0) {
            var noAlbumsRow = albumsTable.insertRow(tableRows);
            var noAlbumsCell = noAlbumsRow.insertCell(0);
 //Jim see section starting at line 187/           noAlbumsCell.innerHTML = "I'm sorry, you do not currently have any albums.  Please <a href='../Uploader/LPUploader.aspx'>click here</a> to add photos.";
        } else {
            for(albumKey in lpAlbums) {
                if(lpAlbums[albumKey].parentAlbum == "0") {
                    hasAlbums = true;
                    if(albumCounter < showNumAlbums) {
                        if(rowCount == 0) {
                            var curRow = albumsTable.insertRow(tableRows);
                            curRow.setAttribute("id", "row" + tableRows);
                            tableRows++;
                        }
                        
                        var newCell = curRow.insertCell(rowCount);
                        newCell.className = "thumbtd1";
                        newCell.setAttribute("valign", "top");
                        
                        var divClass = "";
                        var matte = "";
                        
                        if(lpAlbums[albumKey].ownerID != shopperID)
                        {
                            divClass = "thumbdiv_empty_green";
                            matte = "427d29";
                        }
                        else
                        {
                            divClass = "thumbdiv_empty";
                            matte = "525252"; 
                        }

                        var albumLink = "";
                        //This if was for the old share ui. 
                        /*if(greenType == "share") {
                            albumLink = "/common/album/albumshare.cfm?AlbumTitle=" + lpAlbums[albumKey].name + "&albumid=" + lpAlbums[albumKey].id;
                        } else {*/
                        var str = lpAlbums[albumKey].name;
                        var isSync = false;
                        //  If the folder name contains "Sync" this is a sync folder
                        if(str.match("Sync"))
                        {
                            isSync = true;
                            // Forward the user to the new ListImagesTreeView
                            albumLink = "ListImagesTreeView.aspx?album=" + lpAlbums[albumKey].id;
                            if (lpAlbums[albumKey].shareID > 0)
                            {
                                albumLink = albumLink + "&s=" + lpAlbums[albumKey].shareID;
                            }
                            
                            totalAlbumCount = 0;
                            totalImageCount = 0;
                            CountSubAlbums(albumKey);
                            syncAlbumsID[albumCounter] = lpAlbums[albumKey].plainID;
                            //alert("syncAlbumsID[albumCounter]=" + lpAlbums[albumKey].plainID);
                        }
                        else
                        {
                            albumLink = "ListImages.aspx?album=" + lpAlbums[albumKey].id + "&tabColor=" + tabColor;
                        }
                        //}
                        
                        var childrenLink = "";
                        if(lpAlbums[albumKey].hasChildren == "true") 
                        {
                            if(isSync == true)
                            {
                                childrenLink = "";//"<a style='font-size:11px;' href='javascript:showSubAlbums(" + albumKey + ");'>Sub Folders(" + totalAlbumCount +")</a><div id='childAlbums" + albumKey + "' class='" + popupClass + "' style='display:none;width:300px;position:absolute;'><div class='floatright'><a href='javascript:CloseChildWindow(\"childAlbums" + albumKey + "\");'><img src='/common/images/ColorTabsSkin/" + closeGraphic + "' alt='Close Window' /></a></div></div>";
                            }
                            else
                            {
                                childrenLink = "<a href='javascript:showSubAlbums(" + albumKey + ");'>Sub Albums</a><div id='childAlbums" + albumKey + "' class='" + popupClass + "' style='display:none;width:300px;position:absolute;'><div class='floatright'><a href='javascript:CloseChildWindow(\"childAlbums" + albumKey + "\");'><img src='/common/images/ColorTabsSkin/" + closeGraphic + "' alt='Close Window' /></a></div></div>";
                            }
                        }
                        
                        var imageAddons = "";
                        var isEmptySyncFolder = false;
                        if(lpAlbums[albumKey].thumbnailPath.indexOf("?filespec") > -1) {
                            if(lpAlbums[albumKey].thumbnailPath.indexOf("maxSize") > -1) {
                                imageAddons = "&matte=" + matte;
                            } else {
                                imageAddons = "&maxSize=50";//&matte=" + matte;
                            }
                        }
                        else
                        {
                            //  If this is a sync folder and there are no images, show a different folder image
                            if(isSync == true)
                            {
                                //alert("no images");
                                isEmptySyncFolder = true;
                            }
                        }

                        var theHTML = "<div id='" + lpAlbums[albumKey].id + "'><div class='" + divClass + "' onclick=\"document.location='" + albumLink + "'\" onmouseover=\"this.style.cursor='pointer'\">";
                        if(isEmptySyncFolder == true)
                        {
                            //  Show a different graphic
                            theHTML += "<img src='/common/images/ColorTabsSkin/SyncAlbumIcon.png' " + " alt='" + lpAlbums[albumKey].name + "' /></div>" + childrenLink;
                        }
                        else
                        {
                            theHTML += "<img src=\"" + lpAlbums[albumKey].thumbnailPath + imageAddons + "\" alt='" + lpAlbums[albumKey].name + "' /></div>" + childrenLink;
                            
                        }
                        
                        if(lpAlbums[albumKey].ownerID != shopperID)
                        {
                            theHTML += "<div class='phototitle'><a href=\"" + albumLink + "\">" + lpAlbums[albumKey].name + "</a>&nbsp<a href=\"" + albumLink + "\"><img src=\"/common/images/ColorTabsSkin/GreenDude.gif\" alt=\"This is a Shared Album\" align=\"middle\"></a></div>";
                        }
                        else
                        {
                            theHTML += "<div class='phototitle'><a href=\"" + albumLink + "\">" + lpAlbums[albumKey].name + "</a></div>";
                        }
                                                
                        //  If this is a sync folder we want to show the total number of photos
                        if(isSync == true)
                        {

                            theHTML += "<div class='thumbstats'>" + formatDate(lpAlbums[albumKey].date) + "<br />" + totalImageCount + " Photos<br />";
                        }
                        else
                        {
                            theHTML += "<div class='thumbstats'>" + formatDate(lpAlbums[albumKey].date) + "<br />" + lpAlbums[albumKey].numPics + " Photos<br />";
                        }
                        
                        if(lpAlbums[albumKey].ownerID == shopperID)
                        {
                            theHTML += "<div style='text-align: center;'><a href='/net/Albums/AddEditAlbum.aspx?mode=edit&albumid=" + lpAlbums[albumKey].id + "'><img src='/common/images/ColorTabsSkin/icon_edit.gif' alt='Edit Album' style='vertical-align:bottom;' /></a> <a href='/net/Albums/AddEditAlbum.aspx?mode=edit&albumid=" + lpAlbums[albumKey].id + "'>Edit</a>&nbsp;&nbsp;|";
                            //theHTML += "&nbsp;&nbsp;<a href='/common/album/checkdelete.cfm?abname=" + escape(lpAlbums[albumKey].name) + "&albumid=" + lpAlbums[albumKey].id + "&mode=DeleteAlbum&viewType=Old'>Delete</a> <a href='/common/album/checkdelete.cfm?abname=" + escape(lpAlbums[albumKey].name) + "&albumid=" + lpAlbums[albumKey].id + "&mode=DeleteAlbum&viewType=Old'><img src='/common/images/ColorTabsSkin/icon_delete.gif' alt='Delete This Album' style='vertical-align:bottom;' /></a></div></div></div>";
                            theHTML += "&nbsp;&nbsp;<a href='/net/Albums/checkdelete.aspx?albumid=" + lpAlbums[albumKey].id + "&shopperID=" + shopperID + "&redirect=Albums/ListAlbums.aspx'>Delete</a> <a href='/net/Albums/checkdelete.aspx?albumid=" + lpAlbums[albumKey].id + "&shopperID=" + shopperID + "&redirect=Albums/ListAlbums.aspx'><img src='/common/images/ColorTabsSkin/icon_delete.gif' alt='Delete This Album' style='vertical-align:bottom;' /></a></div></div></div>";
                        }
                        else
                        {
                            //theHTML += "&nbsp;&nbsp;<a href='/common/album/albumaction.cfm?albumid=" + lpAlbums[albumKey].id + "&mode=DeleteSharedAlbum&enc=com'>Delete</a> <a href='/common/album/albumaction.cfm?albumid=" + lpAlbums[albumKey].id + "&mode=DeleteSharedAlbum&enc=com'><img src='/common/images/ColorTabsSkin/icon_delete.gif' alt='Delete This Album' style='vertical-align:bottom;' /></a></div></div></div>";
                            theHTML += "&nbsp;&nbsp;<a href='/net/Albums/DeleteSharedAlbum.aspx?albumid=" + lpAlbums[albumKey].id + "&sharedShopperID=" + shopperID + "&redirect=OrderPrints'>Delete</a> <a href='/net/Helpers/AlbumHelper.ascx?albumid=" + lpAlbums[albumKey].id + "&sharedShopperID=" + shopperID + "&redirect=OrderPrints'><img src='/common/images/ColorTabsSkin/icon_delete.gif' alt='Delete This Album' style='vertical-align:bottom;' /></a></div></div></div>";
                        } 
                        
                        newCell.innerHTML = theHTML;
                        
                        //SET_DHTML(lpAlbums[albumKey].id);

                        rowCount++;
                        albumCounter++;
                        
                        if(rowCount == albumsPerRow) {
                            rowCount = 0;
                            if(albumCounter != showNumAlbums) {
                                if(lpAlbums.length > albumCounter) {
                                    var rowDivider = albumsTable.insertRow(tableRows);
                                    rowDivider.setAttribute("id", "row" + tableRows);
                                    var cellDivider = rowDivider.insertCell(0);
                                    cellDivider.colSpan = albumsPerRow;
                                    cellDivider.innerHTML = "<div class='horz_whitedivider'><!-- &nbsp; --></div>";
                                    tableRows++;
                                }
                            }
                        }
                    }
                }
            }
        }
        if(!hasAlbums) {
             try{
                if((lpAlbums.length == 0) || (lpAlbumsFull[0]==null))// || lpAlbumsFull[0].name==null || lpAlbumsFull[0].name == undefined || lpAlbumsFull[0].name == "") 
                {
                    //  Duplicate this functionality in the error catch
                    var curRow = albumsTable.insertRow(tableRows);
                    var newCell = curRow.insertCell(rowCount);
                    newCell.setAttribute("align", "center");
                    newCell.innerHTML = "I'm sorry, you do not currently have any albums.  Please <a href='../Uploader/LPUploader.aspx'>click here</a> to add photos.";
                } else 
                {
                    var curRow = albumsTable.insertRow(tableRows);
                    var newCell = curRow.insertCell(rowCount);
                    newCell.setAttribute("align", "center");
                    newCell.innerHTML = "None of your albums meet this search criteria.";
                }
            }catch(err)
            {
                var curRow = albumsTable.insertRow(tableRows);
                var newCell = curRow.insertCell(rowCount);
                newCell.setAttribute("align", "center");
                newCell.innerHTML = "You currently do not have any albums.  Please use our <a href='../Uploader/LPUploader.aspx'>Uploader</a> to add albums and photos!";
            }
        }
    } else {
        //Display list style
        var listTable = document.getElementById("listTable");
        var leftListTable = document.getElementById("leftListTable");
        var rightListTable = document.getElementById("rightListTable");
        var actualNumAlbums = lpAlbums.length;
        var albumsPerSide = 0;
        
        if(actualNumAlbums > showNumAlbums) {
            albumsPerSide = Math.round(showNumAlbums / 2);
        } else {
            albumsPerSide = Math.round(actualNumAlbums / 2);
            var showAllBtn = document.getElementById("showall");
            showAllBtn.style.display = 'none';
        }
        
        var leftRowCtr = leftListTable.rows.length;
        var rightRowCtr = rightListTable.rows.length;
        var colNum = 0;
        for(albumKey in lpAlbums) {
            hasAlbums = true;
            if(albumCounter < showNumAlbums) {
                if(albumCounter < albumsPerSide) {
                    var curTableRow = leftListTable.insertRow(leftRowCtr);
                    colNum = 1;
                    leftRowCtr++;
                } else {
                    var curTableRow = rightListTable.insertRow(rightRowCtr);
                    colNum = 2;
                    rightRowCtr++;
                }
                
                var albumLink = "";
                //This if was for the old share ui. 
                        /*
                if(greenType == "share") {
                    albumLink = "/common/album/albumshare.cfm?AlbumTitle=" + lpAlbums[albumKey].name + "&albumid=" + lpAlbums[albumKey].id;
                } else {*/
                    albumLink = "ListImages.aspx?album=" + lpAlbums[albumKey].id;
                
                
                var curTableCell0 = curTableRow.insertCell(0);
                curTableCell0.className = "viewlisttd1col" + colNum;
                if(lpAlbums[albumKey].ownerID != shopperID)
                {
                    curTableCell0.innerHTML = "<a href='" + albumLink + "'>" + lpAlbums[albumKey].name + "</a>&nbsp<a href=\"" + albumLink + "\"><img src=\"/common/images/ColorTabsSkin/GreenDude.gif\" alt=\"This is a Shared Album\" align=\"middle\"></a><br /><span style='font-size: 10px;'>" + formatDate(lpAlbums[albumKey].date) + " - " + lpAlbums[albumKey].numPics + " Photos</span>";
                }
                else
                {
                    curTableCell0.innerHTML = "<a href='" + albumLink + "'>" + lpAlbums[albumKey].name + "</a><br /><span style='font-size: 10px;'>" + formatDate(lpAlbums[albumKey].date) + " - " + lpAlbums[albumKey].numPics + " Photos</span>";
                }
                
                var curTableCell1 = curTableRow.insertCell(1);
                curTableCell1.className = "viewlisttd2col" + colNum;
                
                if(lpAlbums[albumKey].ownerID != shopperID)
                {
                    curTableCell1.innerHTML = "<a href='/net/Albums/DeleteSharedAlbum.aspx?albumid=" + lpAlbums[albumKey].id + "&sharedShopperID" + shopperID + "&redirect=OrderPrints'><img src='/common/images/ColorTabsSkin/icon_delete.gif' alt='Delete This Album' /></a>&nbsp;&nbsp;<a href='/net/Helpers/AlbumHelper.aspx?albumid=" + lpAlbums[albumKey].id + "&sharedShopperID" + shopperID + "&redirect=OrderPrints'>Delete</a>";
                }
                else
                {
                    //curTableCell1.innerHTML = "<a href='/net/Albums/AddEditAlbum.aspx?mode=edit&albumid=" + lpAlbums[albumKey].id + "'><img src='/common/images/ColorTabsSkin/icon_edit.gif' alt='Edit Album' /></a>&nbsp;&nbsp;<a href='/net/Albums/AddEditAlbum.aspx?mode=edit&albumid=" + lpAlbums[albumKey].id + "'>Edit</a><br /><a href='/common/album/checkdelete.cfm?abname=" + lpAlbums[albumKey].name + "&albumid=" + lpAlbums[albumKey].id + "&mode=DeleteAlbum&viewType=Old'><img src='/common/images/ColorTabsSkin/icon_delete.gif' alt='Delete This Album' /></a>&nbsp;&nbsp;<a href='/common/album/checkdelete.cfm?abname=" + lpAlbums[albumKey].name + "&albumid=" + lpAlbums[albumKey].id + "&mode=DeleteAlbum&viewType=Old'>Delete</a>";
                    curTableCell1.innerHTML = "<a href='/net/Albums/AddEditAlbum.aspx?mode=edit&albumid=" + lpAlbums[albumKey].id + "'><img src='/common/images/ColorTabsSkin/icon_edit.gif' alt='Edit Album' /></a>&nbsp;&nbsp;<a href='/net/Albums/AddEditAlbum.aspx?mode=edit&albumid=" + lpAlbums[albumKey].id + "'>Edit</a><br /><a href='/net/albums/checkdelete.aspx?albumid=" + lpAlbums[albumKey].id + "&shopperID=" + shopperID + "&redirect=Albums/ListAlbums.aspx'><img src='/common/images/ColorTabsSkin/icon_delete.gif' alt='Delete This Album' /></a>&nbsp;&nbsp;<a href='/net/albums/checkdelete.aspx?albumid=" + lpAlbums[albumKey].id + "&shopperID=" + shopperID + "&redirect=Albums/ListAlbums.aspx'>Delete</a>";
                }
                albumCounter++;
            }
        }
        if(!hasAlbums) {
            var curRow = listTable.insertRow(tableRows);
            var newCell = curRow.insertCell(rowCount);
            newCell.setAttribute("align", "center");
            newCell.innerHTML = "You currently do not have any albums.  Please use our <a href='../Uploader/LPUploader.aspx'>Uploader</a> to add albums and photos!";
        }
    }
}

function ResortAlbums(sortType) {
    var displayDDL = document.getElementById("displayDDL");
    if(displayDDL.value == "1") {
        var albumsTable = document.getElementById("thumbsTable");
        for(var row = 0; row < albumsTable.rows.length; row++) {
            albumsTable.removeChild(albumsTable.lastChild);
        }
    } else {
        var leftListTable = document.getElementById("leftListTable");
        var rightListTable = document.getElementById("rightListTable");
        for(var row = 0; row < leftListTable.rows.length; row++) {
            leftListTable.removeChild(leftListTable.lastChild);
        }
        for(var row = 0; row < rightListTable.rows.length; row++) {
            rightListTable.removeChild(rightListTable.lastChild);
        }
    }
    RenderAlbums(sortType);
    SaveSortToDB();
}

function ShowAllAlbums() {
    showNumAlbums = 500;
    var sortDDL = document.getElementById("sortDDL");
    var showAllBtn = document.getElementById("showall");
    showAllBtn.style.display = 'none';
    ResortAlbums(sortDDL.value);
    document.body.style.cursor="default";
}

function ChangeViewStyle(displayStyle) {
    var listTable = document.getElementById("listTable");
    var thumbsTable = document.getElementById("thumbsTable");
    if(displayStyle == "1") {
        showNumAlbums = albumsPerRow * 2;
        var showAllBtn = document.getElementById("showall");
        showAllBtn.style.display = '';
        listTable.style.display = 'none';
        thumbsTable.style.display = '';
    } else {
        showNumAlbums = 26;
        listTable.style.display = '';
        thumbsTable.style.display = 'none';
    }
    var sortDDL = document.getElementById("sortDDL");
    ResortAlbums(sortDDL.value);
}


function SearchAlbums(searchCriteria) {
    if(searchCriteria != "Image Keyword Search") {
        var tempAlbums = new Array();
        var newCtr = 0;
        for(albumKey in lpAlbumsFull) {
            var curName = lpAlbumsFull[albumKey].name.toLowerCase();
            
            if(curName.indexOf(searchCriteria) > -1) {
                tempAlbums[newCtr] = new AlbumData();
			    tempAlbums[newCtr].name = lpAlbumsFull[albumKey].name;
			    tempAlbums[newCtr].date = lpAlbumsFull[albumKey].date;
	            zeDate = formatDate(lpAlbumsFull[albumKey].date);
			    zeDate = zeDate.split('/');
			    zeSDate = zeDate[2] + " " + (zeDate[0].length == 1 ? "0" + zeDate[0] : zeDate[0]) + " " + (zeDate[1].length == 1 ? "0" + zeDate[1] : zeDate[1]);
			    tempAlbums[newCtr].sortDate = zeSDate;
			    tempAlbums[newCtr].numPics = lpAlbumsFull[albumKey].numPics;
			    tempAlbums[newCtr].id = lpAlbumsFull[albumKey].id;
			    tempAlbums[newCtr].thumbnailPath = lpAlbumsFull[albumKey].thumbnailPath;
			    tempAlbums[newCtr].thumbnailOrientation = lpAlbumsFull[albumKey].thumbnailOrientation;
			    tempAlbums[newCtr].parentAlbum = lpAlbumsFull[albumKey].parentAlbum;
			    tempAlbums[newCtr].hasChildren = lpAlbumsFull[albumKey].hasChildren;
			    newCtr++;
            }
        }
        lpAlbums = tempAlbums;
        var sortDDL = document.getElementById("sortDDL");
        ResortAlbums(sortDDL.value);
    } else {
        alert("Please enter keyword(s) to enable search functionality.");
    }
}

function formatDate(theDate) {
    theDate = theDate.split(' ');
    var justDate = theDate[0];
    return justDate;
}

// album data structure
function AlbumData()
{
	this.id;				    // unique identifier for fetching this album
	this.name;				    // UI name that shows with the thumbnail for this album
	this.date;				    // date shown with thumbnail
	this.sortDate;              // Date to sort by
	this.numPics;			    // number of pictures in the album
	this.thumbnailPath;		    // small image for showing this album in the UI
	this.thumbnailOrientation;  // thumbnail orientation, landscape or portrait
	this.parentAlbum;           // ID of this album's parent
	this.hasChildren;           // has child albums
}

//Custom Object Array Sort Functions
function sortNameAsc(a, b) {
    var x = a.name.toLowerCase();
    var y = b.name.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function sortNameDesc(a, b) {
    var x = b.name.toLowerCase();
    var y = a.name.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function sortDateAsc(a, b) {
    var x = a.sortDate.toLowerCase();
    var y = b.sortDate.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function sortDateDesc(a, b) {
    var x = b.sortDate.toLowerCase();
    var y = a.sortDate.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function hideAdvanced() {
    var hideDiv = document.getElementById("hideAdvanced");
    var showDiv = document.getElementById("showAdvanced");
    hideDiv.style.display = '';
    showDiv.style.display = 'none';
    advancedOptions = 0;
   
    wsCall = new wsObj();
    wsCall.functionName = "UpdateAdvOptions";
    wsCall.callbackFunction = GetAdvOptionsResponse;
    wsCall.addParam("shopperID", shopperID);
    wsCall.addParam("advancedOptions", advancedOptions);
    wsCall.sendRequest();
}

function showAdvanced() {  
    var hideDiv = document.getElementById("hideAdvanced");
    var showDiv = document.getElementById("showAdvanced");
    hideDiv.style.display = 'none';
    showDiv.style.display = ''; 
    advancedOptions = 1;
    
    wsCall = new wsObj();
    wsCall.functionName = "UpdateAdvOptions";
    wsCall.callbackFunction = GetAdvOptionsResponse;
    wsCall.addParam("shopperID", shopperID);
    wsCall.addParam("advancedOptions", advancedOptions);
    wsCall.sendRequest();
}

function GetAdvOptionsResponse() {
    /*if (GotGoodResponse())
    {
        alert(http.responseText);  
    }*/
}

function imageLoaded(imageObj) {

    var multiplier = 0.0;
    var pLeft = 0;
    var pTop = 0;
    if(imageObj.width == imageObj.height) {
        imageObj.parentNode.className = "thumbdiv_empty";
    } else {
        if(imageObj.width > imageObj.height) {
            multiplier = imageObj.width / imageObj.height;
            imageObj.width = 50;
            imageObj.height = 50 / multiplier;
            imageObj.parentNode.className = "thumbdiv_land";
            pLeft = Math.round(((59 - imageObj.width) / 2) - 2);
            pTop = Math.round(((61 - imageObj.height) / 2) + 2);
            if(pLeft < 0) {
                pLeft = 0;
            }
            if(pTop < 0) {
                pTop = 0;
            }
            imageObj.parentNode.style.paddingLeft = pLeft + "px";
            imageObj.parentNode.style.paddingTop = pTop + "px";
            imageObj.parentNode.style.height = (62 - (Math.round(((61 - imageObj.height) / 2) + 2) - 14)) + "px";
        } else {
            multiplier = imageObj.height / imageObj.width;
            imageObj.height = 50;
            imageObj.width = 50 / multiplier;
            imageObj.parentNode.className = "thumbdiv_port";
            pLeft = Math.round(((59 - imageObj.width) / 2) - 10);
            pTop = Math.round(((61 - imageObj.height) / 2) + 1);
            if(pLeft < 0) {
                pLeft = 0;
            }
            if(pTop < 0) {
                pTop = 0;
            }
            imageObj.parentNode.style.paddingLeft = pLeft + "px";
            imageObj.parentNode.style.paddingTop = pTop + "px";
        }
    }
}

function SaveSortToDB() {
    var sortWord = new String();
    switch(document.getElementById("sortDDL").selectedIndex) {
        case 0:
            sortWord = "title";
            break;
        case 1:
            sortWord = "titleRev";
            break;
        case 2:
            sortWord = "dateNewOld";
            break;
        case 3:
            sortWord = "dateOldNew";
            break;
    }
    
    wsCall = new wsObj();
    wsCall.functionName = "SaveAlbumSortType";
    wsCall.callbackFunction = GetSortTypeResponse;
    wsCall.addParam("ShopperID", shopperID);
    wsCall.addParam("SortType", sortWord);
    wsCall.sendRequest();
}

function GetSortTypeResponse() {
    //alert(http.responseText);
}

function showSubAlbums(pAlbumKey) {
    if(document.getElementById("childAlbums" + pAlbumKey)) {
        var childWindowDiv = document.getElementById("childAlbums" + pAlbumKey);
        if(childWindowDiv.childNodes.length == 1) {
            var childTable = childWindowDiv.appendChild(document.createElement("TABLE"));
            var rowCount = 0;
            var tableRows = 0;
            var albumCounter = 0;
            for(iAlbumKey in lpAlbums) { 
            
                if(lpAlbums[iAlbumKey].parentDec == lpAlbums[pAlbumKey].plainID) {
                    if(rowCount == 0) {
                        var curRow = childTable.insertRow(tableRows);
                        tableRows++;
                    }
                    
                    var newCell = curRow.insertCell(rowCount);
                    newCell.className = "thumbtd1";
                    newCell.setAttribute("valign", "top");
                    
                    var albumLink = "";
                    
//                    if(greenType == "share") {
//                        albumLink = "/net/ShareOrganize/ShareStart.aspx";
//                    } else {
                        albumLink = "ListImages.aspx?album=" + lpAlbums[iAlbumKey].id;
//                    }
                    
                    var divClass = "";
                    if(lpAlbums[iAlbumKey].thumbnailOrientation == "landscape") {
                        divClass = "thumbdiv_land";
                    } else {
                        divClass = "thumbdiv_port";
                    }
                 
                    //  Sync
                    isSyncFolder = false;
                    IsSyncFolder(lpAlbums[iAlbumKey].parentDec);   
                    if(isSyncFolder)
                    {
                        if(lpAlbums[iAlbumKey].numPics == 0)
                        {
                            divClass = "thumbdiv_empty";
                            albumLink = "#";
                        }   
                    }   
                    
                    var childrenLink = "";
                    if(lpAlbums[iAlbumKey].hasChildren == "true") 
                    {
                        if(isSyncFolder == true)
                        {
                            childrenLink = "";//"<a style='font-size:11px;' href='javascript:showSubAlbums(" + iAlbumKey + ");'>Sub Folders</a><div id='childAlbums" + iAlbumKey + "' class='" + popupClass + "' style='display:none;width:300px;position:absolute;'><div class='floatright'><a href='javascript:CloseChildWindow(\"childAlbums" + iAlbumKey + "\");'><img src='/common/images/ColorTabsSkin/" + closeGraphic + "' alt='Close Window' /></a></div></div>";
                        }
                        else
                        {
                            childrenLink = "<a href='javascript:showSubAlbums(" + iAlbumKey + ");'>Sub Albums</a><div id='childAlbums" + iAlbumKey + "' class='" + popupClass + "' style='display:none;width:300px;position:absolute;'><div class='floatright'><a href='javascript:CloseChildWindow(\"childAlbums" + iAlbumKey + "\");'><img src='/common/images/ColorTabsSkin/" + closeGraphic + "' alt='Close Window' /></a></div></div>";                
                        }
                    }              
                    
                    var theHTML = "<div class='" + divClass + "' onclick=\"document.location='" + albumLink + "'\" onmouseover=\"this.style.cursor='pointer'\">";

                        ////Sync
                        if(isSyncFolder)
                        {
                            //alert("isInSyncFolderStructure");
                            if(lpAlbums[iAlbumKey].numPics == 0)
                            {
                                theHTML += "<img src='/common/images/ColorTabsSkin/SyncAlbumIcon.png'  /></div>" + childrenLink;
                            }
                            else
                            {
                                theHTML += "<img src=\"" + lpAlbums[iAlbumKey].thumbnailPath + "\" onload='imageLoaded(this)' width='50' /></div>" + childrenLink;
                            }
                            
                        }
                        else
                        {
                            theHTML += "<img src=\"" + lpAlbums[iAlbumKey].thumbnailPath + "\" onload='imageLoaded(this)' width='50' /></div>" + childrenLink;
                        }
                        theHTML += "<div class='phototitle'><a href='" + albumLink + "'>" + lpAlbums[iAlbumKey].name + "</a></div>";
                        theHTML += "<div class='thumbstats'>" + formatDate(lpAlbums[iAlbumKey].date) + "<br />" + lpAlbums[iAlbumKey].numPics + " Photos<br />";
                        theHTML += "<div style='text-align: center;'><a href='/net/Albums/AddEditAlbum.aspx?mode=edit&albumid=" + lpAlbums[iAlbumKey].id + "'><img src='/common/images/ColorTabsSkin/icon_edit.gif' alt='Edit Album' style='vertical-align:bottom;' /></a> <a href='/net/albums/addeditalbum.aspx?mode=edit&albumid=" + lpAlbums[iAlbumKey].id + "'>Edit</a>&nbsp;&nbsp;|";
                       // theHTML += "&nbsp;&nbsp;<a href='/common/album/checkdelete.cfm?abname=" + lpAlbums[iAlbumKey].name + "&albumid=" + lpAlbums[iAlbumKey].id + "&mode=DeleteAlbum&viewType=Old'>Delete</a> <a href='/common/album/checkdelete.cfm?abname=" + lpAlbums[iAlbumKey].name + "&albumid=" + lpAlbums[iAlbumKey].id + "&mode=DeleteAlbum&viewType=Old'><img src='/common/images/ColorTabsSkin/icon_delete.gif' alt='Delete This Album' style='vertical-align:bottom;' /></a></div></div>";
                        theHTML += "&nbsp;&nbsp;<a href='/net/albums/checkdelete.aspx?albumid=" + lpAlbums[iAlbumKey].id + "&shopperID=" + shopperID + "&redirect=Albums/ListAlbums.aspx'>Delete</a> <a href='/net/albums/checkdelete.aspx?albumid=" + lpAlbums[iAlbumKey].id + "&shopperID=" + shopperID + "&redirect=Albums/ListAlbums.aspx'><img src='/common/images/ColorTabsSkin/icon_delete.gif' alt='Delete This Album' style='vertical-align:bottom;' /></a></div></div>";
                       
                    newCell.innerHTML = theHTML;
                    
                    rowCount++;
                    albumCounter++;
                    
                    if(rowCount == 3) {
                        rowCount = 0;
                    }
                }
            }
            if(albumCounter < 3) {
                childWindowDiv.style.width = (125 * albumCounter) + "px";
            } else {
                childWindowDiv.style.width = "375px";
            }
        }
        childWindowDiv.style.display = '';
    }
}

var totalAlbumCount = 0;
var totalImageCount = 0;
function CountSubAlbums(pAlbumKey) 
{

    //  Recurse down an album's subalbums, adding up all images and creating a total folder count
    //  Used for display purposes only
    for(iAlbumKey in lpAlbums) 
    { 
        if(lpAlbums[iAlbumKey].parentDec == lpAlbums[pAlbumKey].plainID) 
        {
            totalAlbumCount ++;
            totalImageCount = totalImageCount + parseInt(lpAlbums[iAlbumKey].numPics, 10);
            
            if(lpAlbums[iAlbumKey].hasChildren == "true") 
            {
                CountSubAlbums(iAlbumKey);
            }
                    
        }
    }
            
}

    var isSyncFolder = false;

function IsSyncFolder(parentPlainID)
{
//return true;

    for(jAlbumKey in lpAlbums) 
    { 
            
        //  Find the parent folder
                if(lpAlbums[jAlbumKey].plainID == parentPlainID) 
                {
                      if(lpAlbums[jAlbumKey].parentDec == "0" )
                      {
                        for (var i=0; i<syncAlbumsID.length; i++) 
                        {
                            if(syncAlbumsID[i] == lpAlbums[jAlbumKey].plainID)
                            {
                                isSyncFolder = true;
                                break;
                            }
                        }
                        break;
                        
                      }
                      else
                      {
                        IsSyncFolder(lpAlbums[jAlbumKey].parentDec)
                        break;
                      }                
                }
                else
                {
                }
    }

    return isSyncFolder;
}

 

var syncAlbumsID = [];

function CloseChildWindow(objID) {
    document.getElementById(objID).style.display = 'none';
}