var selectors=[' ','.','#','+','>','~',':','::','*','[',']',','];
var keywords=[];

function parse(str) {

	var r = str.split('');


	var selector = undefined;
	var elm  = undefined;
	if(r.length==1 && str=="*") return $T("*"); 

	for(var i=0,ln=r.length;i<ln;i++) {

		if(selectors.indexOf(r[i])!=-1) { 
			selector=r[i];
			
        }

		if(/[a-zA-Z0-9_-]/.test(r[i])) {

			regex='^.{'+i+'}([a-zA-Z0-9_-]+)';
			var reg=new RegExp(regex);
			var result = reg.exec(str);
		        var tag=result[1];
			var ret= getByClass(tag,i,r,selector,elm);
                        if(ret) { 
			    elm=ret;
			    i  = tag.length-1+i;
			     continue;
			}
			var ret= getId(tag,i,r,selector,elm);
			if(tag.length==r.length && ret) return ret;
                        if(ret)  { 
			     elm=ret;
			     i = tag.length-1+i;
			     continue;
			}	

			var ret= getHTMLList(tag,i,r,selector,elm);
			if(tag.length==r.length && ret) return ret;
                        if(ret) { 
			    elm=ret;
			    i  = tag.length-1+i;
			     continue;
			}
			var ret= getDirectChildOf(tag,i,r,selector,elm);
			if(tag.length==r.length && ret) return ret;
                        if(ret) { 
			    elm=ret;
			    i  = tag.length-1+i;
			     continue;
			}
			var ret= getFirstSiblingOf(tag,i,r,selector,elm);
			if(tag.length==r.length && ret) return ret;
                        if(ret) { 
			    elm=ret;
			    i  = tag.length-1+i;
			     continue;
			}
			    i  = tag.length-1+i;
               }

	}
	return elm;
}

function getId(searchTag,inLoop,aSelection,lastSelector,elms) {
     if(lastSelector=='#') return $I(searchTag);
     return false;
}
function getHTMLList(searchTag,inLoop,aSelection,lastSelector,elms) {
	if((lastSelector==' ' || !lastSelector)) return $TS(searchTag,elms);
	return false;
}
function getDirectChildOf(searchTag,inLoop,aSelection,lastSelector,elms) {
	if(lastSelector=='>'){
		var root = elms[0] || elms;
		tags = $TS(searchTag,elms);
		if(!tags) return; 
		var ret=[];
		for(var i=0,ln=tags.length;i<ln;i++) { 
			var s=tags[i];
			var parent=s.parentNode;
			if(parent.nodeType==1 && parent===root) ret.push(s);
		}
		return ret;
	}
	return false;
}
function Parent(elm) {
	return elm.parentNode;
}
function next(elm) {
	if(!elm) return;
	do { elm=elm.nextSibling;} while(elm && elm.nodeType!=1);
	return elm;
}

function getDirectSiblingOf(searchTag,inLoop,aSelection,lastSelector,elms) {
	if(lastSelector=='+'){
	    searchTag=searchTag.toLowerCase();
		var ret=[];
		for(var i=0,ln=elms.length;i<ln;i++) { 
			var s=elms[i];
			    s=next(s);
			if(n && n.nodeName.toLowerCase()==searchTag) ret.push(n);
		}
		return ret;
	}
	return false;
}
function getFirstSiblingOf(searchTag,inLoop,aSelection,lastSelector,elms) {
	if(lastSelector=='~'){
	    searchTag=searchTag.toLowerCase();
		var ret=[];
		elms = (elms.constructor===Array) ? elms : [elms];
		for(var i=0,ln=elms.length;i<ln;i++) { 
			var n=elms[i];
			do {
			   n=next(n);
			} while(n && n.nodeName.toLowerCase()!=searchTag);
			if(n) ret.push(n);
		}
		return ret;
	}
	return false;
}

