﻿var nativeSplit = nativeSplit || String.prototype.split;

String.prototype.split = function(s, limit) {
	// If s (separator) is not a regex, just use the native split method
	if (s instanceof RegExp) {
		var flags = (s.global?"g":"") + (s.ignoreCase?"i":"") + (s.multiline?"m":"");
		if (!s.global) {
			s = new RegExp(s.source, "g" + flags);
		}
	} else {
		return nativeSplit.apply(this, arguments);
	}
	
	/* Behavior for limit: If it's...
	- Undefined: No limit
	- Zero: Return an empty array
	- A positive number: Use limit after dropping any decimal value (if it's then zero, return an empty array)
	- A negative number: No limit, same as if limit is undefined
	- A type/value which can be converted to a number: Convert, then use the above rules
	- A type/value which can't be converted to a number: Return an empty array */
	if (limit === undefined || +limit < 0) {
		limit = false;
	} else {
		limit = Math.floor(+limit);
		if (!limit) return [];
	}
	
	var s2 = new RegExp("^" + s.source + "$", flags),
		match,
		output = [],
		lastLastIndex = 0,
		i = 0;
	
	while ((!limit || i++ <= limit) && (match = s.exec(this))) {
		var zeroLengthMatch = !match[0].length;
		
		// Fix IE's infinite-loop-resistant but incorrect lastIndex
		if (zeroLengthMatch && s.lastIndex > match.index) s.lastIndex = match.index;
		
		if (s.lastIndex > lastLastIndex) {
			// Fix IE and Opera to always return undefined for non-participating capturing groups
			if (match.length > 1) {
				match[0].replace(s2, function() {
					for (var j = 1; j < arguments.length - 2; j++){
						if (arguments[j] === undefined) match[j] = undefined;
					}
				});
			}
			
			output = output.concat(this.substring(lastLastIndex, match.index), (match.index === this.length ? [] : match.slice(1)));
			lastLastIndex = s.lastIndex;
		}
		
		if (zeroLengthMatch) s.lastIndex++;
	}
	
	return (lastLastIndex === this.length)
		? (s.test("") ? output : output.concat(""))
		: (limit ? output : output.concat(this.substring(lastLastIndex)));
};

// This first part is needed for AJAX based reload of content
var xmlhttp = false;

//Check if we are using IE.
try
{
    //If the Javascript version is greater than 5.
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
    //If not, then use the older active x object.
    try
    {
        //If we are using Internet Explorer.
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (E)
    {
        //Else we must be using a non-IE browser.
        xmlhttp = false;
    }
}

//If we are using a non-IE browser, create a javascript instance of the object.
if (!xmlhttp)
{
    xmlhttp = new XMLHttpRequest();
}

var folder;
var index = 0;
var slides;
var prev;
var next;
var rootFolder;

function changeFolder(e)
{
    var select = e;
    slide = document.getElementById('Slide');
    folder = select.value;
    if(folder=='') {
        return false;
    }
    slide.className = 'hide';
    var fotograaf;
    switch(rootFolder) {
        case 'Cursus':
            fotograaf = 'Kitty Verheul';
            break;
        case 'Fotoclub Dinasa':
            fotograaf = 'A.F.C. Dinasa';
            break;
        default:
            fotograaf = 'cursisten';
            break;
    }
    document.getElementById('Fotograaf').innerHTML = fotograaf;
    document.getElementById('AlbumsInfo').className = 'Show';
    prev = document.getElementById('prev');
    next = document.getElementById('next');
    disableNavigation()
    //select empty option in other selects
    var selects = document.getElementsByTagName('select');
    for (i=0; i<selects.length; i++) {
        var options = selects[i].options
	    if (select.id != selects[i].id && options[options.length-1].text.length == 0) {
            options[options.length-1].selected = true;
	    }
    }
    xmlhttp.open("GET", "Slides.ashx?rootfolder="+rootFolder+"&folder="+folder, false);
    xmlhttp.send(null);
    var response = xmlhttp.responseText;
    if(response) {
        slides = response.split("|");
    }
    index = 0;
    displaySlide();
    return false;
}

var slideWidth ;
var slideHeight;
var slide;
function displaySlide()
{
    if (slides.length > 0) {
        var slideInfo = slides[index].split("#");
        var name = slideInfo[0];
        var width = 1*slideInfo[1];
        var height = 1*slideInfo[2]
        
        var size = 360;
        //scale image
        if ( width > height)
        {
            slideWidth = size;
            slideHeight = height * size / width;
        }
        else
        {
            slideWidth = width * size / height;
            slideHeight = size;
        }    
        slide.src = "Images/"+rootFolder+"/"+folder+"/"+name;
        var counter = document.getElementById('Counter');
        //workaround for IE innerHTML bug 
        counter.removeChild(counter.childNodes[0])
        counter.appendChild(document.createTextNode("Foto "+(index+1)+" van "+slides.length));
        window.setTimeout("enableNavigation()",0);
    }
    else
        document.getElementById('Counter').innerHtml = "";
};

function setSlideSize() {
    slide.className = "Show";
    slide.width = slideWidth;
    slide.height = slideHeight;
}

function prevSlide()
{
    prev.disabled = "disabled";
    if(index > 0) {
         index--;
         displaySlide();
    }
};

function nextSlide()
{
    next.disabled = "disabled";
    if(index < slides.length - 1) {
         index++;
         displaySlide();
    }
};

function disableNavigation()
{
    prev.disabled = "disabled";
    next.disabled = "disabled";
};

function enableNavigation()
{
    prev.disabled = (index > 0)?"":"disabled";
    prev.style.cursor = "pointer";
    next.disabled = (index < slides.length - 1)?"":"disabled";
    next.style.cursor = "pointer";
};

function ShowImagePopup(picName,picTitle,TitleSpace)
{
    // Purpose  : Used for showing larger image in a separate window
    // Parameter: picName    = Location and name of the picture
    //            picTitle   = Title or description for current picture
    //            TitleSpace = Default 25px, but is picTitle is a large sentence, it should be 50px or even more.
    // Author   : Rob van Meeuwen
    // Date     : 10 January 2007
    // Version  : 3.0  

    target = new Image();
    target.src = picName;
    picURL=target.src;
    newWindow=window.open(picURL,'newWin','toolbar=no,width='+target.width+',height='+target.height);
    
    newWindow.document.writeln('<html><head><title>DNM - Software Developer<\/title>');
    newWindow.document.writeln('<link href="./style/site.css" rel="stylesheet" type="text/css" />');
    newWindow.document.writeln('<\/head><body leftmargin="0px" topmargin="0px" class="siteImagePopup">');
    newWindow.document.writeln('<img style="cursor:hand;cursor:pointer;padding_bottom:5px" alt="'+picTitle+' (click image to close)." src="'+picName+'" onclick="window.close();" \/><br \/>');
    newWindow.document.writeln('<div align="center"><b>&middot;</b> '+picTitle+' <b>&middot;</b><\/div>');
    newWindow.document.writeln('<script>window.width='+target.width+';');
    newWindow.document.writeln('window.height='+target.height+';<\/script>');
    newWindow.document.writeln('<\/body><\/html>');
    newWindow.resizeBy(target.width-newWindow.document.body.clientWidth,target.height-newWindow.document.body.clientHeight+TitleSpace);
    newWindow.focus();
}