
/**
 * Scripts divers
 * 
 * @author Roland Dufour <roland@rentashop.fr> pour Rentashop eCommerce
 */

var RSPlug_Misc = {};


/** Detection UA **/

RSPlug_Misc.UA = {
	isMobile: function(){
		var ua = navigator.userAgent.toLowerCase();
		if (ua.match(/(ipad|ipod|iphone|android|opera mini|blackberry|pre\/|palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine|iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile|mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320|vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo)/i)){
			return true;
		}
		else {
			var st = [1207,"3gso","4thp","501i","502i","503i","504i","505i","506i",6310,6590,"770s","802s","a wa","acer","acs-","airn","alav","asus","attw","au-m","aur ","aus ","abac","acoo","aiko","alco","alca","amoi","anex","anny","anyw","aptu","arch","argo","bell","bird","bw-n","bw-u","beck","benq","bilb","blac","c55\/","cdm-","chtm","capi","cond","craw","dall","dbte","dc-s","dica","ds-d","ds12","dait","devi","dmob","doco","dopo","el49","erk0","esl8","ez40","ez60","ez70","ezos","ezze","elai","emul","eric","ezwa","fake","fly-","fly_","g-mo","g1 u","g560","gf-5","grun","gene","go.w","good","grad","hcit","hd-m","hd-p","hd-t","hei-","hp i","hpip","hs-c","htc ","htc-","htca","htcg","htcp","htcs","htct","htc_","haie","hita","huaw","hutc","i-20","i-go","i-ma","i230","iac","iac-","iac\/","ig01","im1k","inno","iris","jata","java","kddi","kgt","kgt\/","kpt ","kwc-","klon","lexi","lg g","lg-a","lg-b","lg-c","lg-d","lg-f","lg-g","lg-k","lg-l","lg-m","lg-o","lg-p","lg-s","lg-t","lg-u","lg-w","lg\/k","lg\/l","lg\/u","lg50","lg54","lge-","lge\/","lynx","leno","m1-w","m3ga","m50\/","maui","mc01","mc21","mcca","medi","meri","mio8","mioa","mo01","mo02","mode","modo","mot ","mot-","mt50","mtp1","mtv ","mate","maxo","merc","mits","mobi","motv","mozz","n100","n101","n102","n202","n203","n300","n302","n500","n502","n505","n700","n701","n710","nec-","nem-","newg","neon","netf","noki","nzph","o2 x","o2-x","opwv","owg1","opti","oran","p800","pand","pg-1","pg-2","pg-3","pg-6","pg-8","pg-c","pg13","phil","pn-2","pt-g","palm","pana","pire","pock","pose","psio","qa-a","qc-2","qc-3","qc-5","qc-7","qc07","qc12","qc21","qc32","qc60","qci-","qwap","qtek","r380","r600","raks","rim9","rove","s55\/","sage","sams","sc01","sch-","scp-","sdk\/","se47","sec-","sec0","sec1","semc","sgh-","shar","sie-","sk-0","sl45","slid","smb3","smt5","sp01","sph-","spv ","spv-","sy01","samm","sany","sava","scoo","send","siem","smar","smit","soft","sony","t-mo","t218","t250","t600","t610","t618","tcl-","tdg-","telm","tim-","ts70","tsm-","tsm3","tsm5","tx-9","tagt","talk","teli","topl","hiba","up.b","upg1","utst","v400","v750","veri","vk-v","vk40","vk50","vk52","vk53","vm40","vx98","virg","vite","voda","vulc","w3c ","w3c-","wapj","wapp","wapu","wapm","wig ","wapi","wapr","wapv","wapy","wapa","waps","wapt","winc","winw","wonu","x700","xda2","xdag","yas-","your","zte-","zeto","aste","audi","avan","blaz","brew","brvw","bumb","ccwa","cell","cldc","cmd-","dang","eml2","fetc","hipt","http","ibro","idea","ikom","ipaq","jbro","jemu","jigs","keji","kyoc","kyok","libw","m-cr","midp","mmef","moto","mwbp","mywa","newt","nok6","o2im","pant","pdxg","play","pluc","port","prox","rozo","sama","seri","smal","symb","tosh","treo","upsi","vx52","vx53","vx60","vx61","vx70","vx80","vx81","vx83","vx85","wap-","webc","whit","wmlb","xda-"];
			var uaSt = ua.substr(0, 4);
			for (var i=0; i<st.length; i++){
				if (st[i] == uaSt){
					return true;
				}
			}
		}
		return false;
	},
	
	checkVersionMobile: function(){
		if (RSPlug_Misc.UA.isMobile() && null === RSPlug_Misc.cookie.get('no_sit_mobile')){
			var conf = confirm('Consulter la version mobile ?');
			if (conf){
				RSPlug_Misc.cookie.set('act_sit_mobile', '1', false);
				location.reload();
			} else {
				RSPlug_Misc.cookie.del('act_sit_mobile');
				RSPlug_Misc.cookie.set('no_sit_mobile', '1', false);
			}
		}
	},
	
	isIE: function(){ return (/MSIE (\d+\.\d+);/.test(navigator.userAgent)); },
	isIE6: function(){ return (/MSIE (6\.\d+);/.test(navigator.userAgent)); },
	isIE7: function(){ return (/MSIE (7\.\d+);/.test(navigator.userAgent)); },
	isIE8: function(){ return (/MSIE (8\.\d+);/.test(navigator.userAgent)); },
	isIE9: function(){ return (/MSIE (9\.\d+);/.test(navigator.userAgent)); }
};



/** Evenement **/