function getByClass(searchTag,inLoop,aSelection,lastSelector,elms) {
	if(lastSelector=='.') {
	       if(/ /.test(aSelection[inLoop-2])) elms=$TS(' ',elms);
                     return $C(searchTag,elms);
        }
	return false;
}


	function $A(attr,val,scope) {
			if(scope[attr]==val) return scope;
			if(parseInt(scope[attr])===val) return scope;
			if(parseInt(scope[attr])===parseInt(val)) return scope;
	}
	function $AS(attr,val,scope) {
		scope = (scope.constructor!==Array) ? [scope] : scope;
		var elms=[];
		for(var i=0;i<scope.length;i++) { 
			var s=scope[i];
			var r= $A(attr,val,s);
			if(r) elms.push(r);
		}
		return (elms.length>1) ? elms : elms[0];
	}

	function $C(t,s) {
		var ar=[];

		s= s || document.body.getElementsByTagName('*');
		var e;
		for(var i=0,ln=s.length;i<ln;i++) {
			e=s[i];
			if(e.className) if(e.className===t) ar.push(e);
		}
		if(ar.length==0) return;
		return (ar.length>1) ? ar : ar[0];
	}
	function $T(tag,scope) {
		if(tag=="body") return document.body;
		var elms= (scope || document).getElementsByTagName(tag);
		if(elms.length==0) return;
		var ar=[];
		for(var i=0,ln=elms.length;i<ln;i++) ar.push(elms[i]);
		return (ar.length>0) ? ar : undefined;
	}
	function $TS(tag,scope) {
		if(scope===undefined) return $T(tag);
		scope = (scope.constructor!==Array) ? [scope] : scope;
		var elms=[];
		for(var i=0;i<scope.length;i++) { 
			var s=scope[i][0] || scope[i];
			var r= $T(tag,s);
			if(r) {
			   for(var j=0;j<r.length;j++) elms.push(r[j]);
                     }
		}
		return (elms.length>1) ? elms : elms[0];
	}
	function $I(id) {
		return document.getElementById(id);
	}

if (!Array.prototype.indexOf){  
  Array.prototype.indexOf = function(elt)  {   
        var len = this.length;    
        var from = Number(arguments[1]) || 0;    
        from = (from < 0) ? Math.ceil(from) : Math.floor(from); 
        if (from < 0)  from += len;    
        for (; from < len; from++)    {   
              if (from in this && this[from] === elt)   
                    return from;    
        }   
        return -1;  
   };
}

Array.prototype.each=function (fn) {
	var that=this;
	for(var i=0;i < this.length ;i++) {
		(function() {
			var c=i;
			fn.call(that,that[c],c,that);
		})();
	}
}

JAME = function () {};
JAME = JAME.prototype = function () {};
JAME.VERSION=0.01;

function $(elm) {

		if(document.getElementById) {
			this.$ = function(elm) {
				if(document.getElementById(elm)) return document.getElementById(elm);
			}
		}
		if(document.getElementById(elm)) return document.getElementById(elm);
}

JAME.Package = function (sname) {

    var namespaces=sname.split('.') || [sname];
    var nlen=namespaces.length;
	
    var root = window;
    var F    = function() {};

    for(var i=0;i<nlen;i++) {
        var ns = namespaces[i];
        if(typeof(root[ns])==='undefined') {
            root = root[ns] = F;
            root = root.prototype = F;
        }
        else
           root = root[ns];
    }
}

JAME.Import = function () {
	var nlen=arguments.length;
	for(var i=0;i<nlen;i++) {
		var pk=arguments[i];
		if(pk.hasOwnProperty('Export')) {
			pk.Export();
        }
		else if (pk.prototype.Export!==undefined) {
			pk.prototype.Export();
		}
	}
}

JAME.Exporter = function (oName,sPref) {

	if(!sPref) sPref='';

	for(var method in oName) {
	    if(method==="Export" || /^_/.test(method)) continue;
	    window[sPref+method]=oName[method];
    }


}

JAME.Export = function () {
	JAME.Exporter({ "Package" : JAME.Package, "Import" : JAME.Import});
}

