/*
    (C) MAGYNHARD DESIGN
    Matthäus Beyrle
    www.magynhard.de
*/

function showText(pText)
{
  content_div = document.getElementById("titel_text");
  content_div.innerHTML = '' + pText + '';
}

function noText()
{
  content_div = document.getElementById("titel_text");
  if(content_div.innerHTML != '')
  {
    content_div.innerHTML = '';
  }
}

function showId(pId,pType)
{
  elem = document.getElementById(pId);
  if(pType == "" || pType == 0) pType = "block";
  elem.style.display = pType;
}

function hideId(pId)
{
  elem = document.getElementById(pId);
  elem.style.display = "none";
}

function getContent(file_request) // filerequest z.B.: content=neuigkeiten
{	
  //ladegrafik:
  content_div = document.getElementById("content");
  content_div.innerHTML = '<span style="text-decoration:blink;">...</span>';
  
  var myAjax = new Ajax.Request(file_request, { method: 'get', onComplete: setContent }	);
}

function setContent( originalRequest ) 
{
  content_div = document.getElementById("content");
  content_div.innerHTML = originalRequest.responseText;
}

function showImage(img_path)
{
  var divid = "content_img";
  
  var bgb = document.getElementById("bgb");
  bgb.style.display = "block";
  bgb.setAttribute("title","Zum Schließen auf das Bild klicken!"); 
  
  var root = document.getElementById(divid);
  root.style.display = "block";
  
    var top = 50;
    if(!isBrowser("MSIE")) top = (window.pageYOffset + 50);
    else top = (document.documentElement.scrollTop + 50); 
  
  root.style.top = top + "px";
  root.innerHTML = "";  
  
    img = document.createElement("img");
    img.setAttribute("src",img_path);
    img.style.border = "0px";
    img.style.padding = "0px";
    img.setAttribute("title","Zum Schließen auf das Bild klicken!");
    
    
    a = document.createElement("a");
    a.href = "javascript: closeImage()";
    a.appendChild(img);
  
  root.appendChild(a);
  
}




function closeImage()
{
  var bgb = document.getElementById("bgb");
  bgb.style.display = "none"; 
  
  var divid = "content_img";
  root = document.getElementById(divid);
  root.style.display = "none";
}


function isBrowser(name)
{
  //"Opera" "Gecko" "MSIE"
  if(navigator.appName.indexOf(name)) return true;
  return false;
}