RSPlug_Misc.addEvent = function (element, eventName, callback, inst){
	if (typeof element == 'string'){ element = document.getElementById(element); }
	if (element){
		if (inst){
			if (element.addEventListener){
				element.addEventListener(eventName, function(e){ callback.apply(inst, [(e ? e : window.event), element]); }, false);
			} else if (element.attachEvent){
				element.attachEvent('on' + eventName, function(e){ callback.apply(inst, [(e ? e : window.event), element]); });
			}
		} else {
			if (element.addEventListener){
				element.addEventListener(eventName, function(e){ callback(e ? e : window.event, element); }, false);
			} else if (element.attachEvent){
				element.attachEvent('on' + eventName, function(e){ callback(e ? e : window.event, element); });
			}
		}
	}
};

RSPlug_Misc.stopPropagation = function(e){
	var evt = e || window.event;
	if (evt.stopPropagation) { 
		evt.stopPropagation(); 
	} 
	evt.cancelBubble = true;
}

RSPlug_Misc.preventDefault = function(e){
	var evt = e || window.event;
	if (evt.preventDefault) {
		evt.preventDefault();
	}
	evt.returnValue = false;
}


/** Ajax **/

RSPlug_Misc.ajax = {};
RSPlug_Misc.ajax.getXhr = function (){
	var obj;
	if (window.XMLHttpRequest){
		obj = new XMLHttpRequest();
	} else if (window.ActiveXObject){
		var ieversions = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0'];
		for (var i=0; !obj && i<ieversions.length; i++){
			try {
				obj = new ActiveXObject(ieversions[i]);
			} catch (e){
				obj = null;
			}
		}
	}
	return obj;
};

RSPlug_Misc.ajax.sendGet = function (url, async, callback){
	if (null === async || async){
		async = true;
	} else {
		async = false;
	}
	var xhr = RSPlug_Misc.ajax.getXhr();
	if (null !== xhr){
		xhr.open('GET', url, async);
		if (null !== callback && typeof callback == 'function'){
			xhr.onreadystatechange = function(){
				if (xhr.readyState == 4){
					callback(xhr);
				}
			};
		}
		xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		xhr.send(null);
	}
};

RSPlug_Misc.ajax.sendPost = function (url, data, async, callback){
	if (null === async || async){
		async = true;
	} else {
		async = false;
	}
	var xhr = RSPlug_Misc.ajax.getXhr();
	if (null !== xhr){
		xhr.open('POST', url, async);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");	
		xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		if (null !== callback && typeof callback == 'function'){
			xhr.onreadystatechange = function(){
				if (xhr.readyState == 4){
					callback(xhr);
				}
			};
		}
		xhr.send(data);
	}
};

RSPlug_Misc.ajax.sendForm = function (form, callback, formAction, formMethod){
	var xhr = RSPlug_Misc.ajax.getXhr();
	if (null === xhr){
		return false;
	}
	var data = '';
	var method = ((null !== formMethod && undefined !== formMethod && (formMethod.toLowerCase() == 'get' || formMethod.toLowerCase() == 'post')) ? formMethod.toUpperCase() : (form.method && form.method.toLowerCase() == 'get') ? 'GET' : 'POST');
	var action = (null !== formAction && undefined !== formAction ? formAction : (form.getAttribute('action') && form.getAttribute('action') != '') ? form.getAttribute('action') : location.href);
	
	var inputs = form.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++){
		if ((inputs[i].type == 'text' || inputs[i].type == 'hidden') && inputs[i].name != ''){
			data += '&' + encodeURIComponent(inputs[i].name) + '=' + encodeURIComponent(inputs[i].value);
		}
		if ((inputs[i].type == 'checkbox' || inputs[i].type == 'radio') && inputs[i].checked && inputs[i].name != ''){
			data += '&' + encodeURIComponent(inputs[i].name) + '=' + encodeURIComponent(inputs[i].value);
		}
	}
	var selects = form.getElementsByTagName('select');
	for (var i=0; i<selects.length; i++){
		if (selects[i].options.length > 0 && selects[i].name != ''){
			data += '&' + encodeURIComponent(selects[i].name) + '=' + encodeURIComponent(selects[i].options[selects[i].selectedIndex].value);
		}
	}
	var textareas = form.getElementsByTagName('textarea');
	for (var i=0; i<textareas.length; i++){
		if (textareas[i].name != ''){
			data += '&' + encodeURIComponent(textareas[i].name) + '=' + encodeURIComponent(textareas[i].value);
		}
	}
	if (data.length > 0){
		data = data.substring(1); // On retire le premier &
	}
	
	if (method == 'POST'){
		xhr.open('POST', action, true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
	} else {
		action += (action.indexOf('?') == -1 ? '?' : '&') + data;
		data = null;
		xhr.open('GET', action, true);
	}
	xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
	
	var formu = form;
	if (null !== callback && typeof callback == 'function'){
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4){
				callback(xhr, formu);
			}
		};
	}
	xhr.send(data);
	return true;
};


/** Element **/

RSPlug_Misc.byId = function (id){
	return document.getElementById(id);
};
RSPlug_Misc.$ = RSPlug_Misc.byId;

RSPlug_Misc.byClassName = function (className, element, obj, callback){
	if (typeof(obj) == 'string'){
		obj = RSPlug_Misc.byId(obj);
	}
	if (!obj){
		obj = document;
	}
	var elements = element.split(/\|/);
	var objs = [];
	for (var i=0; i<elements.length; i++){
		var elmts = obj.getElementsByTagName(elements[i]);
		for (var j=0; j<elmts.length; j++){
			if (RSPlug_Misc.css.hasClassName(elmts[j], className)){
				if (typeof(callback) === 'function'){
					callback.call(elmts[j]);
				}
				objs.push(elmts[j]);
			}
		}	
	}
	return objs;
};