function extend(destination, source) {  
    function initClassIfNecessary(obj) {  
        if( typeof obj["_super"] == "undefined" ) {  
            obj["_super"] = function() {  
                var methodName = arguments[0];  
                var parameters = arguments[1];  
                this["__parent_methods"][methodName].apply(this, parameters);  
            }  
        }  
      
        if( typeof obj["__parent_methods"] == "undefined" ) {  
            obj["__parent_methods"] = {}  
        }  
    }  
  
    for (var element in source) {  
	
        if( typeof destination[element] != "undefined" ) {  
            initClassIfNecessary(destination);  
            destination["__parent_methods"][element] = source[element];  
        } else {  

            destination[element] = source[element];  
        }  
    }  
}
JAME.Package('JAME.DOM');



JAME.DOM.Load = function ( F ) {
     var oldOnload = window.onload;
     if (typeof window.onload !== 'function') {
         window.onload = F;
     } else {
         window.onload = function() {
            oldOnload();
            F();
         };
     }
}


JAME.DOM.Ready = function (f) {
           if (JAME.DOM.Ready.done) return f();
           if (!JAME.DOM.Ready.timer) {
			   JAME.DOM.Load(JAME.DOM.__isDOMReady);
               JAME.DOM.Ready.todo = [ f ];
               JAME.DOM.Ready.timer = setInterval(JAME.DOM.__isDOMReady,13);
           }
           else {
	        JAME.DOM.Ready.todo.push(f);
           }
}
JAME.DOM.__isDOMReady = function () {
		if (JAME.DOM.Ready.done) return false;
		if (document && document.getElementsByTagName && document.getElementById && document.body) {

                 clearInterval(JAME.DOM.Ready.timer);
                 JAME.DOM.Ready.timer = null;
		   var len=JAME.DOM.Ready.todo.length;
                 for (var i = 0; i < len; i++){
                    JAME.DOM.Ready.todo[i]();
		   }
		   JAME.DOM.Ready.todo = null;
		   JAME.DOM.Ready.done = true;	   
	     }
}

JAME.Package('JAME.Util.Number');

JAME.Util.Number= {
    d2h : function(dec) { 
       return dec.toString(16);
    },
    h2d :function(hex) { 
       return parseInt(hex,16);
   },
   Export : function() {
	JAME.Exporter(this);

   }
};
JAME.Package('JAME.Util.Color');

(function () {
      var d2h = JAME.Util.Number.d2h;
      var h2d = JAME.Util.Number.h2d;

      JAME.Util.Color = {
           rgb2h:function (r,g,b) { 
              return [d2h(r),d2h(g),d2h(b)];
           },
           h2rgb:function (h,e,x) {  
              return [h2d(h),h2d(e),h2d(x)];
           },
           hexStr2rgbArray:function (color) {
	          return JAME.Util.Color.h2rgb(color.substring(1,3),color.substring(3,5),color.substring(5,7));
           },
           rgbStr2rgbArray:function (color) {
	          return color.substring(4,color.length-1).split(',');
           },
           cssColor2rgb:function (color) {
	          if(color.indexOf('rgb')<=-1) {
		        return JAME.Util.Color.hexStr2rgbArray(color);
	          }
	          return JAME.Util.Color.rgbStr2rgbArray(color);
           },
           Export : function() {
                JAME.Exporter(this);
           }
      };
})();

JAME.Package('JAME.Util.Queue');

JAME.Util.Queue=function(scope) {
	this.q=[];
	this.paused=false;
	if(scope) extend(this,scope);
}

extend(JAME.Util.Queue.prototype,{

	queue : function() {
		for(var i=0;i<arguments.length;i++)
		    this.q.push(arguments[i]);
		return this;
        },
	dequeue : function () {
		if(!this.empty()) this.q.pop();
		return this;
	},
	next : function () {
		if(this.empty()) {
			this.inProgress=false;
			return;
		}
		this.paused=false;
		this.inProgress=true;
		this.q.shift().apply(this);
		return this;
	},
	flush : function () {
		while(!this.empty()) this.next();
	},
	empty : function () {
		if(this.q.length==0) return true;
		return false;
	}
});
JAME.Package('JAME.Util.String');

