/*
javascript portion of php ajax slideshow example by kethinov (http://eric.halo43.com)
this script is BSD licensed
*/

// crossbrowser http request engine
function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E) {
            xmlhttp = false;
        }
    }
    @else
    xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

// performs ajax get requests
function httpRequest(url) {
    var http = getHTTPObject();
    http.open('GET', url, true);
    http.onreadystatechange = function() { if (http.readyState == 4) eval(http.responseText); }
    http.send(null);
}

// removes all children from an element
function removeChildren(cell) {
    if (cell.hasChildNodes()) while (cell.childNodes.length >= 1) cell.removeChild(cell.firstChild);
}

// returns random number betwen m and x
function rand(m, x) {
    return (m + Math.round((x - m) * Math.random()));
}

// shows an image from the slideshow
function iterateSlideshow() {

    // if container exists and there are images to show
    if (document.getElementById('slideshow_img_container') && slideshow_images.length && !overImg) {

		if (random || random_start_pos) {
			imgNum = rand(0, (slideshow_images.length - 1));
			random_start_pos = false;
		}
        
        // if there's an image already there
        if (document.getElementById('slideshow_img')) {
            // delete it
            removeChildren(document.getElementById('slideshow_img_container'));
        }

        // make new img element
        var newLink = document.createElement('span');
		newLink.whereTo = 'slideshow.php?which_dirs='+which_dirs+'&popup=' + slideshow_images[imgNum];
		newLink.style.cursor = 'pointer';
        //newLink.href = slideshow_images[imgNum];
        //newLink.setAttribute('target', '_new_window');
		newLink.onclick = function() {
			if (popup) popup.close();
			popup = window.open(this.whereTo, 'slideshowPopup', 'toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no');
			if (window.focus) popup.focus();
			return false;
		}


        var newImg = document.createElement('img');
        newImg.id = 'slideshow_img';
        newImg.src = 'slideshow.php?thumb='+slideshow_images[imgNum];
        newImg.setAttribute('border', 0);

        newLink.appendChild(newImg);
        document.getElementById('slideshow_img_container').appendChild(newLink);

		document.getElementById('slideshow_img').onmouseover = function() {
			overImg = true;
		}

		document.getElementById('slideshow_img').onmouseout = function() {
			overImg = false;
		}
		
		if (slideshow_descriptions[imgNum]) {
	        //var newP = document.createElement('p');
	        //newP.appendChild(document.createTextNode(slideshow_descriptions[imgNum]));
			//document.getElementById('slideshow_img_container').appendChild(newP);
			newLink.setAttribute('title', slideshow_descriptions[imgNum]);
			newLink.setAttribute('alt', slideshow_descriptions[imgNum]);
		}

        // set img counter for next img
        if (!random) {
			imgNum++;

        	// reset img to first one if there's no next one
        	if (!slideshow_images[imgNum]) imgNum = 0;
		}
    }
    window.setTimeout(iterateSlideshow, slideshow_timer); // refresh img in x milliseconds
}

// vars necessary for script
var overImg = false;
var slideshow_images = Array(); // init slideshow imgs array
var slideshow_descriptions = Array(); // init the descriptions array
var popup = false;
var imgNum = 0; // set img counter

// init
if (!allow_undescribed) httpRequest('slideshow.php?which_dirs='+which_dirs);
else httpRequest('slideshow.php?allow_undescribed=1&which_dirs='+which_dirs);
