var slideshowdata;
var lastslideshowid;

function getslideshowdata() {
	var xmlhttp;
	
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	
	xmlhttp.open('GET','/_feed/slideshow.php',true);
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var result = xmlhttp.responseText;
			
			var json = eval('(' + result + ')');
			
			// kontroller der er produkter
			if (json.length == 0) return false;
			
			// gem i variable
			slideshowdata = json;
			
			// start slideshow
			slideshow();
		}
	}
	
	xmlhttp.send(null);
}

function getslideshowid() {
	return Math.floor(Math.random() * (slideshowdata.length - 1));
}

function in_array(needle, haystack, strict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!strict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}

function slideshow() {
	// tjek om data er hentet, ellers afbryd
	if (slideshowdata == null) {
		getslideshowdata();
		
		return false
	}
	
	// tjek om alle billeder har været vist og nulstil array
	if (lastslideshowid == null || lastslideshowid.length == slideshowdata.length) {
		var lastslideshowid = new Array();
	}
	
	// vælg tilfældigt slideshowprodukter
	var id = 0;
	
	// sikre det ikke er samme billede der vises to gange, hvis mere end 1 billed
	while(in_array(id = getslideshowid(),lastslideshowid));
	
	lastslideshowid[lastslideshowid.length] = id;
	
	var produkt = slideshowdata[id];
	
	// skift billede
	document.getElementById('slideshow').setAttribute('src','/billed/' + produkt[3][0] + '/' + produkt[3][2] + 'x' + produkt[3][3] + '/' + produkt[3][1]);
	
	// skift link-href
	document.getElementById('slideshow_link').setAttribute('href','/' + document.getElementById('systemlang').firstChild.nodeValue + '-' + document.getElementById('systemcur').firstChild.nodeValue + '/webshop/' + produkt[2] + '/' + produkt[0] + '/' + produkt[1] + '.htm');
	// kør igen om 10 sek
	setTimeout('slideshow()',2000);
}