JAME.Util.String={
	camelize: function(str) {
        return str.replace(/-(.)/g, function(m, l){return l.toUpperCase()});
	},
	hyphenize : function(str) {
        return str.replace(/([A-Z])/g, function(m, l){return '-'+l.toLowerCase()});
	},
	firstToUpperCase : function(str) {
         return str.replace(/^([a-z])/, function(m, l){return l.toUpperCase()});
	},
	trim : function(str) {
         return str.replace(/^\s+|\s+$/g, '');
	},
    Export : function() {
        JAME.Exporter(this);
    }
};

JAME.Package('JAME.FX');

JAME.FX.Tween = function (elm,props, duration, fps,easing,async) {
      return this;
}

JAME.FX.Tween.prototype = {

     setAnimation : function() {

	 	this.actualTime = new Date().getTime();

         this.tweener.compute(parseInt( (this.actualTime - this.startingTime) / this.interval));

         if(this.actualTime >= this.startingTime + this.duration) {
			 this.stop();
         }
     },
     setProperties : function() {
		for(var method in this.extendProperties) {
             if(method=='prototype') continue;
             this.extendProperties[method].apply(this);
        }
    },
    stop : function() {
	     this.elm.__ON = false;
         clearInterval(this.timer);
         this.tweener.compute(this.totalframes);
		 this.paused=false;
         this.next();
		 return this;
    },
    setOptions :function (elm,props, duration, fps,easing,async) {

    	this.elm       = elm || this.elm;
 
        this.duration  = duration || this.duration || 1000;
        this.fps       = fps || this.fps || 50;
        this.easing    = easing || this.easing || function(t,b,c,d) { 
                                                    return b + (c/d)*t;
                                                };

        this.interval    = Math.ceil(1000/this.fps);
        this.totalframes = Math.ceil(this.duration/this.interval);
        this.frame       = 1;
	    this.async       = async || this.async || false;

        if(!this.props) {
            this.props      = props;
            this.behavior    = this.props.behavior || this.behavior || 'stop:link';
	        this.setProperties();
        }

    },
    addToQueue : function (that) {
	    var me =this;
         that.queue(function () {
		    return that.Tween2(me.elm,me.props,1);
	     });
				
    },
    setBehavior : function() {

      if(this.elm.__ON===true) {
       var that=this.elm.effect;
       if(this.behavior=='stop:link') { 
				if(this.empty()) this.addToQueue(that);
			    that.stop();
				return;
	   }
	   else if(this.behavior=='stop:play'){
		clearInterval(that.timer);
		this.q=[];
		return true;
	   }
	   else if(this.behavior=='finish:play' || this.behavior=='finish:link') {
		this.addToQueue(that);
		return;
	   }
	   else if(this.behavior=='finish:skip') {
		return;
	   }
	   else {
	 	this.elm.__ON  = false;
		clearInterval(that.timer);
		this.q=[];
		return;		
	   }
      }
	return true;
    },
    setMultiInstance : function () {

      var arg=arguments[0];
      if(this.elm && this.elm.constructor===Array) {
	   var that=this;
	   if(this.async) {
		  for(var i=1;i<this.elm.length;i++) {
	    		(function() {
					var elm=that.elm[i];
					  that.Tween2(elm,that.props);
            		})();
        	   }
	   }
	   else {

	        if(arg===undefined) {
		      for(var i=1;i<this.elm.length;i++) {
	    		  (function() {
				var elm=that.elm[i];
				that.queue(function () {
					  that.Tween2(elm,that.props);
				});
           		  })();
		      }
                }
	        else {

		     for(var i=this.elm.length-1;i>0;i--) {
	    		(function() {
				var elm=that.elm[i];
				that.q.unshift(function () {
					  that.Tween2(elm,that.props);
				});
            		})();
        	     }
		     if(arg) that.paused=false;
	       }
               this.elm=this.elm.shift();
          }
    }
   }
};

