<?php 
//check to see if an artist has been specified
if ($_REQUEST["artist"]){
    
$artist $_REQUEST["artist"];
    
    
//define the url
    
$src_url "http://whitney.org/www/2008biennial/www/?section=artists&page=artist_$artist"
    
    
///get the full contents of the page
    
$whitney_string file_get_contents$src_url );
    
    
//fix the relative links so they work from a remote site
    
$whitney_string str_replace("href =","href=",$whitney_string);
    
$whitney_string str_replace("src =","href=",$whitney_string);
    
$whitney_string =preg_replace('#(href|src)="([^:"]*)(?:")#','$1="http://whitney.org/www/2008biennial/www/$2"',$whitney_string);
    
//then make sure the navigation links point to THIS SITE, NOT THE WHITNEY
    
$whitney_string str_replace("http://whitney.org/www/2008biennial/www/?section=artists&amp;page=artist_"," http://daveomeara.com/kunstmundodiscours/?artist=",$whitney_string);
    
    
    
//THIS PAGE USES SIMPLE STRING FUNCTIONS RATHER THAN REGEX TO REPLACE CERTAIN DIVS
    //fix the head tag, we show our own title and use a local style sheet
    /*<script src="http://whitney.org/www/2008biennial/www/javascripts/prototype.js" type="text/javascript"></script>
    <script src="http://whitney.org/www/2008biennial/www/javascripts/application.js" type="text/javascript"></script>*/
    
$pos =  strpos$whitney_string  '<head>');
    
$head_tag substr$whitney_string  $pos );
    
$pos strpos ($head_tag'<body');
    
$head_tag substr($head_tag  0$pos);
    
$head_tag_new '<head>
        <title>Kunst Mundo Discours 2008 - '
.strtoupper($artist).'</title>
        <link rel="stylesheet" href="styles.css" type="text/css" media="screen" charset="utf-8" />
        
    </head>'
;
    
$whitney_string str_replace($head_tag,$head_tag_new,$whitney_string);
    
    
//now replace the page header, so that we show our branding, not the Whitney's
    //also insert a statement of just what we're doing, and link it to the corresponding page on the Whitney Site.
    
$pos =  strpos$whitney_string  '<div id="logo">');
    
$header_text substr$whitney_string  $pos );
    
$pos strpos ($header_text'<div id="main">');
    
$header_text substr($header_text  0$pos);
    
$header_text_new '<div id="logo">
    <a href=" http://daveomeara.com/kunstmundodiscours/"><img src="header2.gif" width="720" height="54" alt="Kunst Mundo Discours 2008" /></a>
    <h2><a href="'
.$src_url.'"><span id="whitney_link">ALL CONTENT SCRAPED FROM THE WHITNEY BIENNIAL 2008 WEB SITE</span></a></h2>
    </div>'
;
    
$whitney_string str_replace($header_text,$header_text_new,$whitney_string);
    
    
//now replace the copyright notice
    //keep the orignal notice and add credits for this project
    //also add the google analytics tracker at this point
    
$pos =  strpos$whitney_string  '<div id="copyright">');
    
$copyright substr$whitney_string  $pos );
    
$pos strpos ($copyright'</div>');
    
$copyright substr($copyright  0$pos+6);
    
$copyright_new '<div id="copyright">
            All content &copy; 2008 Whitney Museum of American Art<br />
            Additional programming by <a href="/">OT!OM! Labs</a><br />
            This open source project uses PHP for content scraping and the Google Language API for language detection and translation: <a href="show_code.php">Show Code</a>.
        </div>
        <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>

<script type="text/javascript">
_uacct = "UA-400288-3";
urchinTracker();
</script>
'
;

    
$whitney_string str_replace($copyright,$copyright_new,$whitney_string);
    
    
//THE HOUSEKEEPING IS DONE. NOW WE WANT TO REBUILD THE MAIN ARTICLE
    //SO THAT IT CAN BE MANIPULATED BY THE GOOGLE LANGUAGE API
    //get the original main text
    
$pos =  strpos$whitney_string  '<div class="artist_text">');
    
//echo "<!--line 63 $pos -->";
    
$main_text substr$whitney_string  $pos );
    
$pos strpos$main_text  '</div>');
    
//echo "<!--line 66 $pos -->";
    
$main_text substr$main_text  0$pos );
    
