var galleryDir = "fileadmin/gallery/";
var scriptDir = "fileadmin/templates/gallery/"
var albums = [];

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return false;
} 

function loadAlbums(curAlbum){
	var xmlHttp;
	try
  	{	xmlHttp=new XMLHttpRequest(); }catch (e)
  		{ try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch (e)
    		{ try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch (e)
      				{ alert("Your browser does not support AJAX!");return false;}
    		}
  		}

	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	}

	if (curAlbum=="")
		xmlHttp.open("GET", scriptDir+"load_album.php?album=mainAlbum&rand="+Math.floor(Math.random()*100),true);
	else
		xmlHttp.open("GET", scriptDir+"load_album.php?album="+curAlbum+"&rand="+Math.floor(Math.random()*100),true);
  	
  	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
      	{
      		var string = xmlHttp.responseText;
      		string = string.split("<#@%>");
      		albums.length = 0;
      		var j=0;
      		var title = string[0];
      		var description = string[1];
      		for (var i=2; i<string.length-5; i=i+5){
      			albums[j] = [];
      			albums[j]['full_path'] = string[i];
      			albums[j]['path'] = string[i+1];
      			albums[j]['image'] = string[i+2];
      			albums[j]['title'] = string[i+3];
      			albums[j]['description'] = string[i+4];
      			albums[j]['path_no_numbers'] = getTitle(string[i+1]);
      			//alert(string[i+1]+"->"+getTitle(string[i+1]));
      			j++;
      		}
      		fillGallery(curAlbum,title,description);
      	}
    }
  	xmlHttp.send(null);
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function getTitle(string){
	if (string.indexOf(".")!=-1){
		var position = string.indexOf(".");
		var number = string.substr(0,position);
		if (isInteger(number))
			return string.substr(position+1,string.length);
		else
			return string;
	}else{
		return string;
	}
}

function gallery(){
	var album = getQueryVariable("album");
	if (album)
		loadAlbums(album);
	else 
		loadAlbums("");
}

function fillGallery(album,title,description){
	var content = "";
	var a = 0;
	
	var curAlbum = document.createElement("div");
	curAlbum.className = "curent_album";
	
	var curAlbumTitle = document.createElement("div");
	curAlbumTitle.className = "title";
	curAlbumTitle.innerHTML = title;
	
	var curAlbumDesc = document.createElement("div");
	curAlbumDesc.className = "description";
	curAlbumDesc.innerHTML = description;
	
	curAlbum.appendChild(curAlbumTitle);
	curAlbum.appendChild(curAlbumDesc);
	
	var allAlbums = document.createElement("div");
	allAlbums.className = "all_albums";
	
	var allPictures = document.createElement("div");
	allPictures.className = "all_pictures";
	
	for (var i=0; i<albums.length; i++){
		if (albums[i]['image']!=""){
			//is directory	
			var albumEl = document.createElement("div");
			if (a%2==0){
				albumEl.className = "album_left";
			}else{
				albumEl.className = "album_right";
			}
			var albumImage = document.createElement("div");
			albumImage.className = "album_image";
			
			
			var link = document.createElement("a");
			link.setAttribute("href","index.php?id=30&album="+album+albums[i]['path']);
			
			var image = document.createElement("img");
			image.setAttribute("src",albums[i]['full_path']+albums[i]['path']+"thumb/"+albums[i]['image']);
			/*image.setAttribute("width","200px");
			image.setAttribute("height","200px");*/
			
			
			link.appendChild(image);
			albumImage.appendChild(link);
			
			var albumText = document.createElement("div");
			albumText.className = "text";
			
			var p = document.createElement("p");
			var link = document.createElement("a");
			link.setAttribute("href","index.php?id=30&album="+album+albums[i]['path']);
			link.className = "title";
			if (albums[i]['title']!="")
				link.innerHTML = albums[i]['title'];
			else
				link.innerHTML = albums[i]['path_no_numbers'].substring(0,albums[i]['path_no_numbers'].length-1);
			p.appendChild(link);
			albumText.appendChild(p);
			
			
			if (albums[i]['description']!=""){
				var p_desc = document.createElement("div");
				p_desc.className = "description";
				p_desc.innerHTML = albums[i]['description'];
				albumText.appendChild(p_desc);
			}
			
			albumEl.appendChild(albumImage);
			albumEl.appendChild(albumText);
			
			
			if (a%2==0){
				var newAlbum = document.createElement("div");
				newAlbum.className = "album_row";
				newAlbum.appendChild(albumEl);
				allAlbums.appendChild(newAlbum);
			}else{
				newAlbum.appendChild(albumEl);
				allAlbums.appendChild(newAlbum);
			}
			a++;
		}else{
			//is image
			var albumEl = document.createElement("div");
			albumEl.className = "album";
			
			var imageDiv = document.createElement("div");
			imageDiv.className = "images";
			
			var link = document.createElement("a");
			link.setAttribute("href", albums[i]['full_path']+albums[i]['path']);
			link.setAttribute("rel", "lightbox[gallery]");
			
			
			var desc = "";
			if (albums[i]['title']!=""){
				desc += albums[i]['title'];
			}
			if (albums[i]['description']!=""){
				desc += " - "+albums[i]['description'];
			}
			link.setAttribute("title",desc);
			
			
			var image = document.createElement("img");
			image.setAttribute("src",albums[i]['full_path']+"thumb/"+albums[i]['path']);
			/*image.setAttribute("width","200px");
			image.setAttribute("height","200px");*/
			
			
			link.appendChild(image);
			imageDiv.appendChild(link);
			albumEl.appendChild(imageDiv);
			allPictures.appendChild(albumEl);
			
			/*content += "<div class='album'>";
			content += "<div class='images'>";
			content += "<a href='"+album+albums[i]['path']+"' rel='lightbox[gallery]' title='"+albums[i]['title']+"'><img src='"+album+albums[i]['path']+"' height='200px' width='200px'/></a>";
			content += "</div>";
			content += "</div>";
			content += "</div>";*/
		}
	}
	document.getElementById("gallery").appendChild(curAlbum);
	document.getElementById("gallery").appendChild(allAlbums);
	document.getElementById("gallery").appendChild(allPictures);
	Slimbox.scanPage();
}