JAME.FX.Tween.prototype.extendProperties={

        CSS : function() {

            if(this.props['selector']) {

	           var style = (this.props['selector'].style) ? this.props['selector'].style : -1;
			   if(this.style!=style) {
					this.style=style;
	           		if(style && style!=-1) var css    = new JAME.E4CSS(style);
					else var css    = new JAME.E4CSS();
	           		var starts = css[this.props['selector'].start];
	           		var ends   = css[this.props['selector'].end];
	           		for(var prop in starts) this.props[prop]={start:starts[prop],end:ends[prop]};
			   }

               delete this.props['selector'];
           }
       }
};

JAME.FX.T=function (option) {

        extend(this, new JAME.Util.Queue());

	    this.duration  = option.duration || this.duration || 1000;
        this.fps       = option.fps      || this.fps      || 50;
        this.easing    = option.easing   || this.easing   || function(t,b,c,d) { 
                                                       return b + (c/d)*t;
                                                     };
	    this.async       = option.async || this.async || false;
        this.behavior    = option.behavior || this.behavior || 'stop:link';

        this.interval    = Math.ceil(1000/this.fps);
        this.totalframes = Math.ceil(this.duration/this.interval);
        this.frame       = 1;
}
extend(JAME.FX.T.prototype,JAME.FX.Tween.prototype);


extend(JAME.FX.T.prototype, {
 
	Tween2 : function (elm,props) {

	         if(arguments[2]) this.paused=true;

	         this.elm= elm || this.elm;

             this.props      = props || this.props;
			 this.setProperties();

             this.setMultiInstance(arguments[2]);

             if(!this.setBehavior()) { 
					return this;
			 }

             this.elm.__ON  = true;

             this.tweener = new JAME.FX.CSSTweener(this.elm,this.props,this.totalframes,this.easing);

             this.startingTime  = new Date().getTime();
             var that= this;
             this.elm.effect = that;
             this.timer = setInterval(function() { 
                                  that.setAnimation.apply(that)
                               },this.interval);
             return this;
       }

})

JAME.Package('JAME.E4CSS');

JAME.E4CSS = function (val) {
	return (this instanceof JAME.E4CSS) ? this.parse(val) : new JAME.E4CSS(val);
}
	var String = JAME.Util.String;

JAME.E4CSS.prototype={

      styleSheet:0,

      sheets: (document.styleSheets) ? document.styleSheets : undefined,

      lookupTable:{},

	  getCssRules : function(val) {

		if(typeof val==='number') {
			this.styleSheet=val;
			if(val > this.sheets.length) return;
			return this.sheets[val]['cssRules'] || this.sheets[val]['rules'];
		}

		var regex=new RegExp(val);

		for(i=0; i< this.sheets.length;i++) {
			if(regex.test(this.sheets[i].href)) {
				this.styleSheet=i;
				return this.sheets[i]['cssRules']|| this.sheets[i]['rules'];
			}
		}
		return undefined;
	},

	getAll : function () {
		for(i=0; i< this.sheets.length;i++) {
			this.parse(i);
		}

	},

	parse : function(cssFileName) {


		if(cssFileName===undefined) {
			this.getAll();
			extend(this,JAME.E4CSS.prototype);
			return this;
		}

		var rule=this.getCssRules(cssFileName);
		if(!rule) return;

		for(var i=0;i<rule.length;i++) {


			var selector=rule[i].selectorText;
			if(!selector) continue;

			var csss=rule[i].style.cssText.split(';');

			var props={};

			for(var j=0;j<csss.length;j++) {

					var attr=csss[j].split(/:\s?/);

					if(attr[0].length==1 || !attr[0] || !attr[1]) continue;

					var tmp=String.camelize(attr[0].toLowerCase());

					props[String.trim(tmp)]=attr[1];
			}

		    var selectors=selector.split(',') || [selector];

		    for(k=0;k<selectors.length;k++) {

				if(this[selectors[k]]) {
					for(var attr in props)
						this[selectors[k].toLowerCase()][attr]=props[attr];
			    }

			    else 
					this[selectors[k].toLowerCase()]=props;	

				this.lookupTable[selectors[k].toLowerCase()]=this.styleSheet;
			}
		}

		return this;
	}
};
JAME.Package('JAME.Elements.Style');

