// 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
}
// 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;
}
var jso_ie = false;
/*@cc_on @if(@_jscript) jso_ie=true @end @*/
// remove
function ichtRemove()
{
	$( 'a.remove' ).click( function( e ){
		var message = 'Czy na pewno chcesz usunąć: "' + $( 'span', this ).html() + '"?';
		if ( !confirm( message ) ) {
			e.preventDefault();
		}
	});
}
// advancedSearchDomainList
function advancedSearchDomainList(T)
{
	ul = $('ul',T.parentNode.parentNode.parentNode)
	if($(T).attr('checked'))
	{
		$('input',ul).attr('checked',true)
		ul.slideDown({duration:200});
	}
	else
	{
		$('input',ul).attr('checked',false)
		ul.slideUp({duration:200});
	}
}
// truncated
function ichtTruncated()
{
	$('.truncated').each
	(
		function()
		{
			var T = $(this);
			T.truncatedText = T.text();
			T.titleText = T.attr('title').split('').join("\u00AD");
			T.attr('title','');
			if(T.truncatedText != T.titleText)
			{
				T.mouseover(function(){T.text(T.titleText)})
				T.mouseout(function(){T.text(T.truncatedText)})
			}
		}
	)
}
// actions lists
oldHash = "";
function ichtActions()
{
	$( 'ul.actions > li > a[href^=#]' ).each
	(
		function()
		{
			var defaultHash = $(this).attr('hash');
			var form = $(defaultHash);
			var formParent = form.parent();
			form.appendTo( this.parentNode );
			if (!formParent.hasClass('errors'))
			{
				form.hide();
			}
			formParent.remove();
			$(this).click
			(
				function()
				{
					if(defaultHash == document.location.hash)
					{
						$(this).attr('hash',defaultHash.replace('#','') + '_off');
					}
					else
					{
						$(this).attr('hash',defaultHash.replace('#',''));
					}
				}
			);
		}
	);
	window.setInterval(
		function()
		{
			if(oldHash != window.location.hash)
			{
				if($( oldHash ))
				{
					$( oldHash ).hide( 'fast' );
				}
				oldHash = window.location.hash;
				$( oldHash ).show
				(
					'fast' ,
					function()
					{
						var input = $( 'input[type!=hidden], textarea, select',$(this)).get(0);
						if(input)
						{
							input.focus();
						}
					}
				);
			}
		},
		200
	);
}
// autocomplite
function ichtAutocomplite()
{
	$( 'input[name=login], input[name=name], input[name=password]' ).attr( 'autocomplete', 'off' );
}
// add vhost
old_vhost_login = '';
old_vhost_domain = '';
function changeVhostLogin()
{
	var login = $('#form_vhost_add input[name=login]').val().replace(/ /g,'');
	if(login != $('#form_vhost_add input[name=login]').val())
	{
		$('#form_vhost_add input[name=login]').val(login);
	}
	var domain = $('#form_vhost_add select[name=domain] option:selected').text().replace(/\./g,'_') + '_';
	var new_vhost_login = '';
	if(!login || login == old_vhost_domain)
	{
		old_vhost_login = domain;
		new_vhost_login = old_vhost_login;
	}
	else if(old_vhost_domain && old_vhost_domain != domain && login.indexOf(old_vhost_domain) == 0)
	{
		new_vhost_login = login.replace(old_vhost_domain,domain);
	}
	else
	{
		old_vhost_login = '';
		return true;
	}
	old_vhost_domain = domain;
	if(login != new_vhost_login)
	{
		var maxlength = $('#form_vhost_add input[name=login]').attr('maxlength') || new_vhost_login.lenght;
		new_vhost_login = new_vhost_login.substring(0,maxlength);
		$('#form_vhost_add input[name=login]').val(new_vhost_login);
	}
	return true;
}

