// JavaScript Document
// tallbot scale system  
function tallbot_scale()
{
	var x;
	var i;
		 for(i=0;i<100;i++)
		{
			if(document.getElementById('tallbot_scale_'+i))
			{
				x=document.getElementById('tallbot_scale_'+i);
				scale_adjust(x);
			}
		}
}

function scale_adjust(obj)
{
	var d=obj.innerHTML.split(' ').join('');
	d=d.split(';');
	var S=new tallbot_scaleObj();
	S.obj=obj.parentNode;
	var i=0;
	while(d[i])
	{
		S.setProperties(d[i]);
		i++;
	}
	S.FinalValue();
}

function tallbot_scaleObj()
{
	this.p=1;// property
	this.a='100%';//amount
	this.Amount=false;
	this.m='smooth';//method
	this.Max=0;
	this.Min=0;
	this.start=0;
	this.end='px';
	this.obj;
	this.ref=false;
	this.i='10%';
	this.Int;
	
	var pdim;
	var l;//local length;
	var i;
	var maths=false;
	var mathi=0;
	var conditions=false;
	var conditioni=0;
	
	this.setObj=function(v)
	{
		try
		{
			v=document.getElementById(v);
			this.obj=(v);
		}
		finally
		{}
		
	}
	
	this.setMathArrayItem=function(arr)
	{
		if(maths===false)maths=new Array();
		maths[mathi]=arr;
		mathi++;
	}
	this.setConditionArrayItem=function(arr)
	{
		if(conditions===false)conditions=new Array();
		conditions[conditioni]=arr;
		conditioni++;
	}
	this.seta=function(v)// set amount
	{
		this.a=v;
		this.Amount=false;
		this.setAmount();
	}
	this.setm=function(v)
	{
		var V=false;
		if(v.match(/smooth/ig)!=null)V='smooth';
		else if(v.match(/step/ig)!=null)V='step';
		else if(x.match(/switch/ig)!=null)V='switch';
		else if(x.match(/stage/ig)!=null)V='stage';
		if(V!=false)this.m=V;
	}
	this.setMax=function(v)
	{
		var val=this.CalculateValue(v);
		this.Max=val;
	}
	this.setMin=function(v)
	{
		var val=this.CalculateValue(v);
		this.Min=val;
	}
	this.setProperties=function(arr)//----------------------------set the p
	{
		var d=arr.split(':');
		var x=d[0];
		var y=d[1];
		if(x.match(/method/ig)!=null)this.setm(y);
		else if(x.match(/smooth/ig)!=null||x.match(/step/ig)!=null||x.match(/switch/ig)!=null||x.match(/stage/ig)!=null)this.setm(x);
		else if(x.match(/property/ig)!=null)this.SetP(y);
		else if(x.match(/value/ig)!=null||x.match(/amount/ig)!=null)this.seta(y);
		else if(x.match(/start/ig)!=null||x.match(/begin/ig!=null)||x.match(/front/ig)!=null||x.match(/befor/ig)!=null)this.start=y;
		else if(x.match(/end/ig)!=null||x.match(/back/ig)!=null||x.match(/after/ig)!=null)this.end=y;
		else if(x.match(/max/ig)!=null)this.setMax(y);
		else if(x.match(/min/ig)!=null)this.setMin(y);
		else if(x.match(/math/ig)!=null)this.setMin(y);
		else if(x.match(/int/ig)!=null)this.i=y;
		else if(x.match(/obj/ig)!=null)this.setObj(y);
		else if(x.match(/condition/ig)!=null||x.match(/change/ig)!=null)this.setConditionArrayItem(y);
		else if('-'==x[0]||'+'==x[0]||'/'==x[0]||'*'==x[0])
		{
			this.setMathArrayItem(x);
		}
	}
	
	this.SetP=function(arr)//----------------------------set the p
	{
		switch(arr)
		{
			case 'height': this.p=1;break;
			case 'margin-top': this.p=2;break;
			case 'margin-bottom': this.p=3;break;
			case 'padding-top': this.p=4;break;
			case 'padding-bottom': this.p=5;break;
			case 'top': this.p=6;break;
			case 'bottom': this.p=7;break;
			case 'font-size': this.p=8;break;
			case 'line-height': this.p=9;break;
	
			case 'width': this.p=10;break;
			case 'margin-left': this.p=11;break;
			case 'margin-right': this.p=12;break;
			case 'padding-left':this.p=13;break;
			case 'padding-right': this.p=14;break;
			case 'left': this.p=15;break;
			case 'right': this.p=16;break;
		}
	}
	
	this.setAmount=function()//----------------------------set the amount
	{
		if(this.Amount===false)
		{	
			var val=this.CalculateValue(this.a);
			this.Amount=val;
		}
	}
	this.setInt=function()
	{
		var val=this.CalculateValue(this.i);
		this.Int=val;
	}
	this.step=function()
	{
		var integ=this.Amount/this.Int;
		integ=Math.floor(integ);
		integ*=this.Int;
		this.Amount=integ;
	}
	this.CalculateValue=function(v)//----------------------------standard value calculator
	{
		var l=v.length;
		var rex=/[0-9]/g;
		var n=(v.match(rex)).join('')*1;
		if(v.match('%')!=null)
		{
			this.setRef();
			n=((this.ref/100)*n);
		}
		else if(v.match(/em/ig)!=null)
		{
			this.setRef();
			n=((this.ref/100)*n);
		}
		else(v.match(/px/ig)!=null)
		{
			n=n;
		}
		n=Math.round(n);
		if(!n)n=0;
		return n;
	}
	
	this.setRef=function()//----------------------------get reference mesure
	{
		if(this.ref===false)
		{
			var ref;
			if(this.obj.id!='tallbotBody')ref=tallbotBodyDim();
			else ref=Dimentions();
			if(this.p<10)ref=ref['height'];
			if(this.p>9)ref=ref['width'];
			this.ref=ref;
		}
	}
	
	this.MathAdjust = function()//----------------------------Maths adjustment
	{
		if(maths!=false)
		{
			this.setAmount();
			var ma=maths;
			var i=0;
			while(ma[i])
			{
				var Amount=this.Amount;
				var a=ma[i].slice(0,1);
				var b=ma[i].slice(1);
				var B=this.CalculateValue(b);
				switch(a)
				{
					case '-': this.Amount=(Amount-B); break;
					case '+': this.Amount=(Amount+B); break;
					case '/': this.Amount=(Amount/B); break;
					case '*': this.Amount=(Amount*B); break;
				}
				i++;
			}
		}
	}
	
	this.ContrainAjdust = function(arr)//----------------------------constraints
	{
		this.setAmount();
			if(this.Max>0&&this.Amount>this.Max)
			{
				this.Amount=this.Max;
			}
			if(this.Min>0&&this.Amount<this.Min)
			{
				this.Amount=this.Min;
			}
 	}
	
	this.runMethod=function()
	{
		if(this.m=='smooth')
		{
			this.MathAdjust();
			this.ContrainAjdust();
			this.runConditions();
		}
		else if(this.m=='step')
		{
			this.MathAdjust();
			this.setInt();
			this.step();
			this.ContrainAjdust();
			this.runConditions();  
		}
		else if(this.m=='switch')
		{
		}
		else if(this.m=='stage')
		{
		}
	}
	this.runConditions=function()
	{
		if(conditions!=false)
		{
			for(j in conditions)
			{
				var c=conditions[j];
				alert(c);
			}
		}
	}
	
	this.FinalValue=function()
	{
		this.setAmount();
		this.runMethod();
		var V=this.start+this.Amount+this.end;
		var o=this.obj.style;
		var p=1;
		if(V[0]=='-')
			p=0;
		switch(this.p)
		{
			case 1: if(p==1)o.height=V;break;
			case 2: o.marginTop=V;break;
			case 3: o.marginBottom=V;break;
			case 4: if(p==1)o.paddingTop=V;break;
			case 5: if(p==1)o.paddingBottom=V;break;
			case 6: o.top=V;break;
			case 7: o.bottom=V;break;
			case 8: if(p==1)o.fontSize=V;break;
			case 9: if(p==1)o.lineHeight=V;break;
			
			case 10: if(p==1)o.width=V;break;
			case 11: o.marginLeft=V;break;
			case 12: o.marginRight=V;break;
			case 13: if(p==1)o.paddingLeft=V;break;
			case 14: if(p==1)o.paddingRight=V;break;
			case 15: o.left=V;break;
			case 16: o.right=V;break;
		}
		//alert('case : '+this.p+'\n'+V);
	}		
}
window.onresize=tallbot_scale;
window.onload=tallbot_scale;