JAME.Elements.Style={

   setOpacity : function (obj,amount) {

	JAME.Elements.Style.setOpacity=(window.attachEvent)
        ? function (obj,amount) {
	         obj.style.zoom = 1;
	         obj.style.filter = "alpha(opacity=" + amount*100 + ")";		
          }
        : function (obj,amount) {
	          obj.style.opacity = amount;		
          }
       JAME.Elements.Style.setOpacity(obj,amount);

    },

   _float : function() {
	var prop = (window.attachEvent) ? 'styleFloat' : 'cssFloat';
	this.styleFloat=function() { return prop; };
	return prop;
   },

   setStyle : function (elm,prop,val) {
        elm = (elm.constructor===Array) ? elm : [elm];
	for(var i=0;i<elm.length;i++) {

             if(prop=='opacity') { 
                JAME.Elements.Style.setOpacity(elm[i],parseFloat(val));
		continue;
             }

             if(prop=='float')   prop = JAME.Elements.Style._float();

             prop = JAME.Util.String.camelize(prop);
             unit=(prop=='zIndex'||prop=='zoom'||/olor/.test(prop)) ? '':'px';
             elm[i].style[prop] = (typeof val=='string') ? val : val+unit;
 	}
   },
   setStyles : function (elm,props) {
	for(var prop in props) {
		JAME.Elements.Style.setStyle(elm,prop,props[prop]);
	}
   }
};
JAME.Package('JAME.FX.CSSTweener');
 
JAME.FX.CSSTweener = function(elm,props,frames,easing) {
	this.elm    = elm;
	this.props  = props;
	this.frames = frames;
	this.easing = easing;
}

JAME.FX.CSSTweener.prototype ={

     compute : function(frame) {

       this.frame=frame;

       for(var prop in this.props){

	   this.prop = prop;
	   this.from = this.props[prop].start;
	   this.to   = this.props[prop].end;

       if(/[0-9]+/.test(parseInt(this.from)) && !/\s/.test(this.from)) {
 	        this.Numeric();
		    continue;
       } 
       var method=JAME.Util.String.camelize(JAME.Util.String.firstToUpperCase(prop));
       if(this[method]) { 
	        this[method]();
       }
       else {
         this.String();
       }
     }
  },
  String : function(obj,ratio) {
	ratio=ratio || 0.2;
	var set = ((this.frame/this.frames)>=ratio) ? this.to : this.from;
	JAME.Elements.Style.setStyle(this.elm,this.prop,set);
	
  },
  Numeric : function () {

      var begin  = (/[a-z]/.test(this.from)) ? parseInt(this.from)*10000 : this.from*10000;
      var end    = (/[a-z]/.test(this.to))   ? parseInt(this.to)  *10000 : this.to  *10000;

      var displacement = this.easing(this.frame, begin, end-begin, this.frames);
	  try { JAME.Elements.Style.setStyle(this.elm,this.prop,displacement/10000);}catch(e){};
  }

};

function QuadBezier(frame,begin,end,deviator,totalframes) {
    var t=frame/totalframes;
    return (1-t)*(1-t)*begin + 2*t*(1-t)*deviator + t*t*end;
}
JAME.Package('JAME.FX.Transition');
JAME.FX.Transition.Linear=function(t, b, c, d) {
           return b + (c/d)*t;
}

JAME.Package('JAME.FX.CSSTweener');

