var hdagt       = navigator.userAgent.toLowerCase();
var hdis_major  = parseInt(navigator.appVersion);
var hdis_ie     = ((hdagt.indexOf("msie") != -1) && (hdagt.indexOf("opera") == -1));
var hdis_ie4up  = (hdis_ie && (hdis_major >= 4));

var pathGallery = "images";
var ii;
var arrGallery = new Array();

ii=0;
arrGallery[ii++] = "oakpic2small.jpg";
arrGallery[ii++] = "oakpic5small.jpg";
arrGallery[ii++] = "oakpic8small.jpg";
arrGallery[ii++] = "oakpic8asmall.jpg";
arrGallery[ii++] = "oakpic9small.jpg";
arrGallery[ii++] = "oakpic11small.jpg";
arrGallery[ii++] = "oakpic27small.jpg";
arrGallery[ii++] = "oakpicg2small.jpg";
arrGallery[ii++] = "oakpicg4small.jpg";
arrGallery[ii++] = "oakpicg5small.jpg";
arrGallery[ii++] = "oakpicg7small.jpg";
arrGallery[ii++] = "oakpicg11small.jpg"; 
arrGallery[ii++] = "oakpicg14small.jpg";
arrGallery[ii++] = "oakpicgbarnsmall.jpg";
arrGallery[ii++] = "oakpichb9small.jpg";
arrGallery[ii++] = "oakpichb10small.jpg";
arrGallery[ii++] = "oakpichb11small.jpg";
arrGallery[ii++] = "oakpichb12small.jpg";
arrGallery[ii++] = "oakpichb16small.jpg";
arrGallery[ii++] = "oakpichb21small.jpg";
arrGallery[ii++] = "oakpichb26small.jpg";
arrGallery[ii++] = "oakpichb27small.jpg";
arrGallery[ii++] = "oakpicppg6small.jpg";
arrGallery[ii++] = "oakpicppg8small.jpg";
arrGallery[ii++] = "oakpicppg9small.jpg";
arrGallery[ii++] = "oakpicppg22small.jpg";
arrGallery[ii++] = "oakpic7small.bmp";
arrGallery[ii++] = "oakpicppg24small.jpg";
arrGallery[ii++] = "oakpicg15small.jpg"; 
arrGallery[ii++] = "oakpichbplans1small.jpg"; 
arrGallery[ii++] = "oakpicg1small.jpg";  
arrGallery[ii++] = "oakpicg10small.jpg";
arrGallery[ii++] = "oakpicg12small.jpg";
arrGallery[ii++] = "oakpicg13small.jpg";

/*-- FUNCTIONS ----------------------------------------------------------------------*/	

function clickImage(strImage) {

	pic = new Image();
	pic.src = strImage;
	//w = pic.width;
	//h = pic.height;
	w = 800;
	h = 600;
	l = (screen.width/2)-(w/2);
	t = (screen.height/2)-(h/2);

	if (hdis_ie4up) {
		args = "dialogWidth:"+w+"px; DialogHeight:"+h+"px; Center:Yes; Status:No; Scroll:No; Help:No; Edge:Raised; Resizable:Yes;";
		window.showModalDialog(strImage,"",args);
	} else {
		window.open(strImage,'','width='+w+',height='+h+',top='+t+',left='+l+',toolbars=0');
	}
	
}

function showGallery() {
	var s = "";
	var srcImage = "";
	var clickImage = "";
	for (i=0; i<arrGallery.length; i++) {

			srcImage = pathGallery+"/"+arrGallery[i];
			clickImage = srcImage.replace(/small/gi, '');
			//s = s + "<img class='gallery' src='"+srcImage+"' alt='Click to open large picture' onmouseover=\"this.style.cursor='pointer';\" onClick=\"clickImage('"+clickImage+"');\" />";
            //s = s + "<img class='gallery' src='"+srcImage+"' alt='Click to open large picture' onmouseover=\"this.style.cursor='pointer';\" onClick=\"clickImage('"+clickImage+"');\" />";
            //popupShow(this);
            s = s + "<img class='gallery' src='"+srcImage+"' alt='Click to open large picture' onmouseover=\"this.style.cursor='pointer';\" onClick=\"popupShow(this);\" />";
            
	}
	document.write(s);	
}

/*Preload images in passed array*/	
function preloadGallery() {

	for (i=0; i<arrGallery.length; i++) {
		srcImage = pathGallery+"/"+arrGallery[i];
		preloadImg = new Image();
		preloadImg.src = srcImage
		preloadImg1 = new Image();
		preloadImg1.src = srcImage.replace(/small/gi, '');
	}
}

/*-- OVERLAY FUNCTIONS ----------------------------------------------------------------------*/     

var popupOverlay = "popupOverlay";

function getById(elementId) {return document.getElementById(elementId);}

function hideElementById(elementId) {
    element = getById(elementId);
    if (element) {element.style.display = "none";}
}

function unhideElementById(elementId) {
    if (elementId != "") {
        element = getById(elementId);
        if (element) {element.style.display = "inline";}
    }
}

function maximiseElementSize( elementId ) {
    var resizeElement = getById( elementId );
    var windowWidth = Math.max(document.body.clientWidth, document.body.scrollWidth);
    var windowHeight = Math.max(document.body.clientHeight, document.body.scrollHeight);
    
    resizeElement.style.width = windowWidth + "px";
    resizeElement.style.height = windowHeight + "px";
    
}

function centerElement( elementId ) {
    var cElement = getById( elementId );
    var windowWidth = document.body.clientWidth;
    var windowHeight = document.body.clientHeight;
    var elementWidth = cElement.scrollWidth;
    var elementHeight = cElement.scrollHeight;
    var xPos = ( windowWidth - elementWidth ) / 2;
    var yPos = ( windowHeight - elementHeight ) / 3;
        
    cElement.style.left = xPos;
    cElement.style.top = (document.body.scrollTop-30) + yPos;
}

function fireNativeEvent(element, eventName) { 
    if (element.fireEvent) { 
        // MSIE 
        element.fireEvent(eventName); 
    } else { 
        // W3C 
        var event = document.createEvent("HTMLEvents"); 
        event.initEvent(eventName.replace(/^on/, ""), true, true); 
        element.dispatchEvent(event); 
    } 
}
    
function popupShow(obj) {

    //p = getPosition();
    //alert(p.x);
    //alert(p.y);
    
    popupId = "popupPic";
    
    //Replace popup picture
    objPic = getById("popupImg");
    objPic.src = obj.src.replace(/small/gi, '');  
    
    //Prepare the overlay
    maximiseElementSize(popupOverlay);
    unhideElementById(popupOverlay);

    //Show popup
    unhideElementById(popupId);
    centerElement(popupId);

}

function popupClose(obj) {

    popupId = obj.parentNode.id;
        
    //Hide popup
    hideElementById(popupId);

    //Hide the overlay
    hideElementById(popupOverlay);    
    
    //unhide all select elements - IE6 hack
    var formElements = document.forms[0].elements;
    if (formElements) {
        for (i = 0; i < formElements.length; i++) {
            elem = formElements[i];
            if (elem.type == "select-one") {unhideElementById(elem.id);}
        }        
    }
}

function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}
