/*
 * Scripts to support the River Guardians website
 */

// hide the Pages document library
function HidePagesLibrary()
{
    var a=document.getElementById("ctl00_PlaceHolderTitleBreadcrumb_ContentMap").getElementsByTagName("a");
    var i;
    for(i=0;i<a.length;i++)
    {
       var link = a[i];
       if(link.href.indexOf("/Pages/Forms/PageFolderView.aspx")!=-1 && link.href.indexOf("?")==-1)
       {
          var span = link.parentNode;
          span.className = "ms-hidden";
          span = span.nextSibling;
          if( span.nodeValue == "\n" )
      	      span = span.nextSibling;
          span.className = "ms-hidden";
       }
    }
}

// truncate a list item
function TruncateListItem( length, id )
{
    var listItem = document.getElementById(id);
    
    if( listItem.innerHTML.length > length )
    {
        // truncate the list item
        var summary = listItem.innerHTML.substr(0, length );
        
        // find the last open and close tags for anchors within the summary
        var lastAnchorOpen = summary.lastIndexOf("<a");
        var lastAnchorClose = summary.lastIndexOf("</a");
        
        // if the there is an open anchor tag without a corresponding close tag
        if( lastAnchorOpen > lastAnchorClose )
        {
            // find the close tag and expand the summary to include it
            var remainder = listItem.innerHTML.substr( length );
            var anchorClose = remainder.indexOf("</a");
            remainder = remainder.substr(0, anchorClose + 4);
            summary = summary.concat(remainder);
        }
        listItem.innerHTML = summary.concat("...");
    }
}

// inserts an image tag based upon the query string for the page
function InsertPageFolderViewHeader()
{
    var query = window.location.search.substring(1);
    var params = query.split('&');
    for (var i=0; i<params.length; i++) 
    {
        // find the RootFolder parameter
        var index = params[i].indexOf("=");
        var key = params[i].substr(0, index);
        var value = params[i].substr(index+1);
        if( key == "RootFolder" )
        {
            // find name of page folder being viewed
            value = unescape(value);
            value = value.substr( value.lastIndexOf("/")+1);

	    // output an image tag with the correct image name and title
            var imagename = value.toLowerCase();
            imagename = imagename.replace(" ","");
            document.write( "<img src='/_layouts/images/River%20Guardians/header_" + imagename + ".jpg' alt='" + value + "'/>" );
        }
    }
}
				 
/*
 * Original from: http://brainerror.net/scripts/javascript/blendtrans/demo.html
 *
 * Edits by ASC:
 *   - Removed unnecessary OO code that caused MSIE to choke
 *   - Added pause between setting bg image and setting opacity to zero to
 *     prevent MSIE image flash
 *   - Increased opacity check argument to 103 for completely smooth fade
 *
 */

//find next image
function nextImage(o) {
    do o = o.nextSibling;
    while(o && o.tagName != 'IMG');
    return o;
}

//find first image inside an element
function firstChildImage(o) {
    
    o = o.firstChild;
        
    while(o && o.tagName != 'IMG') {
        o = o.nextSibling;
    }
    
    return o;
}

//set the opacity of an element to a specified value
function setOpacity(obj, o) {

    obj.style.opacity = (o / 100);
    obj.style.MozOpacity = (o / 100);
    obj.style.KhtmlOpacity = (o / 100);
    obj.style.filter = 'alpha(opacity=' + o + ')';
}

//make image invisible and set next one as current image
function getNextImage(image) {
    
    image.style.display = 'none';
    image.style.zIndex = 0;

    next = nextImage(image);

    if( !next )
    {
	// get first image again
	next = firstChildImage(image.parentNode);
    }

    next.style.display = 'block';
    next.style.zIndex = 100;

    return next;
}

//set default values for parameters and starting image
function blendImages(id, speed, pause, caption) {

    if(speed == null) {
        speed = 30;
    }
    
    if(pause == null) {
        pause = 3000;
    }

    var blend = document.getElementById(id);

    var image = firstChildImage(blend);

    startBlending(image, speed, pause, caption);
}

//make image a block-element and set the caption
function startBlending(image, speed, pause, caption) {

    image.style.display = 'block';

    if(caption != null) {
	document.getElementById(caption).innerHTML = image.alt;
    }

    continueFadeImage(image, 0, speed, pause, caption);
}

// ASC: copied from http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm
function pausecomp(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate-date < millis);
} 

//set an increased opacity and check if the image is done blending
function continueFadeImage(image, opacity, speed, pause, caption) {

    opacity = opacity + 3;

    if (opacity < 103) {

	setTimeout(function() {fadeImage(image, opacity, speed, pause, caption)}, speed);

    } else {
	//if the image is done, set it to the background and make it transparent
	image.parentNode.style.backgroundImage = "url("+image.src+")";

	// ASC: pause 1sec here to prevent MSIE image flash ...
	var paws=pause-1000;
	if (paws < 0 ) {
		paws = 0;
	}
	pausecomp(1000);

	setOpacity(image,0);
	//get the next image and start blending it again
	image = getNextImage(image);
	setTimeout(function() {startBlending(image, speed, pause, caption)}, paws);		
    }
}

//set the opacity to a new value and continue the fading
function fadeImage(image, opacity, speed, pause, caption) {
    setOpacity(image,opacity);
    continueFadeImage(image, opacity, speed, pause, caption);
}
				 