//padding & margin

extend(JAME.FX.CSSTweener.prototype, { 

    _position : ['Top','Right','Bottom','Left'],

    Padding : function () {
	this.setMultiPart('Numeric');
    },
    Margin : function () {
	this.setMultiPart('Numeric');
    },

    setMultiPart : function (method,sub) {
	var elms = this.splitMultiPart();
	if(sub==undefined) sub='';
	var i=0,j=0,k=0,l=0;
	if(elms.start.length==2) i=0,j=1,k=0,l=1;
	if(elms.start.length==3) i=0,j=1,k=2,l=1;
	if(elms.start.length==4) i=0,j=1,k=2,l=3;

	var v = [i,j,k,l];

	var prop=this.prop;

	for(var m=0;m<v.length;m++) {
		this.prop   = prop+this._position[m]+sub;
		this.from   = elms.start[v[m]];
		this.to     = elms.end[v[m]];
	   this[method]();
	}
    }, 
    splitMultiPart : function () {

		var start = this.from.replace(/(, ?)/g,function(m,l) { return ','});
		var end   = this.to.replace(/(, ?)/g,function(m,l) { return ','});


		var starts  = start.split(/\s/) || [start];
		var ends    = end.split(/\s/)   || [end];
		return {start:starts,end:ends};
    }
});

JAME.Package('JAME.FX.CSSTweener');