RSPlug_Misc.remove = function (el){
	if (el && el.parentNode){
		el.parentNode.removeChild(el);
	}
};

RSPlug_Misc.el = function (el){
	if (typeof(el) == 'string'){
		el = document.getElementById(el);
	}
	return el;
};

RSPlug_Misc.createElement = function(elName, attribs, objParent){
	var el = document.createElement(elName);
	for (var k in attribs){
		if (typeof(attribs[k]) == 'string' || typeof(attribs[k]) == 'number'){
			if (k == 'html'){
				el.innerHTML = attribs[k];
			} else if (k == 'className') {
				el.className = attribs[k];
			} else if (k == 'value' && elName == 'textarea'){
				el.value = attribs[k];
			} else {
				el.setAttribute(k, attribs[k]);
			}
		} else if (k.substr(0, 2) == 'on' && typeof(attribs[k]) == 'function'){ // Event JS
			el[k] = attribs[k];
		} else if (k == 'style'){
			for (var j in attribs[k]){
				if (typeof(attribs[k][j]) == 'string' || typeof(attribs[k][j]) == 'number'){
					el.style[j] = attribs[k][j];
				}
			}
		}
	}
	if (typeof(objParent) == 'string'){
		objParent = document.getElementById(objParent);
	}
	if (objParent){
		objParent.appendChild(el);
	}
	return el;
};

RSPlug_Misc.selectByValue = function(sel, value){
	if (typeof(sel) == 'string'){
		sel = document.getElementById(sel);
	}
	if (sel && sel.options){
		for (var i=0; i<sel.options.length; i++){
			if (sel.options[i].value == value){
				sel.selectedIndex = i;
				return i;
			}
		}
	}
	return false;
};

RSPlug_Misc.getChilds = function (obj, nodeType, nodeName){
	if (typeof obj == 'string'){
		obj = RSPlug_Misc.byId(obj);
	}
	var childs = [];
	if (obj && obj.childNodes){
		childs = obj.childNodes;
		if ((undefined !== nodeType && null !== nodeType) || (undefined !== nodeName && null !== nodeName)){
			var cp_childs = [];
			for (var i=0; i<childs.length; i++){
				if (undefined !== nodeType && null !== nodeType && childs[i].nodeType != nodeType){
					continue;
				}
				if (undefined !== nodeName && null !== nodeName && childs[i].nodeName.toLowerCase() != nodeName.toLowerCase()){
					continue;
				}
				cp_childs.push(childs[i]);
			}
			childs = cp_childs;
		}
	}
	return childs;
};

RSPlug_Misc.insertBefore = function (obj, objReference){
	obj = RSPlug_Misc.el(obj);
	objReference = RSPlug_Misc.el(objReference);
	objReference.parentNode.insertBefore(obj, objReference);
};
RSPlug_Misc.insertAfter = function (obj, objReference){
	obj = RSPlug_Misc.el(obj);
	objReference = RSPlug_Misc.el(objReference);
	var objParent = objReference.parentNode;
	objParent.insertBefore(obj, objReference);
	objParent.insertBefore(objReference, obj);
};

RSPlug_Misc.getPos = function (obj){
	if (typeof obj == 'string'){
		obj = RSPlug_Misc.byId(obj);
	}
	var pos = {x:0, y:0};
	if (obj.offsetParent){
		while (true){ 
			pos.x += obj.offsetLeft;
			pos.y += obj.offsetTop;
			if (!obj.offsetParent){
				break;
			}
			obj = obj.offsetParent;
        }
	} else {
		if (obj.x) {
			pos.x += obj.x;
		}
		if (obj.y) {
			pos.y += obj.y;
		}
	}
	return pos;
};

RSPlug_Misc.setPos = function (obj, pos){
	if (typeof obj == 'string'){
		obj = RSPlug_Misc.byId(obj);
	}
	if (null !== pos.x && undefined !== pos.x){
		obj.style.left = pos.x +'px';
	}
	if (null !== pos.y && undefined !== pos.y){
		obj.style.top = pos.y +'px';
	}
};


/** Cookies **/

RSPlug_Misc.cookie = {};

RSPlug_Misc.cookie.set = function (cookieName, value, validity){
	if (false !== validity){
		var date_exp = new Date();
		date_exp.setTime(date_exp.getTime()+(null !== validity && undefined !== validity ? validity : 365*24*3600*1000));
	}
	document.cookie = cookieName + '=' + escape(value) + ';' + (false !== validity ? ' expires=' + date_exp.toGMTString() + ';' : '') + ' path=/';
};

RSPlug_Misc.cookie.del = function (cookieName){
	var date_exp = new Date();
	date_exp.setTime(date_exp.getTime()-3600);
	document.cookie = cookieName + '=; expires=' + date_exp.toGMTString() + '; path=/';
};

RSPlug_Misc.cookie.get = function (cookieName){
	var arg = cookieName + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg){
			return RSPlug_Misc.cookie._getVal(j);
		}
		i = document.cookie.indexOf(' ', i) + 1;
		if (i == 0){
			break;
		}
	}
	return null;
};

