/*************************************************************************************
 Copyright © 2006-2010 by Walerian "Cobra" Walawski - http://walerian.info/
 Feel free to use code below however you'd like, but please keep this comment intact.
*************************************************************************************/

// Let's make document.getElementById shorter ;-) (by-id)
function bid(name)
{
	return document.getElementById(name);
}

// <body> onload
function bodyStart()
{
	toolTip();
	mirrorSettings('colors',getCookie('mirrorSettings_colors'));
	mirrorSettings('size',getCookie('mirrorSettings_size'));
}

// Tooltip
function toolTip()
{
	// Add :->
	for(var i=0,d,dE,D=document,span;d=D.getElementsByTagName('*')[i++];)
	{
		if(d.getAttribute('title'))
		{
			d.onmouseover=function(e)
			{
				if(this.getAttribute('title')!=undefined)
				{
					var e=e||event;
					box=D.createElement('span');
					box.id='tooltip';
					box.appendChild(D.createTextNode(this.getAttribute('title')));
					with(box.style)
					{
						position='absolute';
						top=e.clientY+(dE=D.documentElement).scrollTop+15+'px';
						left=e.clientX+dE.scrollLeft+15+'px';
					}
					// Old box? (Gecko [maybe not only] bug) - part 1
					if(this.title!=null&&this.title!='')
					{
						this.title_ex=this.title;
					}
					this.title='';
					D.body.appendChild(box);
				}
			}
			d.onmouseout=function()
			{
				if(this.getAttribute('title')!=undefined&&this.title_ex!=undefined)
				{
					this.title=this.title_ex;
					D.body.removeChild(box);
					// Old box? (Gecko [maybe not only] bug) - part 2
					if(bid('tooltip')!=null)
					{
						D.body.removeChild(bid('tooltip'));
					}
				}
			}
		}
	}
}

// New text mirror
function tmNew()
{
	setTimeout('bid("newForm").innerHTML=\'<div>Mirror creation in progress, please wait...</div>\';',500);
}

// Mirror settings
function mirrorSettings(setting,newValue)
{
	if(newValue&&bid('textMirror'))
	{
		var box=bid('textMirror');
		if(setting=='colors')
		{
			box.setAttribute('class',box.getAttribute('class').replace(/(.*) (.*)/,'mirrorSettings_'+newValue+' $2'));
			bid('selectColors').value=newValue;
			setCookie('mirrorSettings_colors',newValue);
		}
		if(setting=='size')
		{
			box.setAttribute('class',box.getAttribute('class').replace(/(.*) (.*)/,'$1 mirrorSettings_'+newValue));
			bid('selectSize').value=newValue;
			setCookie('mirrorSettings_size',newValue);
		}
	}
}

// Clean domain (new entry)
function domainClean()
{
	var domain=bid('domain');
	var cleanA=domain.value.split('/');
	var cleanB=cleanA[0].split('&');
	var cleanC=cleanB[0].split('*');
	var cleanD=cleanC[0].split('%');
	var cleanE=cleanD[0].split('@');
	var cleanF=cleanE[0].split(';');
	var cleanG=cleanF[0].split('#');
	var cleanH=cleanG[0].split('"');
	var cleanI=cleanH[0].split('\'');
	var cleanJ=cleanI[0].split('<');
	var cleanK=cleanJ[0].split('>');
	var cleanL=cleanK[0].split(':');
	var cleanM=cleanL[0].toLowerCase();
	domain.value=cleanM;
	bid('domainCleanA').innerHTML='http://'+cleanM+'/';
	if(bid('domainCleanB')!=null)
	{
		bid('domainCleanB').innerHTML='http://'+cleanM+'/';
	}
}

// Clean seo-link (new entry)
function seoClean()
{
	if(bid('seo')!=null)
	{
		var seo=bid('seo');
		var cleanA=seo.value.split('"');
		var cleanB=cleanA[0].split('\'');
		var cleanC=cleanB[0].split('<');
		var cleanD=cleanC[0].split('>');
		seo.value=cleanD[0];
		bid('seoClean').innerHTML=cleanD[0];
	}
}

// Delete example value
function exampleValue(box)
{
	if(box.getAttribute('class').match('^(.*)exampleValue(.*)'))
	{
		box.value='';
		box.setAttribute('class',box.getAttribute('class').replace(/(.*)exampleValue(.*)/,'$1$2'));
	}
}

// Cookies
function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function getCookie(name)
{
	var arg = name + "=";
	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 getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
		break;
	}
	return null;
}
function setCookie(name,value)
{
	var cdate=new Date();
	cdate.setTime(cdate.getTime()+1000*60*60*24*7);
	document.cookie=name+'='+escape(value)+";expires="+cdate.toGMTString();
}
