Style={};

Style.Settings={
	toolBoxOpen:false
};

Style.MainElems={};

AutoAjaxer = {};
AutoAjaxer.getContentLength=function(ajaxInstance) {
	try {
		return ajaxInstance.getResponseHeader('Content-Length');
	} catch(e) {
		//alert(e.message);
		return -1;
	}
}
AutoAjaxer.BaseHref=location.protocol+'//'+location.host+'/aularon/';
AutoAjaxer.suffixToMatch='.'+CMS.Lang+'.html';
AutoAjaxer.loadHref=function(hrefTarget) {
	var BaseCut=0;
	if(hrefTarget.indexOf('http')===0) {
		if(hrefTarget.indexOf(AutoAjaxer.BaseHref)!==0) {
			//alert('allowed');
			return true;
		}
		BaseCut=AutoAjaxer.BaseHref.length;
	}
	
	if(hrefTarget.lastIndexOf(AutoAjaxer.suffixToMatch)===(hrefTarget.length-AutoAjaxer.suffixToMatch.length)) {
		var AjaxInstance=createAJAX();
		//a=AjaxInstance;
		//var AjaxInstance=new XMLHttpRequest();
		var contentLength=0;
		AutoAjaxer.BytesToLoad=0;
		AutoAjaxer.updateProgressBar(0);
		AjaxInstance.onreadystatechange = function()
		{
			
			try {
				if((temp=AutoAjaxer.getContentLength(AjaxInstance))==-1) {//ie
					AutoAjaxer.BytesToLoad=100;
					if(!contentLength){ contentLength=10;}
				} else {
					if(!contentLength && (temp)) {//ffx
						AutoAjaxer.BytesToLoad=temp;
						//alert(contentLength);
					}
				}
				try {
					if(AjaxInstance.responseText.length>0) {
						contentLength=AjaxInstance.responseText.length;
					}
				} catch(e) {
					contentLength+=1;
				}
				AutoAjaxer.updateProgressBar(contentLength);
				if (AjaxInstance.readyState==4)
				{
					document.title=decodeURIComponent(AjaxInstance.getResponseHeader('X-myBlog-Title')).replace(/\+/g, ' ').replace(/\|/g, '«')
					
					Style.MainElems.ReplaceableContent.innerHTML='';
					var newContentDiv=document.createElement('DIV');
					newContentDiv.innerHTML=AjaxInstance.responseText;
					Style.MainElems.ReplaceableContent.appendChild(newContentDiv);
					AutoAjaxer.prepareLinks(Style.MainElems.ReplaceableContent);
					AutoAjaxer.updateProgressBar(AutoAjaxer.BytesToLoad);
					window.location='#redirect='+encodeURIComponent(hrefTarget.substr(BaseCut));
					//oplang preparing
					Style.MainElems.OplangLink.onclick=null;
					Style.MainElems.OplangLink.href=hrefTarget.replace(new RegExp('\\.'+CMS.Lang +'\\.html$'), '.'+CMS.OpLang +'.html');
				}
			} catch (e) {
				alert('Can NOT Connect : (\n'+e.message);
				//alert();
			}
		}
		
		AjaxInstance.open('POST', hrefTarget, true);
		AjaxInstance.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		AutoAjaxer.updateProgressBar(0.0001);
		AjaxInstance.send('naked=yes');
		
		
		return false;
	}
	return true;
}
AutoAjaxer.updateProgressBarShown=false;
AutoAjaxer.BytesToLoad=0;
AutoAjaxer.updateProgressBar=function(loadedBytes) {
	newPercentage=loadedBytes/AutoAjaxer.BytesToLoad;
	//alert(loadedBytes+'/'+AutoAjaxer.BytesToLoad+'=='+newPercentage);
	if((newPercentage==1) && AutoAjaxer.updateProgressBarShown) {
		Effects.SteppyChange.change(Style.MainElems.ProgressBar.id,
					'left', '-14em', {len:'0em',opacity:1},
					{delay:25, step:0.5});
		AutoAjaxer.updateProgressBarShown=false;
	}
	//alert(newPercentage);
	//if(newPercentage<1 && !AutoAjaxer.updateProgressBarShown) {
	if((loadedBytes==0) && (!AutoAjaxer.updateProgressBarShown)) {
		Effects.SteppyChange.change(Style.MainElems.ProgressBar.id,
					'left', '0em', {len:'-14em',opacity:1},
					{delay:25, step:0.5});
		//alert(1);
		AutoAjaxer.updateProgressBarShown=true;
	}
	//console.debug(newPercentage);
	newPercentage=parseInt(newPercentage*1000)/10;
	if(isNaN(newPercentage)) {
		newPercentage='?';
	} else if(newPercentage>=100) {
		newPercentage='100%, Done!';
	} else {
		newPercentage+='% ('+loadedBytes+'/'+AutoAjaxer.BytesToLoad+' B)';
	}
	
	Style.MainElems.ProgressPercentage.innerHTML=newPercentage;
}
AutoAjaxer.prepareLinks=function(parentNode) {
	var aElems=parentNode.getElementsByTagName('a');
	var foundRightLinks=0;
	for(var i=0; i<aElems.length; i++) {
		var thisAElem=aElems[i];
		thisAElem.onfocus=function() {
			//alert(1);
			this.blur();
		}
		
		thisAElem.onclick=function () {
			return AutoAjaxer.loadHref(this.href);
		}
		if(thisAElem.id.indexOf('rightlink-')===0) {
			thisAElem.style.position='absolute';
			thisAElem.style.right='0pt';
			thisAElem.style.top=(foundRightLinks*2.5)+'em';
			thisAElem.style.MozBorderRadiusTopleft='0.5em';
			
			thisAElem.style.width='0em';
			//alert(thisAElem.id);
			thisAElem.onmouseover=thisAElem.onfocus=function() {
				//alert(this.id);
				Effects.SteppyChange.change(this.id,
					'width', '6em', {len:'0em',opacity:1},
					{delay:40, step:0.8});
			}
			thisAElem.onmouseout=thisAElem.onblur=function() {
				//alert(this.id);
				Effects.SteppyChange.change(this.id,
					'width', '0em', {len:'6em',opacity:1},
					{delay:50, step:0.8});
			}

			foundRightLinks++;
		}
	}
}
AutoAjaxer.HashRedirectPrefix='#redirect='
if(location.hash.indexOf(AutoAjaxer.HashRedirectPrefix)==0) {
	location=AutoAjaxer.BaseHref+decodeURIComponent(location.hash.substr(AutoAjaxer.HashRedirectPrefix.length));
}