//some pages are missing the closing paragraph tag for the "Born..." para...
    //$main_text = preg_replace('(</strong>)(.)(<p>)','</strong></p><p>',$main_text);
    
$main_text str_replace('</strong>\s<p>','</strong></p><p>',$main_text);
    
//$pos = strpos( $main_text  , '</strong></p>');
    
$pos strpos$main_text  '</p>');
    
//echo "<!--line 66 $pos \n $main_text-->";
    
$main_text substr$main_text  $pos+);
    
    
//now,  create the new main text
    //start by stripping html tags (paragraphs, mostly)
    
$main_text_new strip_tags($main_text);
    
$main_text_new trim($main_text_new);
    
//replace html special characters--these cause problems for the translation
    //$search = array("&rsquo;","&mdash;","&ldquo;","&rdquo;");
    
$search = array("&rsquo;","&mdash;","&ldquo;","&rdquo;","&asymp;","&Omega;",);
    
$replace = array ("'"" - ","\"","\"","ff","ff");
    
    
//the cleaned up string
    
$main_text_new =str_replace($search,$replace,$main_text_new);
    
    
//now build new sentences
    
$sentencespreg_split('/\.\s/'$main_text_new, -PREG_SPLIT_NO_EMPTY);
    
$num_sentences count ($sentences);
    
$main_text_new_sentences "\n<p>" ;
    for (
$i=0;$i<$num_sentences;$i++){
    
$sentences[$i] = trim($sentences[$i]);
    
$main_text_new_sentences .= "\n<span id=\"text$i\">".$sentences[$i].". </span>";
    if (
$i%3==2){$main_text_new_sentences .= "</p>\n<p>";}
    
    }
    
$main_text_new_sentences .= "</p>\n";  
    
$whitney_string str_replace($main_text,$main_text_new_sentences,$whitney_string);
    
    
//trim the string from the </body> tag on, because we're going to add our javascript at the bottom of the page
    
$pos strpos$whitney_string  '</body>');
    
$whitney_string substr$whitney_string  0$pos );
    
    echo 
$whitney_string;
    
//now the javascript
    
?>
    <!--<script type="text/javascript" src="http://translate.googleapis.com"></script>-->
    
    <script>
    
    
    function translate(src_elm,dest_elm,dest_lang) {
    //alert(src_elm + dest_elm + dest_lang);
    var source = document.getElementById(src_elm);
    var sourceText = document.getElementById(src_elm).innerHTML;
    var destination = document.getElementById(dest_elm);
    
    
    var transUrl = "https://translation.googleapis.com/language/translate/v2?key=AIzaSyDY_-FtyTogUqPUw2mJ4kgjIF4ekMKhq_Y&target="
    + dest_lang +"&q="
    + encodeURI(sourceText);
    
    var ajax = new XMLHttpRequest();
    ajax.open("GET",transUrl, true);
    ajax.send(null);
    
    console.log(ajax);
    ajax.onreadystatechange = function () {
    
        if (ajax.readyState == 4 && (ajax.status == 200)) {
            console.log("ready")            
            var Data = JSON.parse(ajax.responseText);
            console.log(Data);
        } else {
            console.log("not ready yet")            
        }
        var myTranslation = Data.data.translations["0"].translatedText; 
        var mySource = Data.data.translations["0"].detectedSourceLanguage;
        if(myTranslation !== 'undefined'){
            //alert(mySource+ " to " + myTranslation);
            console.log(mySource);
            console.log(myTranslation);
            
            destination.innerHTML = myTranslation;
        }
   
    }
}
    
    <?php 
    $lang_array 
= array("fr","it","es","de","nl","ja","ru","ar","zh");
    
$euro_lang = array("fr","it","es","de","nl");
    
