/**
 * Base on Nifty (corners by copyright 2006 Alessandro Fulciniti (a.fulciniti@html.it)
 * Licensed under the GNU GPL
 * 
 * TODO: update comment 
 */
oRcsf.RegisterObject('Htmlcorners');
var RCSF_Base_Htmlcorners = 
{
	 msExtends		: 'RCSF_Base_Base'
	
	,mbOk			: (document.getElementById && document.createElement && Array.prototype.push)
	,mbCssLoaded	: false

	,OnDomLoaded : function()
	{	
		this.AddCss();
	}
	
	/**
	 * 
	 */
	,Round : function(selector,options)
	{
		if (!this.Check()) return;
	
		var i,v=selector.split(","),h=0;
		if(options == null) options = '';
		if(options.include("fixed-height")) h=$$(v[0])[0].offsetHeight;
		for(i=0;i<v.length;i++) this.Rounded(v[i],options);
		if(options.include("height")) this.SameHeight(selector,h);
	}
	
	/**
	 * 
	 */
	,Check : function()
	{
		if(this.mbOk==false) return false;
		this.AddCss();
		return true;	
	}
	
	
	/**
	 * TODO: load css from same dir as js
	 */
	,AddCss : function()
	{
		if(this.mbCssLoaded) return;
		this.mbCssLoaded = true;
		var l=new Element("link");
		l.setAttribute("type","text/css");
		l.setAttribute("rel","stylesheet");
		l.setAttribute("href",this.moCore.maPaths.a_url.s_base_root + "files/css/htmlcorners.css");
		l.setAttribute("media","screen");
		document.getElementsByTagName("head")[0].appendChild(l);
	}
	
	
	/**
	 * Wrapper for rounding elemens
	 */
	,Rounded : function (selector,options)
	{
		var i,top="",bottom="",v=new Array();
		if(options!=""){
		    options=options.replace("left","tl bl");
		    options=options.replace("right","tr br");
		    options=options.replace("top","tr tl");
		    options=options.replace("bottom","br bl");
		    options=options.replace("transparent","alias");
		    if(options.include("tl"))
		    {
		        top="both";
		        if(!options.include("tr")) top="left";
			}
		    else if(options.include("tr")) top="right";
		    if(options.include("bl"))
		    {
		        bottom="both";
		        if(!options.include("br")) bottom="left";
			}
		    else if(options.include("br")) bottom="right";
		}
		if(top=="" && bottom=="" && !options.include("none"))
		{
			top="both";bottom="both";
		}
		v=$$(selector);
		for(i=0;i<v.length;i++)
		{
		    this.FixIE(v[i]);
		    if(top!="") this.AddTop(v[i],top,options);
		    if(bottom!="") this.AddBottom(v[i],bottom,options);
	    }
	}
	
	/**
	 * Rounds element top
	 */
	,AddTop : function(el,side,options)
	{
		var d=new Element("b"),lim=4,border="",p,i,btype="r",bk,color;
		d.style.marginLeft="-"+this.GetPadding(el,"Left")+"px";
		d.style.marginRight="-"+this.GetPadding(el,"Right")+"px";
		if(options.include("alias") || (color=this.GetBk(el))=="transparent")
		{
			color	= "transparent";
			bk		= "transparent";
			border	= this.GetParentBk(el);
			btype	= "t";
		}
		else
		{
			bk=this.GetParentBk(el);
			border=this.Mix(color,bk);
		}
		
		d.style.background = bk;
		d.className="niftycorners";
		p=this.GetPadding(el,"Top");
		if(options.include("small"))
		{
		    d.style.marginBottom=(p-2)+"px";
		    btype+="s"; lim=2;
		}
		else if(options.include("big"))
		{
			d.style.marginBottom=(p-10)+"px";
			btype+="b"; lim=8;
		}
		else d.style.marginBottom=(p-5)+"px";
		for(i=1;i<=lim;i++)
		    d.appendChild(this.CreateStrip(i,side,color,border,btype));
		el.style.paddingTop="0";
		el.insertBefore(d,el.firstChild);
	}


	/**
	 * Rounds element bottom
	 */
	,AddBottom : function(el,side,options)
	{
		var d=new Element("b"),lim=4,border="",p,i,btype="r",bk,color;
		d.style.marginLeft="-"+this.GetPadding(el,"Left")+"px";
		d.style.marginRight="-"+this.GetPadding(el,"Right")+"px";
		if(options.include("alias") || (color=this.GetBk(el))=="transparent")
		{
	    	color="transparent";bk="transparent"; border=this.GetParentBk(el);btype="t";
	    }
		else bk=this.GetParentBk(el); border=this.Mix(color,bk);
		d.style.background=bk;
		d.className="niftycorners";
		p=this.GetPadding(el,"Bottom");
		if(options.include("small"))
		{
		    d.style.marginTop=(p-2)+"px";
		    btype+="s"; lim=2;
		}
		else if(options.include("big"))
		{
		    d.style.marginTop=(p-10)+"px";
		    btype+="b"; lim=8;
		}
		else d.style.marginTop=(p-5)+"px";
		for(i=lim;i>0;i--)
		    d.appendChild(this.CreateStrip(i,side,color,border,btype));
		el.style.paddingBottom=0;
		el.appendChild(d);
	}
	
	
	/**
	 * 
	 */
	,SameHeight : function (selector,maxh)
	{
		var i,v=selector.split(","),t,j,els=[],gap;
		for(i=0;i<v.length;i++)
		{
		    t=$$(v[i]);
		    els=els.concat(t);
	    }
		for(i=0;i<els.length;i++)
		{
		    if(els[i].offsetHeight>maxh) maxh=els[i].offsetHeight;
		    els[i].style.height="auto";
		}
		for(i=0;i<els.length;i++)
		{
		    gap=maxh-els[i].offsetHeight;
		    if(gap>0)
		    {
		        t=new Element("b");t.className="niftyfill";t.style.height=gap+"px";
		        nc=els[i].lastChild;
		        if(nc.className=="niftycorners") els[i].insertBefore(t,nc);
		        else els[i].appendChild(t);
			}
	    }
	}
	
	
	/**
	 * 
	 */
	,CreateStrip : function(index,side,color,border,btype)
	{
		var x=new Element("b");
		x.className=btype+index;
		x.style.backgroundColor=color;
		x.style.borderColor=border;
		if(side=="left")
		{
		    x.style.borderRightWidth="0";
		    x.style.marginRight="0";
		}
		else if(side=="right")
		{
		    x.style.borderLeftWidth="0";
		    x.style.marginLeft="0";
		}
		return(x);
	}
	
	
	/** 
	 * 
	 */
	,GetParentBk : function (x)
	{
		var el=x.parentNode,c;
		while(el.tagName.toUpperCase()!="HTML" && (c=this.GetBk(el))=="transparent")
		    el=el.parentNode;
		if(c=="transparent") c="#FFFFFF";
		return(c);
	}
	
	
	/**
	 * 
	 */
	,GetBk : function(x)
	{
		var c=this.GetStyleProp(x,"backgroundColor");
		if(c==null || c=="transparent" || c.include("rgba(0, 0, 0, 0)"))
		    return("transparent");
		if(c.include("rgb")) c=this.rgb2hex(c);
		return(c);
	}
	
	
	/**
	 * 
	 */
	,GetPadding : function(x,side)
	{
		var p=this.GetStyleProp(x,"padding"+side);
		if(p==null || !p.include("px")) return(0);
		return(parseInt(p));
	}	
	
	
	/**
	 * 
	 */
	,GetStyleProp : function(x,prop)
	{
		if(x.currentStyle)
		    return(x.currentStyle[prop]);
		if(document.defaultView.getComputedStyle)
		    return(document.defaultView.getComputedStyle(x,'')[prop]);
		return(null);
	}	
	
	
	/**
	 * 
	 */
	,Mix : function (c1,c2)
	{
		var i,step1,step2,x,y,r=new Array(3);
		step1 = (c1.length==4) ? 1 : 2;
		step2 = (c2.length==4) ? 1 : 2;
		for(i=0;i<3;i++)
		{
		    x=parseInt(c1.substr(1+step1*i,step1),16);
		    if(step1==1) x=16*x+x;
		    y=parseInt(c2.substr(1+step2*i,step2),16);
		    if(step2==1) y=16*y+y;
		    r[i]=Math.floor((x*50+y*50)/100);
		    r[i]=r[i].toString(16);
		    if(r[i].length==1) r[i]="0"+r[i];
		}
		return("#"+r[0]+r[1]+r[2]);
	}
	
	/**
     * 
     */
    ,FixIE : function(el)
	{
		if(el.currentStyle!=null && el.currentStyle.hasLayout!=null && el.currentStyle.hasLayout==false)
	    	el.style.display="inline-block";
	}
	
	
	/**
	 * Move 2 util??
	 */
	,rgb2hex : function (value)
	{
		var hex="",v,h,i;
		var regexp=/([0-9]+)[, ]+([0-9]+)[, ]+([0-9]+)/;
		var h=regexp.exec(value);
		for(i=1;i<4;i++)
		{
		    v=parseInt(h[i]).toString(16);
		    hex += (v.length==1) ? "0"+v : v;
		}
		return("#"+hex);
	}
}