ijs.__theme_controller = {
	
	'debug_worker': 0,
	'actions': {},
	'worker_interval_id': 0,
	'actions_length': 0,
	'worker_step': 0, 
	'show_loading': 1,
	'anim_onload': 1,
	
	
	'init': function(){
		
		if(this.debug_worker){
			
			this.debug_worker = document.createElement('div');
			this.debug_worker.style.position = 'absolute';
			this.debug_worker.style.top = '0px';
			this.debug_worker.style.left = '0px';
			this.debug_worker.style.width = '100px';
			this.debug_worker.style.height = '100px';
			this.debug_worker.style.background = 'red';
			this.debug_worker.style.color = '#FFF';
			this.debug_worker.style.fontFamily = 'Lucida Console';
			document.body.appendChild(this.debug_worker);
			
			}
		
		
		
		this.wrapper = ijs.$('main_wrapper').first();
		this.buttons = this.wrapper.getElementsByTagName('a');
		
		
		
		if(this.show_loading){
			
			this.preload_images = [];
			
			for(var i = 0, c = this.buttons.length; i < c ; ++i)
				this.preload_images.push({'url': 'images/'+ this.buttons[i].className +'.jpg'});
			
			
			this.show_loading = document.createElement('div');
			this.show_loading.className = 'loading';
			this.show_loading.innerHTML = 'Loading...';
			document.body.appendChild(this.show_loading);
			
			//this.preload_images.push({'url': 's.php'});
			
			var img;
			for(var i = 0, c = this.preload_images.length; i < c; ++i){
				
				img = document.createElement('img');
				img._id = i;
				
				img.onload = function(){
				//	console.log(this.src, new Date);
					ijs.__theme_controller.preload_images[this._id].loaded = true;
					}
				img.src = this.preload_images[i].url;
				
				//console.log(img.cloneNode(true));
				
				}
			this.add_work('preload_images', ijs.__theme_controller.chek_images, this, 1, function(){
				
				this.show_loading.parentNode.removeChild(this.show_loading);
				this.render_box();
				
				});
			
			}
		
		},
	
	
	
	
	'init_recurse': function(){
		
		if(this._inti_iretator)
			this.add_work(this.buttons[this._inti_iretator - 1]._id, this.hide_image, this.buttons[this._inti_iretator - 1], 4, this._inti_iretator == this.buttons.length -1 ? function(){
				ijs.__theme_controller._loaded = true;
				//console.log(ijs.__theme_controller.actions);
				ijs.__theme_controller.add_work(this._id, ijs.__theme_controller.hide_image, this, 1);
				
				} : false);
		
		if(this._inti_iretator < this.buttons.length )
			this.add_work(this.buttons[this._inti_iretator]._id, this.show_image, this.buttons[this._inti_iretator ], 1);
		
		++this._inti_iretator;
		
		if( this._inti_iretator <= this.buttons.length)
			setTimeout(function(){ ijs.__theme_controller.init_recurse(); }, 150);
			
		},
	
	
	
	
	'render_box': function(){
		
		var button;
		for(var i = 0, c = this.buttons.length; i < c ; ++i){
			button = this.buttons[i];
			button._background = document.createElement('div');
			button._background.style.background = 'url(images/'+ button.className +'.jpg) top left no-repeat';
			//button._background.style.backgroundPosition = ijs.css.get(button, 'background-position');
			this.preload_images.push({'url': 'images/'+ button.className +'.jpg'});
			
			this.wrapper.appendChild(button._background);
			button.innerHTML = '';
			button.style.overflow = 'visible';
			
			var span = document.createElement('span');
			//span.title = button.title;
			button.appendChild(span);
			
			button._id = 'button_' + i;
			button._opacity = 0;
			button._v = 20;
			ijs.css.set_opacity(button._background, button._opacity);
			
			button.onmouseup = function(){ this.blur(); return false; }
			button.onmousepress = function(){ this.blur(); return false; }
			button.onmousedown = function(){ this.blur(); return false; }
			button.onclick = function(){ this.blur(); return false; }
			button.onmouseover = function(){
					
					if(ijs.__theme_controller._loaded)
						ijs.__theme_controller.add_work(this._id, ijs.__theme_controller.show_image, this, 1);
					//return false;
					
				}
			
			button.onmouseout = function(){
					
					if(ijs.__theme_controller._loaded)
						ijs.__theme_controller.add_work(this._id, ijs.__theme_controller.hide_image, this, 2);
					//return false;
					
				}
			}
		
		
		if(this.anim_onload){
			this._loaded = false;
			this._inti_iretator = 0;
			this.init_recurse();
			}
		else
			this._loaded = true;
		
		},
	
	
	
	
	'chek_images': function(){
		
		for(var i = 0, c = this.preload_images.length; i < c; ++i)
			if(!this.preload_images[i].loaded)
				return true;
		
		return false;
		
		},
	
	
	
	
	'hide_image': function(){
		
		if(this._opacity - this._v > 0){
			this._opacity -= this._v;
			ijs.css.set_opacity(this._background, this._opacity);
			return true;
			}
		else {
			ijs.css.set_opacity(this._background, 0);
			return false;
			}
		
		},
	
	
	
	
	'reset_worker': function(){
		
		clearInterval(this.worker_interval_id);
		this.worker_interval_id = null;
		this.actions_length = 0;
		this.actions = {};
		
		},
	
	
	
	
	'show_image': function(){
		
		if(this._opacity + this._v < 100){
			this._opacity += this._v;
			ijs.css.set_opacity(this._background, this._opacity);
			return true;
			}
		else {
			ijs.css.set_opacity(this._background, 100);
			return false;
			}
		
		},
	
	
	
	
	'add_work': function(id, callback, context, odd, compleate_callback, wait){
		
		if(!this.actions[id])
			++this.actions_length;
		
		this.actions[id] = {
			'callback': callback,
			'context': context ? context : this,
			'id': id,
			'odd': odd ? odd : 1,
			'compleate_callback' : compleate_callback
			};
		
		if(!this.worker_interval_id){
			this.worker_step = 0;
			this.worker_interval_id = setInterval(function(){ ijs.__theme_controller.work(); } , 10);
			}
		
		},
	
	
	
	
	'work': function(){
		
		if(this.debug_worker){
			
			this._odd = this._odd ? this._odd : 0;
			
			switch(this._odd){
				case 0: this.debug_worker.innerHTML = '-';break;
				case 1: this.debug_worker.innerHTML = '/';break;
				case 2: this.debug_worker.innerHTML = '|';break;
				case 3: this.debug_worker.innerHTML = '\\';this._odd = -1;break;
				}
			this.debug_worker.innerHTML += ' ' + this.actions_length +' ' + this.worker_step;
			++this._odd;
			}
		
		if(!this.actions_length){
			clearInterval(this.worker_interval_id);
			this.worker_interval_id = null;
			if(this.worker_end_callback)
				this.worker_end_callback();
			}
		else{
			var action;
			for(var id in this.actions){
				action = this.actions[id];
				if(action && this.worker_step % action.odd == 0){
					if(action.callback.call(action.context) !== true){
						this.actions[id] = false;
						--this.actions_length;
						if(action.compleate_callback){
							
							action.compleate_callback.call(action.context);
							
							}
						}
					}
				}
			++this.worker_step;
			}
		}
	
	
	
	}


	
	

ijs.onload(function(){ijs.__theme_controller.init()});