//$lang_array = array("fr","it","es","de","nl");

    
for ($i $i $num_sentences-$i++ ){
            if (
$tmp_array){unset($tmp_array);}
            
$tmp_array=array();
            for (
$a $a $a++ ){
                 
$rand1 rand(1count($lang_array));
                 
$rand1--;
                 
                 if (!
in_array($rand1,$tmp_array)){
                     
$tmp_array[]=$rand1;
                     
//echo '/* ';
                     //var_dump($tmp_array);
                     //echo ' */'."\n";
                     
if (in_array($lang_array[$rand1],$euro_lang)){
                     
$rand2 rand (12005000);
                     }else{
                     
$rand2 rand (40008000);
                     }
                     
$rand3 rand (28000,29000);
                     
/* $rand2 = rand (5000, 60000);
                     $rand3 = rand (120000,130000);*/
                     
$rand4 30000+($i*1000)+($a*100);
                     
$lang $lang_array[$rand1];
                     echo 
"var textInterval_".$i."_$lang = setInterval('translate(\"text$i\",\"text$i\",\"$lang\")',$rand2);\n";
                     echo 
"setTimeout('clearInterval(textInterval_".$i."_$lang)',$rand3);\n";
                     echo 
"setTimeout('translate(\"text$i\",\"text$i\",\"en\")',$rand4);\n";
                    
//echo "setInterval('translate(\"text$i\",\"text$i\",\"$lang\")',$rand2);\n";
                    //echo "setTimeout('clearInterval(textInterval_".$i."_$lang)',$rand3);\n";
                    //echo "setInterval('translate(\"text$i\",\"text$i\",\"en\")',$rand2);\n";
                 
}else{
                    
$a--;
                 }
            }
            
$rand5 rand (300030000);
            
$rand6 rand (31000,33000);
            echo 
"var textInterval_".$i."_en = setInterval('translate(\"text$i\",\"text$i\",\"en\")',$rand5);\n";
            echo 
"setTimeout('clearInterval(textInterval_".$i."_en)',$rand6);\n";
    }
    
    echo 
"\n";    
    for (
$i $i 10 $i++ ){
         
$rand1 rand(1,count($lang_array));
         
$tmp_array=array();
         
$rand1--;
         if (!
in_array($rand1,$tmp_array)){
             
$tmp_array[]=$rand1;
             
$rand2 rand (2000 $i*100010000$i*1000);
             
$rand3 30000;
             
$rand4 32000;
             echo 
"var whitneyLinkInterval_$i = setInterval('translate(\"whitney_link\",\"whitney_link\",\"".$lang_array[$rand1]."\")',$rand2);\n";
             echo 
"setTimeout('clearInterval(whitneyLinkInterval_$i)',$rand3);\n";
             echo 
"setTimeout('translate(\"whitney_link\",\"whitney_link\",\"en\")',$rand4);\n";
         }else{
            
$i--;
         }
    }
    
?>
    
    <?php 
    
//debugging uncomment to debug
    //echo "<!-- $main_text -->\n";
    //echo "<!-- $main_text_new -->\n";
    //echo "<!-- $main_text_new_sentences -->\n";
    //echo "<!-- $header_text -->\n";
    //echo "<!-- $header_text_new -->\n";
    //echo "<!-- $head_tag -->\n";
    //echo "<!-- $head_tag_new -->\n";
    //echo "<!-- $copyright -->";
    //echo "<!-- $copyright_new -->";
    
?>
    
<?php 
} else { //NOT if ($_REQUEST["artist"])

    //IN THIS CASE, NO ARTIST HAS BEEN SPECIFIED.  GET THE WHITNEY ARTIST'S INDEX PAGE
    //define the url
    
$src_url "http://whitney.org/www/2008biennial/www/?section=artists"
    
    
///get the full contents of the page
    
$whitney_string file_get_contents$src_url );
    
    
//FIX THE LINKS
    
$whitney_string str_replace("href =","href=",$whitney_string);
    
$whitney_string str_replace("src =","href=",$whitney_string);
    
    
$whitney_string =preg_replace('#(href|src)="([^:"]*)(?:")#','$1="http://whitney.org/www/2008biennial/www/$2"',$whitney_string);
    
    
$whitney_string str_replace("http://whitney.org/www/2008biennial/www/?section=artists&amp;page=artist_"," http://daveomeara.com/kunstmundodiscours/?artist=",$whitney_string);
    
    
//fix the head tag
    
$pos =  strpos$whitney_string  '<head>');
    
//echo "<!-- $pos -->";
    
$head_tag substr$whitney_string  $pos );
    
    
$pos strpos ($head_tag'<body');
    
//echo "<!-- $pos -->";
    
$head_tag substr($head_tag  0$pos);
    
$head_tag_new '<head>
        <title>Kunst Mundo Discours 2008</title>
        <link rel="stylesheet" href="styles.css" type="text/css" media="screen" charset="utf-8" />
        <script src="http://whitney.org/www/2008biennial/www/javascripts/prototype.js" type="text/javascript"></script>
        <script src="http://whitney.org/www/2008biennial/www/javascripts/application.js" type="text/javascript"></script>
    </head>'
