window.addEvent("domready", function(){
	var iglooCookie = new Cookie('iglooauth').read();
	
	
	if($('galleryCentral') && $('galleryCentral').innerHTML.length > 0){
		var galleryId = $('galleryCentral').innerHTML;
		var apiUri = "folders/" + galleryId + "/children";			
		
		var ajax = new ApiClient({
	        'headers': {
	            'Accept': 'application/json'
	        },
	        'apimethod': apiUri,
	        'method': 'get',
	        'queryparams': new Hash({'maxcount': '1000', 'orderby': 'None'}),
	        'sendimmediately': false,
	        onRequest: function() {
	            //alert(url)
	        },
	        onSuccess: function() {
	            var returnstring = this.response.text;
				var returnjson = JSON.decode(returnstring);		
				
				var contents = returnjson.response.items;
				
				if(contents.length > 0){
					var rnd = Math.floor( Math.random() * contents.length);    			    			    				    				
					var href = contents[rnd].href;    				
					$('header').style.backgroundImage = "url(/download" + href + ")";
				}
	        },
	        onFailure: function() {
	            //alert('Error: malformed request.');
	        }
        });
		
		try{
			ajax.send();
		}catch(err){
			$('header').style.backgroundImage = "url(/download/gallery/img/headerjpg)";
		}
	}
});