/* 
	AJAX & window library routines
	bx]l[ab 2007 - 2010
*/

function xmlObj(){
	if (window.XMLHttpRequest){
		var obj = new XMLHttpRequest();
	} else {
		var obj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return obj;
}

function getEl(el){
	if (document.getElementById) {
		return document.getElementById(el);
	} else if (document.all) {
		return document.all[id];
	}
}

function setEl(el, content){
	if (document.getElementById) {
		var obj = document.getElementById(el);
		if(obj)
			obj.innerHTML = content;
	} else if (document.all) {
		var obj = document.all[id];
		if(obj)
			obj.innerHTML = content;
	}
}

function setFormEl(el, content){
	if (document.getElementById) {
		var obj = document.getElementById(el);
		obj.value = content;
	} else if (document.all) {
		var obj = document.all[id];
		obj.value = content;
	}
}

document.write('<span id=tooltip style="position: absolute; display: hidden; top:0px; left: 0px;"></span>');

function showTooltip(text){
	var obj=getEl("tooltip");
	obj.style.visibility="visible";
	setEl('tooltip',text);
}

function hideTooltip(){
	var obj=getEl("tooltip");
	obj.style.visibility="hidden";
}

function showWindow(title){
	var obj=getEl("window");
	obj.style.visibility="visible";
	setEl('title','<b>'+title+'</b>');
}

function hideWindow(){
	var obj=getEl("window");
	obj.style.visibility="hidden";
}
/*
	set element  
*/
function ajaxSetEl(url, el, call){
	var obj = xmlObj();
	//setEl(el,"Loading..." + url);
	obj.onreadystatechange = function(){
		if (this.readyState==4) {
			if (this.status==200) {
				var str = this.responseText;
				var re = /<script*?>([\S\s]*?)<\/script>/i;
				var m = re.exec(str);
				if(el){
					setEl(el,this.responseText);
					if (m != null) {
						for(var i=1; i < m.length; i++){
							eval(m[i]);
						}
					}
				}
				if(call) call();
			} else {
				//alert("warning: ajax request returned "+obj.status+"\r\nRequest: "+url);
				setEl(el,"Error loading "+url);
			}

		} else {
			//alert(obj.status+url +  obj.readyState);
		}
	}
	obj.open("GET",url,true);
	obj.send(null);
}

/*
function ajaxLoader(url, el, call) {
	this.url = url;
	this.el = el;
	this.call = call;
	this.value = 10;
	this.step = -.1;
	this.interval = 10;
	this.start = ajaxLoaderStart;
	this.load = ajaxLoaderLoad;
	this.setOpacity2 = ajaxLoadersetOpacity;
	this.fadeObj2 = ajaxLoaderfadeObj;

	this.fadeinObj2 = ajaxLoaderfadeinObj;
	this.fadeoutObj2 = ajaxLoaderfadeoutObj;
}

function ajaxLoaderStart(){
	this.fadeoutObj2();
}

function ajaxLoaderLoad(){
	alert(this.url);
	var obj = xmlObj();
	obj.onreadystatechange=function(){
		if (obj.readyState==4) {
			if (obj.status==200) {
				setEl(this.el,obj.responseText);
				this.step = .3;
				this.fadeinObj2();
				if(this.call) this.call();
			} else {
				setEl(this.el,"Error loading "+this.url);
			}

		} 
	}
	obj.open("GET",this.url,true);
	obj.send(null);
}

function ajaxLoaderfadeinObj(){
	this.value = 0;
	this.setOpacity2(this.el,0);
	setTimeout(function(){this.fadeObj2();},this.interval);
}

function ajaxLoaderfadeoutObj(){
	this.value = 10;
	this.setOpacity2(this.el,10);
	setTimeout(function(){this.fadeObj2();},this.interval);
}

function ajaxLoaderfadeObj(){
	this.value += this.step;
	this.setOpacity2(this.el,this.value);
	if(this.value < 10 && this.value > 0){
		setTimeout(function(){this.fadeObj2();},this.interval);
	} else {
		if(this.step < 0) // 
			this.load();
	}
}

function ajaxLoadersetOpacity(id,value) {
	var obj = getEl(id);
	obj.style.opacity = value/10;
	obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}



function ajaxSetEl2(url, el, call){
	l = new ajaxLoader(url,el,call);
	l.start();
}

*/


/*
	Set Element Extended 
*/
function ajaxSetElExt(url, el, call){
	var obj = xmlObj();
	//setEl(el,"Loading..." + url);
	obj.onreadystatechange=function(){
		if (obj.readyState==4) {
			if (obj.status==200) {
				setEl(el,obj.responseText);
				if(call) 
					call(el,obj.responseText);
					obj = null;
			} else {
				setEl(el,"Error loading "+url);
			}

		}
	}
	obj.open("GET",url,true);
	obj.send(null);
}


function ajaxGetJS(url, el){
	var obj = xmlObj();
	obj.onreadystatechange=function(){
		if (obj.readyState==4) {
			if (obj.status==200) {
				//alert(" "+obj.status+"\r\nRequest: "+url);
				eval(obj.responseText);
			} else {
				alert("warning: ajax request returned "+obj.status+"\r\nRequest: "+url);
			}
		}
	}
	obj.open("GET",url,true);
	obj.send(null);
}


function sizeElement(el,width, height, steps){
	var e=getEl(el);
	var x=parseInt(e.style.width);
	var y=parseInt(e.style.height);
	var newx=x + Math.round((width - x) / steps);
	var newy=y + Math.round((height - y) / steps);
	e.style.width=newx+'px';
	e.style.height=newy+'px';
	e.width=newx;
	e.height=newy;

	steps--;
	if (steps>0)
		setTimeout('sizeElement("'+el+'",'+width+','+height+','+steps+');',500);
}

function bounceEl(el,width, height, steps){
	var e=getEl(el);
	e.width=0;
	e.height=0;
	e.display='none';
	e.style.width='auto';
	e.style.height='auto';
	setTimeout('sizeElement("'+el+'",'+width+','+height+','+steps+');',500);
}

function ajaxGetDoc(url, callbackFunc){
	var obj = xmlObj();
	obj.onreadystatechange=function(){
		if (obj.readyState==4) {
			if (obj.status==200) {
				callbackFunc(obj.responseText);
				//setEl(el,obj.responseText);
			} else {
				alert("warning: ajax request returned "+obj.status+"\r\nRequest: "+url);
			}
		}
	}
	obj.open("GET",url,true);
	obj.send(null);
}

function ajaxSetFormEl(url, el){
	var obj = xmlObj();
	obj.onreadystatechange=function(){
		if (obj.readyState==4) {
			if (obj.status==200) {
				setFormEl(el,obj.responseText);
			} else {
				alert("warning: ajax request returned "+obj.status+"\r\nRequest: url");
			}
		}
	}
	obj.open("GET",url,true);
	obj.send(null);
}

function ajaxPostForm(url){
	var obj = xmlObj();
	obj.onreadystatechange=function(){
		if (obj.readyState==4) {
			if (obj.status==200) {
				//alert(obj.responseText+' element: '+ el);
			} else {
				alert("warning: ajax request returned "+obj.status+"\r\nRequest: "+url);
			}
		}
	}
	obj.open("POST",url,true);
	obj.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	obj.send(null);
}

function toggleDelBtns(formname){
	var form=eval('document.'+formname);
	var newstate=form.delbtn.checked;
	for(var i=0;i<form.elements.length;i++){
		var e=form.elements[i];
		if(e.type=='checkbox'){
			e.checked=newstate;
		}
	}
}

function mover(e){
	e = !e ? window.event : e;
	alert(e.pageX);
}
function ToggleDiv(e,id,visible){
	var el = getEl(id);
	if(el){
		el.style.display=(visible?"block":"none");
		//el.style.position=(visible?"absolute":"relative");
	}
	/*if(visible){
		e = !e ? window.event : e;
		alert(e.pageX);
		el.style.left = e.pageX+'px';
		el.style.top = e.pageY+'px';
	}*/
	
}

function ToggleLang(level,index,max,caller){
	for(var i=0;i<max;i++){
		var obj = getEl('form'+i+'_'+level);
		if(obj!=null){
			obj.style.display = (i==index)?'block':'none';
		}
		
	}
	for(var i=0;i<max+1;i++){
		var obj = getEl('tab'+i+'_'+level);
		if(obj!=null){
			obj.className='tab';
		}
	}
	caller.className = 'tab tabselected';
}

function ToggleLangAll(level,max,caller){
	for(var i=0;i<max;i++){
		var obj = getEl('form'+i+'_'+level);
		if(obj!=null){
			obj.style.display = 'block';
		}
	}

	for(var i=0;i<max+1;i++){
		var obj = getEl('tab'+i+'_'+level);
		if(obj!=null){
			obj.className='tab';
		}
	}

	caller.className = 'tab tabselected';

}

// value = 1 - 10
function setOpacity(id,value) {
	var obj = getEl(id);
	if(obj){
		obj.style.opacity = value/10;
		obj.style.filter = 'alpha(opacity=' + value*10 + ')';
	}
}

function fadeinObj(id,step,interval,call){
	setOpacity(id,0);
	setTimeout('fadeObj(\''+id+'\',0,'+step+','+interval+','+call+');',interval);
}

function fadeoutObj(id,step,interval,call){
	setOpacity(id,10);
	setTimeout('fadeObj(\''+id+'\',10,'+step+','+interval+','+call+');',interval);
}

function fadeObj(id,value,step,interval,call){
	value += step;
	setOpacity(id,value);
	if(value < 10 && value > 0){
		setTimeout('fadeObj(\''+id+'\','+value+','+step+','+interval+','+call+');',interval);
	} else {
		if(call) call();
	}
}

function CreateHint(el){
	var el1 = document.getElementById('hint');
	if(!el1) {
		var el1 = document.createElement('DIV');
		el1.setAttribute('id','hint');
		el1.setAttribute('class','hint');
		document.body.appendChild(el1);
	}
	el1.innerHTML = el.getAttribute('hint');
	CreateEvent(el,'mousemove',MoveHint);
	CreateEvent(el,'mouseout',RemoveHint)
}

function MoveHint(e){
	var tempX,tempY;

	if (!e) {
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {
		tempX = e.pageX
		tempY = e.pageY
	}
	var el1 = document.getElementById('hint');
	if(el1){
		//window.status=parseInt(el1.offsetWidth)/2;
		var w = parseInt(el1.offsetWidth);
		w = !w ? 0 : w;
		el1.style.left = (tempX + 0)+'px';
		el1.style.top = (tempY+30)+'px';
	}

}

function RemoveHint(){
	var el = document.getElementById('hint');
	if(el)
		el.parentNode.removeChild(el);
}

function CSSWindow(url,nobackground,text,width){
	if(nobackground==false || !nobackground){
		var el1 = document.createElement('DIV');
		el1.setAttribute('id','cssbgwindow');
		el1.setAttribute('class','cssbgwindow');
		document.body.appendChild(el1);
	}

	var el2 = document.createElement('DIV');
	el2.setAttribute('id','csscontainerwindow');
	el2.setAttribute('class','csscontainerwindow');

	var el3 = document.createElement('DIV');
	el3.setAttribute('id','csswindow');
	if(width){
		el3.setAttribute('style','width: '+width+'px');
	}
	el3.setAttribute('class','csswindow roundbox');
	
	el2.appendChild(el3);


	if(text){
		el3.innerHTML=text;
	}

	document.body.appendChild(el2);
	ajaxSetEl(url,'csswindow');
}

function CloseCSSWindow(timeout){
	if(timeout){
		var el = document.getElementById('cssbgwindow');
		if(el)
			el.parentNode.removeChild(el);

		fadeoutObj('csscontainerwindow',-.5,10,CloseCSSWindow);
		return;
	}


	var el = document.getElementById('cssbgwindow');
	if(el)
		el.parentNode.removeChild(el);

	var el = document.getElementById('csscontainerwindow');
	if(el)
		el.parentNode.removeChild(el);
}

function saveForm(id,name,value){
	ajaxSetEl('?event='+id+'&'+name+'='+value,'csswindow');
}

function ReloadImage(id){
	var img = document.getElementById(id);
	if(img){
		img.src = img.src + '?'+new Date();

	}
}

function removeItem(id){
	var el = document.getElementById(id);
	if(el){
		el.parentNode.removeChild(el);
	}
}

function replaceClassItem(id,oldval,newval){
	var el = document.getElementById(id);
	if(el){
		var classobj = el.getAttribute('class');
		var re = new RegExp(oldval, "i");
		classobj = classobj.replace(re, newval)
		el.setAttribute('class', classobj);
	}
}

function CreateEvent(el,event,call){
	if(el.attachEvent){
		window.attachEvent("on"+event, call);
	} else if(window.addEventListener){
		window.addEventListener(event, call,true);
	}
}

//CreateEvent(window,'mouseover',mousemove);

function CSSEdit(url){
	el = document.getElementsByClassName('albumeditform');
	el[0].style.display="block";
	ajaxSetEl(url,'albumeditform');
}

function InsertFile(){
	var iafter = document.getElementById('lastfile');

	var newtr = document.createElement('TR');
	//newtr.setAttribute('id','lastfile');

	var newTD = document.createElement('TD');
	newTD.innerHTML='File:';
	newtr.appendChild(newTD);

	var newTD = document.createElement('TD');

	var newinp = document.createElement('INPUT');
	newinp.setAttribute('type','file');
	newinp.setAttribute('name','fname[]');

	newTD.appendChild(newinp);
	newtr.appendChild(newTD);
	iafter.parentNode.insertBefore(newtr,iafter);

}
