/*****************************************************************/
/**************		 	 Lightbox		       *******************/
/*****************************************************************/
YAHOO.util.Event.onDOMReady(initLigthBox);

//var imageLightBox = null;

var imageLightBox = new Array();
function showLightBox(feature_number){
	
	imageLightBox[feature_number].show();
	carousel[feature_number].show();
}
function hideLightBox(){
	imageLightBox[this].hide();
	carousel[this].hide();	
}
function scrollControlHandler(o,feature_number){

	var curItem =carousel[this].get("selectedItem");
	var numItems = carousel[this].get("numItems");
	
	if (curItem == 0){
		YAHOO.util.Dom.setStyle("scroll_backward_"+this, "display", "none");	
	}
	else {
		YAHOO.util.Dom.setStyle("scroll_backward_"+this, "display", "block");	
	}
	if (curItem == numItems-1){
		YAHOO.util.Dom.setStyle("scroll_forward_"+this, "display", "none");	
	}
	else {
		YAHOO.util.Dom.setStyle("scroll_forward_"+this, "display", "block");	
	}	
}
function scrollHeightHandler(o,feature_number){
	var curItem =carousel[this].get("selectedItem");
	var curEl = carousel[this].getElementForItem(curItem);
	var curHeight = YAHOO.util.Dom.getRegion(curEl).bottom - YAHOO.util.Dom.getRegion(curEl).top;
	var targetEl;
	var targetHeight;
	if (o.dir == "forward"){
		targetEl = carousel[this].getElementForItem(curItem+1); 
		targetHeight = YAHOO.util.Dom.getRegion(targetEl).bottom - YAHOO.util.Dom.getRegion(targetEl).top;
	}
	else {
		targetEl = carousel[this].getElementForItem(curItem-1); 	
		targetHeight = YAHOO.util.Dom.getRegion(targetEl).bottom - YAHOO.util.Dom.getRegion(targetEl).top;
	}
	if (curHeight != targetHeight){
	// this might be an issue
		var anim = new YAHOO.util.Anim("carousel_"+this, {height:{to:targetHeight}}, 0.7, YAHOO.util.Easing.easeOutStrong);
		anim.animate();
	}
	
}

//var carousel;
var carousel = new Array();


//var current_feature = 0;
function initLigthBox(){
	var carousels = YAHOO.util.Dom.getElementsByClassName("carousel_container", "div");
	for(var i in carousels){
	var current_feature =carousels[i].id.substr(19);
	YAHOO.util.Dom.setStyle("lightbox_"+current_feature, "display", "block");
	carousel[current_feature]    = new YAHOO.widget.Carousel("carousel_container_"+current_feature, {
                        animation: { speed: .70, effect:YAHOO.util.Easing.easeOutStrong },
						numVisible:1
                });

                        
            carousel[current_feature].render(); // get ready for rendering the widget
            //carousel.show();   // display the widget
			if (carousel[current_feature].get("numItems") > 1){
				YAHOO.util.Dom.setStyle("scroll_forward_"+current_feature, "display", "block");	
			}
			else {
				YAHOO.util.Dom.setStyle("scroll_forward_"+current_feature, "display", "none");		
			}
			YAHOO.util.Dom.setStyle("scroll_backward_"+current_feature, "display", "none");		
			carousel[current_feature].hide();
	//if (current_feature == 0)
	imageLightBox[current_feature] = new YAHOO.widget.Panel("lightbox_"+current_feature, { fixedcenter:true, 
                                                 			visible:false,
															modal:true,
															draggable:false,
															iframe:true,
															zIndex:100000001,
															underlay:"none",
															close:false} );
	
	
	imageLightBox[current_feature].render();
	imageLightBox[current_feature].hide();
	//imageLightBox.show();
	YAHOO.util.Event.addListener(["close_lightbox_"+current_feature, "lightbox_mask"], "click", hideLightBox, current_feature, true );
	YAHOO.util.Event.addListener("scroll_forward_"+current_feature, "click", function(o){this.scrollForward();},carousel[current_feature],true);
	
	YAHOO.util.Event.addListener("scroll_backward_"+current_feature, "click", function(o){this.scrollBackward();},carousel[current_feature],true);
	carousel[current_feature].addListener("afterScroll", scrollControlHandler,current_feature,true);
	carousel[current_feature].addListener("beforeScroll", scrollHeightHandler,current_feature,true);
	}
	//current_feature = current_feature + 1;
	
}

