//opsIsDebugOn = 'con';

YAHOO.namespace("OPS");

var opsCarousel = {};

YAHOO.OPS.Carousel = function(opts) {
    opsDebugDump('opts', opts, 1);
    this.createEvent('expand');
    this.createEvent('collapse');
    opsCarousel[opts.carouselID] = this;

    YAHOO.util.Event.onDOMReady(this.init, opts, this, true);
};

YAHOO.OPS.Carousel.prototype = {
    init: function(ev, other, opts) {
	myopts = {
	    carouselID: '' // Must be defined
	    , cntnrID: '' // Default carouseID+'_cntnr'
	    , carouselEl: 'OL'
	    , carouselItemEl: 'LI'

	    , numVisible: 1
	    , minVisible: 1 // Used only when itemWidth != 0
	    , maxVisible: 0 // Used only when itemWidth != 0, 0=no max
	    , itemWidth: 0 // Width in pixels of each item
	    , carouselPadding: 0 // Num pixels to allocate for padding to left and right of all items

	    , prevBtnID: '' // Default carouselID+'_prevbtn'
	    , nextBtnID: '' // Default carouselID+'_nextbtn'
	    , ppBtnID: '' // Default carouselID+'_ppbtn'
	    , ppImageURL: {} // {paused, playing, stopped} URLs of images for ppBtn in various states
	    , ppBtnClass: {} // {paused, playing, stopped} CSS classes for ppBtn in various states

	    , scrollMode: 'page' // 'page' to scroll one page at a time, 'item' to scroll one item at a time
	    , scrollSpeed: 0.2 // Speed in seconds when paging forward/backwward
	    , scrollEffect: YAHOO.util.Easing.easeOut

	    , autoplay: false
	    , autoplaySpeed: 20 // Speed in seconds when autoplay is on
	    , autoplayInterval: 1000 // Speed in seconds when autoplay is on
	    , autoplayEffect: YAHOO.util.Easing.easeOut

	    , linkClassname: '' // CSS class of A tag that's triggered when user clicks on an item
	    , hoverClassname: '' // CSS class for items when cursor is hovering over
	};

	// Merge options.
	var debugout = '';
	for(var opt in myopts) {
	    if(typeof opts[opt] != 'undefined') {
		debugout += opt+'='+opts[opt]+"\n";
		myopts[opt] = opts[opt];
	    }
	}
	this.opts = myopts;
	this.carouselID = this.opts.carouselID;
	if(!this.opts.cntnrID)
	    this.opts.cntnrID = this.carouselID+'_cntnr';
	if(!this.opts.prevBtnID)
	    this.opts.prevBtnID = this.carouselID+'_prevbtn';
	if(!this.opts.nextBtnID)
	    this.opts.nextBtnID = this.carouselID+'_nextbtn';
	if(!this.opts.ppBtnID)
	    this.opts.ppBtnID = this.carouselID+'_ppbtn';
	opsDebug(debugout);

	// Get various elements of interest.
	this.cntnrEl = YAHOO.util.Dom.get(this.opts.cntnrID);
	this.prevBtnEl = YAHOO.util.Dom.get(this.opts.prevBtnID);
	this.nextBtnEl = YAHOO.util.Dom.get(this.opts.nextBtnID);
	this.ppBtnEl = YAHOO.util.Dom.get(this.opts.ppBtnID);

	// Calculate number of visible items at start.
	this.curVisible = this.opts.numVisible;
	this.curWidth = 0;
	var cntnrRegion = YAHOO.util.Dom.getRegion(this.cntnrEl);
	if(this.opts.itemWidth && cntnrRegion && cntnrRegion.width) {
	    this.curVisible = this.calcNumVisible();
	}

	// Create Carousel instance.
	this.instance = new YAHOO.widget.Carousel(this.carouselID, {
	    revealAmount: 0
	    , numVisible: this.curVisible
	    , isCircular: true
	    , autoPlayInterval: this.opts.autoplayInterval
	    , animation: {
		  speed: this.opts.autoplaySpeed
		, effect: this.opts.autoplayEffect
	    }
	    , navigation: { prev: this.opts.prevBtnID, next: this.opts.nextBtnID }
	    , carouselEl: this.opts.carouselEl
	    , carouselItemEl: this.opts.carouselItemEl
//	    , selectOnScroll: false
	});

	if(this.ppBtnEl) {
	    this.instance.subscribe("startAutoPlay", this.handleAPChange, this, true);
	    this.instance.subscribe("stopAutoPlay", this.handleAPChange, this, true);
	    YAHOO.util.Dom.setStyle(this.ppBtnEl, 'cursor', 'pointer');
	    YAHOO.util.Event.on(this.ppBtnEl, 'click', this.handlePP, this, true);
	}

	if(this.prevBtnEl) {
	    YAHOO.util.Dom.setStyle(this.prevBtnEl, 'cursor', 'pointer');
	    YAHOO.util.Event.on(this.prevBtnEl, 'click', this.handlePrev, this, true);
	}

	if(this.nextBtnEl) {
	    YAHOO.util.Dom.setStyle(this.nextBtnEl, 'cursor', 'pointer');
	    YAHOO.util.Event.on(this.nextBtnEl, 'click', this.handleNext, this, true);
	}

	this.instance.render(); // get ready for rendering the widget
	this.instance.show();   // display the widget

	YAHOO.lang.later(2500, this, this.handleSizeChange);

	this.carouselEl = this.instance.get('element');

	if(this.instance.get('numItems') <= this.curVisible) {
	    if(this.prevBtnEl)
		YAHOO.util.Dom.setStyle(this.prevBtnEl, 'visibility', 'hidden');
	    if(this.nextBtnEl)
		YAHOO.util.Dom.setStyle(this.nextBtnEl, 'visibility', 'hidden');
	}

	if(this.opts.autoplay)
	    this.instance.startAutoPlay();

	YAHOO.util.Event.on(this.carouselEl, 'focus', this.handleFocus, this, true);
	YAHOO.util.Event.on(this.carouselEl, 'mouseover', this.handleFocus, this, true);
	YAHOO.util.Event.on(this.carouselEl, 'blur', this.handleBlur, this, true);
	YAHOO.util.Event.on(this.carouselEl, 'mouseout', this.handleBlur, this, true);
        this.instance.on('click', this.handleClick, this, true);
    },

    //=========================================================================
    calcNumVisible: function() {
	var cntnrRegion = YAHOO.util.Dom.getRegion(this.cntnrEl);
	this.curWidth = cntnrRegion.width;

	if(this.curWidth-this.opts.carouselPadding <= this.opts.itemWidth)
	    return 1;

	else {
	    var numVisible = Math.floor((this.curWidth-this.opts.carouselPadding)/this.opts.itemWidth);
	    if(numVisible < this.opts.minVisible)
		return this.opts.minVisible;
	    if(this.opts.maxVisible && numVisible > this.opts.maxVisible)
		return this.opts.maxVisible;
	    return numVisible;
	}
    },

    //=========================================================================
    getTargetItem: function(ev) {
	var el;
	var target = YAHOO.util.Event.getTarget(ev);
	while(target && target != this.carouselEl && target.id != this.instance._carouselEl) {
	    el = target.nodeName;
	    if(el.toUpperCase() == this.instance.get("carouselItemEl"))
		break;
	    target = target.parentNode;
	}

	var item = this.instance.getItemPositionById(target.id);
	if(item < 0)
	    return null;

	return target;
    },

    //=========================================================================
    handleAPChange: function(ev) {
	var state;
	if(this.instance.isAutoPlayOn())
	    state = 'playing';
	else if(this.isPaused)
	    state = 'paused';
	else
	    state = 'stopped';

	if(this.opts.ppImageURL[state])
	    YAHOO.util.Dom.setAttribute(this.ppBtnEl, 'src', this.opts.ppImageURL[state]);

	for(var i in this.opts.ppBtnClass) {
	    if(i != state)
		YAHOO.util.Dom.removeClass(this.ppBtnEl, this.opts.ppBtnClass[i]);
	}
	opsDebug('numItems='+this.instance.get('numItems')+', curVisible='+this.curVisible);
	if(state == 'paused') {
	    if(this.opts.ppBtnClass[state] && this.instance.get('numItems') > this.curVisible)
		YAHOO.util.Dom.addClass(this.ppBtnEl, this.opts.ppBtnClass[state]);
	} else if(this.opts.ppBtnClass[state])
	    YAHOO.util.Dom.addClass(this.ppBtnEl, this.opts.ppBtnClass[state]);
    },

    //=========================================================================
    handleBlur: function(ev) {
	var target = this.getTargetItem(ev);
	if(!target)
	    return;

	if(this.opts.hoverClassname)
	    YAHOO.util.Dom.removeClass(target, this.opts.hoverClassname);

	if(this.isPaused) {
	    this.isPaused = false;
	    this.instance.startAutoPlay();
	}
    },

    //=========================================================================
    handleClick: function(ev) {
	var target = this.getTargetItem(ev);
	if(!target)
	    return;

	var links = YAHOO.util.Dom.getElementsByClassName(this.opts.linkClassname, 'A', target);
	if(!links.length)
	    return;

	var href = YAHOO.util.Dom.getAttribute(links[0], 'href');
	if(!href)
	    return;

	YAHOO.util.Event.stopEvent(ev);
	document.location = href;
    },

    //=========================================================================
    handleFocus: function(ev) {
	var target = this.getTargetItem(ev);
	if(!target)
	    return;

	YAHOO.util.Dom.addClass(target, this.opts.hoverClassname);

	if(this.instance.isAutoPlayOn()) {
	    this.isPaused = true;
	    this.instance.stopAutoPlay();
	}
    },

    //=========================================================================
    handleNext: function(ev) {
	this.instance.stopAutoPlay();
	this.instance.set('animation', {
	      speed: this.opts.scrollSpeed
	    , effect: this.opts.scrollEffect
	});
	if(this.opts.scrollMode == 'page')
	    this.instance.scrollPageForward();
	else
	    this.instance.scrollForward();
    },

    //=========================================================================
    handlePP: function(ev) {
	if(this.instance.isAutoPlayOn()) {
	    this.instance.stopAutoPlay();
	} else {
	    this.instance.set('animation', {
		  speed: this.opts.autoplaySpeed
		, effect: this.opts.autoplayEffect
	    });
	    this.instance.scrollPageForward();
	    this.instance.startAutoPlay();
	}
    },

    //=========================================================================
    handlePrev: function(ev) {
	this.instance.stopAutoPlay();
	this.instance.set('animation', {
	      speed: this.opts.scrollSpeed
	    , effect: this.opts.scrollEffect
	});
	if(this.opts.scrollMode == 'page')
	    this.instance.scrollPageBackward();
	else
	    this.instance.scrollBackward();
    },

    //=========================================================================
    handleSizeChange: function(ev) {
	var cntnrRegion = YAHOO.util.Dom.getRegion(this.cntnrEl);
	opsDebug(this.carouselID+'.curWidth='+this.curWidth+', cntnrRegion.width='+cntnrRegion.width);
	if(!this.curWidth)
	    return;

	if(cntnrRegion.width != this.curWidth) {
	    var numVisible = this.calcNumVisible();
	    if(this.curVisible != numVisible) {
		opsDebug(this.carouselID+'->'+numVisible);
		this.curVisible = numVisible;

		if(this.instance.get('numItems') <= this.curVisible) {
		    if(this.prevBtnEl)
			YAHOO.util.Dom.setStyle(this.prevBtnEl, 'visibility', 'hidden');
		    if(this.nextBtnEl)
			YAHOO.util.Dom.setStyle(this.nextBtnEl, 'visibility', 'hidden');

		} else {
		    if(this.prevBtnEl) {
			YAHOO.util.Dom.setStyle(this.prevBtnEl, 'visibility', 'visible');
//			YAHOO.util.Event.on(this.prevBtnEl, 'click', this.handlePrev, this, true);
		    }
		    if(this.nextBtnEl) {
			YAHOO.util.Dom.setStyle(this.nextBtnEl, 'visibility', 'visible');
//			YAHOO.util.Event.on(this.nextBtnEl, 'click', this.handleNext, this, true);
		    }

		}

		this.instance.set('numVisible', this.curVisible);
		this.instance._setNumVisible(this.curVisible);
	    }

	}

	YAHOO.lang.later(2500, this, this.handleSizeChange);
    },

    endofclass: ''
};

YAHOO.lang.augmentProto(YAHOO.OPS.Carousel, YAHOO.util.EventProvider);

