// JSO
// Get Element By Id - id, "parentNode";
function jso_gI(a,b)
{
	b=b||document;
	return b.getElementById(a)
}
// Get Elements By Tag Name - tagName, "parentNode";
function jso_gT(a,b,i)
{
	a=a||"*";
	b=b||document;
	b=b.getElementsByTagName(a)
	a=[]
	for(i=0;i<b.length;i++)a[a.length]=b[i];
	return a
}
// Get Elements By Attribute Value - tagName, "parentNode", attibute name, attibute value;
function jso_gAV(a,b,c,d,i)
{
	a=a||"*";
	b=b||document;
	if(!c)return false;
	b=b.getElementsByTagName(a)
	a=[]
	for(i=0;i<b.length;i++)if((b[i][c]||b[i].getAttribute(c))&&(!d||((b[i][c]||b[i].getAttribute(c)).toString().match(new RegExp("(^| )"+d+"($| )","gi"))!=null)))a[a.length]=b[i];;
	return a
}
// Create Element - tagName, attributes, content, parentNode, events;
function jso_cE(a,b,c,d,e,i)
{
	a=document.createElement(a)
	if(b)for(i in b)a[i]=b[i];;
	if(c)for(i in c)if(typeof c[i] != "function")a.appendChild(c[i].nodeType?c[i]:document.createTextNode(c[i]));;
	if(e)for(i in e)jso_aE(a,i,e[i]);;
	if(d)d.appendChild(a);
	return a
}
// Get Event Triggered Element
function jso_gE(x)
{
	x=x||window.event;
	x=x.target||x.srcElement;
	if(x.nodeType==3)x=x.parentNode;
	return x
}
// Add Event
function jso_aE(O,E,F)
{
	return(O.aE=O.addEventListener)?O.aE(E,F,0):(O.aE=O.attachEvent)?O.aE("on"+E,F):!1;
}
// Remove Event
function jso_rE(O,E,F)
{
	return(O.rE=O.removeEventListener)?O.rE(E,F,0):(O.rE=O.detachEvent)?O.rE("on"+E,F):!1;
}
// Stop Event Propagation
function jso_sE(e)
{
	e = e||window.event;
	if(e.preventDefault)
		e.preventDefault();
	e.returnValue=false
	return false
}
// Set Opacity
function jso_sO(a,b)
{
	if(jso_ie)
		a.style.filter='alpha(opacity='+(100*b)+')';
	else
		a.style.opacity = b;
}
// Clear Opacity
function jso_cO(a)
{
	if(jso_ie)
		a.style.filter='';
	else
		a.style.opacity = '';
}
// IE ?
var jso_ie = false;
/*@cc_on @if(@_jscript) jso_ie=true @end @*/
// TOLPA
// tolpaBanners
function tolpaBanners()
{
	this.domContainer = jso_gI('c_aside_top_banners');
	if(this.domContainer)
	{
		this.change = true;
		this.active = 0;
		this.newitem = 1;
		this.step = 0;
		this.items = [];
		this.wtf = false;
		var T = this,i=0,domAnachors = jso_gT('a',this.domContainer);
		if(domAnachors.length > 1)
		{
			for(;i<domAnachors.length;i++)
			{
				this.items[i] = {domAnachor:domAnachors[i],image:domAnachors[i].getAttribute('data-image')}
				domAnachors[i].setAttribute('data-bannerid',i);
				jso_aE(domAnachors[i],'mouseover',function(e){T.stopF.call(T,jso_gE(e||event).getAttribute('data-bannerid'))})
				jso_aE(domAnachors[i],'mouseout',function(e){T.startF.call(T)})
			}
			this.domContainer.style.backgroundImage = 'url(' + this.items[this.active].image + ')';
			this.domNew = jso_cE('div',{id:'c_aside_top_banners_new'},0,this.domContainer);
			jso_sO(this.domNew,0);
			this.domNew.style.backgroundImage = 'url(' + this.items[this.newitem].image + ')';
			this.interval = window.setInterval(
				function(){T.changeF.call(T)},
				40
			);
		}
	}
}
tolpaBanners.prototype.changeF = function()
{
	if(this.change || this.step != 0)
	{
		if(!this.change)
		{
			if(this.wtf)
			{
				if(this.step <=50)
				{
					this.step = 0;
					this.wtf = false;
				}
				else
				{
					this.step -= 2;
					if(this.step < 0)
					{
						this.step = 0;
					}
				}
			}
			else
			{
				this.step += 2;
			}
		}
		else
		{
			this.step ++;
		}
		if(this.step >= 50)
		{
			if(this.step >= 75)
			{
				this.step = 0;
				this.items[this.active].domAnachor.parentNode.className = '';
				this.active = this.newitem;
				this.domContainer.style.backgroundImage = 'url(' + this.items[this.active].image + ')';
				this.items[this.active].domAnachor.parentNode.className = 'active';
				jso_sO(this.domNew,0);
				this.newitem = this.active + 1 == this.items.length ? 0 : this.active + 1;
				this.domNew.style.backgroundImage = 'url(' + this.items[this.newitem].image + ')';
			}
			else
			{
				jso_sO(this.domNew,(this.step-50)/25);
			}
		}
	}
}
tolpaBanners.prototype.stopF = function(id)
{
	id *= 1;
	if(this.active == id)
	{
		this.wtf = true;
	}
	else
	{
		this.wtf = false;
		this.newitem = id;
		this.domNew.style.backgroundImage = 'url(' + this.items[this.newitem].image + ')';
		if(this.step<50)
		{
			this.step = 50;
		}
	}
	this.change = false;
}
tolpaBanners.prototype.startF = function()
{
	this.wtf = false;
	this.change = true;
}
// tolpaMenu
function tolpaMenu()
{
	this.domContainer = jso_gI('c_aside_left_navigation');
	if(this.domContainer)
	{
		this.items = [];
		var domHeaders = jso_gT('h3',this.domContainer),i=0;
		for(;i<domHeaders.length;i++)
		{
			this.items[i] = new tolpaMenuElement(this,domHeaders[i]);
		}
		this.lis = [];
		var domLis = jso_gT('li',this.domContainer);
		for(i=0;i<domLis.length;i++)
		{
			this.lis[i] = new tolpaMenuLi(this,domLis[i]);
		}
	}
}
function tolpaMenuElement(oMenu,domHeader)
{
	this.oMenu = oMenu;
	this.domHeader = domHeader;
	this.domContainer = this.domHeader.parentNode;
	this.active = this.domContainer.className == 'active';
	var T = this;
	jso_aE(this.domHeader,'click',function(){T.activeF.call(T)});
}
tolpaMenuElement.prototype.activeF = function()
{
	if(!this.active)
	{
		for(var i=0;i<this.oMenu.items.length;i++)
		{
			if(this.oMenu.items[i] == this)
			{
				this.active = true;
				this.domContainer.className = 'active';
			}
			else
			{
				this.oMenu.items[i].inactiveF.call(this.oMenu.items[i]);
			}
		}
	}
}
tolpaMenuElement.prototype.inactiveF = function()
{
	if(this.active)
	{
		this.domContainer.className = '';
		this.active = false;
	}
}
function tolpaMenuLi(oMenu,domLi)
{
	this.oMenu = oMenu;
	this.domLi = domLi;
	this.folder = this.domLi.className == 'folder';
	this.timeout = false;
	if(this.folder)
	{
		this.domUl = jso_gT('ul',this.domLi)[0];
		this.domA = jso_gT('a',this.domLi)[0];
	}
	var T = this;
	jso_aE(this.domLi,'mouseover',function(){T.openF.call(T)})
	if(this.folder)
	{
		jso_aE(this.domLi,'mouseout',function(){T.closeF.call(T)});
	}
}
tolpaMenuLi.prototype.clearF = function()
{
	if(this.timeout)
	{
		window.clearTimeout(this.timeout);
		this.hideF();
	}
}
tolpaMenuLi.prototype.hideF = function()
{
	this.timeout = false;
	this.domLi.className = 'folder';
	this.domUl.className = '';
	this.domA.className = '';
}
tolpaMenuLi.prototype.openF = function()
{
	if(this.timeout)
	{
		window.clearTimeout(this.timeout);
		this.timeout = false;
	}
	for(var i = 0;i < this.oMenu.lis.length;i++)
	{
		this.oMenu.lis[i].clearF.call(this.oMenu.lis[i]);
	}
	if(this.folder)
	{
		this.domLi.className = 'folder-hover';
		this.domUl.className = 'folder-hover';
		this.domA.className = 'folder-hover';
	}
}
tolpaMenuLi.prototype.closeF = function()
{
	if(this.timeout)
	{
		window.clearTimeout(this.timeout);
	}
	var T = this;
	this.timeout = window.setTimeout
	(
		function()
		{
			T.hideF.call(T)
		},
		1000
	);
}
// tolpaWindows
function tolpaWindows()
{
	this.domInsert = jso_gI('container');
	this.domHtml = jso_gT('html')[0];
	this.domBody = jso_gT('body')[0];
	this.items = [];
}
tolpaWindows.prototype.addF = function(domHeader,domAnachor)
{
	this.items.push(new tolpaWindowsElement(this,domHeader,domAnachor));
}
function tolpaWindowsElement(oWindows,domHeader,domAnachor)
{
	this.oWindows = oWindows;
	this.domHeader = domHeader;
	if(domAnachor)
	{
		this.domAnachor = domAnachor;
	}
	else
	{
		this.domAnachor = jso_gT('a',this.domHeader)[0];
	}
	if(!this.domAnachor)
		return;
	this.domContainer = this.domHeader.parentNode;
	this.domForm = jso_gT('form',this.domContainer)[0];
	this.domInput = jso_gT('input',this.domForm)[0];
	this.domWindow = jso_cE('div',{id:'action_window'},[
		jso_cE('a',{id:'action_window_close'},[i18n.close],0,{click:function(e){jso_sE(e);T.closeF.call(T)}}),
		jso_cE('h2',0,[this.domHeader.textContent||this.domHeader.innerText]),
		this.domForm
	]);
	this.domWindow.actionElement = this;
	var T = this;
	this.domAnachor.hash = 'action_window';
	jso_aE(this.domAnachor,'click',function(e){jso_sE(e);T.openF.call(T)});
	if(this.domForm.className == 'error')
	{
		this.openF();
	}
}
tolpaWindowsElement.prototype.openF = function()
{
	var oldWindow = jso_gI('action_window');
	if(oldWindow)
	{
		if(oldWindow.actionElement == this)
		{
			return;
		}
		else
		{
			oldWindow.actionElement.closeF.call(oldWindow.actionElement);
		}
	}
	this.top = 0;
	this.oWindows.domInsert.appendChild(this.domWindow);
	this.moveF();
	if(this.domInput)
	{
		this.domInput.focus();
	}
	/*
	var T = this;
	this.interval = window.setInterval(
		function(){T.moveF.call(T)},
		50
	);
	*/
}
tolpaWindowsElement.prototype.closeF = function()
{
	this.oWindows.domInsert.removeChild(this.domWindow);
// 	window.clearInterval(this.interval);
}
tolpaWindowsElement.prototype.moveF = function()
{
	var clientHeight = ( this.oWindows.domHtml.clientHeight || this.oWindows.domBody.clientHeight );
	var windowHeight = this.domWindow.offsetHeight;
	if(windowHeight > clientHeight)
	{
		return;
	}
	var current = Math.round(( this.oWindows.domHtml.scrollTop || this.oWindows.domBody.scrollTop ) + ((clientHeight - windowHeight) / 2));
	if(this.top == 0)
	{
		this.top = current;
		this.domWindow.style.top = this.top + 'px';
	}
	else if(this.top != current)
	{
		var add = Math.ceil(Math.abs(current - this.top) / 5);
		if(this.top > current)
		{
			this.top -= add;
			if(this.top < current)
			{
				this.top = current;
			}
		}
		else
		{
			this.top += add;
			if(this.top > current)
			{
				this.top = current;
			}
		}
		this.domWindow.style.top = this.top + 'px';
	}
}
// tolpaInfo
function tolpaInfo(domElement)
{
	if(domElement)
	{
		this.step = 0;
		this.pi = Math.PI / 40;
		this.domElement = domElement;
		this.height = this.domElement.offsetHeight;
		this.heightChange = (this.height / 10);
		var T = this;
		this.interval = window.setInterval
		(
			function()
			{
				T.changeF.call(T)
			},
			24
		)
	}
}
tolpaInfo.prototype.changeF = function()
{
	if(this.step < 100)
	{
		jso_sO(this.domElement, Math.abs(Math.cos(this.step++ * this.pi))) / 2 + 0.5;
	}
	else if(this.step < 110)
	{
		if(this.step == 100)
		{
			this.domElement.style.padding = 0;
			this.domElement.style.height = this.height + 'px';
			jso_sO(this.domElement,0);
		}
		else
		{
			this.height -= this.heightChange;
			this.domElement.style.height = Math.round(this.height) + 'px';
		}
		this.step++;
	}
	else
	{
		window.clearInterval(this.interval);
		this.domElement.parentNode.removeChild(this.domElement);
	}
}
// tolpaPlaceholder
function tolpaPlaceholder(domInput,defaultText)
{
	if(!domInput)
		return;
	if(!('placeholder' in domInput) || jso_ie)
	{
		this.domInput = domInput;
		this.changeInputType = (!jso_ie && this.domInput.type == 'password');
		if(defaultText)
		{
			this.defaultText = defaultText;
		}
		else
		{
			this.defaultText = this.domInput.getAttribute('placeholder');
		}
		if(this.defaultText)
		{
			var T = this;
			jso_aE(this.domInput,'focus',function(){T.focus.call(T)})
			jso_aE(this.domInput,'blur',function(){T.blur.call(T)})
			jso_aE(this.domInput.form,'submit',function(){T.focus.call(T)})
			this.blur();
		}
	}
}
tolpaPlaceholder.prototype.focus = function()
{
	this.domInput.className = this.domInput.className.replace(/ *default */gi,'');
	if(this.domInput.value == this.defaultText)
	{
		this.domInput.value = '';
		if(this.changeInputType && this.domInput.type == 'text')
		{
			this.domInput.type = 'password';
			if(window.opera)
			{
				this.domInput.focus();
			}
		}
	}
}
tolpaPlaceholder.prototype.blur = function()
{
	if(this.domInput.value == '' || this.domInput.value == this.defaultText)
	{
		this.domInput.className = this.domInput.className.replace(/ *default */gi,'') + ' default';
		this.domInput.value = this.defaultText;
		if(this.changeInputType)
		{
			this.domInput.type = 'text';
		}
	}
}
function tolpaRequired(domInput)
{
	this.domInput = domInput;
	this.domInput.required = '';
	this.domInput.removeAttribute('required');
	this.domForm = this.domInput.form;
	if(this.domInput.type == 'checkbox' || this.domInput.type == 'radio')
	{
		this.errorText = i18n.select_fields;
	}
	else
	{
		if(this.domInput.parentNode.nodeName.toLowerCase() == 'label')
		{
			var domLabel = this.domInput.parentNode;
		}
		else
		{
			var domLabel = jso_gAV('label',this.domInput.parentNode,'htmlFor',this.domInput.id)[0];
		}
		if(domLabel)
		{
			this.errorText = i18n.fill_in_field + ' ' + domLabel.firstChild.nodeValue.replace(/(^[ 	\n]*|[ 	\n]*$|\*+)/gi,'');
		}
		else
		{
			this.errorText = i18n.fill_in_fields;
		}
	}
	if(!this.domForm.tolpaRequired)
	{
		this.domForm.tolpaRequired = [];
		this.domForm.onsubmit = function()
		{
			var i=0,validateF;
			for(;i<this.tolpaRequired.length;i++)
			{
				validateF = this.tolpaRequired[i];
				if(!validateF())
				{
					return false;
				}
			}
			return true;
		}
	}
	var T = this;
	this.domForm.tolpaRequired.push(function(){return T.validateF.call(T)});
}
tolpaRequired.prototype.validateF = function()
{
	if(!this.domInput.offsetHeight || !this.domInput.offsetWidth)
	{
		return true;
	}
	if(this.domInput.type == 'checkbox')
	{
		if(!this.domInput.checked)
		{
			return this.errorF();
		}
	}
	else if(this.domInput.type == 'radio')
	{
		var i=0;domInputs = this.domForm[this.domInput.name];
		for(;i<domInputs.length;i++)
		{
			if(domInputs[i].checked)
			{
				return true;
			}
		}
		return this.errorF();
	}
	else
	{
		if(!this.domInput.value.match(/[^ 	]+/))
		{
			return this.errorF();
		}
		if(this.domInput.getAttribute('type') == 'search' && this.domInput.value.length < 4)
		{
			this.errorText = i18n.to_short;
			return this.errorF();
		}
		if(this.domInput.getAttribute('type') == 'email' && !this.domInput.value.match(/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/i))
		{
			this.errorText = i18n.invalid_email;
			return this.errorF();
		}
	}
	return true;
}
tolpaRequired.prototype.errorF = function()
{
 	alert(this.errorText);
	this.domInput.focus();
	return false;
}
// tolpaSubmit
function tolpaSubmit(domInput)
{
	this.domInput = domInput;
	this.type = this.domInput.getAttribute('type')
	if(this.type == 'search' || this.type == 'image' || this.type == 'submit')
	{
		return;
	}
	this.domForm = this.domInput.form;
	if(this.domForm)
	{
		var T = this;
		jso_aE(this.domInput,'keypress',function(e){T.pressF.call(T,e)})
	}
}
tolpaSubmit.prototype.pressF = function(e)
{
	var keycode = e ? e.which : window.event.keyCode;
	if (keycode == 13)
   {
		jso_sE(e);
	}
	else
	{
		return true;
	}
}
// tolpaVat
function tolpaVat()
{
	this.domInput = jso_gI('cart_vat');
	if(this.domInput)
	{
		this.active = this.domInput.checked;
		this.domTextarea = jso_gI('cart_vat_data');
		var T = this;
		jso_aE(this.domInput,'click',function(e){T.activateF.call(T)})
	}
}
tolpaVat.prototype.activateF = function()
{
	this.active = !this.active;
	this.domInput.checked = this.active;
	this.domTextarea.parentNode.className = this.active ? 'textarea' : 'auraltext';
	if(this.active)
	{
		this.domTextarea.focus();
	}
}
/*
// tolpaReceiver
function tolpaReceiver()
{
	this.domAnachor = jso_gI('cart_reveiver_show');
	if(this.domAnachor)
	{
		this.domContent = jso_gI('cart_reveiver');
		this.domInput = jso_gI('cart_reveiver_enable');
		this.active = this.domInput.checked;
		var T = this;
		jso_aE(this.domAnachor,'click',function(e){jso_sE(e);T.activateF.call(T)})
	}
}
tolpaReceiver.prototype.activateF = function()
{
	this.active = !this.active;
	this.domInput.checked = this.active;
	this.domContent.className = this.active ? 'data' : 'auraltext';
	if(this.active)
	{
		jso_gT('input',this.domContent)[1].focus();
	}
}
*/
// tolpaInvite
function tolpaInvite()
{
	this.domSelect = jso_gI('invite_gallery');
	if(this.domSelect && this.domSelect.options.length > 0)
	{
		this.domHtml = jso_gT('html')[0];
		this.domBody = jso_gT('body')[0];
		this.active = false;
		this.domForm = this.domSelect.form;
		this.domMessage = jso_gI('invite_message');
		this.domImage = jso_cE
		(
			'img',
			{
				id:'invite_gallery_image',
				src:'/images/blank.gif',
				width:60,
				height:40,
				alt:''
			},0,this.domSelect.parentNode
		);
		this.changeF();
		var T = this;
		jso_aE(this.domSelect,'change',function(){T.changeF.call(T)});
		this.domPreviewAnachor = jso_cE
		(
			'a',
			{href:'#preview'},
			[i18n.preview],
			0,
			{click:function(e){jso_sE(e);T.openF.call(T)}}
		);
		var domSubmitLi = jso_gAV('li',this.domForm,'className','submit')[0];
		domSubmitLi.insertBefore(this.domPreviewAnachor,domSubmitLi.firstChild);
		this.domPreviewContainer = jso_cE('div',{id:'invite_gallery_preview'},[
			this.domPreview = jso_cE('div',0,[
				jso_cE('a',{href:'close'},[i18n.close],0,{click:function(e){jso_sE(e);T.closeF.call(T)}}),
				this.domPreviewImage = jso_cE('img',{src:'/images/blank.gif',width:600,height:400}),
				this.domPreviewLabel = jso_cE('p',0,['null'])
			])
		]);
	}
}
tolpaInvite.prototype.changeF = function()
{
	this.domActiveOption = this.domSelect.options[this.domSelect.selectedIndex];
	this.domImage.src = this.domActiveOption.getAttribute('data-smallimage');
}
tolpaInvite.prototype.openF = function()
{
	if(!this.active)
	{


		this.domPreviewContainer.style.height = (this.domHtml.offsetHeight > this.domBody.offsetHeight ? this.domHtml.offsetHeight : this.domBody.offsetHeight) + 'px';
		this.domPreviewContainer.style.width = (this.domHtml.offsetWidth > this.domBody.offsetWidth ? this.domHtml.offsetWidth : this.domBody.offsetWidth) + 'px';
		this.domPreview.style.marginTop = (this.domHtml.scrollTop || this.domBody.scrollTop) + 20 + 'px';
		this.domPreviewImage.src = this.domActiveOption.getAttribute('data-bigimage');
		var message = this.domMessage.value ? this.domMessage.value : i18n.preview_text ;
		this.domPreviewLabel.innerHTML = message.replace(/\n/gi,'<br>');
		this.domHtml.className = 'hide_container';
		this.domBody.className = 'hide_container';
		this.domBody.appendChild(this.domPreviewContainer);
		this.active = true;
	}
}
tolpaInvite.prototype.closeF = function()
{
	if(this.active)
	{
		this.active = false;
		this.domBody.removeChild(this.domPreviewContainer);
		this.domHtml.className = '';
		this.domBody.className = '';
	}
}
// tolpaStart
function tolpaStart()
{

	$("a[rel=lightbox]").fancybox({
		'transitionIn'	:	'none',
		'transitionOut'	:	'none',
		'overlayShow'	:	false,
		'titleShow' : false
	});


	var oWindows = new tolpaWindows();
	var domInputs = jso_gAV('input',0,'placeholder','.+'),i=0;
	for(;i<domInputs.length;i++)
	{
		new tolpaPlaceholder(domInputs[i]);
	}
	domInputs = jso_gAV('*',0,'required');
	for(i=0;i<domInputs.length;i++)
	{
		new tolpaRequired(domInputs[i]);
	}
	domInputs = jso_gT('input');
	for(i=0;i<domInputs.length;i++)
	{
		new tolpaSubmit(domInputs[i]);
	}
	new tolpaInvite(oWindows);
	new tolpaVat();
// 	new tolpaReceiver();
	new tolpaInfo(jso_gI('c_treatments_cart_info'));
	new tolpaBanners();
	new tolpaMenu();
	var domContainer = jso_gI('c_treatments_aside_actions');
	if(domContainer)
	{
		var domHeaders = jso_gT('h3',domContainer);
		for(i=0;i<domHeaders.length;i++)
		{
			oWindows.addF.call(oWindows,domHeaders[i]);
		}
	}
	domContainer = jso_gI('c_index_invite');
	if(domContainer)
	{
		var domHeader = jso_gT('h2',domContainer)[0];
		var domAnachor = jso_gT('a',domContainer)[0];
		if(domHeader && domAnachor)
		{
			oWindows.addF.call(oWindows,domHeader,domAnachor);
		}
	}
}
jso_aE(window,'load',tolpaStart);
