var YUE = YAHOO.util.Event;
var YUD = YAHOO.util.Dom;
var carousel;
var imageLightBox = null;
var lightboxConfig = { 
	"buttons" : { 
		"prev"  : { 
			"id" : "scroll_backward", 
			"img" : "/common/images/lightbox/prev.png" 
		}, 
		"next"  : { 
			"id" : "scroll_forward", 
			"img" : "/common/images/lightbox/next.png" 
		}
	},
	"containers" : {
		"carousel" : "carousel_container",
		"panel"    : "lightbox"
	},
	"items" : { 
		"count" : 0 
	},
	"width" : "414"
};


/**
 * Launches a new viewer instance
 * @param page_id Target page within current flyer.
 */
function launchViewer( page_id )
{
	var index = page_id - 1;
	if( carousel.get( "selectedItem") == index ) {
		showViewer();
	} else {
		carousel.set( "selectedItem", index );
		carousel.addListener( "afterScroll", showViewer );  // delay scroll for slide seek 		
	}
}


/**
 * Shows lightbox instance
 */
function showViewer() 
{
	var region = YAHOO.util.Dom.getClientRegion();
	imageLightBox.cfg.setProperty("xy", [((region.right-434)/2),region.top+5]);
	imageLightBox.show();
	carousel.show();
	carousel.removeListener( "afterScroll", showViewer );
}

/**
 * Hides lightbox instance
 */
function hideViewer()
{
	imageLightBox.hide();
	carousel.hide();	
}

/**
 * Updates prev and next button display after scroll
 */
function updateControls()
{
	
	var curItem = carousel.get( "selectedItem" );

	var display = ( curItem == 0 ) ? "none" : "block";
	YUD.setStyle( lightboxConfig.buttons.prev.id, "display", display );

	display = ( curItem == lightboxConfig.items.count - 1 ) ? "none" : "block";
	YUD.setStyle( lightboxConfig.buttons.next.id, "display", display );
}


/**
 * Creates the carousel and lightbox instances on load.
 */
function initLightBox()
{
	document.getElementById('lightbox').style.display = 'block';
    
	carousel = new YAHOO.widget.Carousel( lightboxConfig.containers.carousel , {
			animation: { 
				speed: .60, 
				effect: YAHOO.util.Easing.easeBothStrong 
			},
			numVisible:1
	});
                        
	carousel.render();

	lightboxConfig.items.count = carousel.get( "numItems" );
	
	if ( lightboxConfig.items.count > 1 ){
		YUD.setStyle( lightboxConfig.buttons.next.id, "display", "block" );	
	} else {
		YUD.setStyle( lightboxConfig.buttons.next.id, "display", "none" );		
	}
	
	YUD.setStyle( lightboxConfig.buttons.prev.id, "display", "none" );		
	
	carousel.hide();
	
	imageLightBox = new YAHOO.widget.Panel( lightboxConfig.containers.panel, { 
			/*fixedcenter:true,*/ 
        	visible:false,
			modal:true,
			draggable:false,
			iframe:true,
			zIndex:90002,
			underlay:"none",
			close:false
	});
	
	imageLightBox.render();
	imageLightBox.hide();

	YUE.addListener( "lightbox_mask" , "click", hideViewer );
	YUE.addListener( lightboxConfig.buttons.next.id, "click", function(o){carousel.scrollForward();});
	YUE.addListener( lightboxConfig.buttons.prev.id, "click", function(o){carousel.scrollBackward(); });
	carousel.addListener( "afterScroll", updateControls );	
}

YUE.onDOMReady( initLightBox );