
function setNewPic( url, target, url_to_save, text_field, title )
{
  var img = document.getElementById(target);  

  img.src = url;
  
  document.getElementById(url_to_save).value = url;
  // document.getElementById(text_field).value = title;
}

function ahah(url, target, silent, callback) {
  if( !silent )
	document.getElementById(target).innerHTML = ' Fetching data...';

  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
	
	req.onreadystatechange = function() {ahahDone(url, target,callback);};   

    req.open("GET", url, true);
    req.send("");
  }
}  

function ahahDone(url, target, callback) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
	  if( callback != null )
		eval( callback );
    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}

function load(name, div, callback) {
  ahah(name,div,false, callback);
	return false;
}

function loadSilent(name, div, callback) {
  ahah(name,div,true, callback);
	return false;
}

function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // %          note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                             
    var hash_map = {}, unicodeStr='', hexEscStr='';
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urldecode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
 
    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
}

function trim (zeichenkette) {
  // Erst führende, dann Abschließende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zurückliefern
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}

function defaultMessage()
{
  return "Wähle ein Bild: " +
	"<a href=\"javaScript:checkPicFilter('movie')\">movie</a> " +
	"<a href=\"javaScript:checkPicFilter('dvd')\">dvd</a> " +
	"<a href=\"javaScript:checkPicFilter('poster')\">poster</a> " +
	"<a href=\"javaScript:checkPicFilter('');\">nix</a> ";
}

function checkPic()
{
  var name = document.getElementById("title").value;

  if( ( name == "" ||
		name.length < 3 ) )
	{
	  alert("Bitte zuerst einen Titel eingeben");
	  //document.getElementById("images").innerHTML = "";
	  //document.getElementById("images_title").innerHTML = "";
	  return;
	}
  
  document.getElementById("images_title").innerHTML = defaultMessage();

  load( "loadgooglepictures.php?filter=" + 
		urlencode(document.getElementById('title').value) + 
		"&kategorie=dvd&picperrow=199" , "images" );
}

function checkPicFilter(filter)
{
  var name = document.getElementById("title").value;

  if( ( name == "" ||
		name.length < 3 ) )
	{
	  document.getElementById("images").innerHTML = "";
	  document.getElementById("images_title").innerHTML = "";
	  return;
	}
  
  document.getElementById("images_title").innerHTML = defaultMessage();

  if( filter.length > 0 )
	{
	  load( "loadgooglepictures.php?filter=" + 
			urlencode(document.getElementById('title').value) + 
			"&kategorie=" + urlencode(filter) + "&picperrow=199" , "images" );
	}
  else
	{
	  load( "loadgooglepictures.php?filter=" + 
			urlencode(document.getElementById('title').value) + 
			"&picperrow=199" , "images" );	  
	}
}

function show_edit()
{
  if( edit_content_save != null )
	{
	  var edit = document.getElementById("edit");
	  edit.innerHTML = edit_content_save;
	}
}

function hide_edit()
{
  var edit = document.getElementById("edit");
  
  if( edit_content_save == null )
	edit_content_save = edit.innerHTML;
  
  edit.innerHTML=
	"<div align=right><br/>" +
	"<a class=\"newmovie\" href=\"javaScript:show_edit();\">Neuen Film hinzufügen</a>\n" +
	"</div>";
}

function updateTop()
{
  loadSilent('index.php?ajax=1&updatelist=top','group_top','updateBottom()');
}

function updateBottom()
{
  loadSilent('index.php?ajax=1&updatelist=bottom','group_bottom','updateStats()');
}

function updateStats()
{
  loadSilent('index.php?ajax=1&updatestats=1','stats','updateUserStats()');
}

function updateUserStats()
{
  loadSilent('index.php?ajax=1&updateuserstats=1','userstats');
}

function updateEntry( idx, start )
{
  var entries = new Array( 'all', 'next', 'top','bottom','new','dist','seen' );

  var started = false;
  var found = false;

  for( var i = 0; i < entries.length; i++ )
	{
	  if( !started )
		{
		  if( entries[i] != start )
			continue;

		  started = true;
		}
	  
	  if( document.getElementById( "entry_" + entries[i] + "_" + idx  ) != null )	
		{		  		
		  var name = "entry_" + entries[i] + "_" + idx;
		  var field = document.getElementById(name).innerHTML;
		  var type = "even";
		  
		  if( field.indexOf( 'even' ) < 0 )
			type = "odd";


		  var next = null;

		  if( i+1 < entries.length )
			{
			  next = "updateEntry( '" + idx + "', \"" + entries[i+1] + "\" );";
			}
		  else
			{
			  next = "updateTop();";
			}
  
		  loadSilent( "index.php?ajax=1&updateidx=" + idx + "&what=" + "entry_" + entries[i] + "_"  + idx 
					  + "&type=" + type,
					  "entry_" + entries[i] + "_"  + idx,					  
					  next );
		  found = true;
		  break;
		}
	}

  if( !found )
	updateTop();

}


function vote( idx, url )
{
  var name = "entry_all_" + idx;
  var field = document.getElementById(name).innerHTML;
  var type = "even";

  if( field.indexOf( 'even' ) < 0 )
	type = "odd";

  loadSilent( url + "&ajax=1&updateidx=" + idx + "&type=" + type, 
			  name, 
			  "updateEntry( '" + idx + "', \"next\" );");
}


function sendComment( idx )
{
  var text = document.getElementById( 'comment_text' );

  if( text == null )
	return;

  text = urlencode(text.value);
  
  loadSilent( "index.php?savecomment=" + idx + "&comment=" + text + "&ajax=1", 'comment_box',  
			  "updateEntry( '" + idx + "', \"all\" );");
}

function hideCommentInput() 
{
  var commentBox = document.getElementById('comment_box');

  if( commentBox == null )
	return;

  commentBox.innerHTML = "";
}

function showCommentBox( idx, category )
{
  hideCommentInput();

  var location = document.getElementById( 'comment_' + idx + '_' + category );

  location.innerHTML = 
	"<div id=\"comment_box\">" +
	"<textarea id=\"comment_text\" rows=5 cols=30 class=\"commentbox\"></textarea><br>\n" +	
	"<input id=\"comment_submit\" type=\"submit\" onClick=\"sendComment('" + idx + "');\">\n"
	"</div>";
}


function setSeen( idx )
{
  var name = "entry_all_" + idx;
  var field = document.getElementById(name).innerHTML;
  var type = "even";

  if( field.indexOf( 'even' ) < 0 )
	type = "odd";

  loadSilent( 'index.php?seen=' + idx + "&updateidx=" + idx + "&ajax=1&type=" + type, name, "updateEntry( '" + idx + "', \"next\" );");
}




