var no_bg = 14;
var curr_bg = 1;

document.observe("dom:loaded", function() {
	// initial animation
	load('about');
	Effect.BlindDown('selectors', { duration: 1.0 });			
	setInterval( "change_bg()", 25000);
	
	$$(".selector").each(function(item) {
		item.observe("mouseover", function() {
			item.addClassName("selector_hover");
		});
		item.observe("mouseout", function() {
			item.removeClassName("selector_hover");
		});		
	});
});


function load(page_name, sel_obj) {
	// load page & change title
	page_title = "VNC Open 2009 - " + page_name.capitalize();
	document.title = page_title;
	page_name = "pages/" + page_name + ".php";
	new Ajax.Updater('content_wrapper', page_name);				
	
	// change selector
	if (sel_obj != null) {
		clear_selector();
		sel_obj.addClassName("page_nav_selected");
	}
}

function load_sport(sport_name, sel_obj) {
	// load page & change title
	page_title = "VNC Open 2009 - " + sport_name.capitalize();
	document.title = page_title;			
	page_name = "pages/sport_" + sport_name + ".php";
	new Ajax.Updater('content_wrapper', page_name);
	
	// change selector
	if (sel_obj != null) {
		clear_selector();
		sel_obj.addClassName("selected");		
	}
}

function clear_selector() {
	$$(".selector").each(function(item) {
		item.removeClassName("selected");
	});			
	$$(".page_nav").each(function(item) {
		item.removeClassName("page_nav_selected");
	});
}

function change_bg() {
	// get random number
	next_bg = curr_bg;
	while (next_bg == curr_bg) {
		next_bg = Math.floor(Math.random() * no_bg) + 1;
	}
	curr_bg = next_bg;
	
	// fade in new background
	$('bg2').setAttribute( 'src', $('bg').src );
	$('bg2').show();
	next_bg = "images/bg" + next_bg + ".jpg";
	$('bg').setAttribute( 'src', next_bg );
	setTimeout( "$('bg2').fade( {duration:1.0} )", 5000 ); 
}
		
String.prototype.capitalize = function(){
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.slice(1).toLowerCase();
    });
};