var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
   
   var xmlHttp;
   
   if(window.ActiveXObject){
       try{
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch(e) {
          xmlHttp = false;
       }
   } else {
       try {
          xmlHttp = new XMLHttpRequest();
       } catch(e) {
          xmlHttp = false;
       } 
    }
    if (!xmlHttp){
        alert("Ошибка создания объекта XMLHttpRequest.");
    } else {
        return xmlHttp;
    }
}

var currentid = 0;

function replace_string(sourceString,searchString,pasteString){
	var f;
	var resultString;
	resultString = sourceString;
	currentIndex = resultString.indexOf(searchString);
	while (currentIndex >= 0){
		if (currentIndex >= 0){
			resultString = resultString.substring(0,currentIndex) + pasteString + resultString.substring(currentIndex + searchString.length);
		}
		currentIndex = resultString.indexOf(searchString);
	}
	return resultString;
}

function replacetags(input){
	
	var output;
	output = replace_string(input,'/]\n[br/]','##br##');
	output = replace_string(output,']\n[br/]',']');
	output = replace_string(output,'##br##','/]\n[br/]');
	output = replace_string(output,'[','<');
	output = replace_string(output,']','>');
	
	return output;
	
}

function process(id){
	
	currentid = id;
	currdisp = document.getElementById("sci"+currentid).style.display;
	
	if (!(currdisp == "block")){
	
		document.getElementById("sh"+currentid).innerHTML = "";
		document.getElementById("sci"+currentid).style.display = "block";
		document.getElementById("pic"+currentid).style.display = "block";
		document.getElementById("sci"+currentid).innerHTML = "<i>загрузка...</i>";
		document.getElementById("sh"+currentid).innerHTML = "Свернуть подробности";
	
    	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
        	xmlHttp.open("GET", "modules/simplecatalog/gettext.php?id="+id, true);
        	xmlHttp.onreadystatechange = handleServerResponse;
	       	xmlHttp.send(null);
    	} else {
    		setTimeout('process(' + id + ')',10000);
    	}
    
	} else {
		document.getElementById("sh"+currentid).innerHTML = "Показать подробности";
		document.getElementById("sci"+currentid).style.display = "none";	
		document.getElementById("pic"+currentid).style.display = "none";
	}
    
}

function handleServerResponse(){
    if (xmlHttp.readyState == 4){
         if (xmlHttp.status == 200){
             xmlResponse = xmlHttp.responseXML;
             xmlRoot = xmlResponse.documentElement;
             
             filesArray = xmlRoot.getElementsByTagName("file");
             var title;
             var hash;
             var ext;
             var html = "";
             
             fullArray = xmlRoot.getElementsByTagName("full");
             for (var i = 0; i < fullArray.length; i++){
                html += fullArray.item(i).firstChild.data;
             }
             
             html += "<ul>";
             for (var i = 0; i < filesArray.length; i++){
             	
             	fileRoot = filesArray.item(i);
             	
             	title = fileRoot.getElementsByTagName("title").item(0).firstChild.data;
             	hash = fileRoot.getElementsByTagName("hashcode").item(0).firstChild.data;
             	ext = fileRoot.getElementsByTagName("ext").item(0).firstChild.data;
             	
                html += "<li><a href=\"uploads/" + hash + "." + ext + "\" target=\"_blank\"> " + title + "</a></li>";
                
             }
             
             html += "</ul>";
             
             document.getElementById("sci"+currentid).innerHTML = replacetags(html);
             
             imagesArray = xmlRoot.getElementsByTagName("image");
             
             html = "";
             for (var i = 0; i < imagesArray.length; i++){
                html += "<a href=\"uploads/" + imagesArray.item(i).firstChild.data + ".jpg\" target=\"_blank\"><img src=\"uploads/th" + imagesArray.item(i).firstChild.data + ".jpg\" alt=\"@@@\" /></a>";
             }           
             
             document.getElementById("pic"+currentid).innerHTML = html;
             
         } else {
              alert("При обращении к серверу возникли проблемы: " + xmlHttp.statusText);
         }
    }       
}


function processgal(id){
	
	currentid = id;
	currdisp = document.getElementById("gal"+currentid).style.display;
	
	if (!(currdisp == "block")){
	
		document.getElementById("galsp"+currentid).innerHTML = "";
		document.getElementById("gal"+currentid).style.display = "block";
		document.getElementById("galsp"+currentid).innerHTML = "<i>загрузка...</i>";
		document.getElementById("galsp"+currentid).innerHTML = "Свернуть галерею";
	
    	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
        	xmlHttp.open("GET", "modules/simplecatalog/getpics.php?id="+id, true);
        	xmlHttp.onreadystatechange = handleServerResponseGallery;
	       	xmlHttp.send(null);
    	} else {
    	    setTimeout('processgal(' + id + ')',10000);
    	}
    
	} else {
		document.getElementById("galsp"+currentid).innerHTML = "Показать галерею";
		document.getElementById("gal"+currentid).style.display = "none";	
	}
    
}

function handleServerResponseGallery(){
    if (xmlHttp.readyState == 4){
         if (xmlHttp.status == 200){
             xmlResponse = xmlHttp.responseXML;
             xmlRoot = xmlResponse.documentElement;
             
             imagesArray = xmlRoot.getElementsByTagName("image");
             
             var html = "";
             for (var i = 0; i < imagesArray.length; i++){
                 html += "<a href=\"uploads/" + imagesArray.item(i).firstChild.data + ".jpg\" target=\"_blank\"><img src=\"uploads/th" + imagesArray.item(i).firstChild.data + ".jpg\" alt=\"\" /></a>";
             }
             document.getElementById("gal"+currentid).innerHTML = html;
             if (html == ""){
                          document.getElementById("galsp"+currentid).style.display = "none";
             }
             
         } else {
              alert("При обращении к серверу возникли проблемы: " + xmlHttp.statusText);
         }
    }       
}

