(function(){


var cookie = function(id){
	this.id = id;
}

cookie.prototype = {

	set: function(name, value, days) {
		var expire = new Date();
		days = days || 30;
		expire.setTime((new Date).getTime() + 3600000 * 24 * days);
		document.cookie	= (this.id+ name) + "="+escape(value) + ";expires="+expire.toGMTString();
	},

	get: function(name) {
		var dc = document.cookie;
		document.cookie = '';
		var prefix = (this.id+name) + "=";

		var from = dc.indexOf("; " + prefix);
		if (from == -1) {
			from =	dc.indexOf(prefix);
			if (from != 0)	return null;
		} else {
			from += 2;
		}
		var end	= document.cookie.indexOf(";", from);
		if (end	== -1) {
			end = dc.length;
		}
		var value = unescape(dc.substring(from + prefix.length, end));
		try {
			var x;
			eval( "x = " + value );
			return x;
		}
		catch(e){};

		return value;
	},
	remove: function(name) {
		this.set(name, '', -100);
	}
};

var reqGET = function(url, onSuccess, onError){
	var req;
	if (window.XMLHttpRequest) req = new XMLHttpRequest();
	else if	(window.ActiveXObject)
	{
		req = new ActiveXObject("Msxml2.XMLHTTP");
		if (!req) req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(!req)
		throw "Unable to create ajax request object";

	var res = {};
	req.onreadystatechange = function() {
		if(req.readyState != 4) return;
		res.done = true;
		res.readyState = req.readyState;
		res.responseText = req.responseText;
		if (req.parseError && req.parseError.errorCode != 0) {
			res.errorMsg = req.parseError.reason;
			res.error = true;
		}
		else
			res.success = true;
	}

	req.open("GET", url, true);
	var done = function(){
		if(res.done) {
			clearInterval(tm);
			res.success && onSuccess && onSuccess(res);
			res.error && onError && onError(res);
		}
	}
	var tm = setInterval(done, 10);
	try{
		req.send(null);
	} catch(e){
		res.done = true;
		done();
	}
}

var sl = window.Slider = function(name, container, o) {
//return
	elem = document.getElementById(name);
	container = document.getElementById(container);
	if(!elem || !container) return;
	this.options = o;
	this.name = name;

	var pref = name + window.location.href.toString().replace(/(.*\/)[^\/]+$/, "$1").length;
	this.cookie = new cookie(pref);

	var self = this;

	elem.style.position = 'relative';
	this.element = elem;
	var vis = this.visible();
	this.initialVisible = vis;

	if(vis){
		if(o.content && o.onVisibleGetContent != 'display' ){
			w = 1;
		} else {
			var w = Math.round( Math.abs( o.dx.visible - o.dx.hidden ));
		}
	} else {
		w = 1;
		container.style.display = 'none';
	}
	container.style.width = w + 'px';
	container.style.overflow = 'hidden';
	this.container = container;

	var oCallback = {
		onChange: function(){
                	var prop = this.container.resize;
                	var pos = Math.round(Math.abs(this._pos + this.container.size)) + 1;
                	self.container.style[prop] = pos + "px";
		}
	}

	var anim = o.animation;
	anim = typeof anim == 'number' ? Tween[Slider.animFunctions[anim]] : typeof anim == 'string' ? Tween[anim] : anim;
	if(typeof anim != 'function')
		anim = Tween.backEaseOut;

	if(o.dx && o.dx.visible !== null && o.dx.hidden !== null){
		var options = {
			property: "left",
			fn: anim,
			duration: o.duration / 1000
		};

		var dx = new Tween(this.element.style, options, oCallback);

		dx.container = {
			size: Math.round( Math.abs( o.dx.visible - o.dx.hidden )),
			resize: "width"
		};

		this.dx = dx;
		this.first = true;
	}
	if(vis) {
		this.onDocumentReady(function(){
			self.start();
		});
	}
};

sl.prototype = {

	// set start animation
	start: function(){

		var
			o = this.options,
			self = this;

		if(o.content && !this.loaded ){
			reqGET(o.content,
				function(res){

					var left_panel_content = res.responseText;
					self.element.innerHTML = left_panel_content + self.element.innerHTML;
					self.loaded = true;
					self.visible(true);
					var o = self.options;
					o.onShow && o.onShow();
					self.container.style.display = 'block';

					if(self.initialVisible === true && o.onVisibleGetContent === 'display')
						return;

					self.startAnim();
				}
			);
		}
		else {
			self.container.style.display = 'block';
			if(!self.loaded && self.initialVisible === true && o.onVisibleGetContent === 'display'){
				var w = Math.round( Math.abs( o.dx.visible - o.dx.hidden ));
				self.container.style.width = w + 'px';
				self.container.style.overflow = 'hidden';
			} else {
				var vis = this.visible();
				if(!self.loaded && self.initialVisible === true)
					vis = !vis;
				this.visible(!vis);
				self.startAnim(vis);
			}
			self.loaded = true;
		}
	},

	startAnim: function(hide){
		var
			dx = this.dx,
			dy = this.dy,
			o = this.options,
			s, e;

		if(!dx.completed || (!this.first && o.toggle === false)) return false;

		if(hide) {
			s = o.dx.visible;
			e = o.dx.hidden;
			this.options.onHide && this.options.onHide();
		} else {
			s = o.dx.hidden;
			e = o.dx.visible;
			this.options.onShow && this.options.onShow();
		}

		this.first = false;

		dx._start = dx._pos = s;
		dx.setFinish(e);
		dx.start();
	},

	onDocumentReady: function(callback) {
		if(window.attachEvent)
			window.attachEvent("onload", callback)
		else if(window.addEventListener)
			window.addEventListener("load", callback, false)
	},

	// get/set visible state into/from cookies
	visible: function(state){
		var c = this.cookie;
		if(state == null){
			var v = c.get("_state");
			v = v !== null ? v : this.options.defaultVisible == 'hidden' ? false : true;
			return v;
		} else {
			c.set("_state", !!state);
		}
		return v;
	}
}


var Delegate = function (o, f) {
	return function() {
		f.apply(o, arguments);
	}
}

Tween = function(){
	this.init.apply(this, arguments);
}
var t = Tween.prototype = {
	obj: {},
	prop: '',
	func: function (t, b, c, d) { return c*t/d + b; },
	looping: false,
	name: '',
	_listeners: [],

	init: function(obj, options, oCallback){
		var o = options;
		this._listeners = []
		this.addListener(this);
		this.obj = obj;
		this.prop = o.property;
		this._start = o.start;
		this._pos = o.start;
		this.oCallback = oCallback || {};
		this.setDuration(o.duration);
		this._func = o.fn || this.func;
		this.completed = true;
		this.setFinish(o.end);
	},

	setTime: function(t){
		this.prevTime = this._time;
		if (t > this.getDuration()) {
			if (this.looping) {
				this.rewind (t - this._duration);
				this.update();
				this.fire('onLoop');
			} else {
				this._time = this._duration;
				this.update();
				this.stop();
				this.completed = true;
				this.fire('onFinish');
			}
		} else if (t < 0) {
			this.rewind();
			this.update();
		} else {
			this._time = t;
			this.update();
		}
	},

	getTime: function(){
		return this._time;
	},

	setDuration: function(d){
		this._duration = d;
	},

	getDuration: function(){
		return this._duration;
	},

	setPosition: function(p){
		this.prevPos = this._pos;
		this.obj[this.prop] = Math.round(p);
		this._pos = p;
		this.fire('onChange');
	},

	getPosition: function(t){
		if (t == undefined) t = this._time;
		return this._func(t, this._start, this.change, this._duration);
	},

	setFinish: function(f){
		this.change = f - this._start;
	},
	getFinish: function(){
		return this._start + this.change;
	},
	start: function(){
		this.rewind();
		this.fire('onStart');
		this.completed = false;
		this.startEnterFrame();
	},
	rewind: function(t){
		this.stop();
		this._time = (t == undefined) ? 0 : t;
		this.fixTime();
		this.update();
	},
	forward: function(){
		this._time = this._duration;
		this.fixTime();
		this.update();
	},
	update: function(){
		this.setPosition(this.getPosition(this._time));
	},
	startEnterFrame: function(){
		this.isPlaying = true;
		this.onEnterFrame();
	},
	onEnterFrame: function(){
		if(this.isPlaying) {
			this.setTime((this.getTimer() - this._startTime) / 1000);
			setTimeout(Delegate(this, this.onEnterFrame), 0);
		}
	},
	stop: function(){
		this.isPlaying = false;
		this.fire('onStop');
	},

	continueTo: function(finish, duration){
		this._start = this._pos;
		this.setFinish(finish);
		if (this._duration != undefined)
			this.setDuration(duration);
		this.start();
	},

	resume: function(){
		this.fixTime();
		this.startEnterFrame();
		this.fire('onResume');
	},

	addListener: function(o){
		this.removeListener (o);
		return this._listeners.push(o);
	},

	removeListener: function(o){
		var a = this._listeners;
		var i = a.length;
		while (i--) {
			if (a[i] == o) {
				a.splice (i, 1);
				return true;
			}
		}
		return false;
	},

	fire: function(type){
		this.oCallback[type] && this.oCallback[type].apply(this);
	},

	fixTime: function(){
		this._startTime = this.getTimer() - this._time * 1000;
	},
	getTimer: function(){
		return new Date().getTime() - this._time;
	}
}

t._start = t.change = t.prevTime = t.prevPos = t._duration = t._time = t._pos = t._position = t._startTime = t._finish = 0;
sl.animFunctions =
	["backEaseIn","backEaseOut", "backEaseInOut","elasticEaseIn", "elasticEaseOut",
	"elasticEaseInOut", "bounceEaseOut", "bounceEaseIn", "bounceEaseInOut","strongEaseInOut",
	"regularEaseIn", "regularEaseOut","regularEaseInOut","strongEaseIn","strongEaseOut",
	"strongEaseInOut"];

var a =	{
	0: "var s = 1.70158; return c*(t/=d)*t*((s+1)*t - s) + b;",
	1: "var s = 1.70158; return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;",
	2: "var s = 1.70158; if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;",
	3: "if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3; if (!a || a < Math.abs(c)) { a=c; var s=p/4; } else {var 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;",
	4: "if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3; if (!a || a < Math.abs(c)) { a=c; var s=p/4; } else 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);",
	5: "if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) var p=d*(.3*1.5); if (!a || a < Math.abs(c)) {var a=c; var s=p/4; } else 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;",
	6: "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;}",
	7: "return c - Tween.bounceEaseOut (d-t, 0, c, d) + b;",
	8: "if (t < d/2) return Tween.bounceEaseIn (t*2, 0, c, d) * .5 + b; else return Tween.bounceEaseOut (t*2-d, 0, c, d) * .5 + c*.5 + b;",
	9: "return c*(t/=d)*t*t*t*t + b;",
	10: "return c*(t/=d)*t + b;",
	11: "return -c *(t/=d)*(t-2) + b;",
	12: "if ((t/=d/2) < 1) return c/2*t*t + b; return -c/2 * ((--t)*(t-2) - 1) + b;",
	13: "return c*(t/=d)*t*t*t*t + b;",
	14: "return c*((t=t/d-1)*t*t*t*t + 1) + b;",
	15: "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;"
}

for(var i=0; sl.animFunctions[i]; i++){
	var fn = sl.animFunctions[i];
	eval("Tween." + fn + " = function (t,b,c,d,a,p){" + a[i] + "};");
}


})();