;
    
$whitney_string str_replace($head_tag,$head_tag_new,$whitney_string);
    
    
//now replace the page header
    
$pos =  strpos$whitney_string  '<div id="logo">');
    
$header_text substr$whitney_string  $pos );
    
$pos strpos ($header_text'<div id="main">');
    
$header_text substr($header_text  0$pos);
    
$header_text_new '<div id="logo">
    <a href=" http://daveomeara.com/kunstmundodiscours/"><img src="header2.gif" width="720" height="54" alt="Kunst Mundo Discours 2008" /></a>
    <h2><a href="'
.$src_url.'"><span id="whitney_link">ALL CONTENT SCRAPED FROM THE WHITNEY BIENNIAL 2008 WEB SITE</a></span></h2>
    <h2><span id="how_to">Select an artist and (try to) read the essay</span></h2>
    </div>'
;
    
$whitney_string str_replace($header_text,$header_text_new,$whitney_string);
    
    
//now replace the copyright notice
    
$pos =  strpos$whitney_string  '<div id="copyright">');
    
    
$copyright substr$whitney_string  $pos );
    
$pos strpos ($copyright'</div>');
    
//echo "line: ".__LINE__."pos: .$pos\n";
    
$copyright substr($copyright  0$pos);
    
$copyright_new '<div id="copyright">
            All content &copy; 2008 Whitney Museum of American Art<br />
            Additional programming by <a href="/">OT!OM! Labs</a><br />
            This open source project uses PHP for content scraping and the Google Language API for language detection and translation: <a href="show_code.php">Show Code</a>.
        </div>
        <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-400288-1";
urchinTracker();
</script>'
;
    
$whitney_string str_replace($copyright,$copyright_new,$whitney_string);
    
    
//trim the string from the </body> tag on, because we're going to add our javascript at the bottom of the page
    
$pos strpos$whitney_string  '</body>');
    
$whitney_string substr$whitney_string  0$pos );
    
    echo 
$whitney_string;
    
//now the javascript
    
?>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
        google.load("language", "1");
        </script>
    <script>
    
    
    function translate(src_elm,dest_elm,dest_lang) {
          var text = document.getElementById(src_elm).innerHTML;
          google.language.detect(text, function(result) {
          //alert (result.language);
            if (!result.error && result.language) {
                if (result.language != "en"){
                    dest_lang = "en";
                }
              google.language.translate(text, result.language, dest_lang,
                                        function(result) {
                var translated = document.getElementById(dest_elm);
                if (result.translation) {
                    translated.innerHTML = result.translation;
                }
              } );
            }
          });
        }
    
    
    <?php 
    
    $lang_array 
= array("fr","it","es","de","nl","ja","ru","ar","zh");
    
     
    echo 
"\n";
    for (
$i $i $i++ ){
         
$rand1 rand(1,count($lang_array));
         
$tmp_array=array();
         
$rand1--;
         if (!
in_array($rand1,$tmp_array)){
             
$tmp_array[]=$rand1;
             
$rand2 rand (300012000);
             
$rand3 24000;
             
$rand4 32000;
             echo 
"var howToInterval_$i = setInterval('translate(\"how_to\",\"how_to\",\"".$lang_array[$rand1]."\")',$rand2);\n";
             echo 
"setTimeout('clearInterval(howToInterval_$i)',$rand3);\n";
             echo 
"setTimeout('translate(\"how_to\",\"how_to\",\"en\")',$rand4);\n";
         }else{
            
$i--;
         }
    }
    
        
        
    echo 
"\n";    
    for (
$i $i $i++ ){
         
$rand1 rand(1,count($lang_array));
         
$tmp_array=array();
         
$rand1--;
         if (!
in_array($rand1,$tmp_array)){
             
$tmp_array[]=$rand1;
             
$rand2 rand (300012000);
             
$rand3 24000;
             
$rand4 32000;
             echo 
"var whitneyLinkInterval_$i = setInterval('translate(\"whitney_link\",\"whitney_link\",\"".$lang_array[$rand1]."\")',$rand2);\n";
             echo 
"setTimeout('clearInterval(whitneyLinkInterval_$i)',$rand3);\n";
             echo 
"setTimeout('translate(\"whitney_link\",\"whitney_link\",\"en\")',$rand4);\n";
         }else{
            
$i--;
         }
    }
         
}
//end else
    
?>
    
    </script></body></html>