/* ************************************ */
/* Copyright 2007 Rosgovas

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* ******* This is the Ouzi API ******* 
  Terms of use 
      There are only 2 rules : 
          1/ You are free to use/modify it the way you want 
          2/ Improper/illegal use of that API may cause access restrictions for your 
          site 
  Instructions 
    The API main function is "ouzi.gimMe" : 
          ouzi.gimMe(this[,"flag","source element id","target element id"]); 
    What is between brackets can be user defined. 
    You can define a flag, a source element id, and a target element id 
      FLAG :    this is what you want from the API 
                "tags" : provides tags 
                "pages": provides pages 
                "feeds": provides feeds 
                "By default, tags are provided" 
      SOURCE :  this is the DOM/HTML element's id whose content will be used to 
                provide tags, pages, or feeds 
                By default, this is the element from which the event (onclick,...) 
                is fired 
                "all" stands for the entire document
                If this is not a valid id, the source may be the entire document 
      TARGET :  this is the DOM/HTML element's id where the tags, pages, or feeds will 
                be displayed 
                By default, this is a new div element created immediately after the 
                one from which the event (onclick,...) is fired 
                If this is not a valid id, the target may be located before the </body>
                closing tag 
    What the "ouzi.gimMe" function does :
        1/  It creates a JS array called "ouzi.content" that you can use to display 
            results like you want.
            The "ouzi.content" array contains either the requested tags or the requested
            Web pages or feeds ordered by relevance (from most to least)
            The "ouzi.content" array has the following structure :
                a)  First level : Numeric keys from 0 to the total number of results 
                    (ouzi.content[0], ouzi.content[1] ...)
                b)  Second level : Alphanumeric keys from the list below :
                      "page_title" & "page_uri" : if you are requesting Web pages 
                      or feeds
                      Second level is only for RSS feeds and Web pages
            Examples : 
                ouzi.content[3] : the fourth tag in the list
                ouzi.content[2]["page_title"] : the title of the third Web page or RSS 
                                                feed found
        2/ It creates a formatted html string called "ouzi.contentString" with the results  
            Examples : 
                <p onclick="return ouzi.gimMe(this)">My text... I'd like to tag it</p> 
                <p onclick="return ouzi.gimMe(this,'feeds')">My text... I'd like to get 
                RSS/Atom feeds from it</p> 
                <p onclick="return ouzi.gimMe(this,'pages','source_id','target_id')">
                I'd like Web pages related with my 'source_id' element to be displayed 
                in my 'target_id' element</p> 
    What else can you personnalize ? 
      1/  You can add an event handler that will display tags for every individual 
          element of your page with the "ouzi.populate" function
          Just script "ouzi.populate()" somewhere and you're done 
      2/  You can add CSS style to div named "ouzi_div" where results will be
          displayed 
          Example : 
              <style> 
                .ouzi_div { 
                    border:1px solid black; 
                    display:inline; 
                } 
              </style>
      3/  You can re-define the "Please wait ..." html string (ouzi.waitMsg) :  
            <script type="text/javascript">
                ouzi.waitMsg="My own <b>stand by</b> ...";
            </script>
      4/  You can use the "ouzi.user_action" function to fully manage the results of 
          your query (displaying, storing in a data base, etc...) :
          By default, the "ouzi.user_action" function may be something like this :
                ouzi.user_action = function (what, where) { 
                    where.innerHTML=ouzi.contentString;
                }
          You can redefine it by inserting your own function somewhere on your page
          Example :
                <script type="text/javascript">
                    ouzi.user_action = function (what, where) {
                        if (what == 'tags') { where.innerHTML = ouzi.content; }
                        else { where.innerHTML = ouzi.contentString; }
                    }
                </script>
      5/  How to use those tags/feeds/pages in your own server-side environment ?
            You might want to use this for example to tag your posts on your blog, and for          
            this you may need to send them to some script on your server.
            There is a function for this : "ouzi.sendToScript".
            This will send the "ouzi.Content" array to a user-defined script.
            If you do so, just use the user-defined "ouzi.user_action" function to perform    
            some AJAX interactions.
            Define the ouzi.serverScriptUri to the url of your script (must be on your 
            domain)
            Example : 
              <script type="text/javascript">
                ouzi.user_action = function (what, where) {
                  ouzi.serverScriptUri = "YOUR SCRIPT URL";
                  where.innerHTML = "<a onclick=\"ouzi.closeTab(this)\">Close</a><br />";
                  if (what == 'tags') { where.innerHTML += ouzi.content; }
                  else { where.innerHTML += ouzi.contentString; }
                  where.innerHTML += "<br /><a onclick=\"ouzi.sendToScript(this)\">Send to Server</a>";
                  return false;
                }
              </script>
            This will just add a "Send to script" link. "ouzi.content" will be sent to 
            your script as an associative array similar to the JS "ouzi.content" one.
   
   Have fun ! */
/* *************************** */     

/*** Setup ***/

if (!ouzi) {var ouzi=new Object();}
ouzi.waitMsg="Please wait ...";

/*** Functions Users can define themselves ***/

ouzi.user_display=function(what,where){ // You may override this function with yours (see doc)

  // Server script URI (for server-side calls)
    ouzi.serverScriptUri="";

  // Adding a close tab option
    where.innerHTML="<a onclick=\"ouzi.closeTab(this)\">Close</a><br />";
  
  // Displaying the tags/pages/feeds
    if(what=='tags'){where.innerHTML+=ouzi.content;}
    else{where.innerHTML+=ouzi.contentString;}
  
  // Adding a send to script option
    if(ouzi.serverScriptUri!="" && ouzi.numResults>0){where.innerHTML+="<br /><a onclick=\"ouzi.sendToScript(this)\">Send to Server</a>";}
  
    return false;
  
};

