//Tiny lib, containers widget (c)Alexander Melanchenko, http://alexnd.com

// id:'elementid', type:'swap'
function containercontroller()
{
	this.containers = [];
	this._e = [];

	var j=0;
	for(var i=0; i<arguments.length; i++){
		if($CORE.is_object(arguments[i])){
			this.containers[j] = arguments[i];
			j++;
		}
	}
	var _r=this;
	this.turn = function(ci,ri)
	{
		if(!(ci>=0))return;
		
		//alert(ci + '\n' + ri);
		if(ci==null)ci=0;
		if(ri==null)ri=0;
		
		switch(this.type){
			default:
			case 'swap':
				var cid='';
				//alert(_r.containers + '\n' + ci);
				try{
					if('undefined'!=typeof this.containers[ci].id)cid=this.containers[ci].id;
				}catch(_e){}
				if(cid=='')break;
				var el=$(cid);
				
				if($CORE.is_object(el)){
				if('undefined'!=typeof el.style){
					var dispstate=((!el.style.display || el.style.display=='' || el.style.display=='none') ? 0 : 1);
					if('undefined'==typeof this.containers[ci].state) this.containers[ci].state=dispstate;
					if('undefined'==typeof this.containers[ci].class_off || ('undefined'!=typeof this.containers[ci].usedisplay && this.containers[ci].usedisplay))
						el.style.display = ((this.containers[ci].state) ? 'none' : 'block');
					if('undefined'!=typeof this.containers[ci].class_on && 'undefined'!=typeof this.containers[ci].class_off){
						var cl = ((this.containers[ci].state) ? this.containers[ci].class_off : this.containers[ci].class_on);
						el.className=cl;
						el.setAttribute('className', cl);
					}
				}}
				if('undefined'!=typeof this.containers[ci].rels){
					for(var j=0; j<this.containers[ci].rels.length; j++){
						var rel=$(this.containers[ci].rels[j].id);
						if('undefined'!=this.containers[ci].rels[j].class_on && 'undefined'!=this.containers[ci].rels[j].class_off){
							var cl='';
							try{
								this.containers[ci].rels[j].class_on;
								if(this.containers[ci].state){
									this.containers[ci].rels[j].class_off;
								}
							}catch(_e){}
							rel.className=cl;
							rel.setAttribute('className', cl);
						}
					}
				}
				try{
					for(var j=0; j<this.containers[ci].chains.length; j++){
						if('undefined'==typeof this.containers[ci].chains[j].usedisplay || (this.containers[ci].chains[j].usedisplay)){
							var st=(this.containers[ci].chains[j].at)?['block','none']:['none','block'];
							$(this.containers[ci].chains[j].id).style.display = (this.containers[ci].state)?st[1]:st[0];
						}
						if('undefined'!=typeof this.containers[ci].chains[j].class_on){
							if(!this.containers[ci].state){
								$(this.containers[ci].chains[j].id).className=this.containers[ci].chains[j].class_on;
								$(this.containers[ci].chains[j].id).setAttribute('className', this.containers[ci].chains[j].class_on);
							}
						}
						if('undefined'!=typeof this.containers[ci].chains[j].class_off){
							if(this.containers[ci].state){
								$(this.containers[ci].chains[j].id).className=this.containers[ci].chains[j].class_off;
								$(this.containers[ci].chains[j].id).setAttribute('className', this.containers[ci].chains[j].class_off);
							}
						}
					}
				}catch(ex){}
				this.containers[ci].state=((this.containers[ci].state) ? 0 : 1);
				if('undefined'!=typeof this.containers[ci].on_turn && $CORE.is_function(this.containers[ci].on_turn)){
					var f=this.containers[ci].on_turn;
					if('undefined'!=typeof this.containers[ci].on_turn_arg){
						f(this,ci,ri,this.containers[ci].state,this.containers[ci].on_turn_arg);
					}else{
						f(this,ci,ri,this.containers[ci].state);
					}
				}
				break;
		}
	}
	
	this.init = function()
	{
		var _=this;
		for(var i=0; i<this.containers.length; i++){
			if('undefined'==typeof this.containers[i].id) continue;
			this._e[i]=$(this.containers[i].id);
			if(!$CORE.is_object(this._e[i])) continue;
			if('undefined'==typeof this.containers[i].rels) continue;
			if(!$CORE.is_array(this.containers[i].rels)) continue;
			for(var j=0; j<this.containers[i].rels.length; j++){
				if('undefined'==this.containers[i].rels[j].id) continue;
				var rel=$(this.containers[i].rels[j].id);
				rel.ci=i;rel.setAttribute("ci",i);
				rel.ri=j;rel.setAttribute("ri",j);
				$DOM.attach_event(rel, 'click', function (e,ee){
					if(e && e.srcElement){ e.returnValue=false; }else if(window.event && window.event.preventDefault){ window.event.preventDefault(); }
					if(ee)
						var it=ee;
					else if(e && e.srcElement)
						var it=e.srcElement;
					else if(window.event && window.event.srcElement)
						var it=window.event.srcElement;
					else
						var it = this;
					if('undefined'!=typeof it.ci){ var ci=it.ci; }else{ var ci=it.getAttribute("ci"); }
					if('undefined'!=typeof it.ri){ var ri=it.ri; }else{ var ri=it.getAttribute("ri"); }
					if(_.turn) _.turn(ci,ri);
					return false;
				});
			}
			var type=(('string'==typeof this.containers[i].type && this.containers[i].type!='') ? this.containers[i].type : 'swap');
			type=type.toLowerCase();
			switch(type){
				default:
				case 'swap':
					if('undefined'!=typeof this._e[i].style){
						var dispstate=((!this._e[i].style.display || this._e[i].style.display=='' || this._e[i].style.display=='none') ? 0 : 1);
						if('undefined'==typeof this.containers[i].state) this.containers[i].state=dispstate;
						else{
							if('undefined'!=this.containers[i].skipinit && this.containers[i].skipinit) this._e[i].style.display = (this.containers[i].state) ? 'block' : 'none';
						}
					}
					try{
					for(var j=0; j<this.containers[i].chains.length; j++){
						if('undefined'!=this.containers[i].chains[j].skipinit && this.containers[i].chains[j].skipinit) continue;
						if('undefined'==typeof this.containers[i].chains[j].usedisplay || (this.containers[i].chains[j].usedisplay)){
							$(this.containers[i].chains[j].id).style.display = ((this.containers[i].chains[j].at)?'block':'none');
						}
						if('undefined'!=typeof this.containers[i].chains[j].class_on){
							if(this.containers[i].state){
								$(this.containers[i].chains[j].id).className=this.containers[i].chains[j].class_on;
								$(this.containers[i].chains[j].id).setAttribute('className', this.containers[i].chains[j].class_on);
							}
						}
						if('undefined'!=typeof this.containers[i].chains[j].class_off){
							if(this.containers[i].state){
								$(this.containers[i].chains[j].id).className=this.containers[i].chains[j].class_off;
								$(this.containers[i].chains[j].id).setAttribute('className', this.containers[i].chains[j].class_off);
							}
						}
					}
					}catch(ex){}
					break;
			}
		}
	}
	
}