	function getHTTPObject(){
//	  alert ("opening");
	   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	   else if (window.XMLHttpRequest) return new XMLHttpRequest();
	   else {
	      alert("Your browser does not support AJAX.");
	      return null;
	   }
	}

    function checktimer(direction, current, lastwidth, lastheight, lastleft, auto){
        if ((typeof(autophoto)!="undefined") && (typeof(autophoto)!="string")){
            clearTimeout(autophoto);
            autophoto="";
            document.getElementById("middlebutton").src="/images/imgcontrolsm.png";
            document.getElementById("middlebutton").onmouseover="this.src='images/imgcontrolsmon.png'; this.style.cursor='pointer';";
            document.getElementById("middlebutton").onmouseout="this.src='images/imgcontrolsmon.png'; this.style.cursor='pointer';";
        } else {
            changeimg(direction, current, lastwidth, lastheight, lastleft, auto)
        }
    }

	function changeimg(direction, current, lastwidth, lastheight, lastleft, auto){
//	  alert("change")
            hidecontrols("hidden");
    	    httpObject = getHTTPObject();
            var current = current.replace("+", "--");
    	    if (httpObject != null) {
    	        httpObject.open("GET", "/ourwork.php?direction="+direction+"&current="+current+"&lastwidth="+lastwidth+"&lastheight="+lastheight+"&lastleft="+lastleft+"&auto="+auto, true);
    	        httpObject.send(null);
                httpObject.onreadystatechange = setOutput;
    	    }
            setOutput();
	}

	function setimg(image){
//	  alert("change")
            hidecontrols("hidden");
    	    httpObject = getHTTPObject();
            var image = image.replace("+", "--");
    	    if (httpObject != null) {
    	        httpObject.open("GET", "/ourwork.php?image="+image, true);
    	        httpObject.send(null);
                httpObject.onreadystatechange = setOutput;
    	    }
            setOutput();
	}

    function hidecontrols(status){
        document.getElementById("controls").style.visibility=status;
    }

//    function waittillhidden(id){
////        alert("wait till hidden="+isvisible(id));
//        if (isvisible(id)===true){
////            alert("true");
//            setOutput();
//        } else {
//            setTimeout("waittillhidden('" + id + "')",1000);
//        }
//    }

//    function isvisible(id){
//        if (document.getElementById(id).style.visibility=='hidden')return true; else return false;
//    }

	function setOutput(){
        if(httpObject.readyState == 4){
            var response=httpObject.responseText;
            document.getElementById('content').innerHTML=response;

            imageshade=document.getElementById('imgshade').style;
            oldwidth=removepx(imageshade.width);
            oldheight=removepx(imageshade.height);

            newimg=document.getElementById('mainimg').style;
            newwidth=removepx(newimg.width);
            newheight=removepx(newimg.height);

            widthdir=(oldwidth>newwidth)?"shrink":"grow";
            heightdir=(oldheight>newheight)?"shrink":"grow";
            changeboxsize(oldwidth,newwidth,oldheight,newheight,widthdir,heightdir);
            if (response.indexOf("automaticgallery")>0){
                autophoto=setTimeout("changeimg('1', '"+document.getElementById("current").value+"', '"+document.getElementById("lastwidth").value+"', '"+document.getElementById("lastheight").value+"', '"+document.getElementById("leftpos").value+"', '1')",5000);
            }
            var photono;
            if (document.getElementById("photono").value){
                photono=parseInt(document.getElementById("photono").value)+1;
            } else {
                photono=0;
            }
            thumbwidth=parseInt(100);
            thumbpadding=parseInt(10)
            leftval=(((2*thumbpadding)+thumbwidth)*photono)+(2*thumbpadding);
            //document.getElementById('thumbcontainer').scrollLeft=leftval+'px';
            document.getElementById('thumbcontainer').scrollLeft+=leftval;
        }
	}

function stopchange(){
    clearTimeout(autophoto);
    document.getElementById("middlebutton").src="/images/imgcontrolsm.png";
    document.getElementById("middlebutton").onmouseover="this.src='images/imgcontrolsmon.png'; this.style.cursor='pointer';";
    document.getElementById("middlebutton").onmouseout="this.src='images/imgcontrolsmon.png'; this.style.cursor='pointer';";
}

//function outputresponse(response){
//    document.getElementById('content').innerHTML =response;
//}

function fade(id, direction){
//    alert("fade"+direction);
    if (direction=='in'){
        opacity(id, 0, 100, 500);
    } else if (direction=='out'){
        opacity(id, 100, 0, 500);
    }
}

function changeboxsize(oldwidth,newwidth,oldheight,newheight,widthdir,heightdir){
    newobj=document.getElementById('imgshade').style
    oldwidth=removepx(oldwidth);
    newwidth=removepx(newwidth);
    oldheight=removepx(oldheight);
    newheight=removepx(newheight);
    currentwidth="";
    currentheight="";
    if (oldwidth!=""){
        if (widthdir=="grow"){
          //alert("expand width "+oldwidth+" "+newwidth);
    // this expands the width
            if (oldwidth<newwidth){
                currentwidth=oldwidth+10;
                newobj.width=currentwidth+"px";
                leftval=removepx(newobj.left)-5
                newobj.left=leftval+"px";
            }
        } else {
          //alert("shrink width "+oldwidth+" "+newwidth);
    // this reduces the width
            if (oldwidth>newwidth){
                currentwidth=oldwidth-10;
                newobj.width=currentwidth+"px";
                leftval=removepx(newobj.left)+5
                newobj.left=leftval+"px";
            }
        }
    }
    if (oldheight!=""){
        if (heightdir=="grow"){
          //alert("expand height "+oldheight+" "+newheight);
    // this expands the height
            if (oldheight<newheight){
                currentheight=oldheight+10;
                newobj.height=currentheight+"px";
            }
        } else {
          //alert("shrink height "+oldheight+" "+newheight);
    // this reduces the height
            if (oldheight>newheight){
                currentheight=oldheight-10;
                newobj.height=currentheight+"px";
            }
        }
    }

    if (currentwidth!="" || currentheight!=""){
        setTimeout("changeboxsize('"+currentwidth+"','"+newwidth+"','"+currentheight+"','"+newheight+"','"+widthdir+"','"+heightdir+"')",1);
    } else {
        fade("imgshade","out")
        fade("mainimg","in")
//        hidecontrols("visible");
    }
}

//function changewidth(id, newwidth){
//    newobj=document.getElementById(id).style
//    newobj.width=newwidth;
//    //alert("changewidth");
//    alert(newobj.left);
//    leftval=removepx(newobj.left)-1
//    newobj.left=leftval+"px";
//}

//function changeheight(id, newheight){
//    newobj=document.getElementById(id).style
//    newobj.height=newheight;
//}

function removepx(val){
    val=val+'';
    if (val=="undefined") return false;
    if (val.indexOf("px")!=-1){
        val=parseInt(val.replace(/px/i, ""));
    } else {
        val=parseInt(val);
    }
    return val;
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    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++;
        }
    }
    if ((id=="mainimg") && (opacEnd==100)) hidecontrols("visible");
//    if (swap=="1") fadeimage("in")
//    if (opacEnd==0) document.getElementById(id).innerHTML = "";
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
//    alert(opacity)
    if (opacity==0){
//      alert("hide");
        document.getElementById(id).style.visibility = "hidden";
    } else if (document.getElementById(id).style.visibility=="hidden"){
//      alert("show");
        document.getElementById(id).style.visibility = "visible";
    }
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

