//FADING ELEMENTS

//opacity function, fades image from start opacity to end opacity during set timespan
function opacity(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;
    //direction for the blending
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
} 

//sets opacity of an element. Should be browser-independent
//this does throw warnings, because different browsers don't understand some elements, but it doesn't throw errors and works
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

// ajax functions

// :)
var xmlHttp

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

//testing function
//this is the most convenient ajax function I've managed to build, it's essentially an all-in-one solution and also
//gives an example how to use a loading bar. I recommend always using loading bars in ajax, to give user some feedback.
function ajaxtest(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp){
		var url="ajax_functions.php";
		url=url+"?act=test";
		url=url+"&random="+Math.random();
		document.getElementById("ajaxtest").innerHTML="<img title='loading' src='loading.gif'/>";
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4){
				document.getElementById("ajaxtest").innerHTML=xmlHttp.responseText;
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

//execute ajax function for loading new portfolio images. 'from' is essentially order index.
function loadportfolio(from,parentid,ajaxurl,languageid){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp){
		var url="ajax.php";
		url=url+"?act=portfolio";
		url=url+"&from="+from;
		url=url+"&parentid="+parentid;
		url=url+"&url="+ajaxurl;
		url=url+"&languageid="+languageid;
		url=url+"&random="+Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4){
				document.getElementById("gallery_porfolio").innerHTML=xmlHttp.responseText;
				opacity('gallery_table', 0, 100, 200);
				setTimeout('document.getElementById("gallery_table").style.display="block"',50);
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

//execute ajax function for loading new gallery images. 'from' is essentially order index.
function loadgallery(from,parentid,languageid){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp){
		var url="ajax.php";
		url=url+"?act=gallery";
		url=url+"&from="+from;
		url=url+"&parentid="+parentid;
		url=url+"&languageid="+languageid;
		url=url+"&random="+Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4){
				document.getElementById("gallery_porfolio").innerHTML=xmlHttp.responseText;
				opacity('gallery_table', 0, 100, 200);
				setTimeout('document.getElementById("gallery_table").style.display="block"',50);
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

//execute ajax function for displaying gallery image.
function opengallery(galleryid,imageurl){
	document.getElementById('inactive').style.display='block';
	document.getElementById('popupbox').style.display='block';
	document.body.style.overflow='hidden';
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp){
		var url="ajax.php";
		url=url+"?act=browser";
		url=url+"&galleryid="+galleryid;
		url=url+"&imageurl="+imageurl;
		url=url+"&random="+Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4){
				document.getElementById("popupbox").innerHTML=xmlHttp.responseText;
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

//closing gallery. This also restores the hidden scrolls.
//scrolls were hidden, because some browsers misunderstood the 100% width and height property
function closegallery(){
	document.getElementById('inactive').style.display='none';
	document.getElementById('popupbox').style.display='none';
	document.body.style.overflow='auto';
}

//gallery navigation ajax function
function movebrowser(galleryid,newposition){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp){
		var url="ajax.php";
		url=url+"?act=browsermove";
		url=url+"&galleryid="+galleryid;
		url=url+"&newposition="+newposition;
		url=url+"&random="+Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4){
				document.getElementById("popupbox").innerHTML=xmlHttp.responseText;
				opacity('currentpic', 0, 100, 200);
				setTimeout('document.getElementById("currentpic").style.display="block"',50);
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

//various small functions
//these are hardcoded functions, were written to overcome quote tag syntax problems
function setloadingbar(){
	document.getElementById("gallery_porfolio").innerHTML="<table class='porfolio'><tr><td class='galleryloading'><img title='loading' src='images/loading.gif'></td></tr></table>";
}

function movegallery(galleryid,newposition){
	opacity('currentpic', 100, 0, 200);
	setTimeout('movebrowser('+galleryid+','+newposition+')',210);
}

function portfolionav(from,parentid,ajaxurl,languageid){
	opacity('gallery_table', 100, 0, 200);
	setTimeout('loadportfolio('+from+','+parentid+',"'+ajaxurl+'",'+languageid+')',210);
}

function gallerynav(from,parentid,languageid){
	opacity('gallery_table', 100, 0, 200);
	setTimeout('loadgallery('+from+','+parentid+','+languageid+')',210);
}

//hide or display intranet box
function intranetbox(element){
	if(document.getElementById(element).style.display == 'none'){
		document.getElementById(element).style.display = '';
	} else {
		document.getElementById(element).style.display = 'none';
	}
}

//this displays full image with fade effect
function viewFull(imagelocation,fw,fh){
	if (imagelocation){
		document.getElementById('fullimage').style.opacity=0;
		document.getElementById('fullimage').style.MozOpacity=0;
		document.getElementById('fullimage').style.KhtmlOpacity=0;
		document.getElementById('fullimage').style.filter = "alpha(opacity=" + opacity + ")";
		document.getElementById('fullimage').style.width=fw+'px';
		document.getElementById('fullimage').style.height=fh+'px';
		document.getElementById('fullimage').src=imagelocation;
		opacity('fullimage', 0, 100, 500)
		}
}