YAHOO.namespace("EU.widget");

YAHOO.EU.widget.Carousel = function(carouselElementID, carouselCfg) {
 		this.init(carouselElementID, carouselCfg);
	}

YAHOO.EU.widget.Carousel.prototype = {

	
	init: function(carouselElementID, carouselCfg) {

 		this.carouselElemID = carouselElementID;
 		this.carouselElem = YAHOO.util.Dom.get(carouselElementID);


 		this.start = 1;
 		this.prevEnabled = true;
 		this.nextEnabled = true;
 		
 		// Create the config object
 		this.cfg = new YAHOO.util.Config(this);

		this.cfg.addProperty("numVisible", { 
				value:3, 
				suppressEvent:true
		} );
		this.cfg.addProperty("animationSpeed", { 
				value:.25, 
				suppressEvent:true
		} );
		this.cfg.addProperty("animationMethod", { 
				value:  YAHOO.util.Easing.easeOut, 
				suppressEvent:true
		} );
		this.cfg.addProperty("scrollInc", { 
				value:3, 
				suppressEvent:true
		} );
		this.cfg.addProperty("size", { 
				value:1000000, 
				suppressEvent:true
		} );
		this.cfg.addProperty("loadInitHandler", { 
				value:null, 
				suppressEvent:true
		} );
		this.cfg.addProperty("loadNextHandler", { 
				value:null, 
				suppressEvent:true
		} );
		this.cfg.addProperty("loadPrevHandler", { 
				value:null, 
				suppressEvent:true
		} );
		this.cfg.addProperty("prevButtonStateHandler", { 
				value:null, 
				suppressEvent:true
		} );
		this.cfg.addProperty("nextButtonStateHandler", { 
				value:null, 
				suppressEvent:true
		} );
		/*LC Change*/
		this.cfg.addProperty("showPagination", { 
				value:false, 
				suppressEvent:true
		} );
		
		this.cfg.addProperty("numPages", { 
				value:null, 
				suppressEvent:true
		} );
		this.cfg.addProperty("currentPage", { 
				value:1, 
				suppressEvent:true
		} );		
		this.cfg.addProperty("nextConfig", { 
				value:null, 
				suppressEvent:true
		} );  
		this.cfg.addProperty("prevConfig", { 
				value:null, 
				suppressEvent:true
		} );
		this.cfg.addProperty("navConfig", { 
				value:null, 
				suppressEvent:true
		} );
		/*END LC Changes */
		this.cfg.addProperty("autoPlay", { 
				value:0, 
				suppressEvent:true
		} );
		this.cfg.addProperty("wrap", { 
				value:false, 
				suppressEvent:true
		} );
		
 		if(carouselCfg) {
 			this.cfg.applyConfig(carouselCfg);
 		}
 		
 		this.numVisible = this.cfg.getProperty("numVisible");
 		this.scrollInc = this.cfg.getProperty("scrollInc");
		this.navMargin = this.cfg.getProperty("navMargin");
 		this.animSpeed = this.cfg.getProperty("animationSpeed");
		this.initHandler = this.cfg.getProperty("loadInitHandler");
		this.size = this.cfg.getProperty("size");
		this.animationMethod = this.cfg.getProperty("animationMethod");
		/*LC Change*/
		this.nextConfig = this.cfg.getProperty('nextConfig');
		this.prevConfig = this.cfg.getProperty('prevConfig');
		this.navConfig = this.cfg.getProperty('navConfig');		
		this.showPagination = this.cfg.getProperty("showPagination");
		this.numPages = this.cfg.getProperty('numPages');
		this.currentPage = this.cfg.getProperty('currentPage');
		/*END LC Change */
		this.wrap = this.cfg.getProperty("wrap");
		this.autoPlay = this.cfg.getProperty("autoPlay");
		this.autoPlayTimer = null;
 		
//		this.calculateDimensions();
		if((this.showPagination) && (this.size!=1000000)) { //for finite scrolling
			this.numPages = parseInt(this.size/this.scrollInc);
		}

		/* LC Change*/
		 this.renderElements();
		/*END LC Change */		 
		 
 		// Set up event handlers
 		this.scrollNextParams = { points: { by: [-this.scrollAmountPerInc*this.scrollInc, 0] } };
 		this.scrollPrevParams = { points: { by: [ this.scrollAmountPerInc*this.scrollInc, 0] } };
 		
 		this.scrollNextAnim = new YAHOO.util.Motion(this.carouselList, this.scrollNextParams, 
   								this.animSpeed, this.animationMethod);
 		this.scrollPrevAnim = new YAHOO.util.Motion(this.carouselList, this.scrollPrevParams, 
   								this.animSpeed, this.animationMethod);
			
		
		
		if (isValidObj(this.carouselNext)) {
			YAHOO.util.Event.addListener(this.carouselNext, "click", this._scrollNext, this);
		}
		if (isValidObj(this.carouselPrev)) {
			YAHOO.util.Event.addListener(this.carouselPrev, "click", this._scrollPrev, this);
		}	
		// --- Event Handling
		
		// for now wire up handlers in our class... this will wire to handlers passed into config
		if(isValidObj(this.initHandler)) {
			this.loadInitialEvt = new YAHOO.util.CustomEvent("onLoadInit", this);
			this.loadInitialEvt.subscribe(this.initHandler, this);
		}
		this.nextHandler = this.cfg.getProperty("loadNextHandler");
		if(isValidObj(this.nextHandler)) {
			this.loadNextEvt = new YAHOO.util.CustomEvent("onLoadNext", this);
			this.loadNextEvt.subscribe(this.nextHandler, this);
		}
		this.prevHandler = this.cfg.getProperty("loadPrevHandler");
		if(isValidObj(this.prevHandler)) {
			this.loadPrevEvt = new YAHOO.util.CustomEvent("onLoadPrev", this);
			this.loadPrevEvt.subscribe(this.prevHandler, this);
		}
		this.prevButtonStateHandler = this.cfg.getProperty("prevButtonStateHandler");
		if(isValidObj(this.prevButtonStateHandler)) {
			this.prevButtonStateEvt = new YAHOO.util.CustomEvent("onPrevButtonStateChange", this);
			this.prevButtonStateEvt.subscribe(this.prevButtonStateHandler, this);
		}
		this.nextButtonStateHandler = this.cfg.getProperty("nextButtonStateHandler");
		if(isValidObj(this.nextButtonStateHandler)) {
			this.nextButtonStateEvt = new YAHOO.util.CustomEvent("onNextButtonStateChange", this);
			this.nextButtonStateEvt.subscribe(this.nextButtonStateHandler, this);
		}
		
  		YAHOO.util.Event.onAvailable(this.carouselElemID + "-item-1", this.update, this,true);  		
		this.loadInitial();	
	},
	
	/*LC Change
	 * All HTML elements are either created here or retrieved from DOM here.
	 * Moved from init function
	 * END LC Change
	 */
	
	renderElements:function() {
		
		
		var carouselListClass = "carousel-list";
 		var carouselNextClass = "carousel-next";
 		var carouselPrevClass = "carousel-prev";
 		var carouselClipRegionClass = "carousel-clip-region";
 		
 		this.carouselList = YAHOO.util.Dom.getElementsByClassName(carouselListClass, 
												"ul", this.carouselElem)[0];
		this.clipReg = YAHOO.util.Dom.getElementsByClassName(carouselClipRegionClass, 
												"div", this.carouselElem)[0];
		
		/* LC Change
		 * add a 'js' enabled class to the clip region
		 */
		YAHOO.util.Dom.addClass(this.clipReg,carouselClipRegionClass+"-js");
		/*END LC Change*/
		
		this.carouselNext = createImgLink(this.nextConfig);


		this.carouselPrev = createImgLink(this.prevConfig);
		

		if (isValidObj(this.carouselNext)) {
			YAHOO.util.Dom.addClass(this.carouselNext,carouselNextClass);
			this.carouselElem.appendChild(this.carouselNext);
		}
		if (isValidObj(this.carouselPrev)) {
			YAHOO.util.Dom.addClass(this.carouselPrev,carouselPrevClass);		
			this.carouselElem.appendChild(this.carouselPrev);
		}

		
	},
	
	renderPagination:function() {
		if(!isValidObj(this.navConfig)) {
			return false;
		}
		//if textual separator is found create textual info 
		if(this.navConfig.sSep) { 
			
			if (!this.carouselPagination) {
				var carouselPaginationClass = "carousel-page";
				this.carouselPagination = document.createElement('p');
				YAHOO.util.Dom.addClass(this.carouselPagination,carouselPaginationClass);
			}
			else { //otherwise remove all children
				this.carouselPagination.innerHTML = '';

			}
		}
		else { //create img based
			//create elem if not done yet
			if (!this.carouselPagination) {
				var carouselPaginationClass = "carousel-page";
				this.carouselPagination = document.createElement('ul');
				YAHOO.util.Dom.addClass(this.carouselPagination,carouselPaginationClass);
			}
			else { //otherwise remove all children
				while(this.carouselPagination.hasChildNodes()) {
				this.carouselPagination.removeChild(this.carouselPagination.lastChild)	
				}
			}	
				
			//repopulate carouselPagination

			var navConfigCpy= {}
			for (i in this.navConfig) {
		        navConfigCpy[i] = this.navConfig[i];
    		}

			var sAlt= navConfigCpy.sAlt;	

			for(var i=0;i<this.numPages;i++) {
				var li = document.createElement('li');
				var navConfig = navConfigCpy;
	
				navConfigCpy.sAlt = sAlt.replace('?',(i+1));;

				
				var el = createImgLink(navConfigCpy);
				
				
				var idx = i;
				var handler = function(e,oArgs){ 
	
					var pg = oArgs.index; //0-indexed
					oArgs.o.scrollToPage(pg);
					oArgs.o.stopAutoPlay();	
					YAHOO.util.Event.stopEvent(e);
					
					return false;
				};
				
				this.enableNav(el,handler,idx);
				
				li.appendChild(el);
	
				this.carouselPagination.appendChild(li);
		
			}
			navConfigCpy = null;
			
		}
		this.carouselElem.appendChild(this.carouselPagination);
		this.updatePagination(this.currentPage);
		
		
	},
	enableNav: function(el,func,idx) {
		YAHOO.util.Event.addListener(el,'click',func,{o:this,index:idx},true);
	},
	
	disableNav: function(el,func,idx) {
		YAHOO.util.Event.removeListener(el,'click',func,{o:this,index:idx});
	},
	
	/*LC Change 
	 * Given an index, calcualte what page is on
	 * */
	calculatePage : function(newStart) {
//		window.log((newStart/(this.scrollInc))+1);
	
	},
	/*END LC Change*/
	
	/* LC Changed 
	 * Calculates number of pages
	 * Removes carproto from document after metrics have been calculated so 
	 * elements are obsolete
	 * 
	 */
	calculateDimensions: function() {
		
 		// create a proto set of elements for getting the margin & border widths
 		// create the same hierarchy... everything is under .carousel-component
 		var carproto = document.createElement("div");
 		YAHOO.util.Dom.addClass(carproto, "carousel-component");
 		
 		var divproto = document.createElement("div");
 		carproto.appendChild(divproto);
 		var aproto = document.createElement("a");
 		var iproto = document.createElement("img");
 		var liproto = document.createElement("li");
 		YAHOO.util.Dom.addClass(divproto, "carousel-list");
 		divproto.appendChild(liproto);
 		liproto.appendChild(aproto);
 		aproto.appendChild(iproto);
 		YAHOO.util.Dom.setStyle(divproto, "visibility", "hidden")
 		document.body.appendChild(carproto);		

		this.aBorderWidth = parseInt(YAHOO.util.Dom.getStyle(aproto, "borderLeftWidth"));
		this.iBorderWidth = parseInt(YAHOO.util.Dom.getStyle(iproto, "borderRightWidth"))
		this.liMargRight  = parseInt(YAHOO.util.Dom.getStyle(liproto, "marginRight"));
		this.itemWidth = this.thumbnailWidth + this.aBorderWidth*2 + this.iBorderWidth*2 + this.liMargRight;
		this.liWidth = this.itemWidth - this.liMargRight;
		this.clipWidth = this.itemWidth * this.numVisible;

		/*LC Change */
		if((this.showPagination) && (this.size!=1000000)) { //for finite scrolling
			this.numPages = parseInt(this.size/this.scrollInc);
		}
		/* END LC Change*/
 		this.scrollAmountPerInc = this.itemWidth;
	
		/* LC Change*/
 		document.body.removeChild(carproto);
 		/* END LC Change*/

	}, 
	update: function() {
		this.firstElementIsLoaded();
	},
	
	firstElementIsLoaded: function() {
		var ulKids = this.carouselList.childNodes;
 		var li = null;
		for(var i=0; i<ulKids.length; i++) {
			li = ulKids[i];
			if(li.tagName == "LI" || li.tagName == "li") {
				break;
			}
		}
		
		var liPaddingWidth = parseInt(YAHOO.util.Dom.getStyle(li, "paddingLeft")) + 
					parseInt(YAHOO.util.Dom.getStyle(li, "paddingRight")) + 
					parseInt(YAHOO.util.Dom.getStyle(li, "marginLeft")) + 
					parseInt(YAHOO.util.Dom.getStyle(li, "marginRight"));
		
		this.scrollAmountPerInc = (li.offsetWidth+liPaddingWidth);
		this.itemWidth = li.offsetWidth;
		if (this.showPagination) {
			this.calibrateUI();
		}
		
		YAHOO.util.Dom.setStyle(this.carouselElem, "visibility", "visible");
	},
	
	startAutoPlay: function(interval) {
		// if interval is passed as arg, then set autoPlay to this interval.
		if(isValidObj(interval)) {
			this.autoPlay = interval;
		}
		// if we already are playing, then do nothing.
		if(this.autoPlayTimer !== null) {
			return this.autoPlayTimer;
		}
				
		var oThis = this;  
		var autoScroll = function() { oThis.scrollNext(); };
		var timeoutId = setTimeout( autoScroll, this.autoPlay );
		return timeoutId;
	},

	stopAutoPlay: function() {
		if (this.autoPlayTimer !== null) {
			clearTimeout(this.autoPlayTimer);
			this.autoPlay=0;
			this.autoPlayTimer = null;
		}
	},
	
	scrollNext: function() {
		this._scrollNext(null, this);
		// we know the timer has expired.
		this.autoPlayTimer = null;
		if(this.autoPlay != 0) {
			this.autoPlayTimer = this.startAutoPlay();
		}
	},
	
	scrollPrev: function() {
		this._scrollPrev(null, this);
	},
	
	// scroll to new start with animation
	scrollTo: function(newStart) {
		this.position(newStart, true);
	},
	
	/* LC Change
	 * page is 0-indexed page to scroll to
	 * */
	
	//scroll to page with animation
	scrollToPage:function(page) {
		this.scrollTo((this.scrollInc*(page))+1);
		if(this.showPagination) {
			this.updatePagination(this.currentPage=Math.min(++page,this.numPages));
		}
	},
	/* LC Change
	 * page is 0-indexed page to move to
	 * */
	// move to page, no animation.
	moveToPage: function(page) {
		this.position((this.scrollInc*page)+1, false);
	},
	
	//update pagination*/
	updatePagination : function(page) {
		this.currentPage=page;
		if (isValidObj(this.carouselPagination)) {
			if(this.navConfig.sSep) {  
				var pageInfo = this.currentPage + this.navConfig.sSep + this.numPages;
				this.carouselPagination.innerHTML = (pageInfo); //lazeee!
			}
			else {
				var els = this.carouselPagination.getElementsByTagName('a'); 
				for (var i=0,el;el=els[i];i++) {
					var img = el.getElementsByTagName('img')[0];
					if(img) {
						if (i==(this.currentPage-1)) {
							img.src = this.navConfig.sImgUrl;
						}
						else img.src = this.navConfig.sImgDisabledUrl;
					}
				}
			}
		}
		
	},
	/*END LC Change*/	
	// move to start, no animation.
	moveTo: function(newStart) {
		this.position(newStart, false);
	},
	
	position: function(newStart, showAnimation) {
		// do we bypass the isAnimated check?
		if(newStart > this.start) {
			var inc = newStart - this.start;
			this._scrollNextInc(this, inc, showAnimation);
		} else {
			var dec = this.start - newStart;
			this._scrollPrevInc(this, dec, showAnimation);
		}
	},
	
	// event handler
	_scrollNextPage: function(e, carousel) {

		this._scrollNext(e,carousel);		

	},
	
	// event handler
	_scrollNext: function(e, carousel) {
		if(carousel.scrollNextAnim.isAnimated()) {
			return false; // might be better to set ourself waiting for animation completion and
			// then just do this function. that will allow faster scroll responses.
		}
		// if fired by an event and wrap is set and we are already at end then wrap
		var currEnd = carousel.start + carousel.numVisible-1;
		if(carousel.wrap && currEnd == carousel.size) {
			var currAnimSpeed = carousel.animSpeed;
			carousel.scrollTo(1);
		} else if(e!==null || (currEnd == carousel.size)) { // event fired this so disable autoplay
			carousel.stopAutoPlay();
			carousel._scrollNextInc(carousel, carousel.scrollInc, (carousel.animSpeed != 0));
		} else {
			carousel._scrollNextInc(carousel, carousel.scrollInc, (carousel.animSpeed != 0));
		}
		
		/*LC Change*/
		if (carousel.showPagination) {
			if(carousel.wrap && currEnd == carousel.size) {
			 carousel.updatePagination(1);					
			}
			else {
				carousel.updatePagination(Math.min(++carousel.currentPage,carousel.numPages));
			}
		}
		/*END LC Change*/
		
//		carousel._scrollNextInc(carousel, carousel.scrollInc, (carousel.animSpeed != 0));
		if(isValidObj(e)) {
			YAHOO.util.Event.preventDefault(e);	
		}
		return false;
	},
	
	// probably no longer need carousel passed in, this should be correct now.
	_scrollNextInc: function(carousel, inc, showAnimation) {
		var newEnd = carousel.start + inc + carousel.numVisible - 1;
		newEnd = (newEnd > carousel.size) ? carousel.size : newEnd;
		var newStart = newEnd - carousel.numVisible + 1;
		inc = newStart - carousel.start;
		carousel.start = newStart;
		// if the prev button is disabled and start is now past 1, then enable it
		if((carousel.prevEnabled == false) && (carousel.start > 1)) {
			carousel.enablePrev();
		}
		// if next is enabled && we are now at the end, then disable
		if((carousel.nextEnabled == true) && (newEnd == carousel.size)) {
			carousel.disableNext();
		}
		
		if(inc > 0) {
			if(isValidObj(carousel.nextHandler)) {
				var first = carousel.start;
				var last = 	carousel.start + carousel.numVisible - 1;
				var alreadyCached = carousel.areAllItemsLoaded(first, last);
				carousel.loadNextEvt.fire(first, last, alreadyCached);
			}
			
			if(showAnimation) {
	 			var nextParams = { points: { by: [-carousel.scrollAmountPerInc*inc, 0] } };
 		
	 			carousel.scrollNextAnim = new YAHOO.util.Motion(carousel.carouselList, nextParams, 
   								carousel.animSpeed, carousel.animationMethod);
				carousel.scrollNextAnim.animate();
			} else {
				var currX = YAHOO.util.Dom.getX(carousel.carouselList);
				YAHOO.util.Dom.setX(carousel.carouselList, currX - carousel.scrollAmountPerInc*inc);
			}
			
		}
				
	
		return false;
	},
	
	areAllItemsLoaded: function(first, last) {
		for(var i=first; i<=last; i++) {
			// if item is not loaded then at least one is not loaded
			if(!isValidObj(this.getCarouselItem(i))) {
				return false;
			}
		}
		return true;
	}, 
	
	_scrollPrev: function(e, carousel) {
		if(carousel.scrollPrevAnim.isAnimated()) {
			return false;
		}
		/*LC Change*/
		if (carousel.showPagination) {
			carousel.updatePagination(Math.max(--carousel.currentPage,1));
		}
		/*END LC Change*/
		carousel._scrollPrevInc(carousel, carousel.scrollInc, (carousel.animSpeed != 0));
		if(isValidObj) {
			YAHOO.util.Event.preventDefault(e);
		}
		return false;
	},
	
	_scrollPrevInc: function(carousel, dec, showAnimation) {

		var newStart = carousel.start - dec;
		newStart = (newStart <= 1) ? 1 : (newStart);
		var newDec = carousel.start - newStart;
		carousel.start = newStart;
		
		// if prev is enabled && we are now at position 1, then disable
		if((carousel.prevEnabled == true) && (carousel.start == 1)) {
			carousel.disablePrev();
		}
		// if the next button is disabled and end is < size, then enable it
		if((carousel.nextEnabled == false) && ((carousel.start + carousel.numVisible - 1) < carousel.size)) 		
		{
			carousel.enableNext();
		}

		// if we are decrementing
		if(newDec > 0) {			
			if(isValidObj(carousel.prevHandler)) {
				var first = carousel.start;
				var last = carousel.start + newDec - 1;
				var alreadyCached = carousel.areAllItemsLoaded(first, last);

				carousel.loadPrevEvt.fire(first, last, alreadyCached);
			}
			
			if(showAnimation) {
	 			var prevParams = { points: { by: [carousel.scrollAmountPerInc*newDec, 0] } };
 		
	 			carousel.scrollPrevAnim = new YAHOO.util.Motion(carousel.carouselList, prevParams, 
   								carousel.animSpeed, carousel.animationMethod);
				carousel.scrollPrevAnim.animate();
			} else {
				var currX = YAHOO.util.Dom.getX(carousel.carouselList);
				YAHOO.util.Dom.setX(carousel.carouselList, currX + carousel.scrollAmountPerInc*newDec);
			}
		}
		
		return false;
	},
	
	loadInitial: function() {
		this.start = 1;
		this.disablePrev();
		/* LC Change*/
		this.enableNext();
		/*End LC Change*/
		if(isValidObj(this.initHandler)) {
			this.loadInitialEvt.fire(this.start, this.start + this.numVisible -1);
		}
		
		if(this.autoPlay != 0) {
			this.autoPlayTimer = this.startAutoPlay();
		}	
	},
	
	disablePrev: function() {
		//this.debugMsg("DISABLE PREV");
		this.prevEnabled = false;
		if(isValidObj(this.prevButtonStateEvt)) {
			this.prevButtonStateEvt.fire(false);
		}
		if(this.prevConfig && this.prevConfig.sImgOverUrl) {
			var elImg = this.carouselPrev.getElementsByTagName('img')[0];
			YAHOO.util.Event.purgeElement(elImg,false,'mouseover');
			YAHOO.util.Event.purgeElement(elImg,false,'mouseout');
		}
		YAHOO.util.Event.purgeElement(elImg,false,'click');
//		YAHOO.util.Event.removeListener(this.carouselPrev, "click", this._scrollPrev);
	},
	
	enablePrev: function() {
		//this.debugMsg("ENABLE PREV");
		this.prevEnabled = true;
		if(isValidObj(this.prevButtonStateEvt)) {
			this.prevButtonStateEvt.fire(true);
		}
		if(this.prevConfig && this.prevConfig.sImgOverUrl) {
			var imgUrl = this.prevConfig.sImgUrl;
			var imgOverUrl = this.prevConfig.sImgOverUrl;
			var elImg = this.carouselPrev.getElementsByTagName('img')[0];
			YAHOO.util.Event.addListener(elImg,'mouseover',function(e) {  this.src=imgOverUrl },elImg,true);
			YAHOO.util.Event.addListener(elImg,'mouseout',function(e) {this.src=imgUrl },elImg,true);				
		}
		YAHOO.util.Event.addListener(this.carouselPrev, "click", this._scrollPrev, this);
	},
		
	disableNext: function() {
		//this.debugMsg("DISABLE NEXT");
		this.nextEnabled = false;
		if(isValidObj(this.nextButtonStateEvt)) {
			this.nextButtonStateEvt.fire(false);
		}

		if((!this.wrap) && this.nextConfig && this.nextConfig.sImgOverUrl) {
			var elImg = this.carouselNext.getElementsByTagName('img')[0];
			YAHOO.util.Event.purgeElement(elImg,false,'mouseover');
			YAHOO.util.Event.purgeElement(elImg,false,'mouseout');
		}
		
		YAHOO.util.Event.removeListener(this.carouselNext, "click", this._scrollNext);
	},
	
	enableNext: function() {
		//this.debugMsg("ENABLE NEXT");
		this.nextEnabled = true;
		if(isValidObj(this.nextButtonStateEvt)) {
			this.nextButtonStateEvt.fire(true);
		}
		if(this.nextConfig && this.nextConfig.sImgOverUrl) {
			var imgUrl = this.nextConfig.sImgUrl;
			var imgOverUrl = this.nextConfig.sImgOverUrl;
			var elImg = this.carouselNext.getElementsByTagName('img')[0];
			YAHOO.util.Event.addListener(elImg,'mouseover',function(e) {  this.src=imgOverUrl },elImg,true);
			YAHOO.util.Event.addListener(elImg,'mouseout',function(e) {this.src=imgUrl },elImg,true);				
		}
		
		YAHOO.util.Event.addListener(this.carouselNext, "click", this._scrollNext, this);
	},
	/*LC Change*/
	
	addItem: function(idx, innerHTML) {
 		var liElem = this.getCarouselItem(idx);

	    // does not exist, create it
	    if(!isValidObj(liElem)) {
	    	liElem = document.createElement("li");
			liElem.id = this.carouselElemID + "-item-" + idx;
			YAHOO.util.Dom.setStyle(liElem, "width", this.liWidth+"px");
			//liElem.style.width = this.thumbnailWidth + "px";
			liElem.innerHTML = innerHTML;
		
			this.carouselList.appendChild(liElem);
		}
		
	},

	getCarouselItem: function(idx) {
		var elemName = this.carouselElemID + "-item-" + idx;
 		var liElem = YAHOO.util.Dom.get(elemName);
		return liElem;	
	},
	/**LC Change
	 * Handler for text resize event
	 * recalculates number of visible items and scrollInc and pages
	 * 
	 * */

	calibrateUI:function() {
		this.numVisible = Math.floor(this.clipReg.offsetWidth/this.itemWidth);
		this.scrollInc = this.numVisible;
		this.numPages = Math.ceil(this.size/this.scrollInc);
		this.renderPagination();
	},
	
	


	/* should be moved */
	debugMsg: function(msg) 
	{
		var debugArea = document.getElementById("debug-area");
		debugArea.innerHTML = debugArea.innerHTML + "<br/>" + msg;
	},
	/* should be moved */
	clearDebug: function(msg) 
	{
		var debugArea = document.getElementById("debug-area");
		debugArea.innerHTML = "";
	}

}
function isValidObj(obj) {

		if (null == obj) {
			return false;
		}
		if ("undefined" == typeof(obj) ) {
			return false;
		}
		return true;

}

function createLink(oConfig) {	
	if(!isValidObj(oConfig)) {
		return null;
	}
	var el = document.createElement('a');
	if (oConfig.sUrl)
		el.setAttribute('href',oConfig.sUrl);
	else {
		el.setAttribute('href',"#");		
	}
	if (oConfig.sTitle)
		el.setAttribute('title','oConfig.sTitle');	
	return el;
}

function createImgLink(oConfig) {
		if(!isValidObj(oConfig)) {
			return null;
		}
		
		var el = createLink(oConfig);
		if (el) {
			if (oConfig.sImgUrl && oConfig.sImgUrl!='') { //create img
	
				var elImg = document.createElement('img');
				if (oConfig.sImgUrl) {
					elImg.setAttribute('src',oConfig.sImgDisabledUrl);
				}
				if (oConfig.sAlt) {
					elImg.setAttribute('alt',oConfig.sAlt);
				}
				if (oConfig.sClass) {
					elImg.className = oConfig.sClass;
				}
	
	
				el.appendChild(elImg);
	
			}
			else { //no imgs, just text
				el.innerHTML = oConfig.sText;		
			}
			return el;
		}
		else return null;

	};

