var twinkleCount = 0;
window.addEvent("domready", function() {
	// initialize all elements
	var artists = new Fx.Slide("artists", {mode: "horizontal"});
	var news = new Fx.Slide("news", {mode: "horizontal"});
	var store = new Fx.Slide("store", {mode: "horizontal"});
	var about = new Fx.Slide("about", {mode: "horizontal"});
	
	// artist info kept in two arrays
	// array one name (index is offset), array two has all slide elements
	// order of buttons: bio, releases, shows, media
	var artistArray = new Array("poboys","guidice","manaia","capitals","max","beata");
	var infoButtons = new Array("Bio","Releases","Shows","Media");
	var artistDivArray = new Array();
	var artistInfoDiv = new Fx.Slide("artistInfo");
	var storeButtons = new Array( "beataCD", "capitalsFarFetched","capitalsEP",	"max1","max2","max3","max4" );
	var storeButtonArtist = new Array( 5,3,3,4,4,4,4 );
	
	var openContent = null;
	var openInfo = null;
	
	//twinkle background
	var cds = new Fx.Tween( "main", {duration:"long"});
		
	// set up initial look
	artists.hide();
	news.hide();
	store.hide();
	about.hide();
	
	$("twinkle0").fade('hide');
	$("twinkle1").fade('hide');
	$("twinkle2").fade('hide');
	$("twinkle3").fade('hide');
	$("twinkle4").fade('hide');
	
	artistInfoDiv.hide();

	// functions
	var handleContent = function( element ) {
		if( openContent != element ) {
			if( closeOpenContent() ) {
				setTimeout( function() { 
					$("artists").scrollTop = 0;
					$("news").scrollTop = 0;
					$("store").scrollTop = 0;
					$("about").scrollTop = 0;
					element.slideIn();
				}, 550 );
			} else {
				// first
				$("artists").scrollTop = 0;
				$("news").scrollTop = 0;
				$("store").scrollTop = 0;
				$("about").scrollTop = 0;
				element.slideIn();
			}			
			openContent = element;
			twinkle();
		} else {
			// already open
		}
	};
	var closeOpenContent = function() {
		if( openContent != null ) {
			closeOpenInfo();
			openContent.slideOut();
			openContent = null;
			return true;
		}
		return false;
	};
	var handleInfo = function( name ) {
		if( openInfo != name ) {
			if( closeOpenInfo() ) {
				setTimeout( function() { 
					// artistDivInfo changing
					$("artistInfo").innerHTML = artistInfoArray[name];
					$("artistInfo").scrollTop = 0;
					artistInfoDiv.slideIn();
					Slimbox.scanPage();
					Videobox.scan();
				}, 550 );
				twinkle();
			} else {
				// first (extra delay, because it will be auto-opened)
				setTimeout( function() {
					$("artistInfo").innerHTML = artistInfoArray[name];
					$("artistInfo").scrollTop = 0;
					artistInfoDiv.slideIn();
					Slimbox.scanPage();
					Videobox.scan();
				}, 1000 );
			}			
			openInfo = name;
		} else {
			// already open
		}
	};
	
	// if nothing is open, return false
	// if something is open, close it, return true
	var closeOpenInfo = function() {
		if( openInfo != null ) {
			artistInfoDiv.slideOut();
			setTimeout( function() {
				$("artistInfo").innerHTML = "";
			}, 510 );
			openInfo = null;
			return true;
		}
		return false;
	}
	
	var addInfoClickEvent = function( name ) {
		$( name + "Button" ).addEvent("click", function(e) {
			
			e.stop();
			handleInfo( name );
		});
	};
	var addArtistClickEvent = function( element, name ) {
		$( name + "Button" ).addEvent("click", function(e) {
			//e.stop();
			handleContent( element )
			handleInfo( name + infoButtons[ 0 ] );
		});
	};
	
	var addArtistStoreButtonEvent = function( button, name, content ) {
		$(button).addEvent("click", function(e) {
			e.stop();
			handleContent( content );
			handleInfo( name + infoButtons[ 1 ] );
		});
	};
	
	// logo event handler
	$("logo-button").addEvent("click", function(e) {
		e.stop();
		handleContent( artists );
	});
	
	// nav-bar event handlers
	$("artist-button").addEvent("click", function(e) {
		e.stop();
		handleContent( artists );
	});
	
	$("news-button").addEvent("click", function(e) {
		e.stop();
		handleContent( news );
	});
	
	$("store-button").addEvent("click", function(e) {
		e.stop();
		handleContent( store );
	});
	
	$("about-button").addEvent("click", function(e) {
		e.stop();
		handleContent( about );
	});
	
	var twinkle = function( delay ) {
		if( delay == null ) {
			delay = 300;
		}
		var current = 'twinkle' + twinkleCount;
		$(current).fade('in');
		setTimeout( function() {
			$(current).fade('out');
		}, delay );
		
		twinkleCount++;
		twinkleCount%=5;
	};
	
	// artist event handlers
	for( var x = 0; x < artistArray.length; x++ ) {
		artistDivArray[x] = new Fx.Slide(artistArray[x], {mode: "horizontal"});
		addArtistClickEvent( artistDivArray[x], artistArray[x] );
		artistDivArray[x].hide();
	}
	
	for( var x = 0; x < artistArray.length; x++ ) {
		for( var y = 0; y < infoButtons.length; y++ ) {
			var index = (x*infoButtons.length) + y;
			var name = artistArray[x] + infoButtons[y];
			addInfoClickEvent( name );
		}
	}
	
	// store buttons
	for( var i = 0; i < storeButtons.length; i++ ) {
		addArtistStoreButtonEvent( storeButtons[ i ], artistArray[ storeButtonArtist[ i ] ], artistDivArray[ storeButtonArtist[ i ] ] );
	}
	
	// open artists first
	handleContent( artists );
});
