//news box
YUI().use("node", function(Y) {

    var node = Y.one('#close_news');
 
    var onClick = function(e) {

		var background = Y.one('#news_transparent_box');
		var news = Y.one('#latest_news');

		background.setStyle('display', 'none');
		news.setStyle('display', 'none');
		node.setStyle('display', 'none');
    	
    };
 
    node.on('click', onClick);
});

//backgrounds
YUI().use("node", "anim", function(Y) {
	
var backgrounds = Y.all('#background-container .background'),
	show_links = Y.all('#home_productions_menu a'),
	transitionTime = 1,//seconds
	halfTransitionTime = transitionTime / 2,
	transitionDelay = 5000,//ms
	visibleZIndex = 10,
	hiddenZIndex = -10,
	showOrder = [],
	current = {showName:'', backgroundNum:0, node:null},
	next = {showName:'', backgroundNum:0, node:null};

if ( backgrounds.size() > 1 ){//has to be more than one background before we proceed
	for ( var v in show_info ){
		showOrder.push(v);//push the show names into an indexable array
	}
	
	current.showName = showOrder[0];//take first item as default
	current.node = Y.one('#' + show_info[current.showName][current.backgroundNum]['id']);

	backgrounds.setStyle('display', 'block');
	backgrounds.setStyle('zIndex', hiddenZIndex);
	current.node.setStyle('zIndex', visibleZIndex);
	
	//console.log(backgrounds, show_links);

	/**
	 * 
	 */
	function hideCurrent(){
		next.node.setStyle('zIndex', visibleZIndex - 1);
		next.node.setStyle('opacity', 1);
		
		var fadeOut = new Y.Anim({
			node: current.node,
			to: {
				opacity: 0
			},
			easing: Y.Easing.easeOut,
			duration: transitionTime
		});
		fadeOut.on('end', function(e){
			current.node.setStyle('zIndex', hiddenZIndex);
			next.node.setStyle('zIndex', visibleZIndex);
		});
		fadeOut.run();
	}
	
	/**
	 * 
	 */
	function getNext(){
		if ( show_info[ current.showName ].length > current.backgroundNum + 1 ){ //if there are still images in the current set, increment
			next.showName = current.showName;
			next.backgroundNum = current.backgroundNum + 1;
			
		} else {
			for (var i = 0; i < showOrder.length; i++){
				//console.log(showOrder[i]);
				if ( showOrder[i] == current.showName ){//found the current show
					if ( i == showOrder.length - 1 ){//if we're at the end
						next.showName = showOrder[0];//start from the beginning
					} else {
						next.showName = showOrder[i+1];
						break;
					}
				}
			}
			next.backgroundNum = 0;
		}
		next.node = Y.one('#' + show_info[next.showName][next.backgroundNum]['id']);
		//console.log(next);
		return next;
	}
	
	
	/**
	 * 
	 */
	function setCurrentToNext(){
		current.backgroundNum = next.backgroundNum;
		current.showName = next.showName;
		current.node = next.node;
	}
	
	/**
	 * 
	 */
	function setSelectedShow(){
		var currentLink = Y.one('#showlink_' + current.showName),
			nextLink = Y.one('#showlink_' + next.showName);
		
		currentLink.removeClass('selected');
		nextLink.addClass('selected');
		
	}
	

	/**
	 * 
	 */
	function showNext(){
		getNext();
		hideCurrent();
		setSelectedShow();
		setCurrentToNext();
		setTimeout(showNext, transitionDelay);
	}
	
	function init(){
		setTimeout(showNext, transitionDelay);
	}
	
	Y.on("load", init, window);
	
}

});