function replaceEmoticons(text)
{
	path = "bilder/seite/";
	text = text.replace(/:-\)/g,"<img src='" + path + "emoticon_smile.png'>");
	text = text.replace(/:\)/g,"<img src='" + path + "emoticon_smile.png'>");
	text = text.replace(/;-\)/g,"<img src='" + path + "emoticon_wink.png'>");
	text = text.replace(/;\)/g,"<img src='" + path + "emoticon_wink.png'>");
	text = text.replace(/xD/g,"<img src='" + path + "emoticon_evilgrin.png'>");
	text = text.replace(/:D/g,"<img src='" + path + "emoticon_grin.png'>");
	text = text.replace(/:-D/g,"<img src='" + path + "emoticon_grin.png'>");
	text = text.replace(/\^\^/g,"<img src='" + path + "emoticon_happy.png'>");
	text = text.replace(/:-o/g,"<img src='" + path + "emoticon_surprised.png'>");
	text = text.replace(/:o/g,"<img src='" + path + "emoticon_surprised.png'>");        
	text = text.replace(/:P/g,"<img src='" + path + "emoticon_tongue.png'>");
	text = text.replace(/:-P/g,"<img src='" + path + "emoticon_tongue.png'>");                
	text = text.replace(/:-\(/g,"<img src='" + path + "emoticon_unhappy.png'>");
	text = text.replace(/:\(/g,"<img src='" + path + "emoticon_unhappy.png'>");  
	return text;
}

function tagsToText(text)
{
    text = text.replace(/</g,"&lt;");
    text = text.replace(/>/g,"&gt;");
    return text;
}

function getIsoDate()
{
    var jetzt = new Date();
        var year = jetzt.getFullYear();
        var month = (jetzt.getMonth()+1);
        if(month < 10) month = "0" + month;
        var day = jetzt.getDate();
        if(day < 10) day = "0" + day;
        
        var hours = jetzt.getHours();
        if(hours < 10) hours = "0" + hours;
        var minutes = jetzt.getMinutes();
        if(minutes < 10) minutes = "0" + minutes;
        var seconds = jetzt.getSeconds();
        if(seconds < 10) seconds = "0" + seconds;
         
    var date_now = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
    return date_now;
}

function getGbDate()
{
    var jetzt = new Date();
        var year = jetzt.getFullYear();
        var month = (jetzt.getMonth()+1);
        if(month < 10) month = "0" + month;
        var day = jetzt.getDate();
        if(day < 10) day = "0" + day;
        
        var hours = jetzt.getHours();
        if(hours < 10) hours = "0" + hours;
        var minutes = jetzt.getMinutes();
        if(minutes < 10) minutes = "0" + minutes;
        var seconds = jetzt.getSeconds();
        if(seconds < 10) seconds = "0" + seconds;
         
    var date_now = day + "." + month + "." + year + " " + hours + ":" + minutes;
    return date_now;
}

// Vorschaufunktion beim Gästebucheintragen
function gb_preview_refresh()
{
    var name    = tagsToText(document.getElementById("gb_name").value); 
    var epost   = tagsToText(document.getElementById("gb_epost").value);
    var www     = tagsToText(document.getElementById("gb_www").value);
    var text    = tagsToText(document.getElementById("gb_text").value);
    
    text = text.replace(/\n/g, "<br/>");
    
    var root = document.getElementById("gb_vorschau");
    root.innerHTML = "";
    
    var h4_t = document.createElement("h4");

        var content = "#00 | " + getGbDate() + " | " + name + "";
        if(epost)   content += '&nbsp;<a href="mailto:' + epost + '" title="' + name + ' kontaktieren"><img src="bilder/seite/symbol_kontakt.png" border="0"></a>';
        if(www)     
        {
            //url ggf. http:// voranstellen
            if(www.indexOf("://") < 1) www = "http://" + www;
            content += '&nbsp;<a href="' + www + '" target="_blank" title="Netzpr&auml;senz von ' + name + ' besuchen"><img src="bilder/seite/symbol_startseite.png" border="0"></a>';
        }
    h4_t.innerHTML = content;
    text = replaceEmoticons(text) + "<br/>";
    
    var text_t = document.createElement("span");
    text_t.innerHTML = text;
    
    root.appendChild(h4_t);
    root.appendChild(text_t);  
}

// aequivalent zu php [ohne 2. parameter]
// entfernt leerzeichen (siehe liste unten) am anfang und ende eines strings
// bis erstes anderes zeichen auftritt
function trim(string)
{
/* // leerzeichen, die entfernt werden
" " (ASCII 32 (0x20)), an ordinary space. 
"\t" (ASCII 9 (0x09)), a tab. 
"\n" (ASCII 10 (0x0A)), a new line (line feed). 
"\r" (ASCII 13 (0x0D)), a carriage return. 
"\0" (ASCII 0 (0x00)), the NUL-byte. 
"\x0B" (ASCII 11 (0x0B)), a vertical tab.
*/
    
    zeichen = new Array(" ","\t","\n","\r","\0","\x0B");    
    
    for(iz = 0; iz < zeichen.length; iz++)
    {
	    if(string.length > 2)
	    {
		    while(string.indexOf(zeichen[iz]) == 0)
		    {	        
		        //vorn eins abzwacken
		        string = string.substring(1,string.length);
		    }
		    while(string.lastIndexOf(zeichen[iz]) == string.length-1)
		    {
		        //hinten eins abzwacken
		        string = string.substring(0,string.length-1);      
		    }
	    }
	}
	return string;
}

// ermittelt, welche textart gewaehlt und zeigt entspr. optionen an
function check_input_text()
{
    normaltext = document.getElementById("typ_text").checked;
    verweis    = document.getElementById("typ_verweis").checked;
    
    if(normaltext)
    {
        hideId("td_verweis");
        showId("td_texttyp","table-row");
    }
    else if(verweis)
    {
        hideId("td_texttyp");
        showId("td_verweis","table-row");    
    }
    else
    {
        hideId("td_texttyp");
        hideId("td_verweis");    
    }
}


function klapp_display(id)
{
  id_div = "div_klapp_" + id;
  id_img = "img_klapp_" + id;

  var block = document.getElementById(id_div);
  var img = document.getElementById(id_img);
  if(block.style.display == "block")
  {
    block.style.display = "none";
    img.setAttribute("src","bilder/seite/klapp_plus.png");
  } 
  else
  {
    block.style.display = "block";
    img.setAttribute("src","bilder/seite/klapp_minus.png");
  } 
}

// fotoalbum

function album_add_file_dlg()
{
  id_table = document.getElementById("list_img");
  
  count = id_table.getElementsByTagName("tr").length;
    
  tr = document.createElement("tr");
  td = document.createElement("td");
  
  input = document.createElement("input");
  input.setAttribute("style","color: brown;");
  input.setAttribute("type","file");
  input.setAttribute("name","file_img_" + (count+1));
  input.setAttribute("size","64");
  
  td.appendChild(input);
  tr.appendChild(td);  
  
  id_table.appendChild(tr);  
}


function get_album(p_id_album, p_page, p_abt_id, p_u_prev)
{  
  $('div#album_' + p_id_album).html('<img src="bilder/seite/ajax-loader.gif"/>');
  	
  $.post('ajax.php',
      {
        abt_id: 	'' + p_abt_id + '',
		u_prev: 	'' + p_u_prev + '',
		
		command: 		'get_album',
        seite:      '' + p_page + '',
        id_album:   '' + p_id_album + ''
      },
      function (data)
      {
            $('div#album_' + p_id_album).html(data);
      },
  "html");   
}


function album_move_left(p_pos_img, p_id_album)
{
  if(p_pos_img > 0)
  {  
	  alt_1 = $('div#id_img_cont_' + p_id_album + '_' + p_pos_img).html();
	  alt_2 = $('div#id_img_cont_' + p_id_album + '_' + (p_pos_img-1)).html();	  
	  
	  $('div#id_img_cont_' + p_id_album + '_' + p_pos_img).html('<img src="bilder/seite/ajax-loader.gif"/>');
	  $('div#id_img_cont_' + p_id_album + '_' + (p_pos_img-1)).html('<img src="bilder/seite/ajax-loader.gif"/>');
		
	  $.post('ajax.php',
		  {		
			command: 		'album_move',
			direction:	'left',
			pos_img:      '' + (p_pos_img+1) + '',
			id_album:   '' + p_id_album + ''
		  },
		  function (data)
		  {
			if(data.return_code == 1)
			{
				$('div#id_img_cont_' + p_id_album + '_' + p_pos_img).html(alt_2);
				$('div#id_img_cont_' + p_id_album + '_' + (p_pos_img-1)).html(alt_1);
			}
			else
			{
				//alert("Beim Verschieben trat ein Fehler auf! Die Reparaturfunktion wurde ausgeführt. Versuchen Sie es nun erneut.");
				$('div#id_img_cont_' + p_id_album + '_' + p_pos_img).html(alt_1);
				$('div#id_img_cont_' + p_id_album + '_' + (p_pos_img-1)).html(alt_2);				
			}
		  },
	  "json");  	
  }
}


function album_move_right(p_pos_img, p_id_album)
{
  {  
	  alt_1 = $('div#id_img_cont_' + p_id_album + '_' + p_pos_img).html();
	  alt_2 = $('div#id_img_cont_' + p_id_album + '_' + (p_pos_img+1)).html();	  

	  $('div#id_img_cont_' + p_id_album + '_' + p_pos_img).html('<img src="bilder/seite/ajax-loader.gif"/>');
	  $('div#id_img_cont_' + p_id_album + '_' + (p_pos_img+1)).html('<img src="bilder/seite/ajax-loader.gif"/>');

	  $.post('ajax.php',
		  {		
			command: 		'album_move',
			direction:	'right',
			pos_img:      '' + (p_pos_img+1) + '',
			id_album:   '' + p_id_album + ''
		  },
		  function (data)
		  {
			if(data.return_code == 1)
			{
				$('div#id_img_cont_' + p_id_album + '_' + p_pos_img).html(alt_2);
				$('div#id_img_cont_' + p_id_album + '_' + (p_pos_img+1)).html(alt_1);
			}
			else
			{
				//alert("Beim Verschieben trat ein Fehler auf! Die Reparaturfunktion wurde ausgeführt. Versuchen Sie es nun erneut.");
				$('div#id_img_cont_' + p_id_album + '_' + p_pos_img).html(alt_1);
				$('div#id_img_cont_' + p_id_album + '_' + (p_pos_img+1)).html(alt_2);	
			}
		  },
	  "json");  	
  }
}


function album_delete(p_pos_img, p_id_album)
{		  
  $('div#id_img_cont_' + p_id_album + '_' + p_pos_img).html('<img src="bilder/seite/ajax-loader.gif"/>');
	
  $.post('ajax.php',
	  {		
		command: 		'album_delete',
		pos_img:      '' + (p_pos_img+1) + '',
		id_album:   '' + p_id_album + ''
	  },
	  function (data)
	  {
		if(data.return_code == 1)
		{
			pos = p_pos_img;
			anzahl = data.img_count;
					
			while(pos < anzahl)
			{
				nachfolger = $('div#id_img_cont_' + p_id_album + '_' + (pos+1)).html();
				$('div#id_img_cont_' + p_id_album + '_' + pos).html(nachfolger);
				++pos;
			}
			//pfeil nach rechts bei vorletzten entf.
			$('a#album_rechts_' + p_id_album + '_' + (pos-1)).hide();
			// wenn anzahl == 1, auch pfeil links
			if(anzahl == 1) $('a#album_rechts_' + p_id_album + '_0').hide();
			//letztes element leeren
			$('td#album_' + p_id_album + '_foto_' + anzahl).html("<br />");
		}
		else
		{
			alert("Beim Entfernen trat ein Fehler auf! [" + data.return_code + "][" + data.error + "]");
		}
	  },
  "json");  	
}
