	var onloadFlag = false;
	function setLoadFlag(){onloadFlag = true;}
	if(window.addEventListener){window.addEventListener('load', setLoadFlag, false);}

/*  Subset of: Condenet Slider Version 1.0 - esmiling@condenet.com  */
	function Slider(sliderArgs){
		this.container = document.getElementById(sliderArgs.containerId);
		this.contentWrapper = getElements('content_wrapper','DIV',this.container)[0];
		this.contentLists = new Array();
		this.args = sliderArgs;
		this.setArgs();
		this.getData();
	}
    Slider.prototype.setArgs = function(){
        this.groupSize = this.args.groupSize;
        this.direction = this.args.direction;
		this.dataFormat = this.args.dataFormat || 'document';
		this.dataObj = this.args.dataObj || null;
		this.dataPath = this.args.dataPath || null;
        this.hasLoop = this.args.hasLoop || false;
		this.effectType = this.args.effectType || false;
		this.speed = this.args.speed || 7;
		this.itemWidth = this.args.itemWidth;
    }
	Slider.prototype.getData = function(){
		var slider = this;
		switch(this.dataFormat){
			case 'document':
				this.contentLists = this.container.getElementsByTagName('UL');
				this.initializeBehavior();
				return;
			case 'ajaxJson':
				var xhr = this.getXHR();
				xhr.onreadystatechange = function() {
					if (xhr.readyState == 4) {
						if (xhr.status == 200){
							var jsonData = eval('(' + xhr.responseText + ')');
							slider.makeList(jsonData);
							slider.initializeBehavior();
							slider.handleImageRendering();
						}
					}
				}
				xhr.open("GET", this.dataPath);
				xhr.send(null);
				return;
				
			case 'json':
				var jsonData = this.dataObj;;
				slider.makeList(jsonData);
				slider.initializeBehavior();
				slider.handleImageRendering();	
		}
	}
	Slider.prototype.makeList = function(dataSource){
		this.contentData = dataSource;
		this.contentImagePaths = new Array();
		this.contentImageEls = new Array();
		this.contentImageCredits = new Array();
		var contentListEl = document.createElement('UL');
		for(var i=0; i<this.contentData.items.length; i++){
			var dataItem = this.contentData.items[i];
			var listItem = document.createElement('LI');
			if(dataItem.imagePath){
				var imgContainer = document.createElement('SPAN');
				imgContainer.className = 'image_data';
				var img = document.createElement('IMG');
				if(dataItem.path){
					var imgLink = document.createElement('A');
					imgLink.href = dataItem.path;
					imgLink.appendChild(img);
					imgContainer.appendChild(imgLink);
				}
				else imgContainer.appendChild(img);
				dataItem.imageCredit ? this.contentImageCredits.push(dataItem.imageCredit): this.contentImageCredits.push('');
				this.contentImagePaths.push(dataItem.imagePath);
				this.contentImageEls.push(img);
				listItem.appendChild(imgContainer);
			}
			contentListEl.appendChild(listItem);
		}
		this.contentList = contentListEl;
		this.contentLists.push(this.contentList);
		this.contentWrapper.appendChild(this.contentList);
	}
	Slider.prototype.getXHR = function(){
		XHR = new Object();
		XHR._attempts = [
			function() { return new XMLHttpRequest() },
			function() { return new ActiveXObject("Msxml2.XMLHTTP") },
			function() { return new ActiveXObject("Microsoft.XMLHTTP") }
		];
		XHR._attempt = null;
		XHR.newRequest = function() {
			if(XHR._attempt != null) return XHR._attempt();
			for(var i=0; i<XHR._attempts.length; i++) {
				try{
					var attempt = XHR._attempts[i];
					var request = attempt();
					if (request != null){
						XHR._attempt = attempt;
						return request;
					}
				}
				catch(e){continue;}
			}
			XHR._attempt = function(){throw new Error("XMLHttpRequest not supported");}
			XHR._attempt();
		}
		return XHR.newRequest();
	}
	Slider.prototype.handleImageRendering = function(){
		var slider = this;
		for(var i=0; i<slider.groupSize; i++)
			if(slider.contentImagePaths[i] && slider.contentImageEls[i])
				slider.contentImageEls[i].src = slider.contentImagePaths[i]
		var initialPreloader = function(){
			var group2Start = slider.groupSize;
			var group4Start = slider.groupSize *3;
			for(var i=group2Start; i<group4Start; i++)
				if(slider.contentImagePaths[i] && slider.contentImageEls[i])
					slider.contentImageEls[i].src = slider.contentImagePaths[i]
		}
		var lookAheadPreloader = function(){
			var activeGroup = slider.activeGroup;
			var groupFetch = 2;
			var groupToLoad = activeGroup + groupFetch;
			var groupSize = slider.groupSize;
			var firstImageIndex = (groupToLoad-1)*groupSize;
			var cutoff = firstImageIndex + groupSize;
			for(var i=firstImageIndex; i<cutoff; i++)
				if(slider.contentImagePaths[i] && slider.contentImageEls[i])
					slider.contentImageEls[i].src = slider.contentImagePaths[i]
		}
		if(window.addEventListener){
			if(onloadFlag) initialPreloader()
			else window.addEventListener('load', initialPreloader, false);
			if(this.nextTrigger) this.nextTrigger.addEventListener('click', lookAheadPreloader, false);
		}
		if(window.attachEvent){
			window.attachEvent('onload', initialPreloader);
			if(this.nextTrigger) this.nextTrigger.attachEvent('onclick', lookAheadPreloader);
		}
	}
    Slider.prototype.initializeBehavior = function(){
		this.setList(0); 
		this.scanList();
        this.activeGroup = 1;
        this.assembleControls();
        this.updateControlDisplay();
        this.setupLayout();
		this.setupRolloverEffect();
        this.speed = this.calculateSpeed(this.speed);
    }
    Slider.prototype.setList = function(context_index){this.contentList = this.contentLists[context_index];}
	Slider.prototype.scanList = function(){
	    this.contentItems = this.contentList.getElementsByTagName('LI');
        this.numItems = this.contentItems.length;
        this.numGroups = Math.ceil(this.numItems/this.groupSize);
        this.itemWidth = this.getItemWidth();
        this.itemHeight = this.getItemHeight();
	}
    Slider.prototype.assembleControls = function(){
        var slider = this;
        if( this.numGroups > 1 ){
            this.nextTrigger = this.createTrigger('slide_control_next');
            this.prevTrigger = this.createTrigger('slide_control_previous');
            if(window.addEventListener){
                this.nextTrigger.addEventListener("click", function(event){ slider.advance(1); }, false);
                this.prevTrigger.addEventListener("click", function(event){ slider.retract(1); }, false);
            }
            if(window.attachEvent){
                this.nextTrigger.attachEvent("onclick", function(event){ slider.advance(1); });
                this.prevTrigger.attachEvent("onclick", function(event){ slider.retract(1); });
            }
        }
		if( this.numGroups == 1 ){
			this.nextTrigger = this.createTrigger('slide_control_next next_disabled');
            this.prevTrigger = this.createTrigger('slide_control_previous previous_disabled');
		}
    }
    Slider.prototype.createTrigger = function(){
        var triggerClass = arguments[0];
        var control = document.createElement('A');
        control.className = triggerClass;
        this.container.appendChild(control);
        return control;
    }
    Slider.prototype.updateControlDisplay = function(){
        if( this.numGroups <=1 ) return;
        if( !this.hasLoop ){
        this.activeGroup == 1 ? this.prevTrigger.className += ' previous_disabled' : this.prevTrigger.className = 'slide_control_previous';
        this.activeGroup == this.numGroups ? this.nextTrigger.className += ' next_disabled' : this.nextTrigger.className = 'slide_control_next';
        }
    }
    Slider.prototype.setupLayout = function(){
		this.groupWidth = this.setContentWrapperWidth();
		this.setScrollingListWidth();
    }
    Slider.prototype.getItemWidth = function(){
		if(this.itemWidth) return this.itemWidth;
		else return this.contentItems[0].offsetWidth;
	}
    Slider.prototype.getItemHeight = function(){
		if(this.itemHeight) return this.itemHeight;
		else return this.contentItems[0].offsetHeight;
	}
    Slider.prototype.calcuateScrollingListWidth = function(){
		return this.numItems * this.itemWidth;
	}
    Slider.prototype.setScrollingListWidth = function(){
		var activeListWidth = this.calcuateScrollingListWidth();
		activeListWidth += 100;
        this.contentList.style.width = activeListWidth + 'px';
    }
    Slider.prototype.setContentWrapperWidth = function(){
        //this.contentWrapper.style.width = (this.itemWidth * this.groupSize) + 'px'; coded in css for this case. will revisit later to make dynamic for varying numbers of items
        return (this.itemWidth * this.groupSize)
    }
    Slider.prototype.setContentWrapperHeight = function(){    
        this.contentWrapper.style.height = (this.itemHeight * this.groupSize) + 'px';
        return (this.itemHeight * this.groupSize)
    }
    Slider.prototype.getPositionLeft = function(){
        var leftValue = parseInt(this.contentList.style.left) || 0;
        return leftValue;
    }
    Slider.prototype.getPositionTop = function(){
        var topValue = parseInt(this.contentList.style.top) || 0;
        return topValue;
    }