RSPlug_Misc.cookie._getVal = function (offset){
	var endstr = document.cookie.indexOf (';', offset);		
	if (endstr == -1){
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
};



/** ScrollY **/

RSPlug_Misc.onScrollY = function (callback, from, to){
	RSPlug_Misc.addEvent(window, 'scroll', function(){ RSPlug_Misc._onScrollY(from, to, callback); });
	if (null === from || from <= 0){
		RSPlug_Misc._onScrollY(from, to, callback);
	}
	return RSPlug_Misc;
};
RSPlug_Misc._onScrollY = function (from, to, callback){
	var scrollY = RSPlug_Misc.getScrollY();
	if ((null !== from && scrollY < from) || (null !== to && scrollY > to)){
		return false;
	}
	callback(scrollY);
};
RSPlug_Misc.getScrollY = function (){
	var db = !document.documentElement.clientWidth ? document.body : document.documentElement;
	return (db.scrollTop) ? db.scrollTop : window.pageYOffset;
};


/** Easing (http://www.robertpenner.com/easing/) **/

RSPlug_Misc.Easing = {};

RSPlug_Misc.Easing.linear = function (t, b, c, d){ return c*t/d + b; };

RSPlug_Misc.Easing.inQuad = function (t, b, c, d){ return c*(t/=d)*t + b; };
RSPlug_Misc.Easing.outQuad = function (t, b, c, d){ return -c * (t/=d)*(t-2) + b; };
RSPlug_Misc.Easing.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;
};

RSPlug_Misc.Easing.inCubic = function (t, b, c, d){ return c * Math.pow (t/d, 3) + b; };
RSPlug_Misc.Easing.outCubic = function (t, b, c, d){ return c * (Math.pow (t/d-1, 3) + 1) + b; };
RSPlug_Misc.Easing.inOutCubic = function (t, b, c, d){
	if ((t/=d/2) < 1) return c/2 * Math.pow (t, 3) + b;
	return c/2 * (Math.pow (t-2, 3) + 2) + b;
};

RSPlug_Misc.Easing.inQuart = function (t, b, c, d){ return c * Math.pow (t/d, 4) + b; };
RSPlug_Misc.Easing.outQuart = function (t, b, c, d){ return -c * (Math.pow (t/d-1, 4) - 1) + b; };
RSPlug_Misc.Easing.inOutQuart = function (t, b, c, d){
	if ((t/=d/2) < 1) return c/2 * Math.pow (t, 4) + b;
	return -c/2 * (Math.pow (t-2, 4) - 2) + b;
};

RSPlug_Misc.Easing.inSine = function (t, b, c, d){ return c * (1 - Math.cos(t/d * (Math.PI/2))) + b; };
RSPlug_Misc.Easing.outSine = function (t, b, c, d){ return c * Math.sin(t/d * (Math.PI/2)) + b; };
RSPlug_Misc.Easing.inOutSine = function (t, b, c, d){ return c/2 * (1 - Math.cos(Math.PI*t/d)) + b; };

RSPlug_Misc.Easing.inExpo = function (t, b, c, d){ return c * Math.pow(2, 10 * (t/d - 1)) + b; };
RSPlug_Misc.Easing.outExpo = function (t, b, c, d){ return c * (-Math.pow(2, -10 * t/d) + 1) + b; };
RSPlug_Misc.Easing.inOutExpo = function (t, b, c, d){
	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;
};

RSPlug_Misc.Easing.inCirc = function (t, b, c, d){ return c * (1 - Math.sqrt(1 - (t/=d)*t)) + b; };
RSPlug_Misc.Easing.outCirc = function (t, b, c, d){ return c * Math.sqrt(1 - (t=t/d-1)*t) + b; };
RSPlug_Misc.Easing.inOutCirc = function (t, b, c, d){
	if ((t/=d/2) < 1) return c/2 * (1 - Math.sqrt(1 - t*t)) + b;
	return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
};


/** Animation **/

RSPlug_Misc.Motion = function(callback, func, begin, finish, duration, frames){
	this.init();
	this.setCallback(callback);
	this.setFunc(func);
	this.setBegin(begin);
	this.setFinish(finish);
	this.setFrames(undefined === frames || null === frames || isNaN(frames) ? 24 : frames);
	this.setDuration(duration);
};
RSPlug_Misc.Motion.prototype = {
	init: function(){
		this.started = false;
		this._listeners = {};
		this._change = this.t = this.begin = this.finish = this.duration = this._duration = 0;
		this.framesSec = this._framesSec = 1;
	},
	addListener: function(eventName, callback){
		if (typeof(callback) == 'function'){
			if (!this._listeners[eventName]){
				this._listeners[eventName] = [callback];
			} else {
				this._listeners[eventName].push(callback);
			}
		}
		return this;
	},
	fire: function(eventName){
		if (this._listeners[eventName]){
			for (var i=0; i<this._listeners[eventName].length; i++){
				this._listeners[eventName][i].apply(this);
			}
		}
	},
	setCallback: function(callback){
		this.callback = (typeof(callback) == 'function' ? callback : function(){});
		return this;
	},
	setFunc: function(func){
		this.func = RSPlug_Misc.Easing[func] || RSPlug_Misc.Easing.linear;
		return this;
	},
	setBegin: function(begin){
		this.begin = parseInt(begin);
		this._updateChange();
		return this;
	},
	setFinish: function(finish){
		this.finish = parseInt(finish);
		this._updateChange();
		return this;
	},
	setDuration: function(duration){ // Set in milliseconds
		this._duration = duration;
		this.duration = Math.round(duration *this._framesSec /1000);
		return this;
	},
	setFrames: function(frames){ // Set in images / seconds
		if (!isNaN(frames)){
			this._framesSec = Math.abs(frames);
			this.framesSec = Math.round(1000 / this._framesSec);
			this.setDuration(this._duration);
		}
		return this;
	},
	start: function(){
		if (!this.started){
			this.started = true;
			this.t = 0;
			this._process();
			this.fire('start');
		}
		return this;
	},
	rewind: function(t){
		this.t = (undefined === t) ? 0 : t;
		this.fire('rewind');
		return this;
	},
	resume: function(){
		if (!this.started){
			this.started = true;
			this._process(this.t);
			this.fire('resume');
		}
		return this;
	},
	stop: function(){
		if (this.started){
			this.started = false;
			this.fire('stop');
		}
		return this;
	},
	
	_updateChange: function(){
		this._change = (this.finish - this.begin);
		return this;
	},
	_process: function(){
		if (this.started){
			if (this.t > this.duration){
				this.stop();
				this.fire('finish');
			} else {
				if (this.t < 0){
					this.rewind();
				}
				if (this.t == Math.floor(this.duration / 2)){
					this.fire('middle');
				}
				if (this.t == this.duration){
					this.callback.call(this, this.finish, this.begin, this.finish);
				} else {
					this.callback.call(this, Math.round(this.func(this.t, this.begin, this._change, this.duration)), this.begin, this.finish);
				}
				this.t++;
				if (this.t <= this.duration){
					var inst = this;
					setTimeout(function(){ inst._process(); }, this.framesSec);
				} else {
					this.stop();
					this.fire('finish');
				}
			}
		}
		return this;
	}
};


