// --------------------------------------------------------------------------
// LinkWeaver 1.0
// 
// Geoff Davis, http://www.geoffdavis.net/
// Dan Gezelter, http://www.openscience.org/~gezelter
// 
// Copyright (c) 2001, The OpenScience Project
// 
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// --------------------------------------------------------------------------
DynAPI.setLibraryPath(DynAPIdir)

DynAPI.include('dynapi.api.browser.js')
if (getCookie('entryID')) {
   DynAPI.include('dynapi.api.dynlayer.js')
   DynAPI.include('dynapi.api.dyndocument.js')
}

DynAPI.onLoad = function() {
   if (getCookie('clearEntryCookies')) {
      clearEntryCookies()
   } else {
      if (getCookie('entryID')) {
         showRateWindow()
      }
   }
}

myLayer = null

function floatLayer() {
   yOffset = 0
   if (window && window.pageYOffset) yOffset = window.pageYOffset
   else if (document && document.body && document.body.scrollTop) yOffset = document.body.scrollTop
   yDest = yOffset+DynAPI.document.getHeight()/2-myLayer.getHeight()/2
   xOffset = 0
   if (window && window.pageXOffset) xOffset = window.pageXOffset
   else if (document && document.body && document.body.scrollLeft) xOffset = document.body.scrollLeft
   xDest = xOffset+DynAPI.document.getWidth()/2-myLayer.getWidth()/2
   myLayer.moveBy((xDest-myLayer.x)/2,(yDest-myLayer.y)/2)
   setTimeout('floatLayer()',25)
}

function showRateWindow() {
   // Set flag to make sure we don't prompt twice for same entry
   //    if user doesn't rate link or if img fails to load
   setCookie('clearEntryCookies', 1)
   
   width = 200
   height = 250
   
   myLayer = new DynLayer()
   myLayer.setSize(width, height)
   myLayer.setBgColor('#000000')
//   myLayer.moveTo(DynAPI.document.getWidth()/2-width/2, DynAPI.document.getHeight()/4-height/2)

   myLayer.setHTML('<table cellspacing="0" cellpadding="2" width="100%" border="0" class="rateline" align="center">'+
                   '<tr><th class="sectionName" align="center">What did you think of ' + unescape(getCookie('entryName')) + '?</th>'+
                         '<th valign="top" width="10"><div id="rateClose"><a onclick="noRating(); return false;" class="rateClose" href="">X</a></div></th></tr>' +
                   '<tr><td class="raterow1" colspan="2">&nbsp;</td></tr>' +
                   '<tr><td align="center" class="raterow1" colspan="2"><a class="rate" onclick="rate(5); return false;" href="">*****&nbsp;5 stars</a></td></tr>' +
                   '<tr><td align="center" class="raterow1" colspan="2"><a class="rate" onclick="rate(4); return false;" href="">****&nbsp;4 stars</a></td></tr>' +
                   '<tr><td align="center" class="raterow1" colspan="2"><a class="rate" onclick="rate(3); return false;" href="">***&nbsp;3 stars</a></td></tr>' +
                   '<tr><td align="center" class="raterow1" colspan="2"><a class="rate" onclick="rate(2); return false;" href="">**&nbsp;2 stars</a></td></tr>' +
                   '<tr><td align="center" class="raterow1" colspan="2"><a class="rate" onclick="rate(1); return false;" href="">*&nbsp;1 star</a></td></tr>' +
                   '<tr><td class="raterow1" colspan="2">&nbsp;</td></tr>' +
                   '<tr><td align="center" class="raterow1" colspan="2"><a class="rate" onclick="noRating(); return false;" href="">No rating</a></td></tr>' +
                   '<tr><td class="raterow1" colspan="2">&nbsp;</td></tr>' +
                   '</table>'
                   );
   DynAPI.document.addChild(myLayer)
   myLayer.setSize(myLayer.getWidth(), myLayer.getContentHeight())
   floatLayer(myLayer)
}

function clearEntryCookies() {
   deleteCookie('entryID')
   deleteCookie('entryName')
   deleteCookie('clearEntryCookies')
}

function rate(rating) {
   img = new Image(1,1)
   img.onload = clearEntryCookies
   img.src = "/links/rate.php?r=" + rating
   closeRateWindow()
}

function noRating() {
   clearEntryCookies();
   closeRateWindow();
}

function closeRateWindow() {
   index = DynAPI.document.children.length-1
   while (index >= 0) {
      DynAPI.document.children[index].deleteFromParent()
      --index
   }
}
