<!--

/// <reference path="jquery.js" />
/// <reference path="jquery.vsdoc.js" />

function StyleAlternateRows(selectorPrefix) {    
    $(selectorPrefix + ":even")
        .removeClass("rowStyle1")
        .addClass("rowStyle2");
    
    $(selectorPrefix + ":odd")
        .addClass("rowStyle1")
        .removeClass("rowStyle2");
}

function AdjustRelativePaths(selector, attrName) {
    var prefix = GetRelativePrefix();
    if (!prefix) return;
    
    $(selector).each(function() {
        var attrValue = $(this).attr(attrName);
        
        if (attrValue && attrValue != '#' && attrValue.indexOf('http') === -1) {
            
            var newValue = prefix + attrValue;
            
            switch(attrName) {
                case 'src':
                    $(this).attr({ src: newValue }); 
                    break;
                case 'href':
                    $(this).attr({ href: newValue });
                    break;
            }
        }
    });
}

var gRelativePrefix;

// Get prefix for links, images, etc.
function GetRelativePrefix() {
    if (gRelativePrefix) { 
        return gRelativePrefix;
    }
    
    gRelativePrefix = '';
    
    var url = document.location.href.toLowerCase();
    var indx = url.indexOf('?');
    if (indx > -1) {
        url = url.substring(0, indx);
    }

    if ('file' === url.substring(0, 4)) {
        indx = url.indexOf('trinity');
    }
    else {
        indx = url.indexOf('trinityucity.org');
    }

    url = url.substring(indx);
    indx = url.indexOf('/');
    url = url.substring(indx + 1);
    indx = url.indexOf('/');

    while (indx > -1) {
        gRelativePrefix += '../';
        url = url.substring(indx + 1);
        indx = url.indexOf('/');
    }

    return gRelativePrefix;
}

function RenderScriptureLink(book, chapter, verses, display) {
// Bible passage link:
// book, chapter, verses - e.g. 'John', '3', '16-20'
// display = text between <a></a> tags - default = book, chapter & verses ('John 3:16-20')
    var sDisplay = display;
	if (!display) {
	    sDisplay = book + ' ' + chapter;
	    if (verses) {
	        sDisplay += ': ' + verses;
	    }
	}
    document.write(
	    '<a href="http://bible.oremus.org/?passage='
	    + book
		+ '+' + chapter);
		
	if (verses) {
        document.write('%3A' + verses); 
	}
		
	document.write(
		'&vnum=yes&version=nrsv" target="_blank">'
		+ sDisplay 
		+ '</a>');
		
    return true;
}

function RenderPageFooter(author, copyrightStartYear) {
// Display page footer
// author = sermon author or 'Trinity Presbyterian Church...' (default)
// copyrightStartYear = copyright start year - if blank, copyright not shown
    var sCopyright = copyrightStartYear;
    var sauthor = author || '<a href="http://www.trinityucity.org/">Trinity Presbyterian Church</a>';
	var dtNow = new Date();

	var sYear = dtNow.getFullYear();
	
	if (sCopyright) {
	    sCopyright = '© Copyright ' + sCopyright;
		if (copyrightStartYear < sYear) {
		    sCopyright = sCopyright + '-' + sYear;
		}
		sCopyright = sCopyright + '. ';
	}
			
	// show copyright/name
	var html = '<div class="footer"><p style="margin: 0">' + sCopyright + sauthor + '</p>';
    html += '<div style="font-size: small; padding-top: 1em; color: gray">';
    html += 'Trinity Presbyterian Church';
    html += '<span style="white-space: nowrap"> | 6800 Washington Avenue</span>';																		 
    html += '<span style="white-space: nowrap"> | University City, MO 63130</span>';																		 
    html += '<span style="white-space: nowrap"> | (314) 725-3840</span>';																		 
    html += '<span style="white-space: nowrap"> | <a href="mailto:tpcucity@swbell.net">mailto:tpcucity@swbell.net</a></span>';																		 
    html += '</div></div>';
    document.write(html);
}