/** Scroll **/

RSPlug_Misc.scroll = function(obj){
	this.setObj(obj);
};
RSPlug_Misc.scroll.prototype.setObj = function(obj){
	if (typeof(obj) == 'string'){
		obj = document.getElementById(obj);
	}
	this.obj = obj;
};
RSPlug_Misc.scroll.prototype.getScrollTop = function(){
	if (this.obj == window){
		var bs = RSPlug_Misc.css.browserScroll();
		return bs.top;
	}
	return this.obj.scrollTop;
};
RSPlug_Misc.scroll.prototype.getScrollHeight = function(){
	if (this.obj == window){
		var bfs = RSPlug_Misc.css.browserFullSize();
		return (bfs.y - bfs.h);
	}
	return (this.obj.scrollHeight - this.obj.offsetHeight);
};
RSPlug_Misc.scroll.prototype.getScrollLeft = function(){
	if (this.obj == window){
		var bs = RSPlug_Misc.css.browserScroll();
		return bs.left;
	}
	return this.obj.scrollLeft;
};
RSPlug_Misc.scroll.prototype.getScrollWidth = function(){
	if (this.obj == window){
		var bfs = RSPlug_Misc.css.browserFullSize();
		return (bfs.x - bfs.w);
	}
	return (this.obj.scrollWidth - this.obj.offsetWidth);
};
RSPlug_Misc.scroll.prototype.scrollToY = function(scrollTop, speed, motionType, onFinishCallback){
	if (null === speed || undefined === speed || isNaN(speed) || speed <= 0){
		speed = null;
	}
	var sh = this.getScrollHeight();
	var st = this.getScrollTop();
	if (scrollTop == 'max'){
		scrollTop = sh;
	}
	if (st != scrollTop){
		if (scrollTop > sh){
			scrollTop = sh;
		}
		else if (scrollTop < 0){
			scrollTop = 0;
		}
		if (null === speed){
			this.obj.scrollTo(this.getScrollLeft(), scrollTop);
		} else {
			var motionType = (undefined !== motionType) ? motionType : 'linear';
			var distance = scrollTop - st;
			if (distance != 0){
				var inst = this;
				var ScrollMotion = new RSPlug_Misc.Motion(function(pos){ inst.scrollToY(pos); }, motionType, st, (st+distance), speed);
				if (null !== onFinishCallback && typeof(onFinishCallback) == 'function'){
					ScrollMotion.addListener('finish', function(){ onFinishCallback.call(); });
				}
				ScrollMotion.start();
			}
		}
	}
	else if (null !== onFinishCallback && typeof(onFinishCallback) == 'function'){
		onFinishCallback.call();
	}
	return this.getScrollTop();
};
RSPlug_Misc.scroll.prototype.scrollToX = function(scrollLeft){
	if (scrollLeft == 'max'){
		scrollLeft = this.getScrollWidth();
	}
	if (this.getScrollLeft() != scrollLeft){
		if (scrollLeft > this.getScrollWidth()){
			scrollLeft == this.getScrollWidth();
		}
		else if (scrollLeft < 0){
			scrollLeft = 0;
		}
		this.obj.scrollTo(scrollLeft, this.getScrollTop());
	}
	return this.getScrollLeft();
};
RSPlug_Misc.scroll.prototype.scrollToDown = function(step){
	this.scrollToY(this.getScrollTop() +step);
};
RSPlug_Misc.scroll.prototype.scrollToUp = function(step){
	this.scrollToY(this.getScrollTop() -step);
};
RSPlug_Misc.scroll.prototype.scrollToLeft = function(step){
	this.scrollToX(this.getScrollLeft() +step);
};
RSPlug_Misc.scroll.prototype.scrollToRight = function(step){
	this.scrollToX(this.getScrollLeft() -step);
};
RSPlug_Misc.scroll.prototype.registerAuto = function(obj, direction, eventName, step, speed){
	return new RSPlug_Misc.scrollerAuto(obj, this, direction, eventName, step, speed);
};

