// JavaScript Document
 ( document.getElementById ) ? dom = true : dom = false;

 function installSearchPlugin () {

  if ( window.external && ("AddSearchProvider" in window.external)) { window.external.AddSearchProvider ('http://www.amidalla.com/opensearch.xml'); }
  else { alert ("Sorry, your web browser does not support OpenSearch plugins"); }
 }

 function advertstat_load ( slotvalue , keywordvalue , typevalue ) {

   var bn = navigator.appName;
   var os = navigator.platform;
   var sw = screen.width;
   var sh = screen.height;
   var dr = window.document.referrer;
   var cr = window.location.href;

   var advertstat_url = "http://www.amidalla.de/cgi-bin/advert/advert_stat.pl?";
   advertstat_url = advertstat_url + "slot=" + slotvalue;
   advertstat_url = advertstat_url + "&keyword=" + keywordvalue;
   advertstat_url = advertstat_url + "&type=" + typevalue;

   advertstat_url = advertstat_url + "&bn=" + bn;
   advertstat_url = advertstat_url + "&os=" + os;
   advertstat_url = advertstat_url + "&sh=" + sh;
   advertstat_url = advertstat_url + "&sw=" + sw;

   var dr_ord = new Array();
   for ( i=0; i < dr.length; ++i ) { dr_ord.push(dr.charCodeAt(i)); }
   dr = dr_ord.join("_");

   advertstat_url = advertstat_url + "&dr=" + dr;

   var cr_ord   = new Array();
   for ( i=0; i < cr.length; ++i ) { cr_ord.push(cr.charCodeAt(i)); }
   cr = cr_ord.join("_");

   advertstat_url = advertstat_url + "&cr=" + cr;
   loadXMLDoc( advertstat_url );

   var i = 0;
   while ( i < 500000 ) { i++; }
 }

 function write_advertstat () { document.write('<div style="visibility:visible;"><span id="layer1text"></span></div>'); }

 function vote ( domaintovote ) {

   var cookie_value_string = Get_Cookie( 'amidalla_vote' );
   var domain_is_voted     = false;

   if ( cookie_value_string ) {

    var cookie_values = cookie_value_string.split('|');

    for ( var i=0; i < cookie_values.length; i++ ) {

     if ( cookie_values[i] == domaintovote ) {

      domain_is_voted = true;
      break;
     }
    }
   }

   if ( !domain_is_voted ) {

    var dtv_ord = new Array();
    for ( i=0; i < domaintovote.length; i++ ) { dtv_ord.push(domaintovote.charCodeAt(i)); }
    dtv_ord_string = dtv_ord.join("_");

    var vote_url = "http://www.amidalla.de/cgi-bin/vote/vote.pl?dtv=" + dtv_ord_string;
    loadXMLDoc( vote_url );

    var domain_string = domaintovote;
    if ( cookie_value_string ) { domain_string += "|" + cookie_value_string; }

    Set_Cookie( 'amidalla_vote', domain_string, '5', '/', '', '' );
    document.getElementById(domaintovote).innerHTML = ( document.getElementById(domaintovote).innerHTML * 1 ) + 1;
   }
   else {

    document.getElementById('layer1text').innerHTML = "<font size=1>You have allready voted for this URL</font>";
   }
 }

 function Set_Cookie( name, value, expires, path, domain, secure ) {

  // set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );

  if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }
  var expires_date = new Date( today.getTime() + (expires) );

  document.cookie = name + "=" +escape( value ) +
   ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
   ( ( path ) ? ";path=" + path : "" ) +
   ( ( domain ) ? ";domain=" + domain : "" ) +
   ( ( secure ) ? ";secure" : "" );
 }

 function Get_Cookie( check_name ) {

  var a_all_cookies = document.cookie.split( ';' );
  var a_temp_cookie = '';
  var cookie_name = '';
  var cookie_value = '';
  var b_cookie_found = false;

  for ( i = 0; i < a_all_cookies.length; i++ ) {

   a_temp_cookie = a_all_cookies[i].split( '=' );
   cookie_name   = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

   if ( cookie_name == check_name ) {

    b_cookie_found = true;

    if ( a_temp_cookie.length > 1 ) {

     cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
    }

    return cookie_value;
    break;
   }

   a_temp_cookie = null;
   cookie_name = '';
  }

  if ( !b_cookie_found ) { return null; }
 }

 function loadXMLDoc( url , mode ) {

  xmlhttp = false;
  layerswitch = mode;

  if (window.XMLHttpRequest)     { xmlhttp = new XMLHttpRequest(); } // Mozilla Code, etc.
  else if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } // IE Code.

  if ( xmlhttp ) {

   xmlhttp.onreadystatechange=state_Change;
   xmlhttp.open("GET",url,true);
   xmlhttp.send(null);
  }
  else {

   printXML( "Error processing your Request.<br>Your Browser does not support XMLHTTP." );
  }
 }

 function state_Change() {

  // xmlhttp shows loaded
  if (xmlhttp.readyState==4) {

   if ( xmlhttp.status == 200 ) { printXML( xmlhttp.responseText ); }
   else { printXML( "Problem retrieving Data: " + xmlhttp.statusText ); }
  }
 }

 function printXML ( pXML ) {

  if ( ! layerswitch ) { document.getElementById('layer1text').innerHTML = "<font size=1>" + pXML + "</font>"; }
  if ( layerswitch == "video" ) { document.getElementById('resultslayer').innerHTML = pXML; }
 }

 // video search.
 function setsearchmode ( mode ) {

  if ( mode == "video" ) {

   document.getElementById('searchform').action = "javascript:videosearch()";
   document.getElementById('videot').style.color = "green";
   document.getElementById('webt').style.color = "red";
  }

  if ( mode == "web" ) {

   document.getElementById('searchform').action = "http://www.amidalla.de/cgi-bin/amisearch.cgi";
   document.getElementById('videot').style.color = "red";
   document.getElementById('webt').style.color = "green";
  }
 }

 function videosearch ( searchterm ) {

  var baseurl = "http://www.amidalla.de/cgi-bin/amisearch.cgi?videosearch=";

  if ( ! searchterm ) {

   if ( document.getElementById('qbox').value ) {

    searchterm = document.getElementById('qbox').value + "&formsearch=1";
   }
  }

  if ( searchterm ) {

   baseurl += searchterm;
   loadXMLDoc( baseurl , 'video' );
  }
  else {

   printXML( "Error there is no Searchterm specified." );
  }
 }

 function load_page ( encodedUrl ) { videosearch(encodedUrl); }
 function forw_page ( encodedUrl ) { videosearch(encodedUrl); }

 function getScroll () {

  scrOfX = 0;
  scrOfY = 0;

  if( typeof( window.pageYOffset ) == 'number' ) {
   //Netscape compliant
   scrOfY = window.pageYOffset;
   scrOfX = window.pageXOffset;
  }
  else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
   //DOM compliant
   scrOfY = document.body.scrollTop;
   scrOfX = document.body.scrollLeft;
  }
  else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
   //IE6 standards compliant mode
   scrOfY = document.documentElement.scrollTop;
   scrOfX = document.documentElement.scrollLeft;
  }
 }

 function Mausklick (Ereignis) {

  if (!Ereignis) Ereignis = window.event;
  getScroll();

  if (document.getElementById) {
   document.getElementById("player").style.left  = Ereignis.clientX + scrOfX + "px";
   document.getElementById("player").style.top = Ereignis.clientY + scrOfY + "px";
  } else if (document.all) {
   document.all.player.style.left = Ereignis.clientX + scrOfX + "px";
   document.all.player.style.top = Ereignis.clientY + scrOfY + "px";
  }

  document.onmousedown = "";
 }

 function register () { document.onmousedown = Mausklick; }

 function decode_chr ( encode_string ) {

  var ord   = encode_string.split(".");
  var chars = new Array();

  for ( var i=0; i < ord.length; ++i ) { chars.push( String.fromCharCode(ord[i]) ); }
  var chr_string = chars.join("");

  return chr_string;
 }

 var layer_set = 0;

 function hideplayer() {

  document.getElementById('player').style.visibility = 'hidden';
  document.getElementById('playercode').innerHTML = '&nbsp;';
  layer_set = 0;
 }

 function showplayer() {

  getScroll();

  document.getElementById('player').style.top = scrOfY + 130 + 'px';
  document.getElementById('player').style.left = scrOfX + 30 + 'px';
  document.getElementById('player').style.visibility = 'visible';
  layer_set = 1;
 }

 function writeplayer () {

  document.write('<div id="player" style="position:absolute; left:0px; top:0px; visibility:hidden"><div style="background-color:white; padding:5px; border:1px solid #DAE95A"><div onclick="javascript: register()" style="background-color:#E5E7FC; padding:5px; border:1px solid #DAE95A"><a href="javascript:hideplayer()"><font color="#000000" size=2>close</font></a><font size=2> | to move this window, first click inside this area, then click anywhere in the document to move to the click position.</font></div><p><div id="playercode"></div></div></div>');
 }

 function startPlayer ( titleSP , video_urlSP ) {

  title = decode_chr(titleSP);
  video_url = decode_chr(video_urlSP);

  var player_code = "";
  //player_code += '<font size=4 face=arial>Playing " '+title+' "</font><p>';

  player_code += '<object width="425" height="355"><param name="movie" value="';
  player_code += video_url;
  player_code += '"></param><param name="wmode" value="transparent"></param><embed src="';
  player_code += video_url;
  player_code += '"type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>';

  showplayer();
  document.getElementById('playercode').innerHTML = player_code;
 }
 
 function showbox ( action , id ) {
 
  if ( action == "open" ) { document.getElementById(id).style.visibility = 'visible'; }
  if ( action == "close" ) { document.getElementById(id).style.visibility = 'hidden'; }
 }
 
 function show_result_inline ( action , whichdiv , info ) {
 
  if ( action == "open" ) { 
  
   document.getElementById(whichdiv).style.position = 'static';
   document.getElementById(whichdiv).style.visibility = 'visible'; 
   document.getElementById('id'+whichdiv).href = "javascript:show_result_inline('close' , '"+whichdiv+"');"; 
   document.getElementById('id'+whichdiv).innerHTML = '<font size=2>hide more results from this website</font>';
  }
  
  if ( action == "close" ) { 
  
   document.getElementById(whichdiv).style.position = 'absolute';
   document.getElementById(whichdiv).style.visibility = 'hidden'; 
   document.getElementById('id'+whichdiv).href = "javascript:show_result_inline('open' , '"+whichdiv+"');"; 
   document.getElementById('id'+whichdiv).innerHTML = '<font size=2>show more results from this website</font>';
  }
  
  if ( action == "openadult" ) {
  
   var info_decoded = URLDecode( info );
   var id_array = info_decoded.split( "<jd>" );
  
   document.getElementById('adult'+whichdiv).style.position = 'static';
   document.getElementById('adult'+whichdiv).style.visibility = 'visible'; 
   document.getElementById('wl'+whichdiv).href = id_array[0]; 
   document.getElementById('wl'+whichdiv).innerHTML = id_array[1];
  }
 }
 
 function URLDecode( encoded ) {

 // Replace + with ' '
 // Replace %xx with equivalent character
 // Put [ERROR] in output if %xx is invalid.
 
 var HEXCHARS = "0123456789ABCDEFabcdef"; 
 var plaintext = "";
 var i = 0;
 
 while (i < encoded.length) {
  
  var ch = encoded.charAt(i);
  
	if (ch == "+") {
	 
   plaintext += " ";
	 i++;
	} else if (ch == "%") {
	
	 if (i < (encoded.length-2) 
			 && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
			 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1) {
			
    plaintext += unescape( encoded.substr(i,3) );
		i += 3;
	 } else {
				
    alert( 'Bad escape combination near ...' + encoded.substr(i) );
		plaintext += "%[ERROR]";
		i++;
	 }
	} else {
		  
   plaintext += ch;
	 i++;
	}
 } // while
 
 return plaintext;
 }

 writeplayer();