/*** Other functions ***/
           
ouzi.buildQuery=function(what,source){if(what=="tags"){ouzi.temp="?action=tags";}else if(what=="tags-site"){ouzi.temp="?action=tags-site";}else if(what=="pages"){ouzi.temp="?action=pages";}else if(what=="pages-site"){ouzi.temp="?action=pages-site";}else if(what=="feeds"){ouzi.temp="?action=feeds";}else{ouzi.temp="?action=tags";}ouzi.temp+="&tags-type=";ouzi.temp+="&s="+escape(source.innerHTML).substr(0,8140);return ouzi.temp;}
ouzi.gimMe=function(obj,what,source,target){if(ouzi.hangOn!=1){ouzi.hangOn=1;if(!what){ouzi.what="tags";}else{ouzi.what=what;}if(!source || source=="all"){ouzi.source=obj;}else if(document.getElementById(source)){ouzi.source=document.getElementById(source)}else{ouzi.document=obj;do{ouzi.document=ouzi.document.parentNode;}while(ouzi.document.parentNode.innerHTML);ouzi.source=ouzi.document;}ouzi.target=document.createElement("div");ouzi.target.setAttribute("name","ouzi_div");ouzi.target.setAttribute("className","ouzi_div");ouzi.target.setAttribute("class","ouzi_div");if(ouzi.js){ouzi.js.parentNode.removeChild(ouzi.js);}ouzi.js=document.createElement("script");ouzi.js.type="text/javascript";ouzi.js.src="http://www.ouzi.org/ouziAPI.js"+ouzi.buildQuery(ouzi.what,ouzi.source);if(!target){ouzi.container=obj;if(ouzi.divs=document.getElementsByTagName("*")){for(i=0;i<ouzi.divs.length;i++){if(ouzi.divs[i].getAttribute("name")=="ouzi_div"){if(ouzi.divs[i]==ouzi.container.nextSibling){ouzi.container.parentNode.removeChild(ouzi.divs[i]);}}}}ouzi.container.parentNode.insertBefore(ouzi.target,ouzi.container.nextSibling);}else if(ouzi.container=(document.getElementById(target))?document.getElementById(target):document.body){if(ouzi.divs=document.getElementsByTagName("*")){for(i=0;i<ouzi.divs.length;i++){if(ouzi.divs[i].getAttribute("name")=="ouzi_div"){if(ouzi.divs[i]==ouzi.container.lastChild){ouzi.container.removeChild(ouzi.divs[i]);}}}}ouzi.container.appendChild(ouzi.target);}obj.parentNode.insertBefore(ouzi.js,obj);if(ouzi.target){ouzi.target.innerHTML=ouzi.waitMsg;}}else{return true;}return false;};
ouzi.ajaxConnect=function(){var xmlhttp=false;if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();if(xmlhttp.overrideMimeType){xmlhttp.overrideMimeType('text/xml');}}else if(window.ActiveXObject){try{xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}}}if(!xmlhttp){alert('Error');return false;}else {return (xmlhttp);}};
ouzi.closeTab=function(obj){var where0=obj;var where1=0;do{where0=where0.parentNode;where1++;}while(!ouzi.ajaxQueries[where0.getAttribute("id")] && where1<20);where0.parentNode.removeChild(where0);}
ouzi.sendToScript=function(where,script_uri){var where0=where;var where1=0;do{where0=where0.parentNode;where1++;}while(ouzi.ajaxRealString=!ouzi.ajaxQueries[where0.getAttribute("id")] && where1<20);if(ouzi.ajaxRealString=ouzi.ajaxQueries[where0.getAttribute("id")]){if(!script_uri && ouzi.serverScriptUri){var script_uri=ouzi.serverScriptUri;}if(!script_uri){return false;}if(!ouzi.ajaxString){return false;}xmlhttpOuzi=ouzi.ajaxConnect();var xcall=script_uri;xmlhttpOuzi.open("POST",xcall,true);xmlhttpOuzi.onreadystatechange=function(){if(xmlhttpOuzi.readyState==4){if(xmlhttpOuzi.status==200){var bits=xmlhttpOuzi.responseText;if(bits){where.innerHTML+=" [Results sent to server]";ouzi.serverResponse=bits;return bits;}else{return false;}}else{alert("Error: Your request did not succeed.");}}};xmlhttpOuzi.setRequestHeader('Content-Type','application/x-www-form-urlencoded');xmlhttpOuzi.send(unescape(ouzi.ajaxRealString));}};
ouzi.dataReception=function(what,where){ouzi.currentID=Math.round(Math.random(1,9999)*1000);where.setAttribute("id","ouzi_div"+ouzi.currentID);if(!ouzi.ajaxQueries){ouzi.ajaxQueries=new Array();}ouzi.ajaxQueries["ouzi_div"+ouzi.currentID]=ouzi.ajaxString;if(ouzi.user_display){ouzi.user_display(ouzi.what,ouzi.target);}ouzi.hangOn=0;}
ouzi.populate=function(){ouzi.evth=document.getElementsByTagName("*");for(i=0;i<ouzi.evth.length;i++){if(ouzi.evth[i].getAttribute("name")!="html" && ouzi.evth[i].getAttribute("name")!="body"){ouzi.evth[i].onclick=function(e){var targ;if(!e){var e=window.event;}if(e.target){targ=e.target;}else if(e.srcElement){targ=e.srcElement;}if(targ.nodeType==3){targ=targ.parentNode;}return ouzi.gimMe(targ,'tags');};}}return false;}
ouzi.display=function(str){if(str){if(str!=""){document.write(str);}}};