extend(JAME.FX.CSSTweener.prototype,{

   Color : function () {
	    var func = (/#/.test(this.from)) ? JAME.Util.Color.hexStr2rgbArray 
                                         : JAME.Util.Color.rgbStr2rgbArray;
        var b = func(this.from);
        var e = func(this.to);

	var rgb=[];

        for(j=0;j<3;j++){
		     rgb.push(parseInt(this.easing(this.frame, b[j]*10000, (e[j]-b[j])*10000, this.frames)/10000) || 0);
		}
       JAME.Elements.Style.setStyle(this.elm,this.prop,'rgb('+rgb.join(',')+')'); 
   },
   BackgroundColor : function () {
       this.Color();
   }
});

JAME.Package('JAME.FX.CSSTweener');

extend(JAME.FX.CSSTweener.prototype, { 

    BackgroundPosition : function () {
	     this.setBGMultiPart('Numeric');
    },

    setBGMultiPart : function (method) {
	    var elms = this.splitMultiPart();



      var begin  = (/[a-z]/.test(elms.start[0])) ? parseInt(elms.start[0])*10000 : elms.start[0]*10000;
      var end    = (/[a-z]/.test(elms.end[0]))   ? parseInt(elms.end[0])  *10000 : elms.end[0]  *10000;

      var displacementX = this.easing(this.frame, begin, end-begin, this.frames)/10000;

      begin  = (/[a-z]/.test(elms.start[1])) ? parseInt(elms.start[1])*10000 : elms.start[1]*10000;
      end    = (/[a-z]/.test(elms.end[1]))   ? parseInt(elms.end[1])  *10000 : elms.end[1]*10000;

      var displacementY = this.easing(this.frame, begin, end-begin, this.frames)/10000;
	  var value=displacementX+'px '+displacementY+'px';
      JAME.Elements.Style.setStyle(this.elm,this.prop,value);


    }
});
JAME.Package('JAME.FX.Transition.Basic');

JAME.FX.Transition.Basic= {

    table : {1:'Quad',2:'Cubic',3:'Quart',4:'Quint'},

    In : function(pow) {
	return this['In'+this.table[pow]];
    },
    Out : function(pow) {
	return this['Out'+this.table[pow]];
    },
    InOut : function(pow) {
	return this['InOut'+this.table[pow]];
    },
    InQuad : function (t, b, c, d) {
        return c*(t/=d)*t + b;
    },
    InCubic : function (t, b, c, d) {
        return c*(t/=d)*t*t + b;
    },
    InQuart : function (t, b, c, d) {
        return c*(t/=d)*t*t*t + b;
    },
    InQuint : function (t, b, c, d) {
        return c*(t/=d)*t*t*t*t + b;
    },
    OutQuad : function (t, b, c, d) {
        return -c *(t/=d)*(t-2) + b;
    },
    OutQuart : function (t, b, c, d) {
        return -c * ((t=t/d-1)*t*t*t - 1) + b;
    },
    OutCubic : function (t, b, c, d) {
        return c*((t=t/d-1)*t*t + 1) + b;
    },
    OutQuint : function (t, b, c, d) {
        return c*((t=t/d-1)*t*t*t*t + 1) + b;
    },
    InOutQuint : function (t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
        return c/2*((t-=2)*t*t*t*t + 2) + b;
    },
    InOutCubic : function (t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t + b;
        return c/2*((t-=2)*t*t + 2) + b;
    },
    InOutQuart : function (t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
        return -c/2 * ((t-=2)*t*t*t - 2) + b;
    },
    InOutQuad : function (t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t + b;
        return -c/2 * ((--t)*(t-2) - 1) + b;
    }
};
JAME.Package('JAME.FX.Transition.Sine');

JAME.FX.Transition.Sine ={
    In: function (t, b, c, d) {
        return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
    },
    Out : function (t, b, c, d) {
        return c * Math.sin(t/d * (Math.PI/2)) + b;
    },
    InOut : function (t, b, c, d) {
        return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
    }
}
JAME.Package('JAME.FX.Transition.Bounce');

JAME.FX.Transition.Bounce={
    In : function (t, b, c, d) {
         return c - JAME.FX.Transition.Bounce.Out(d-t, 0, c, d) + b;
    },
    Out : function (t, b, c, d) {
        if ((t/=d) < (1/2.75)) { return c*(7.5625*t*t) + b;} 
        else if (t < (2/2.75)) { return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;} 
        else if (t < (2.5/2.75)) { return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;}
        else { return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b; }
   },
   InOut : function (t, b, c, d) {
       if (t < d/2) return JAME.FX.Transition.Bounce.In(t*2, 0, c, d) * .5 + b;
       return JAME.FX.Transition.Bounce.Out(t*2-d, 0, c, d) * .5 + c*.5 + b;
   },
   Export : function() {
		JAME.Exporter(this,'Bounce');
   }
}
JAME.Package('JAME.FX.Transition.Elastic');

JAME.FX.Transition.Elastic={

    //a: amplitude (optional), p: period (optional)
    In : function (t, b, c, d, a, p) {
        if (t==0) { return b; } 
        if ((t/=d)==1) { return b+c; }
        if (!p) { p=d*.3; }
        if (c==0 || a < Math.abs(c)) {  a=b+c; s=p/4; }
        else { a=Math.abs(c); s = p/(2*Math.PI) * Math.asin(c/a);}
         return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
    },
    Out : function (t, b, c, d, a, p) {
        if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
        if (c==0 || a < Math.abs(c)) { a=b+c; var s=p/4; }
        else {   a=Math.abs(c); var s = p/(2*Math.PI) * Math.asin (c/a);}
        return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
    },
    InOut : function (t, b, c, d, a, p) {
        if (t==0) return b;  
        if ((t/=d/2)==2) return b+c;  
        if (!p) p=d*(.3*1.5);
        if (c==0 || a < Math.abs(c)) { a=b+c; var s=p/4; }
        else {a=Math.abs(c);var s = p/(2*Math.PI) * Math.asin (c/a);}
        if (t < 1) {return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;}
        return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
    },
	Export : function() {
		JAME.Exporter(this,'E');
	}
}
JAME.Package('JAME.FX.Transition.Expo');

JAME.FX.Transition.Expo={
    In : function (t, b, c, d) {
        return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
    },
    Out : function (t, b, c, d) {
        return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
    },
    InOut : function (t, b, c, d) {
        if (t==0) return b;
        if (t==d) return b+c;
        if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
        return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
    }
};
