
/*------------- Shared Gallery Functions -------------*/
var tableId; //set in the page
var themeIPath;
var showButtons = true;

$(document).ready(function() {
    //Check display mode: grid view only has one image button (if any) at the top
    if (tableId == null) {
        return;
    }
    var isInGridView = $(".GalleryThumbStyle tr:first-child td a img").length <= 1;
    if (isInGridView) {
        CustomizeCaps(tableId, showButtons);
        var pageLinkBoxHeight = $('.PagerLinks').height();
        $('.Frame').height(pageLinkBoxHeight);
        $('.iTopBack').height(pageLinkBoxHeight);
        $('.GalleryThumbStyle tr:first-child').hide();
    }
});

function CustomizeCaps(id,showButtons) {
    var table = document.getElementById(id);
    var rowCount = table.getElementsByTagName("tr").length;
    if (rowCount > 2) {
        //get the table caps
        var header = table.getElementsByTagName("tr")[0].getElementsByTagName("td")[0];
        var footer = table.getElementsByTagName("tr")[rowCount - 1].getElementsByTagName("td")[0];
        //define custom content
        var customButtonContent = '<div class="Buttons"><img border="0" src="' + themeIPath + 'gallery/Clickherebuttons.jpg' + '" usemap="#gLinks" width="543" height="19"/></div>';
        if (showButtons == false) {
            customButtonContent = '';
        }
        var customFooterContent = '<br>Click on any of the pages to see more photos.<div class="Frame"><div class="PagerLinks"><div class="Inner">' + footer.innerHTML + '</div></div><img class="iTopBack" src="' + themeIPath + 'gallery/topGradient.jpg' + '" /></div>' + customButtonContent;
        //clear the table based caps
        header.innerHTML = "";
        footer.innerHTML = "";
        //set the placeholder based cap content
        $('#phTop').html("<div class='GalleryPagerBox'>" + header.innerHTML + customFooterContent + "</div>");
        $('#phBottom').html("<div class='GalleryPagerBox'>" + customFooterContent + "</div>");
    }
}

/*----------- Celeb Gallery Functions -------------*/

function FindShoeLink(links)
{
	//links is a collection of a elements
	for(var i = 0; i < links.length; i++)
	{
		if(links[i].id == "shoeLink")
		{
			return links[i];
		}
	}
	return null;
}
function FindGalleryLink(cell)
{
	//cell is the cell to search for the gallery link
	//Find the GalleryDefault link..this will have an onclick event
	var links = cell.getElementsByTagName("a");
	for(var i = 0; i < links.length; i++)
	{
		if(links[i].onclick != null)
		{
			return links[i];
		}
	}
	return null;	
}

function RemoveUnwantedBRsFromRow(row)
{
	var cellLeft;
	var cellRight;
	var cells = row.getElementsByTagName("td");
	
	if(cells.length != 2)
	{
		return;
	}
	
	cellLeft = cells[0];
	cellRight = cells[1];
	
	RemoveUnwantedBRsFromCell(cellLeft);
	RemoveUnwantedBRsFromCell(cellRight);
}

function RemoveUnwantedBRsFromCell(cell)
{
	var elements = cell.childNodes;//FF and IE
	
	//start loop at bottom of cell and look upward until there are no br tags
	for(var i = elements.length-1; i >= 0; i--)
	{
	var HasEmptyStringAtEndOfCell = elements[i].nodeName == "#text" && elements[i].length == 0;
		if(elements[i].tagName == "BR" || HasEmptyStringAtEndOfCell)
		{
			cell.removeChild(elements[i]);
		}
		else
		{
			break;//after you reach the last BR break out because any other BRs might need to be there.
		}
	}
}
function SetLinks(id)
{
	//PreCondition:The table with id must exist.
	//Used to replace existing image links hrefs using an admin defined "shoelink" for cells in a 2 column table.
	var table = document.getElementById(id);
	var rows = table.getElementsByTagName("tr");
	var links;
	var shoeLink;
	var galleryLink;
	
	for(var i = 0; i<rows.length;i++)
	{
		//Replace links
		links = rows[i].getElementsByTagName("a");
		if(links.length > 2)//at least 3 links will be there: 2 gallery and at least one shoelink
		{
			//Find the ShoeLink specified by the admin
			shoeLink = FindShoeLink(links);
			if(shoeLink != null)
			{
				galleryLink = FindGalleryLink(shoeLink.parentNode);
			}
			//Use the shoeLink to replace the gallery default link
			if(shoeLink != null && galleryLink != null)
			{
				galleryLink.href=shoeLink.href;
				//shoeLink.innerHTML='';//hide this link since we no longer need it.
				shoeLink.parentNode.removeChild(shoeLink);
			}
			//cellLeft.className = "Shoe";
		}
		
		//Remove unwanted BR tags (those at the end of the cell)
		RemoveUnwantedBRsFromRow(rows[i]);
	}
	
}