function GetTodaysDate() {
	var today = new Date();
	// remove time portion of today's date:
	today = Date.parse((today.getMonth() + 1) + '/' + today.getDate() + '/' + today.getFullYear());

    return today;
}

// hide elements with startDate/endDate attributes out of range
function CheckStartAndEndDates() {
    // don't do this for news pages:
    if (document.location.href.indexOf('/news/') > 0) return;
    
    var compareDate = new Date();
	var today = GetTodaysDate();
	
	$("*[endDate]").each(function() {
        compareDate = Date.parse($(this).attr('endDate'));
        if (compareDate < today) $(this).hide();
    });
	
	$("*[startDate]").each(function() {
        compareDate = Date.parse($(this).attr('startDate'))
        if (compareDate > today) $(this).hide();
    });
}

function PickRandomNumber(range) 
{
    // pick a random number
    if (range === 0) {
        return 0;
    } else if (Math.random) {
		return Math.round(Math.random() * (range-1));
	} else {
		var now = new Date();
		return (now.getTime() / 1000) % range;
	}
}

function BuildNewsStoryIndex()
{
    var storyIndex = $('#storyIndex');
    if (!storyIndex) return;
    
	var html = '';
	
	$('div.story:visible, div.wordStory:visible, div.trinityStory:visible').each(function() {
	    
	    var href = $(this).attr('id');
	    if (!href) {
	        $(this).find('a[name]:first').each(function() {
                href = $(this).attr('name');
            });
	    }
	    
	    var title = '';
	    $(this).find('div.inlinehd:first').each(function() {
            title = $(this).html();
		});
        
        if (href && title != '') {
            html += '<p><a';
            html += buildAttributeHtml('href', '#' + href);
            html += '>' + title + '</a></p>';	
        }
	});

    if (!html) return;

    html = '<div' +
        buildAttributeHtml('id', 'storyIndexColorBox') +
        buildAttributeHtml('class', 'colorBox') +
        '>' + 
        html + 
        '</div>';
    
    jQuery.each(storyIndex, function() {
        $(this).html(html);
    });
}

// common page initialization
function InitializePage(copyrightAuthor, copyrightStartYear)
{
    RenderPageFooter(copyrightAuthor, copyrightStartYear);
    
    CheckStartAndEndDates();
    
    BuildNewsStoryIndex();
    
    ApplyRoundedCorners();
    
    ApplyYouAreHereStyle();
}

function ApplyRoundedCorners() {
    $("*[class$=olorBox]").corners(); 
}

function DetermineUrlRoot()
{
    var url = document.location.href.toLowerCase();
    var indx = url.indexOf('?');
    if (indx > -1)  {
        url = url.substring(0, indx);
    }
    
    var testString;
    
    if ('file' == url.substring(0, 4)) {
        testString = 'trinity';
    }
    else {
        testString = 'trinityucity.org';
    }
    
    indx = url.indexOf(testString);
    return url.substring(0, indx + testString.length + 1);
}

function DisplayRandomImgFromDiv(divId, padding, maxHeight) {
    var div = document.getElementById(divId);
    if (!div) return;
    
    var imgs = div.getElementsByTagName('img');
    
    var index = PickRandomNumber(imgs.length - 1);
    
    var img = imgs[index];
    var caption = img.alt;
    var moreCaption = '';
    var breakIndex = caption.indexOf('<br');
    if (breakIndex > 0) {
        moreCaption = caption.substring(breakIndex);
        caption = caption.substring(0, breakIndex);
    }
    
    BuildSizedImageHtml(divId, img.src, 
        img.getAttribute('actualWidth'), 
        img.getAttribute('actualHeight'), 
        padding, 
        maxHeight, 
        caption, 
        moreCaption);
        
    return img;
}