/*  Event Handlers:
    --------------- */
    Slider.prototype.advance = function(freq){    
        if( this.activeGroup == this.numGroups && !this.hasLoop ) return;
        var currentPosition = (this.direction == 'horizontal' ? this.getPositionLeft() : this.getPositionTop());
        var groupDimension = (this.direction == 'horizontal' ? this.groupWidth : this.groupHeight);
        if( currentPosition != 0 && Math.abs(currentPosition) % groupDimension != 0 ) return;
        var numScroll = freq || 1;
        var destinationPosition = currentPosition - (groupDimension * numScroll);
        var cssDirection = (this.direction == 'horizontal' ? 'left' : 'top');
        var contentList = this.contentList;
        var speed = this.speed;
        var slider = this;
        if( this.activeGroup == this.numGroups ){this.retract(this.numGroups - 1); return;}
        this.activeGroup = this.activeGroup + numScroll;
        if(this.effectType) this.effectStart();
        var clearer = setInterval(function(){
                if(currentPosition - speed >= destinationPosition){
                    contentList.style[cssDirection] = (currentPosition-speed) + 'px';
                    currentPosition = currentPosition - speed;
                }
                if(currentPosition == destinationPosition){
                    clearInterval(clearer);
                    if( slider.effectType ) slider.effectEnd();
                }
        },10);
        this.updateControlDisplay();
    }
    Slider.prototype.retract = function(freq){
        if( this.activeGroup == 1 && !this.hasLoop ) return;
        var currentPosition = (this.direction == 'horizontal' ? this.getPositionLeft() : this.getPositionTop());
        var groupDimension = (this.direction == 'horizontal' ? this.groupWidth : this.groupHeight);
        if( Math.abs(currentPosition) % groupDimension != 0 ) return;
        if( this.activeGroup == 1){ this.advance(this.numGroups - 1); return;}
        var numScroll = freq || 1;
        var destinationPosition = currentPosition + (groupDimension * numScroll);
        var cssDirection = (this.direction == 'horizontal' ? 'left' : 'top');
        var contentList = this.contentList;
        var speed = this.speed;
        var slider = this;
        this.activeGroup = this.activeGroup - numScroll;
        if( this.effectType) this.effectStart();
        var clearer = setInterval(function(){
                if(currentPosition + speed <= destinationPosition){
                    contentList.style[cssDirection] = (currentPosition + speed) + 'px';
                    currentPosition = currentPosition + speed;
                }
                if(currentPosition == destinationPosition){
                    clearInterval(clearer);
                    if( slider.effectType ) slider.effectEnd();
                }
        },10);
        
        this.updateControlDisplay();
    }
    Slider.prototype.calculateSpeed = function(requestedSpeed){
        var adjustment = requestedSpeed - 1;
        var factors = new Array();
        var groupDimension = (this.direction == 'horizontal' ? this.groupWidth : this.groupHeight);
        for(var i=1; i<=groupDimension/2; i++)
            if( groupDimension%i == 0) factors.push(i);
        return adjustment < factors.length ? factors[adjustment] : factors[factors.length-1];
    }
    Slider.prototype.effectStart = function(){
        for(var i=0; i<this.contentItems.length; i++){
            var item = this.contentItems[i];
            var imageData = item.getElementsByTagName('IMG')[0];
            var textData = getElements('text_data','SPAN',item)[0];
            var itemTitle = getElements('item_title','SPAN',item)[0];
            if(imageData) imageData.style.visibility = 'hidden'
            if(textData) setCssOpacity(textData, 0);
            if(itemTitle) setCssOpacity(itemTitle,0);
        }
    }
    Slider.prototype.effectEnd = function(){
        var firstItemIndex = (this.activeGroup-1) * this.groupSize;
        var lastItemIndex = (firstItemIndex + (this.groupSize - 1) < this.contentItems.length ? firstItemIndex + (this.groupSize-1) : this.contentItems.length - 1);
        var currentItemIndex = firstItemIndex;
        var slider= this;
        var fade_clearer = setInterval(function(){
            var item = slider.contentItems[currentItemIndex];
            var imageData = item.getElementsByTagName('IMG')[0];
            if(imageData){
                setCssOpacity(imageData,0);
                imageData.style.visibility = 'visible';
                fade(imageData,7);
            }
            currentItemIndex < (lastItemIndex) ? currentItemIndex++ : clearInterval(fade_clearer);
        },50)
    }
    Slider.prototype.effectReset = function(){
        for(var i=0; i<this.contentItems.length; i++){
            var item = this.contentItems[i];
            var imageData = item.getElementsByTagName('IMG')[0];
            if(imageData) imageData.style.visibility = ''
        }
    }
	Slider.prototype.setupRolloverEffect = function(){
		var slider = this;
		for(var i=0; i<this.contentItems.length; i++){
			this.contentItems[i].onmouseover = listItemMouseover;
			this.contentItems[i].onmouseout = listItemMouseout;
		}
		function listItemMouseover(event){
			var hoveredListItem = this;
			var nextSlideIndex = slider.activeGroup*slider.groupSize;
			var firstSlideIndex = nextSlideIndex - slider.groupSize;
			for(var i=0; i<slider.contentItems.length; i++){
				if(hoveredListItem == slider.contentItems[i]){
					var currentSlideIndex = i;
					break;
				}
			}
			if(slider.currentTip) return;
			if(currentSlideIndex >= nextSlideIndex) return;
			slider.contentWrapper.className = "stretched content_wrapper";
			hoveredListItem.className = "over";
			var tipText = '';
			for(var i=0; i<slider.contentItems.length; i++){
				if(slider.contentItems[i] == hoveredListItem){
					tipText = slider.contentImageCredits[i];
					break;
				}
			}
			var slideIndex = i;
			var slidePosition = i%slider.groupSize + 1;
			var tipClass = 'carousel_tooltip ' + 'tip_' + slidePosition;
			var tip = new Tooltip(tipClass);
			tip.show(slider.container, tipText);
			slider.currentTip = tip;
			tip.tipEl.onmouseover = function(event){
				var evt = event || window.event;
				var prevElement = evt.relatedTarget || evt.fromElement;
			}
			tip.tipEl.onmouseout = function(event){
				var evt = event || window.event;
				var nextElement = evt.relatedTarget || evt.toElement;
				if(nextElement != this && nextElement != hoveredListItem && !isDescendantOf(hoveredListItem, nextElement)){
					slider.contentWrapper.className = "content_wrapper";
					hoveredListItem.className = "";
					if(slider.currentTip){
						slider.currentTip.hide();
						delete slider.currentTip;
					}
				}
			}
		}
		function listItemMouseout(event){
			var evt = event || window.event;
			var nextElement = evt.relatedTarget || evt.toElement;
			if(slider.currentTip)
				if(nextElement == slider.currentTip.tipEl) return;
			slider.contentWrapper.className = "content_wrapper";
			this.className = "";
			if(slider.currentTip){
				slider.currentTip.hide();
				delete slider.currentTip;
			}
		}
	}
	function Tooltip(tipclass){
		this.tipEl = document.createElement('span');
		this.tipEl.className = tipclass;
	}
	Tooltip.prototype.show = function(parentElement, tipText){
		this.tipEl.innerHTML = tipText;
		if(this.tipEl.parentNode != parentElement) parentElement.appendChild(this.tipEl);
	}
	Tooltip.prototype.hide = function(){
		this.tipEl.parentNode.removeChild(this.tipEl);
	}
    function getElements(classname, tagname, root){
        if(!root) root = document;
        else if (typeof root == "string") root = document.getElementById(root);
        if(!tagname) tagname = "*";
        var all = root.getElementsByTagName(tagname);
        if(!classname) return all;
        var elements = [];
        for(var i = 0; i < all.length; i++){
            var element = all[i];
            if(isMember(element,classname))
                elements.push(element)
        }
        return elements;
    }
    function isMember(element, classname){
        var classes = element.className;
        if(!classes) return false;
        if(classes ==classname) return true;
        var whitespace = /\s+/;
        if (!whitespace.test(classes)) return false;
        var c = classes.split(whitespace);
        for(var i = 0; i < c.length; i++)
            if (c[i] == classname) return true;
        return false;
    }
    function setCssOpacity(targetedElement, opacity) {
        opacity = (opacity == 100)?99.999:opacity;
        targetedElement.style.filter = "alpha(opacity: "+opacity+")";
        targetedElement.style.opacity = opacity/100;
    }
    function fade(fadeElement, fadeSpeed){
        var fadeSpeed =  fadeSpeed || 9;
        var opacity = 0;
        var clearer = setInterval(function(){
            setCssOpacity(fadeElement, opacity);
            opacity < 99 ? opacity+=fadeSpeed : clearInterval(clearer);
        },10)
    }
function isDescendantOf(ancestor, descendant){
	if(!ancestor || !descendant) return;
	var similarDescendants = ancestor.getElementsByTagName(descendant.nodeName);
	for(var i=0; i<similarDescendants.length; i++)
		if( similarDescendants[i] == descendant ) return true;
	else{ return false;}
}