function onLoadHandler() {
	magicBG.doMagic();
	Style.MainElems.MainContent=$('main_content');
	Style.MainElems.ToolBox=$('toolbox');
	Style.MainElems.ReplaceableContent=$('replaceable-content');
	Style.MainElems.ProgressBar=$('progress-spinner');
	Style.MainElems.ProgressPercentage=$('progress-percentage');
	Style.MainElems.OplangLink=$('rightlink-oplang');
	
	Style.MainElems.MainContent.style.marginRight='71px';
	Style.MainElems.ToolBox.style.height='13px';
	Style.MainElems.ToolBox.style.overflow='hidden';
	
	
	Style.MainElems.MainContent.style.MozBorderRadius='2.73em';
	with(Style.MainElems.ToolBox) {
		style.MozBorderRadiusTopright=
			style.MozBorderRadiusBottomright='1.3em';
	}
	
	AutoAjaxer.prepareLinks(document);
}

function toggleToolBox() {
	if (NavigatorInfo.Type==NavigatorInfo.NVG_IE && NavigatorInfo.Version==6) {
		Effects.SteppyChange.change('main_content',
			'right', Style.Settings.toolBoxOpen? '71px':'151px', {len:'71px',opacity:0.83},
			Effects.SteppyChange.PredefinedStepInfos.Fast);
	} else {
		Effects.SteppyChange.change('main_content',
			'marginRight', Style.Settings.toolBoxOpen? '71px':'151px', {len:'71px',opacity:0.83},
			Effects.SteppyChange.PredefinedStepInfos.Fast);
		
	}

	Effects.SteppyChange.change('toolbox',
			'height', Style.Settings.toolBoxOpen? '13px':'171px', {len:'13px',opacity:1},
			Effects.SteppyChange.PredefinedStepInfos.Smooth);
	
	Style.Settings.toolBoxOpen=!Style.Settings.toolBoxOpen;
}

Style.ColorsScheme={
	replace:function(new_style) {
		$('css_colors').href=CMS.StylePath+'css/colors/'+new_style+".css";
	}
};

Style.setChosenLI=function(child) {
	var listItem=child.parentNode;
	var listUL=listItem.parentNode;
	listItem.className='options_item_choosen';
	if (listUL.lastChoosenLI) listUL.lastChoosenLI.className='options_item';
	listUL.lastChoosenLI=listItem;
}