function BuildSizedImageHtml(
    divId, picUrl, actualWidth, actualHeight, 
    padding, maxHeight, 
    caption, moreCaption) {
    
    var picWidth = actualWidth;
    var picHeight = actualHeight;
    
    var div = document.getElementById(divId);
    if (!div) return false;
    
    div.style.float = 'none';
    
    var maxWidth = div.offsetWidth - padding;
    var percent;
    
    if (picWidth > maxWidth) {
        percent = maxWidth / picWidth;
        picWidth = maxWidth;
        picHeight *= percent;
    }
    
    if (picHeight > maxHeight) {
        percent = maxHeight / picHeight;
        picHeight = maxHeight;
        picWidth *= percent;
    }

    var html = '<a href="photo.html?';
    html += picUrl; 
    
    if (caption) {
        html += '&caption=';
        html += caption;
        if (moreCaption) {
            html += moreCaption;
        }
    }
    
    html += '"><img';
    html += buildAttributeHtml('src', picUrl);
    html += buildAttributeHtml('height', picHeight);
    html += buildAttributeHtml('width', picWidth);
    html += buildAttributeHtml('actualHeight', actualHeight);
    html += buildAttributeHtml('actualWidth', actualWidth);
    html += buildAttributeHtml('alt', caption);
    html += buildAttributeHtml('title', caption);
    html += ' /></a>';
    
    div.innerHTML = html;
}

function RenderThumbnail(sFolder, sPhoto, sCaption, bFramed) 
{
    var html = '<div';
    html += buildAttributeHtml('class', 'img-shadow');
    html += buildAttributeHtml('style', 'float: left');
    html += '</div><a';
    html += buildAttributeHtml('href', sFolder + '/' + sPhoto);
    html += buildAttributeHtml('title', sCaption);
    html += buildAttributeHtml('rel', 'lightbox[group]');
    html += '><img';
    html += buildAttributeHtml('src', sFolder + '/thumbs/' + sPhoto);
    html += '/></a></div>';
    
    GetPicturesDiv().innerHTML += html;    
}

function GetPicturesDiv() {
    var div = document.getElementById('PicturesDiv');
    if (!div)
    {
        document.write('<div id="PicturesDiv" />');
        div = document.getElementById('PicturesDiv');
    }
    
    return div;
}

function RenderPicWithClick(oDocument, sPhotoFileName, sCaption, bFramed, sOnClick) 
{
	var sHTML = '';
	var sTitle = '';
	if (sCaption) {
		sTitle = ' title="' + sCaption + '" alt="' + sCaption + '"';
	}
	
	while(sTitle.indexOf('<br>') > -1) {
		sTitle = sTitle.replace('<br>', '\n');
	}
	
	while(sTitle.indexOf('<br/>') > -1)	{
		sTitle = sTitle.replace('<br/>', '\n');
	}
	
	if (bFramed) {
		// sHTML += '<div class="thumb" style="float: left">';
		sHTML += '<div class="img-shadow">';
	}
	
    sHTML += '<a ' + sOnClick 
		+ sTitle
		+ '><img src="' + sPhotoFileName + '">' 
		+ '</a>';
		
	if (bFramed) {
		sHTML += '</div>';
	}

	oDocument.write(sHTML);
	return true;
}

function LoadCalendar(src)
{
    var iframe = $('#calendarFrame').get(0);
    var parent = $('#calendarParent').get(0);
    var top = parent.offsetTop;
    var bodyHeight = document.documentElement.clientHeight;
    
    var srcHeight = bodyHeight - top - 35;
    var frameHeight = srcHeight;
    
    iframe.height = frameHeight;
    
    src += '&height=';
    src += srcHeight;
    iframe.src = src;
}

function buildAttributeHtml(attName, attValue) {
    if (!attValue) return '';
    
    return ' ' + attName + '="' + attValue + '"';
}

function ResizeIFrame(id)
{
    // TODO: Figure out how to resize iframe to fit content
}

//-->