var carousel;


function galleryNext() {
	carousel.scrollForward();
}

function galleryBack() {
	carousel.scrollBackward();
}
    
function fadeImageIn(imageEl, textEl) {

	var imageFader	= YAHOO.util.Dom.get("photo-gallery-image-anim");
	var textTarget	= YAHOO.util.Dom.get("image-caption");
	
	var tmpImage = document.createElement("img");
	tmpImage.src = imageEl.src;
	
	if ( imageFader.firstChild )
		imageFader.removeChild(imageFader.firstChild);
	
	// imageFader.style.display = 'inline';
	imageFader.appendChild(tmpImage);
	
	var viewWidth = YAHOO.util.Dom.getViewportWidth();
	var containerWidth = YAHOO.util.Dom.get("product-container").clientWidth;
	var offsetWidth = ( viewWidth - containerWidth );
	
	if (offsetWidth > 0 )
		offsetWidth = offsetWidth / 2;
	else
		offsetWidth = 0;
	
	offsetWidth += 5;
	
	offsetHeight = YAHOO.util.Dom.getY("photo-gallery-image"); 
	
	var attributes = {
	        points: { from: [-600, offsetHeight], to: [offsetWidth, offsetHeight] }
	    };
    var anim = new YAHOO.util.Motion(imageFader.id, attributes, 1, YAHOO.util.Easing.easeOut);
    anim.onComplete.subscribe(swapItem);
    anim.animate();
    
    textTarget.innerHTML = textEl.innerHTML;
}

function swapItem() {
	var imageFader	= YAHOO.util.Dom.get("photo-gallery-image-anim");
	var imageTarget	= YAHOO.util.Dom.get("photo-gallery-image");
	
	imageTarget.firstChild.src = imageFader.firstChild.src;
	imageFader.style.left = "-2000px";
	// imageFader.style.display = 'none';
}

(function () {
    // Get the image link from within its (parent) container.
    function getImage(parent) {
        var el = parent.firstChild;
                
        while (el) { 
            if (el.nodeName.toUpperCase() == "IMG") {
            	if ( el.className == "photo-gallery-large") {
            		return el;
            	}
            }
            el = el.nextSibling;
        }
        
        return null;
    }

    // Get the text link from within its (parent) container.
    function getText(parent) {
        var el = parent.firstChild;
                
        while (el) { 
            if (el.nodeName.toUpperCase() == "DIV") {
            	if ( el.className == "photo-gallery-prompt") {
            		return el;
            	}
            }
            el = el.nextSibling;
        }
        
        return null;
    }

    YAHOO.util.Event.onDOMReady(function (ev) {
    	var item, itemImage, itemText, imageEl, textEl;
    	document.getElementById("photo-gallery-media").style.display = 'inline';
        carousel    = new YAHOO.widget.Carousel("photo-gallery-media", { isCircular: true, numVisible: 3, animation: { speed: 0.5 } });

        carousel.render(); // get ready for rendering the widget
        carousel.show();   // display the widget
        
        // display the first selected item in the spotlight
//        item = carousel.getElementForItem(carousel.get("selectedItem"));
//        if (item) {
//       	imageEl = getImage(item);
//        	textEl = getText(item);
//        }
        
        // carousel.on("click", function( ev ) { alert( ev ); });
                   
        carousel.on("itemSelected", function (index) {
            // item has the reference to the Carousel's item
            item = carousel.getElementForItem(index);

            if (item) {
            	imageEl = getImage(item);
            	textEl = getText(item);
            	fadeImageIn(imageEl, textEl);
            }
        });
    });
})();