// add email
old_email_login = '';
old_email_domain = '';
function changeEmailLogin()
{
	var name = $('#form_email_add input[name=name]').val().replace(/[^_\-\.0-9a-z]/gi,'')
	if(name.match(/_.*_/g))
	{
		name = name.replace(/(^[^_]*_[^_]*)_/g,'$1');
		$('#form_email_add input[name=name]').val(name);
		alert('Nazwa konta może zawierać tylko jedno podkreślenie.');
	}
	if(name != $('#form_email_add input[name=name]').val())
	{
		$('#form_email_add input[name=name]').val(name);
	}
	var login = $('#form_email_add input[name=login]').val().replace(/[^_\-\.0-9a-z]/gi,'').replace(/^[^a-z]*/gi,'').substring(0, 24);
	if(login != $('#form_email_add input[name=login]').val())
	{
		$('#form_email_add input[name=login]').val(login);
	}
	var domain = $('#form_email_add select[name=domain] option:selected').text().replace(/\./g,'_')+'_';
	var new_email_login = '';
	if(!login || login == old_email_login || login == old_email_domain)
	{
		old_email_login = domain + name;
		new_email_login = old_email_login;
	}
	else if(old_email_domain && old_email_domain != domain && login.indexOf(old_email_domain) == 0)
	{
		new_email_login = login.replace(old_email_domain,domain);
	}
	else
	{
		old_email_login = '';
		return true;
	}
	old_email_domain = domain;
	if(login != new_email_login)
	{
		var maxlength = $('#form_email_add input[name=login]').attr('maxlength') || new_email_login.lenght;
		new_email_login = new_email_login.substring(0,maxlength);
		$('#form_email_add input[name=login]').val(new_email_login);
	}
	return true;
}
// user register
function userRegister()
{
	this.companyFields = ['company','nip','regon'];
	this.privateFields = ['pesel'];
	var T = this;
	$('input#FUD_type_0,input#FUD_type_1').each
	(
		function()
		{
			var TT = T;
			$(this).change(function(){TT.change.call(TT)})
			$(this).click(function(){TT.change.call(TT)})
			T.change.call(T);
		}
	)
}
userRegister.prototype.change = function()
{
	var company = $('input#FUD_type_0').get(0).checked, private = $('input#FUD_type_1').get(0).checked,i;
	for(i=0;i<this.companyFields.length;i++)
	{
		document.getElementById('FUD_'+this.companyFields[i]).parentNode.style.display = company ? '' : 'none';
	}
	for(i=0;i<this.privateFields.length;i++)
	{
		document.getElementById('FUD_'+this.privateFields[i]).parentNode.style.display = private ? '' : 'none';
	}
}
// tooltip
function tooltip(domElement, domText)
{
	var T = this;
	this.active = false;
	this.time = false;
	this.domElement = domElement;
	if(domText)
	{
		this.title = false;
		if(domText.nodeType)
		{
			this.text = domText;
		}
		else
		{
			this.text = this.parseText(domText);
		}
	}
	else
	{
		this.text = this.domElement.getAttributeNode('title');
		if(this.text.nodeType != 2 || this.text.nodeValue == '')
		{
			// IE bug
			return;
		}
		else
		{
			this.title = this.text.nodeValue;
			this.text = this.parseText(this.title);
		}
	}
	jso_aE(this.domElement,'mouseover',function(e){T.on.call(T,e)})
	jso_aE(this.domElement,'mousemove',function(e){T.on.call(T,e)})
	jso_aE(this.domElement,'mouseout',function(){T.off.call(T)})
}
tooltip.prototype.parseText = function(text)
{
	var domElement = jso_cE('div',{className:'wysiwyg'})
	domElement.innerHTML = text.replace(/>/gi,'&gt;').replace(/</gi,'&lt;').replace(/\*\*(\/?(b|strong|p|ul|ol|li|i|sup|sub|h[1-6]|br))\*\*/gi,"<$1>");
	return domElement;
}
tooltip.prototype.create = function()
{
	this.active = true;
	if(this.title)
	{
		this.domElement.setAttribute('title','');
	}
	if(!this.domTooltip)
	{
		this.domTooltip = jso_cE('div',{className:'tooltip'},[this.text,(this.domTooltipSpan = jso_cE('span',{className:'tooltip_span'}))]);
	}
	this.domBody.appendChild(this.domTooltip);
	this.offsetHeight = this.domTooltip.offsetHeight;
	this.domTooltip.style.height = this.offsetHeight - 22 + 'px'
	this.offsetWidth = this.domTooltip.offsetWidth;
	this.domTooltip.style.width = this.offsetWidth - 32 + 'px'
}
tooltip.prototype.remove = function()
{
	this.active = false;
	this.time = false;
	if(this.title && !jso_ie)
	{
		this.domElement.setAttribute('title',this.title);
	}
	if(this.domTooltip && this.domTooltip.parentNode == this.domBody)
	{
		this.domBody.removeChild(this.domTooltip);
	}
}
tooltip.prototype.position = function(e)
{
	var SY = document.documentElement.scrollTop || this.domBody.scrollTop;
	var Y = e.clientY + SY;
	if(Y < SY + this.offsetHeight + 30)
	{
		this.domTooltip.style.top = Y + 25 + 'px';
		this.domTooltipSpan.className = 'tooltip_span tooltip_top';
	}
	else
	{
		this.domTooltip.style.top = Y - 25 - this.offsetHeight + 'px';
		this.domTooltipSpan.className = 'tooltip_span tooltip_bottom';
	}
	var SX = document.documentElement.scrollLeft || this.domBody.scrollLeft;
	var X = e.clientX + SX - 20;
	var XX = this.domBody.offsetWidth + SX - this.offsetWidth - 5 - X;
	this.domTooltip.style.left = (XX<0?X+XX:X)+'px';
	this.domTooltipSpan.style.left = (XX<0?(5-XX+35>this.offsetWidth?this.offsetWidth-35:5-XX):5) + 'px';
}
tooltip.prototype.on = function(e)
{
	e=e||event;
	if(!this.active)
		this.create();
	this.position(e);
	this.timeout(10000)
}
tooltip.prototype.off = function()
{
	this.timeout(100)
}
tooltip.prototype.timeout = function(time)
{
	var T = this;
	if(this.time)
		window.clearTimeout(this.time);
	this.time = window.setTimeout(function(){T.remove.call(T)},time)
}
// form_select_domains
function formSelectDomains()
{
	$('#form_select_domains > ul > li[class!=submit]').each
	(
		function()
		{
			$('h4 input', $(this)).live('click change',function(){
				var section = $('section', $(this).get(0).parentNode.parentNode.parentNode);
				if ($(this).get(0).checked && !$(this).get(0).disabled)
				{
					section.slideDown('fast');
				}
				else
				{
					section.slideUp('fast');
				}
			});
			if($('h4 input', $(this)).get(0).checked && !$('h4 input', $(this)).get(0).disabled)
			{
				$('section',$(this)).show();
			}
			else
			{
				$('section',$(this)).hide();
			}
			$('section a',$(this)).click
			(
				function(event)
				{
					var input = $('input',$(this).parent()).get(0);
					if(!input.checked)
					{
						event.preventDefault();
						input.checked = true;
					}
				}
			);
			if(!$(this).hasClass('available'))
			{
				var domain_name = $.trim($('h4 strong', $(this)).text());
				var T = $(this).get(0);
				$.getJSON
				(
					URL_DOMAIN_WHOIS_INFO,
					{
						domain:domain_name
					},
					function(data)
					{
						var info = $('h4 em',$(T));
						data.text = data.text ? data.text.replace(/<p[^>]>/gi,'').replace(/<\/p>/gi,'<br>') : '';
						if (data.transfer)
						{
							$(info).html
							(
								$('<span>możliwy transfer <img src="/media/images/html5/info.' + (jso_ie ? 'gif':'png') +'" alt="info" width="16" height="16"></span>')
							);
							info = $('h4 em',$(T));
							new tooltip($(info).get(0),$('<div>' + data.text + '</div>').get(0));
							$('h4 input',$(T)).attr('disabled', false);
							$('h4 input',$(T)).addClass('active');
						}
						else
						{
							if (data.text)
							{
								$(info).text(data.text);
							}
							else
							{
								$(info).text('zajęta');
							}
						}
						if (data.expires)
						{
							$('h4',$(T)).append(' <span class="expires">ważna do: '+data.expires+'</span>')
						}
					}
				);
			}
		}
	)
}
// domyślne teksty w inputach
function ichtInputText(domInput,defaultText)
{
	if(!domInput)
		return;
	if(!('placeholder' in domInput) || jso_ie)
	{
		this.domInput = domInput;
		this.changeInputType = (!jso_ie && this.domInput.type == 'password');
		var placeholder = this.domInput.getAttribute('placeholder')
		if(defaultText)
		{
			this.defaultText = defaultText;
		}
		else if(placeholder)
		{
			this.defaultText = placeholder;
		}
		else
		{
			var domLabel = false;
			if(this.domInput.id)
			{
				domLabel = jso_gAV('label',0,'htmlFor',this.domInput.id);
			}
			if(!domLabel)
			{
				if(this.domInput.parentNode.nodeName.toLowerCase() == 'label')
				{
					domLabel = this.domInput.parentNode;
				}
			}
			if(domLabel)
			{
				domLabel = jso_gAV('*',domLabel,'className','auraltext')[0]
				if(domLabel)
				{
					this.defaultText = domLabel.firstChild.nodeValue;
				}
			}
		}
		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();
		}
	}
}
ichtInputText.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();
			}
		}
	}
}
ichtInputText.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';
		}
	}
}
// add events
$(document).ready(function()
{
	ichtTruncated();
	// tooltips
	tooltip.prototype.domBody = $('body').get(0);
	$('*[title]').each
	(
		function()
		{
			new tooltip($(this).get(0));
		}
	);
	// search_domain_adv
	$('#form_search_domain_adv h4 input[type=checkbox]').each
	(
		function()
		{
			var T = $(this).get(0);
			$(this).click(function(){advancedSearchDomainList(T)});
			$(this).change(function(){advancedSearchDomainList(T)});
			advancedSearchDomainList(T);
		}
	);
	// registration
	new userRegister();
	$('input[placeholder]').each
	(
		function()
		{
			new ichtInputText($(this).get(0));
		}
	)
	formSelectDomains();
	ichtActions();
	ichtAutocomplite();
	ichtRemove();
	$('#form_vhost_add input[name=login]').live( 'keyup focus blur',changeVhostLogin);
	$('#form_vhost_add select[name=domain]').change(changeVhostLogin);
	$('#form_email_add input[name=login],#form_email_add input[name=name]').live( 'keyup focus blur', changeEmailLogin );
	$('#form_email_add select[name=domain]').change(changeEmailLogin);
	$('.date input').datepicker({
		inline: true,
		lang: 'pl'
	});

});
