// JavaScript Document

var Preloader = {
	
  callbacks: [],
  images: [],
  loadedImages: [],
  imagesLoaded: 0,

  add: function(image){
	console.debug("add: ",image);
    if (typeof image == 'string') this.images.push(image);
    if (typeof image == 'array' || typeof image == 'object'){
      for (var i=0; i< image.length; i++){
        this.images.push(image[i]);
      }
    }
  },
  onFinish: function(func){
    if (typeof func == 'function') this.callbacks.push(func);
    if (typeof func == 'array' || typeof func == 'object'){
      for (var i=0; i< func.length; i++){
        this.callbacks.push(func[i]);
      }
    }
  },
  load: function(){
	console.debug("load"); 
    for(var i=0; i<this.images.length; i++){
      this.loadedImages[i] = new Image();
      this.loadedImages[i].onload = function(){ Preloader.checkFinished.apply(Preloader) }
      this.loadedImages[i].src = this.images[i];
    }
  },

  checkFinished: function(){
    this.imagesLoaded++;
    if (this.imagesLoaded == this.images.length) this.fireFinish();
  },
  fireFinish: function(){
	console.debug("finished"); 
    for (var i=0; i<this.callbacks.length; i++){
      this.callbacks[i]();
    }
    this.images = [];
    this.loadedImages = [];
    this.imagesLoaded = 0;
    this.callbacks = [];
  }
}


var Application = {

	currentPage: "intro",
	pages: ['talent','models','production','presenters','commercials','interactive','advoice'],
	//pages: ['talent','models','interactive','advoice'],

	showPage: function(page){
		if($('temp')) $('temp').hide();
		if(page == this.currentPage) return false;
		if(this.currentPage){
			$$('#nav_'+this.currentPage+' a').first().removeClassName('selected');
			$(this.currentPage+'_box').hide();
		}
		this.currentPage = page;
		$$('#nav_'+this.currentPage+' a').first().addClassName('selected');
		$(this.currentPage+'_box').show();
		console.debug('Setting bg to: ', "body-background-"+this.currentPage+".jpg");
		if($('wrapper-for-image')){
			$('wrapper-for-image').setStyle({
				background: "url(images/body-background-"+this.currentPage+".jpg) no-repeat scroll left top"										
			});
		}
		return false;
	},
	
	preloadBackgrounds: function(){
		this.pages.each(function(page){
			Preloader.add('images/body-background-'+page+'.jpg');			
		});
		Preloader.load();
	}
	

}

Event.addBehavior({
  '#nav-main a:click' : function(e) {
	 var page = this.href.split("#")[1];
	 Application.showPage(page);
	 e.cancel = true;
	 return false;
  },
  'a.popup' : function(e) {
	 this.onClick= function(){return false};
  },
  'a.popup:click' : function(e) {
	  ///alert(this.href);
	 this.target = '_self';
	 var w=window.open(this.href,'terms','width=570,height=650,status=no,scrollbars=yes');
	 e.cancel = true;
	 return false;
  }
});

Event.onReady(function() {
	$$('div .content_box').each(function(item){
		if(item.id != "intro_box")	{						 
			item.hide();
		}
  	});
	if(location.href.indexOf('#') != -1){
		var page = location.href.split("#")[1];
		if(page) Application.showPage(page);
	}
	document.body.style.display = 'block';
	
});


// $('my_div_id') returns 1 object
// $$('a.selected') returns an array .each(function(item){item.hide()}); .first() .last()