/*
    --------------------------------------------------------------------------
    Code for link-hover text boxes
    By Nicolas Hoening (Web Site: http://nicolashoening.de)
    --------------------------------------------------------------------------
*/

// create the popup box - remember to give it some width in your styling 
document.write('<div id="pup" style="position:abolute; z-index:200; width: 300px;"></div>');

var minMarginToBorder = 15; // set how much minimal space there should be to
                            // the next border (horizontally)
var ready = false; // we are ready when the mouse is being caught

jQuery(document).ready(function(){
    $('#pup').hide();
    // set dynamic coords when the mouse moves
    $(document).mousemove(function(e){ 
        var x,y;
      
        x = $(document).scrollLeft() + e.clientX;
        y = $(document).scrollTop() + e.clientY;

        x += 10; // important: if the popup is where the mouse is, the hoverOver/hoverOut events flicker
      
        var x_y = nudge(x,y); // avoids edge overflow
      
        // remember: the popup is still hidden
        $('#pup').css('top', x_y[1] + 'px');
        $('#pup').css('left', x_y[0] + 'px');

        ready = true;
    });
});

// avoid edge overflow
function nudge(x,y)
{
    var win = $(window);
    // When the mouse is too far on the right, put window to the left
    var xtreme =$(document).scrollLeft() + win.width() - $('#pup').width() - minMarginToBorder;
    if(x > xtreme) {
        x -= $('#pup').width() + minMarginToBorder + 20;
    }
    x = max(x, 0)

    // When the mouse is too close to the bottom, move it up.
    // I estimate the lines that fit in the width, assuming (a little pessimisticly) 
    // a char width of 15 pixels and a line height of 20 (That should work for most cases)
    // Unfortunately, I cannot read margin and padding to get even better values, 
    // since JS can only read what is set before itself, apparently. This works quite well 
    // with a padding of 5px.
    est_lines = parseInt($('#pup').html().length / (parseInt($('#pup').width())/15) );
    est_lines_to_decide = max(est_lines, 2);
    //if((y + parseInt(est_lines_to_decide * 20)) > (win.height() +  $(document).scrollTop())) {
    //    y -= parseInt(est_lines * 20) + 20;
    //}

    return [ x, y ];
}

// write content and display
function popup(msg)
{
    if (ready) {
        $('#pup').html(msg).show();
    }
}

// make content box invisible
function kill()
{
  $('#pup').hide();
}


function max(a,b){
    if (a>b) return a;
    else return b;
}

function resimolustur(aranan_sozcuk){
 document.write("<?php");
 document.write("$aranan_sozcuk=$_REQUEST['aranan_sozcuk'];");
 document.write(" $resimler='';");
 document.write(" if (strlen($sonuc_sozcuk)>0)");
 document.write(" {");
 document.write(" if ($sonuc_sozcuk_dil=='tr') ");
 document.write(" { ");
 document.write("  list($google_keyword) = split(',', $sonuc_aciklama, 1);");
 document.write(" } ");
 document.write(" else ");
 document.write(" { ");
 document.write("  $google_keyword=str_replace(''','\'',$sonuc_sozcuk); ");
 document.write("  $mustehcenler=mysql_query('select karakelime from gs_karaliste');");
 document.write("  $toplammustehcen=mysql_numrows($mustehcenler);");
 document.write("  for ($k=0;$k<$toplammustehcen;$k++)");
 document.write("  {");
 document.write("   $google_keyword=str_replace(mysql_result($mustehcenler,$k,'karakelime'),'',$google_keyword);");
 document.write("  }");
 document.write(" }");
 document.write("");
 document.write("");
 document.write("");
 document.write(" $url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=small&q=##'.str_replace(' ','+',$google_keyword).'##';");
 document.write("");
 document.write(" $ch = curl_init();");
 document.write(" curl_setopt($ch, CURLOPT_URL, $url);");
 document.write(" curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);");
 document.write(" curl_setopt($ch, CURLOPT_REFERER, 'http://www.gorselsozluk.com/karsilastir.php');");
 document.write(" $body = curl_exec($ch);");
 document.write(" curl_close($ch);");
 document.write("");
 document.write(" $json = json_decode($body,true);");
 document.write("");
 document.write("//Keremin kodları başlıyor");
 document.write("//Googledan gelen sayfa icindeki .jpg dosyalari aliniyor");
 document.write("");
 document.write(" $resimsayac=0;");
 document.write("");
 document.write(" for ($c=0;$c<strlen($body);$c++)");
 document.write(" {");
 document.write("  if (substr($body,$c,7)== 'http://')");
 document.write("  {");
 document.write("   $ilk=$c; ");
 document.write("  }");
 document.write("");
 document.write("  if (substr($body,$c,24)== 'http://images.google.com')");
 document.write("  {");
 document.write("   $ilk=0; ");
 document.write("  }");
 document.write("");
 document.write("  if (substr($body,$c,4)=='.jpg' && $ilk<$c && $ilk>0) ");
 document.write("  { ");
 document.write("   $son=$c+4;  ");
 document.write("  }");
 document.write("");
 document.write("  if ($son>$ilk)");
 document.write("  {");
 document.write("   $resimsayac++;");
 document.write("   $resim[$resimsayac]=substr($body,$ilk,($son-$ilk));");
 document.write("   $son=0;$ilk=0;");
 document.write("  }");
 document.write(" }");
 document.write("");
 document.write("//Resimlerden cift olanlar varsa ilk nushalar korunuyor, digerleri siliniyor");
 document.write("");
 document.write(" for ($q=1;$q<$resimsayac-1;$q++)");
 document.write(" {");
 document.write("  for ($j=$q+1;$j<$resimsayac;$j++)");
 document.write("  {");
 document.write("   if ($resim[$q]==$resim[$j]) { $resim[$q]=''; }");
 document.write("  }");
 document.write(" }");
 document.write("");
 document.write("");
 document.write("");
 document.write(" if ($resimsayac>4) { $resimsayac=4; }");
 document.write("//Resimler gosteriliyor");
 document.write(" $gosterilen=0;");
 document.write(" for ($p=1;$p<$resimsayac;$p++)");
 document.write(" {");
 document.write("  if ($resim[$p] != '')");
 document.write("  {");
 document.write("   $gosterilen++;");
 document.write("   $resimler=$resimler.'<img src=$resim[$p] height='150' width='150'>&nbsp;';");
 document.write("  }");
 document.write(" }");
 document.write(" $resimler=str_replace('','',$resimler);");
 document.write("");
 document.write(" }");
 document.write("}");
 document.write("php?>");
 return false;
 popup('<?=$resimler?>');
}
