
var bgIndex = 1;
backgroundImages = ["/img/background/bg_001.jpg",
					"/img/background/bg_002.jpg",
					"/img/background/bg_003.jpg",
					"/img/background/bg_004.jpg",
					"/img/background/bg_005.jpg",
					"/img/background/bg_006.jpg",
					"/img/background/bg_007.jpg",
					"/img/background/bg_008.jpg"];


$(document).ready(function() {
	
	// Setup jQuery Address Listener
	$.address.change(function(event) {
            	//console.log(event.value);
            	if(event.value == "/") return false;
                $('li').each(function() {
                    $(this).toggleClass('selected', $(this).attr("id") == event.pathNames[0] 
                    								|| $(this).attr("id") == event.pathNames[1]);
                });
                loadContent( event.pathNames );
            }).internalChange(function(event) {
            }).externalChange(function(event) {
            });

	setInterval('rotate()', 16000);
	
	// Music Player
	// $('#music-player').hide();

});


function handleFlashClick(){
  if( $.address.value() != '/' ){
  	$.address.value('/');
  	loadContent();
  }
}



function loadContent( path ){
	
	// Show loader
	$('#content').html('<img class="loading-image" src="/img/loader.gif">');
	
	if( path == undefined ){
		ca = '/';
	}else{
		var ca = '';
		cai = 0;
		while( path.length > cai ){
			ca += '/'+path[cai];
			cai++;
		}
	}
	
	$.post( ca, { ajax: true },
            function(data){
            	$('#content').hide();
                $('#content').html(data);
                $('#content').fadeIn();
            }, "html");
	
}

function rotate(){
	
		if( bgIndex >= backgroundImages.length)
			bgIndex = 0;
	
	  var img = new Image();
	  
	  // wrap our new image in jQuery, then:
	  $(img)
	    .load(function () {

	      //$(this).css('marginLeft', $(document).width());
	      $(this).css('opacity', 0);	

	      $('#bg')
	        .prepend(this);

	      $(this).animate({/*marginLeft: '0',*/opacity: 1}, 1250, function(){ 
	      										$('#bg').css('backgroundImage', 'url('+this.src+')')
	      												.html(''); 
	      										});
			
	    })
	    
	    // if there was an error loading the image, react accordingly
	    .error(function () {
	      if(console)
	      	console.log('image load error! oh noes!');
	    })
	    
	    // *finally*, set the src attribute of the new image to our image
	    .attr('src', backgroundImages[bgIndex]);
	    bgIndex++;
	
}