RSPlug_Misc.scrollerAuto = function(obj, scroller, direction, eventName, step, speed){
	this.step = (!isNaN(step) && step > 0) ? step : 5;
	this.speed = (!isNaN(speed) && speed > 0) ? speed : 20;
	if (typeof(obj) == 'string'){
		obj = document.getElementById(obj);
	}
	if (!obj){
		return null;
	}
	this.obj = obj;
	this.scroller = scroller;
	this.direction = direction;
	this.scrollActive = false;
	this._registerEvent(eventName);
	this.gradientSpeed = false;
	this.eventObj = null;
};
RSPlug_Misc.scrollerAuto.prototype.initObj = function(){
	this.objPos = RSPlug_Misc.getPos(this.obj);
	this.objOH = this.obj.offsetHeight;
	this.objOW = this.obj.offsetWidth;
	return this;
};
RSPlug_Misc.scrollerAuto.prototype.setStep = function(step){
	if (!isNaN(step) && step > 0){
		this.step = step;
	}
	return this;
};
RSPlug_Misc.scrollerAuto.prototype.setSpeed = function(speed){
	if (!isNaN(speed) && speed > 0){
		this.speed = speed;
	}
	return this;
};
RSPlug_Misc.scrollerAuto.prototype.setGradientSpeed = function(stepStart, stepEnd){
	this.gradientSpeed = true;
	if (isNaN(stepStart) || stepStart <= 0){
		stepStart = 1;
	}
	if (isNaN(stepEnd) || stepEnd <= 0){
		stepEnd = 2;
	}
	if (stepStart < stepEnd){
		var tmp = stepStart;
		stepStart = stepEnd;
		stepEnd = tmp;
	}
	this.gradientSpeedStart = stepStart;
	this.gradientSpeedEnd = stepEnd;
	this.gradientSpeedCoef = stepEnd - stepStart;
	return this;
};
RSPlug_Misc.scrollerAuto.prototype.getStepFromGradientSpeed = function(step, e, bXAxis, bInverse){
	var step = this._getStepFromGradientSpeed(step, e, bXAxis, bInverse);
	return ((step -1) * this.gradientSpeedCoef) + this.gradientSpeedStart;
};
RSPlug_Misc.scrollerAuto.prototype._getStepFromGradientSpeed = function(step, e, bXAxis, bInverse){
	if ((bXAxis && !this.objOW) || (!bXAxis && !this.objOH)){
		return 1;
	}
	if (e.pageX || e.pageY){
		var posX = (e.pageX -this.objPos.x);
		var posY = (e.pageY -this.objPos.y);
	} else {
		var posX = e.offsetX;
		var posY = e.offsetY;
	}
	if (bXAxis){
		if (!bInverse){
			return (((this.objOW - posX) / this.objOW) +1);
		} else {
			return ((posX / this.objOW) +1);
		}
	} 
	else {
		if (!bInverse){
			return (((this.objOH - posY) / this.objOH) +1);
		} else {
			return ((posY / this.objOH) +1);
		}
	}
};
RSPlug_Misc.scrollerAuto.prototype._scroll = function(){
	if (this.scrollActive){
		var step = this.step;
		var evt = this.eventObj;
		if (this.gradientSpeed && null !== evt){
			if (this.direction == 'up' || this.direction == 'down'){
				step = this.getStepFromGradientSpeed(step, evt, false, (this.direction == 'up' || this.direction == 'left'));
			} else {
				step = this.getStepFromGradientSpeed(step, evt, true, (this.direction == 'up' || this.direction == 'left'));
			}
		}
		switch (this.direction){
			case 'up': this.scroller.scrollToUp(step); break;
			case 'down': this.scroller.scrollToDown(step); break;
			case 'left': this.scroller.scrollToLeft(step); break;
			case 'right': this.scroller.scrollToRight(step); break;
		}
		var inst = this;
		setTimeout(function(){ inst._scroll(); }, this.speed);
	}
};
RSPlug_Misc.scrollerAuto.prototype._registerEvent = function(eventName){
	var inst = this;
	switch (eventName){
		case 'over':
		case 'out':
			this.obj.onmousemove = function(e){
				inst.eventObj = (e ? e : window.event);
			};
			this.obj.onmouseover = function(e){
				if (!inst.scrollActive){
					inst.initObj();
					inst.eventObj = (e ? e : window.event);
					inst.scrollActive = true;
					inst._scroll();
				}
			};
			this.obj.onmouseout = function(){
				inst.scrollActive = false;
			};
			break;
	}
};


/** TIMERS **/

RSPlug_Misc._oTimeouts = {};
RSPlug_Misc.setTimeout = function(sIdTimeout, fCallback, mSpeed, bRecursif){
	if (typeof(fCallback) == 'function'){
		RSPlug_Misc.clearTimeout(sIdTimeout);
		var mSpeedRecursif = (typeof(mSpeed) == 'function' ? mSpeed() : mSpeed); 
		if (bRecursif === true){
			RSPlug_Misc._oTimeouts[sIdTimeout] = setTimeout(function(){ fCallback(); RSPlug_Misc.setTimeout(sIdTimeout, fCallback, mSpeed, bRecursif); }, mSpeedRecursif);
		} else {
			RSPlug_Misc._oTimeouts[sIdTimeout] = setTimeout(function(){ fCallback(); }, mSpeedRecursif);
		}
	}
};
RSPlug_Misc.hasTimeout = function(sIdTimeout){
	return (RSPlug_Misc._oTimeouts[sIdTimeout]);
};
RSPlug_Misc.clearTimeout = function(sIdTimeout){
	if (RSPlug_Misc._oTimeouts[sIdTimeout]){
		clearTimeout(RSPlug_Misc._oTimeouts[sIdTimeout]);
		RSPlug_Misc._oTimeouts[sIdTimeout] = null;
	}
};


