﻿// JScript File

addLoadEvent(InitializeButtons);
function InitializeButtons(){
    document.getElementById('monthsUpButton').className = (isAtStart(document,'monthsPage','selectedPage')) ? 'atEnd' : ' ';
    document.getElementById('monthsDownButton').className = (isAtEnd(document,'monthsPage','selectedPage')) ? 'atEnd' : ' ';
    document.getElementById('tagsUpButton').className = (isAtStart(document,'tagsPage','selectedPage')) ? 'atEnd' : ' ';
    document.getElementById('tagsDownButton').className = (isAtEnd(document,'tagsPage','selectedPage')) ? 'atEnd' : ' ';

}

function MonthsUp(){
    moveClassUp(document,'monthsPage','selectedPage'); 
    document.getElementById('monthsUpButton').className = (isAtStart(document,'monthsPage','selectedPage')) ? 'atEnd' : ' ';
    document.getElementById('monthsDownButton').className = (isAtEnd(document,'monthsPage','selectedPage')) ? 'atEnd' : ' ';
}
function MonthsDown(){
    moveClassDown(document,'monthsPage','selectedPage'); 
    document.getElementById('monthsUpButton').className = (isAtStart(document,'monthsPage','selectedPage')) ? 'atEnd' : ' ';
    document.getElementById('monthsDownButton').className = (isAtEnd(document,'monthsPage','selectedPage')) ? 'atEnd' : ' ';
}


function TagsUp(){
    moveClassUp(document,'tagsPage','selectedPage'); 
    document.getElementById('tagsUpButton').className = (isAtStart(document,'tagsPage','selectedPage')) ? 'atEnd' : ' ';
    document.getElementById('tagsDownButton').className = (isAtEnd(document,'tagsPage','selectedPage')) ? 'atEnd' : ' ';
}
function TagsDown(){
    moveClassDown(document,'tagsPage','selectedPage'); 
    document.getElementById('tagsUpButton').className = (isAtStart(document,'tagsPage','selectedPage')) ? 'atEnd' : ' ';
    document.getElementById('tagsDownButton').className = (isAtEnd(document,'tagsPage','selectedPage')) ? 'atEnd' : ' ';
}


// Finds all elements with the specified cssClass applied. 
// Of those elements, it looks for an element with an additional 'selected' css class applied.
// That 'selected' css class is removed from the current element, and applied to the previous matching element.
// This can be used to create a 'paging' effect. Apply the same css class to each element that represents a page, 
// and apply an additonal css class to the selected page. class="tagsListing selectedPage"
// Then use this like moveClassUp(document,'tagsListing','selectedPage')
function moveClassUp(parentElement, cssClass, selectedCssClass){
    var matches = getElementsByClassName(parentElement,"*",cssClass);
    var index = getIndexOfMatch(matches,selectedCssClass);

    if (index > 0){
        removeClassName(matches[index],selectedCssClass);
        addClassName(matches[index -1],selectedCssClass);
    }
            
    if ((index == -1) && (matches.length > 0))
        addClassName(matches[0],selectedCssClass);
    
}


function moveClassDown(parentElement, cssClass, selectedCssClass){
    var matches = getElementsByClassName(parentElement,"*",cssClass);
    var index = getIndexOfMatch(matches,selectedCssClass);

    if ((index >= 0) && (index < matches.length - 1 )){
        removeClassName(matches[index],selectedCssClass);
        addClassName(matches[index +1],selectedCssClass);
    }
            
    if ((index == -1) && (matches.length > 0))
        addClassName(matches[0],selectedCssClass);       
}

//Returns true if the last match is the currently selected one.
function isAtEnd(parentElement, cssClass, selectedCssClass){
    var matches = getElementsByClassName(parentElement,"*",cssClass);
    var index = getIndexOfMatch(matches,selectedCssClass);
    return (index == matches.length -1);
}
//Returns true if the first match is the currently selected one.
function isAtStart(parentElement, cssClass, selectedCssClass){
    var matches = getElementsByClassName(parentElement,"*",cssClass);
    var index = getIndexOfMatch(matches,selectedCssClass);
    return (index == 0);
}

function getIndexOfMatch(elementArray, cssClass){
    for (var i=0;i<elementArray.length; i++){
        if (containsClassName(elementArray[i],cssClass))
            return i;
        
    }
    return -1; 
}


//Returns true if the specified element contains the specified CSS class
function containsClassName(oElm, strClassName){
    strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	return oRegExp.test(oElm.className);
}

/*
	Copyright Robert Nyman, http://www.robertnyman.com
	Free to use if this text is included
	http://www.robertnyman.com/js/ej.js
*/
// ---
/*function $(strId){
	return document.getElementById(strId);
}*/
// ---
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}
// ---
function addClassName(oElm, strClassName){
	var strCurrentClass = oElm.className;
	if(!new RegExp(strClassName, "i").test(strCurrentClass)){
		oElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
	}
}
// ---
function removeClassName(oElm, strClassName){
	var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
	oElm.className = oElm.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
}
// ---