/** STRING : CHAINES DE CARACTERES **/

RSPlug_Misc.string = {};

RSPlug_Misc.string.trim = function (myString){
	return myString.replace(/^\s+/g, '').replace(/\s+$/g, '');
};


/** CSS **/

RSPlug_Misc.css = {
	initAddClassOnHover: function (element, tagName, className, classNameOnHover, toUp){
		if (null === classNameOnHover || undefined === classNameOnHover){
			classNameOnHover = className;
		}
		if (typeof element == 'string') element = document.getElementById(element);
		if (element){
			var tags = element.getElementsByTagName(tagName);
			for (var i=0; i<tags.length; i++){
				if (RSPlug_Misc.css.hasClassName(tags[i], className)){
					var rel = tags[i].getAttribute('rel');
					if (null !== rel && undefined !== rel && !isNaN(rel) && 0 >= rel && '' != rel){
						RSPlug_Misc.css.addClassOnHover(tags[i], classNameOnHover, rel);
					} else {
						RSPlug_Misc.css.addClassOnHover(tags[i], classNameOnHover, toUp);
					}
				}
			}
		}
	},

	addClassOnHover: function (element, className, toUp){
		if (typeof element == 'string') element = document.getElementById(element);
		var elementToUp = element;
		if (null !== toUp && undefined !== toUp && !isNaN(toUp) && 0 < toUp){
			i = toUp;
			do {
				var elementToUp = elementToUp.parentNode;
				i--;
			} while (i > 0 && elementToUp.parentNode);
		}
		RSPlug_Misc.addEvent(element, 'mouseover', function (){ RSPlug_Misc.css.addClassName(elementToUp, className); });
		RSPlug_Misc.addEvent(element, 'mouseout', function (){ RSPlug_Misc.css.removeClassName(elementToUp, className); });
	},

	show: function (element){
		if (typeof element == 'object' && element instanceof Array){
			var retour = false;
			for (i in element){
				if ('function' !== typeof(element[i])){
					if (RSPlug_Misc.css.show(element[i])){
						retour = true;
					}
				}
			}
			return retour;
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element && element.style){
				element.style.display = '';
				return true;
			}
			return false;
		}
	},

	hide: function (element){
		if (typeof element == 'object' && element instanceof Array){
			var retour = false;
			for (i in element){
				if ('function' !== typeof(element[i])){
					if (RSPlug_Misc.css.hide(element[i])){
						retour = true;
					}
				}
			}
			return retour;
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element && element.style){
				element.style.display = 'none';
				return true;
			}
			return false;
		}
	},

	toogle: function (element, callbackShow, callbackHide){
		if (typeof element == 'object' && element instanceof Array){
			var retour = false;
			for (i in element){
				if ('function' !== typeof(element[i])){
					if (RSPlug_Misc.css.toogle(element[i], callbackShow, callbackHide)){
						retour = true;
					}
				}
			}
			return retour;
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element && element.style){
				if (element.style.display == 'none'){
					element.style.display = '';
					if (typeof callbackShow == 'function'){
						callbackShow.call(element, element);
					}
				} else {
					element.style.display = 'none';
					if (typeof callbackHide == 'function'){
						callbackHide.call(element, element);
					}
				}
				return true;
			}
			return false;
		}
	},

	visible: function (element){
		if (typeof element == 'object' && element instanceof Array){
			var retour = false;
			for (i in element){
				if ('function' !== typeof(element[i])){
					if (RSPlug_Misc.css.visible(element[i])){
						retour = true;
					}
				}
			}
			return retour;
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element && element.style){
				element.style.visibility = 'visible';
				return true;
			}
			return false;
		}
	},
	
	hidden: function (element){
		if (typeof element == 'object' && element instanceof Array){
			var retour = false;
			for (i in element){
				if ('function' !== typeof(element[i])){
					if (RSPlug_Misc.css.hidden(element[i])){
						retour = true;
					}
				}
			}
			return retour;
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element && element.style){
				element.style.visibility = 'hidden';
				return true;
			}
			return false;
		}
	},
	
	opacity: function (element, opacity){ // Opacity compris entre 0 et 100
		if (typeof element == 'object' && element instanceof Array){
			var retour = false;
			for (i in element){
				if ('function' !== typeof(element[i])){
					if (RSPlug_Misc.css.opacity(element[i], opacity)){
						retour = true;
					}
				}
			}
			return retour;
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element && element.style){
				RSPlug_Misc.css._opacity(element, opacity);
				return true;
			}
			return false;
		}
	},
	
	_opacity: function (element, opacity){
		if (element && element.style){
			opacity = Math.round(opacity);
			element.style.opacity = (opacity /100);
			if (navigator.userAgent.toLowerCase().indexOf('msie') >= 0){
				element.style.filter = 'alpha(opacity=' + opacity + ')';
			}
		}
	},

	fadeIn: function (element, speed, onFinishCallback, motionType){
		if (undefined === motionType || null === motionType){
			motionType = 'inOutCubic'; 
		}
		if (typeof element == 'string') element = document.getElementById(element);
		if (element && element.style){
			
			var motion = new RSPlug_Misc.Motion(
				function(op, start, end){
					RSPlug_Misc.css._opacity(element, op);
				}, 
				motionType, 0, 100, speed);
			
			if (typeof(onFinishCallback) == 'function'){
				motion.addListener('finish', function(){
					onFinishCallback.call(element, element);
				});
			}
			motion.start();
			/*
			var nbSteps = speed /1000 *30;
			var i=0;
			while (true){
				var opacity = (i >= nbSteps) ? 100 : (i *100 /nbSteps);
				(function(op, s){
					setTimeout(function(){ RSPlug_Misc.css._opacity(element, op); }, s);
				})(opacity, speed/nbSteps*i);
				if (i > nbSteps){
					if (typeof(onFinishCallback) == 'function'){
						setTimeout(function(){ onFinishCallback(element); }, speed);
					}
					break;
				}
				i++;
			}*/
		}
	},
	
	fadeOut: function (element, speed, onFinishCallback, motionType){
		if (undefined === motionType || null === motionType){
			motionType = 'inOutCubic'; 
		}
		if (typeof element == 'string') element = document.getElementById(element);
		if (element && element.style){
			
			var motion = new RSPlug_Misc.Motion(
				function(op, start, end){
					RSPlug_Misc.css._opacity(element, op);
				}, 
				motionType, 100, 0, speed);
			
			if (typeof(onFinishCallback) == 'function'){
				motion.addListener('finish', function(){
					onFinishCallback.call(element, element);
				});
			}
			motion.start();
			/*
			var nbSteps = speed /1000 *30;
			var i=0;
			while (true){
				var opacity = (i >= nbSteps) ? 0 : (100-(i *100 /nbSteps));
				(function(op, s){
					setTimeout(function(){ RSPlug_Misc.css._opacity(element, op); }, s);
				})(opacity, speed/nbSteps*i);
				if (i > nbSteps){
					if (typeof(onFinishCallback) == 'function'){
						setTimeout(function(){ onFinishCallback(element); }, speed);
					}
					break;
				}
				i++;
			}
			*/
		}
	},

	hasClassName: function (element, className){
		if (typeof element == 'object' && element instanceof Array){
			var retour = false;
			for (i in element){
				if ('function' !== typeof(element[i])){
					if (RSPlug_Misc.css.removeClassName(element[i], className)){
						retour = true;
					}
				}
			}
			return retour;
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element && element.className){
				var classes = element.className.split(/\s+/);
				for (var i=0; i<classes.length; i++){
					if (classes[i] == className){
						return true;
					}
				}
			}
			return false;
		}
	},

	removeClassName: function (element, className){
		if (typeof(element) == 'object' && element instanceof Array){
			for (i in element){
				if ('function' !== typeof(element[i])){
					RSPlug_Misc.css.removeClassName(element[i], className);
				}
			}
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element){
				var classes = element.className.split(/\s+/);
				element.className = '';
				for (var i=0; i<classes.length; i++){
					if (classes[i] != className){
						element.className += (element.className == '' ? '' : ' ') + classes[i];
					}
				}
			}
		}
	},

	addClassName: function (element, className){
		if (typeof element == 'object' && element instanceof Array){
			for (i in element){
				if ('function' !== typeof(element[i])){
					RSPlug_Misc.css.addClassName(element[i], className);
				}
			}
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element && !RSPlug_Misc.css.hasClassName(element, className)){
				element.className += (element.className == '' ? '' : ' ') + className;
			}
		}
	},

	replaceClassName: function (element, className1, className2){
		if (typeof element == 'object' && element instanceof Array){
			for (i in element){
				if ('function' !== typeof(element[i])){
					RSPlug_Misc.css.replaceClassName(element[i], className1, className2);
				}
			}
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element){
				if (RSPlug_Misc.css.hasClassName(element, className1)){
					RSPlug_Misc.css.removeClassName(element, className1);
					RSPlug_Misc.css.addClassName(element, className2);
				}
			}
		}
	},

	toogleClassName: function (element, className1, className2){
		if (typeof element == 'object' && element instanceof Array){
			for (i in element){
				if ('function' !== typeof(element[i])){
					RSPlug_Misc.css.toogleClassName(element[i], className1, className2);
				}
			}
		}
		else {
			if (typeof element == 'string') element = document.getElementById(element);
			if (element){
				if (RSPlug_Misc.css.hasClassName(element, className1)){
					RSPlug_Misc.css.removeClassName(element, className1);
					RSPlug_Misc.css.addClassName(element, className2);
				} else {
					RSPlug_Misc.css.removeClassName(element, className2);
					RSPlug_Misc.css.addClassName(element, className1);
				}
			}
		}
	},
	
	browserFullSize: function (){
		var h, w, x, y;
		// w et h correspondent à la taille de l'espace "visible" de la page
		// x et y correspondent à la taille complète de la page
		// le scroll doit être obtenu par browserScroll

		if (window.innerHeight && window.scrollMaxY) {
			x = document.body.scrollWidth;
			y = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			x = document.body.scrollWidth;
			y = document.body.scrollHeight;
		} else {
			x = document.body.offsetWidth;
			y = document.body.offsetHeight;
		}

		if (self.innerHeight){
			w = self.innerWidth;
			h = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight){
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		} else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}

		if (y < h)			y = h;
		if (x < w)			x = w;

		return {x: x, y: y, w: w, h: h};
	},

	browserScroll: function (){
		var l, t;
		if (self.pageYOffset){
			l = self.pageXOffset;
			t = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			l = document.documentElement.scrollLeft;
			t = document.documentElement.scrollTop;
		} else {
			l = document.body.scrollLeft;
			t = document.body.scrollTop;
		}
		return {left: l, top: t};
	}
};

var $RSPM = RSPlug_Misc;


/** CORRECTIF IE **/
if (!Array.indexOf){
    Array.prototype.indexOf = function (obj){
        for (var i=0; i<this.length; i++){
            if (this[i] == obj){
                return i;
            }
        }
        return -1;
    }
}

