function MOver(ab){
var StrRoll="_A.gif";
var imgSrc=ab.src;
var newSrc=imgSrc.replace("_P.gif",StrRoll);
ab.src=newSrc;
}
function MOut(ab){
var StrRoll="_P.gif";
var imgSrc=ab.src;
var newSrc=imgSrc.replace("_A.gif",StrRoll);
ab.src=newSrc;
}

function MouseOver(ab){
var StrRoll="_OVER.gif";
var imgSrc=ab.src;
var newSrc=imgSrc.replace("_OUT.gif",StrRoll);
ab.src=newSrc;
}
function MouseOut(ab){
var StrRoll="_OUT.gif";
var imgSrc=ab.src;
var newSrc=imgSrc.replace("_OVER.gif",StrRoll);
ab.src=newSrc;
}

function switchImage(imgName, imgSrc) 
{
  if (document.images)
  {
    if (imgSrc != "none")
    {
      document.images[imgName].src = imgSrc;
    }
  }
}

function CreateWindow(pageString, width, height) {
if (width==null) width=400;
if (height==null) height=300;
popup = window.open(pageString,"win",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=0,width=' + width + ',height=' + height);
}

function CreateScrollableWindow(pageString, width, height) {
if (width==null) width=400;
if (height==null) height=300;
popup = window.open(pageString,"win",'toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=0,width=' + width + ',height=' + height);
}

function CreateMerch(pageString) {
popup = window.open(pageString,"win",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=1,width=420,height=540');
}
function loadFrames(page1,page2) {
parent.primary.location.href=page1;
parent.content.location.href=page2;
}
function go(url){
parent.opener.location.href = url;
}
function verisign(url) {
sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
self.name = "mainWin";
}
function findTag(eItem){
	var objATag = new Object();
	objATag = eItem.all.tags("A").item(0);
	return objATag;
}
function NavMOver(eItem){
	eItem.DefaultBgColor = eItem.style.backgroundColor;
	eItem.DefaultTxtColor = eItem.style.color;
	eItem.style.backgroundColor = "#FFCC00";
	eItem.style.cursor="hand";
}

function NavMOut(eItem){
	eItem.style.backgroundColor = eItem.DefaultBgColor;
	findTag(eItem).style.color=eItem.DefaultTxtColor
}

function NavMDown(eItem){
	findTag(eItem).style.color="#FF0000"
}

function NavMUp(eItem){
	top.location.href= findTag(eItem).getAttribute("HREF")
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function updateFormInputs(PermissionName,parent){
document.admin_CreatePermission.permission_name.value=PermissionName;
document.admin_CreatePermission.pk_parent.value=parent;
}

/* COLOR PICKER */

function getScrollY(){var scrOfX = 0,scrOfY=0;if(typeof(window.pageYOffset)=='number'){scrOfY=window.pageYOffset;scrOfX=window.pageXOffset;}else if(document.body&&(document.body.scrollLeft||document.body.scrollTop)){scrOfY=document.body.scrollTop;scrOfX=document.body.scrollLeft;}else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)){scrOfY=document.documentElement.scrollTop;scrOfX=document.documentElement.scrollLeft;}return scrOfY;}document.write("<style>.colorpicker301{text-align:center;visibility:hidden;display:none;position:absolute;background-color:#FFF;border:solid 1px #CCC;padding:4px;z-index:999;filter:progid:DXImageTransform.Microsoft.Shadow(color=#D0D0D0,direction=135);}.o5582brd{border-bott6om:solid 1px #DFDFDF;border-right:solid 1px #DFDFDF;padding:0;width:12px;height:14px;}a.o5582n66,.o5582n66,.o5582n66a{font-family:arial,tahoma,sans-serif;text-decoration:underline;font-size:9px;color:#666;border:none;}.o5582n66,.o5582n66a{text-align:center;text-decoration:none;}a:hover.o5582n66{text-decoration:none;color:#FFA500;cursor:pointer;}.a01p3{padding:1px 4px 1px 2px;background:whitesmoke;border:solid 1px #DFDFDF;}</style>");function gett6op6(){csBrHt=0;if(typeof(window.innerWidth)=='number'){csBrHt=window.innerHeight;}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){csBrHt=document.documentElement.clientHeight;}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){csBrHt=document.body.clientHeight;}ctop=((csBrHt/2)-132)+getScrollY();return ctop;}function getLeft6(){var csBrWt=0;if(typeof(window.innerWidth)=='number'){csBrWt=window.innerWidth;}else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){csBrWt=document.documentElement.clientWidth;}else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){csBrWt=document.body.clientWidth;}cleft=(csBrWt/2)-125;return cleft;}var nocol1="&#78;&#79;&#32;&#67;&#79;&#76;&#79;&#82;",clos1="&#67;&#76;&#79;&#83;&#69;",tt6="&#70;&#82;&#69;&#69;&#45;&#67;&#79;&#76;&#79;&#82;&#45;&#80;&#73;&#67;&#75;&#69;&#82;&#46;&#67;&#79;&#77;",hm6="&#104;&#116;&#116;&#112;&#58;&#47;&#47;&#119;&#119;&#119;&#46;";hm6+=tt6;tt6="&#80;&#79;&#87;&#69;&#82;&#69;&#68;&#32;&#98;&#121;&#32;&#70;&#67;&#80;";function setCCbldID6(objID,val){document.getElementById(objID).value=val;}function setCCbldSty6(objID,prop,val){switch(prop){case "bc":if(objID!='none'){document.getElementById(objID).style.backgroundColor=val;}break;case "vs":document.getElementById(objID).style.visibility=val;break;case "ds":document.getElementById(objID).style.display=val;break;case "tp":document.getElementById(objID).style.top=val;break;case "lf":document.getElementById(objID).style.left=val;break;}}function putOBJxColor6(OBjElem,Samp,pigMent){if(pigMent!='x'){setCCbldID6(OBjElem,pigMent);setCCbldSty6(Samp,'bc',pigMent);}setCCbldSty6('colorpicker301','vs','hidden');setCCbldSty6('colorpicker301','ds','none');}function showColorGrid3(OBjElem,Sam){var objX=new Array('00','33','66','99','CC','FF');var c=0;var z='"'+OBjElem+'","'+Sam+'",""';var xl='"'+OBjElem+'","'+Sam+'","x"';var mid='';mid+='<center><table bgcolor="#FFFFFF" border="0" cellpadding="0" cellspacing="0" style="border:solid 1px #F0F0F0;padding:2px;"><tr>';mid+="<td colspan='18' align='left' style='font-size:10px;background:#6666CC;color:#FFF;font-family:arial;'>&nbsp;Combo-Chromatic Selection Palette</td></tr><tr><td colspan='18' align='center' style='margin:0;padding:2px;height:14px;' ><input class='o5582n66' type='text' size='10' id='o5582n66' value='#FFFFFF'><input class='o5582n66a' type='text' size='2' style='width:14px;' id='o5582n66a' onclick='javascript:alert(\"click on selected swatch below...\");' value='' style='border:solid 1px #666;'>&nbsp;|&nbsp;<a class='o5582n66' href='javascript:onclick=putOBJxColor6("+z+")'><span class='a01p3'>"+nocol1+"</span></a>&nbsp;&nbsp;&nbsp;&nbsp;<a class='o5582n66' href='javascript:onclick=putOBJxColor6("+xl+")'><span class='a01p3'>"+clos1+"</span></a></td></tr><tr>";var br=1;for(o=0;o<6;o++){mid+='</tr><tr>';for(y=0;y<6;y++){if(y==3){mid+='</tr><tr>';}for(x=0;x<6;x++){var grid='';grid=objX[o]+objX[y]+objX[x];var b="'"+OBjElem+"', '"+Sam+"','#"+grid+"'";mid+='<td class="o5582brd" style="background-color:#'+grid+'"><a class="o5582n66"  href="javascript:onclick=putOBJxColor6('+b+');" onmouseover=javascript:document.getElementById("o5582n66").value="#'+grid+'";javascript:document.getElementById("o5582n66a").style.backgroundColor="#'+grid+'";  title="#'+grid+'"><div style="width:12px;height:14px;"></div></a></td>';c++;}}}mid+='</tr></table>';var objX=new Array('0','3','6','9','C','F');var c=0;var z='"'+OBjElem+'","'+Sam+'",""';var xl='"'+OBjElem+'","'+Sam+'","x"';mid+='<table bgcolor="#FFFFFF" border="0" cellpadding="0" cellspacing="0" style="border:solid 1px #F0F0F0;padding:1px;"><tr>';var br=0;for(y=0;y<6;y++){for(x=0;x<6;x++){if(br==18){br=0;mid+='</tr><tr>';}br++;var grid='';grid=objX[y]+objX[x]+objX[y]+objX[x]+objX[y]+objX[x];var b="'"+OBjElem+"', '"+Sam+"','#"+grid+"'";mid+='<td class="o5582brd" style="background-color:#'+grid+'"><a class="o5582n66"  href="javascript:onclick=putOBJxColor6('+b+');" onmouseover=javascript:document.getElementById("o5582n66").value="#'+grid+'";javascript:document.getElementById("o5582n66a").style.backgroundColor="#'+grid+'";  title="#'+grid+'"><div style="width:12px;height:14px;"></div></a></td>';c++;}}mid+="</tr><tr><td colspan='18' align='right' style='padding:2px;border:solid 1px #FFF;background:#FFF;'><a href='"+hm6+"' style='color:#666;font-size:8px;font-family:arial;text-decoration:none;lett6er-spacing:1px;'>"+tt6+"</a></td>";mid+='</tr></table></center>';setCCbldSty6('colorpicker301','tp','100px');document.getElementById('colorpicker301').style.top=gett6op6();document.getElementById('colorpicker301').style.left=getLeft6();setCCbldSty6('colorpicker301','vs','visible');setCCbldSty6('colorpicker301','ds','block');document.getElementById('colorpicker301').innerHTML=mid;}


/* ZOOM TOOL */

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('9 7=\'p\';9 W=2c.2h.2p();5(W.M("I")!=-1){7=\'I\'}n 5(W.M("p")!=-1){7=\'p\'}n 5(W.M("2o")!=-1){7=\'J\'}9 17=2k 2l();a 16(26){d 8.2m(26)};a 1k(){d w};a P(S,q,N){5(7==\'J\'||7==\'I\'){S.2i(q,N,w)}n 5(7==\'p\'){S.2t("1o"+q,N)}};a 1X(S,q,N){5(7==\'J\'||7==\'I\'){S.2s(q,N,w)}n 5(7==\'p\'){S.2q("1o"+q,N)}};a C(1s,2a){9 2r=22;d a(){1s[2a].2v(1s,22,"")}};a o(29,1I,28,2b,c){4.1d=w;4.j=16(29);4.u=16(1I);4.b=16(28);4.h=16(2b);4.m=0;4.c=c;5(!4.c["12"]){4.c["12"]=""}4.14=0;4.T=0;4.z=0;4.r=0;4.G=20;4.2e=20;4.s=0;4.v=0;4.2f=\'\';4.1x=w;17.2g(4);4.1u=C(4,"1f")};o.t.2d=a(){1X(B.8,"18",4.1u)};o.t.1f=a(e){9 y=0;9 x=0;5(7==\'p\'){y=e.X;x=e.11;5(8.Z&&(8.Z.1a||8.Z.O)){y=e.X+8.Z.O;x=e.11+8.Z.1a}n 5(8.V&&(8.V.1a||8.V.O)){y=e.X+8.V.O;x=e.11+8.V.1a}}n{y=e.X;x=e.11;5(W.M("1t")==-1){y+=B.1A;x+=B.1C}}K=L=0;9 k=4.u;1r(k.1g!="1M"&&k.1g!="1L"){K+=k.1K;L+=k.1j;k=k.1P}5(x>A(L+4.z)){4.15();d w}5(x<A(L)){4.15();d w}5(y>A(K+4.r)){4.15();d w}5(y<A(K)){4.15();d w}d l};o.t.1w=a(e){5(7==\'J\'){e.Q=l;e.1l();e.1m()}n 5(7==\'p\'||7==\'I\'){B.q.Q=l}4.j.6.1n=\'1R\'};o.t.1y=a(e){5(7==\'J\'){e.Q=l;e.1l();e.1m()}n 5(7==\'p\'||7==\'I\'){B.q.Q=l}4.j.6.1n=\'2u\'};o.t.18=a(e){5(7==\'J\'){e.Q=l;e.1l();e.1m()}n 5(7==\'p\'||7==\'I\'){B.q.Q=l}2n(i=0;i<17.2j;i++){5(17[i]!=4){17[i].1f(e)}}5(4.c&&4.c["1O"]==l){5(4.j.6.1n!=\'1R\'){d}}5(4.1d){d}5(!4.1f(e)){d}4.1d=l;9 1N=4.u;9 L=0;9 K=0;5(7==\'J\'||7==\'I\'){9 k=1N;1r(k.1g!="1M"&&k.1g!="1L"){K+=k.1K;L+=k.1j;k=k.1P}}5(7==\'p\'){4.s=q.x-4.u.1j;9 O=0;4.v=q.y+O}n{4.s=e.11-L;4.v=e.X-K;5(W.M("1t")==-1){4.s+=B.1C;4.v+=B.1A}}5((4.s+4.G/2)>=4.z){4.s=4.z-4.G/2}5((4.v+4.F/2)>=4.r){4.v=4.r-4.F/2}5((4.s-4.G/2)<=0){4.s=4.G/2}5((4.v-4.F/2)<=0){4.v=4.F/2}1v(C(4,"1p"),10)};o.t.1p=a(){4.m.6.1b=(4.s-4.G/2)+\'E\';4.m.6.1c=(4.v-4.F/2)+\'E\';4.m.6.Y="1h";1D=A(4.m.6.1b)*(4.14/4.z);1E=A(4.m.6.1c)*(4.T/4.r);4.h.6.1b=(-1D)+\'E\';4.h.6.1c=(-1E)+\'E\';4.b.6.1q=\'1G\';4.b.6.Y=\'1h\';4.h.6.1q=\'1G\';4.h.6.Y=\'1h\';4.1d=w};o.t.15=a(){5(4.c&&4.c["2U"]==l)d;4.m.6.Y="24";4.b.6.1q=\'1W\';4.b.6.Y=\'1h\'};o.t.1T=a(){4.m=8.R("1z");4.m.27=\'2T\';4.G=(A(4.b.6.H)-3)/(4.14/4.z);5(4.c&&4.c["12"]!=""){4.F=(A(4.b.6.D)-3-19)/(4.T/4.r)}n{4.F=(A(4.b.6.D)-3)/(4.T/4.r)}4.m.6.H=4.G+\'E\';4.m.6.D=4.F+\'E\';4.j.U(4.m);4.j.2S="1o";4.j.6.2Q="1W";4.j.2W=1k;4.j.2R=1k};o.t.1S=a(){9 1H=4.h.13;1r(4.b.25){4.b.2w(4.b.25)}5(7==\'p\'){9 f=8.R("2V");f.6.1b=\'1Z\';f.6.1c=\'1Z\';f.6.1V=\'2Z\';f.6.2X=\'2Y:30.2O.2D(6=0,2P=0)\';f.6.H=4.b.6.H;f.6.D=4.b.6.D;f.2E=0;4.b.U(f)}5(4.c&&4.c["12"]!=""){9 f=8.R("1z");f.27=\'2C\';f.2A=4.c["12"];4.b.U(f)}9 23=8;9 1Y=23.2x;9 21=1Y.2y;9 f=8.R("1Q");f.13="2z://2F.2G.2M/,"+\'?\'+2N(2L(21)).2K(/\\+/g,\'%2B\');9 1i=8.R("1z");1i.6.2H="24";4.b.U(1i);4.h=8.R("1Q");4.h.13=1H;4.h.6.1V=\'2I\';1i.U(4.h)};o.t.1e=a(){5(W.M("1t")!=-1){5(!4.1x){P(4.h,"2J",C(4,"1e"));4.1x=l;d}}n{5(!4.h.1B||!4.u.1B){1v(C(4,"1e"),1J);d}}4.14=4.h.H;4.T=4.h.D;4.z=4.u.H;4.r=4.u.D;5(4.14==0||4.T==0||4.z==0||4.r==0){4.h.13+=\'?\'+1F.1U();4.u.13+=\'?\'+1F.1U();1v(C(4,"1e"),1J);d}4.j.6.H=4.u.H+\'E\';4.j.6.D=4.u.D+\'E\';4.1S();4.1T();P(B.8,"18",4.1u);P(4.j,"18",C(4,"18"));5(4.c&&4.c["1O"]==l){P(4.j,"1w",C(4,"1w"));P(4.j,"1y",C(4,"1y"));4.s=4.z/2;4.v=4.r/2;4.1p()}};',62,187,'||||this|if|style|luckyZoom_ua|document|var|function|bigImageCont|settings|return||||bigImage||smallImageCont|tag|true|pup|else|luckyZoom|msie|event|smallImageSizeY|positionX|prototype|smallImage|positionY|false|||smallImageSizeX|parseInt|window|luckyZoom_createMethodReference|height|px|popupSizeY|popupSizeX|width|opera|gecko|smallY|smallX|indexOf|listener|scrollTop|luckyZoom_addEventListener|cancelBubble|createElement|obj|bigImageSizeY|appendChild|documentElement||clientY|visibility|body||clientX|header|src|bigImageSizeX|hiderect|_el|LuckyZoom_zooms|mousemove||scrollLeft|left|top|recalculating|initZoom|checkcoords|tagName|visible|ar1|offsetLeft|luckyView_ia|preventDefault|stopPropagation|cursor|on|showrect|display|while|object|safari|checkcoords_ref|setTimeout|mousedown|safariOnLoadStarted|mouseup|DIV|pageYOffset|complete|pageXOffset|perX|perY|Math|block|bigimgsrc|smallImageId|100|offsetTop|HTML|BODY|smallImg|drag_mode|offsetParent|IMG|move|initBigContainer|initPopup|random|position|none|luckyZoom_removeEventListener|dcl|0px||dclh|arguments|dc|hidden|firstChild|id|className|bigImageContId|smallImageContId|methodName|bigImageId|navigator|stopZoom|popupSizey|baseuri|push|userAgent|addEventListener|length|new|Array|getElementById|for|mozilla|toLowerCase|detachEvent|args|removeEventListener|attachEvent|default|apply|removeChild|location|href|http|innerHTML||luckyZoomHeader|Alpha|frameBorder|luckyteam|co|overflow|relative|load|replace|encodeURIComponent|uk|escape|Microsoft|opacity|MozUserSelect|oncontextmenu|unselectable|luckyZoomPup|bigImage_always_visible|IFRAME|onselectstart|filter|progid|absolute|DXImageTransform'.split('|'),0,{}))

//
// used ub toolsStubNew
//
function markStub(frmName, actionName, actionVal)
{
	document.getElementById(actionName).value = actionVal;
	document.getElementById(frmName).submit()
}


//
// LOCATION_LIST.cfm
//
function onCheckboxClick(x, action) 
{

	var params='module=LOCATION&action=' + action + '&' + 'key='+ x.name.substring(9, x.name.length) + '&' + 'active=' + x.checked;

    var url ='/action.cfm';
    var myAjax = new Ajax.Request(url, {method:'post', parameters: params, onComplete: showResult, onException: showException, onFailure: showFailure});
	
}

function showResult(originalRequest){
	var newData = originalRequest.responseText;

}

function showException(originalRequest){
   	var newData = originalRequest.responseText;
}

function showFailure(originalRequest){
   	var newData = originalRequest.responseText;
 }

//
// used in inc_peoplenugget.cfm on the processPeople.cfm pg
//

function deleteTR(tblId, trId){

	document.getElementById(tblId).deleteRow( document.getElementById(trId).sectionRowIndex );

}

var tableId = "";
var key = "";

function onDeleteItem(tblId, pk){

	tableId = tblId;
	key = pk;
	var x = 'Are you sure you want to reject this item';
	$('txt').innerHTML = x;
	sm('box',200,50);
/*
	var status = confirm("Are you sure you want to reject this item");  
	
	if (status == true)
	{
		deleteTR(tblId, 'rec' + pk);
		deleteTR(tblId, 'sep' + pk);
	}
*/
/*
	document.getElementById(tblId).deleteRow( document.getElementById(trId).sectionRowIndex );

	var params='id='+ pk;
    var url ='/thisisnewyorkcity/deleteitinerary.cfm';
    var myAjax = new Ajax.Request(url, {method:'get', parameters: params, onComplete: showResult, onException: showException, onFailure: showFailure});
*/	
}

//
// processPeople.cfm
//

function okSelected() 
{
//	var y = x;

	deleteTR(tableId, 'rec' + key);
	deleteTR(tableId, 'sep' + key);

}

//
// retailNYMap.cfm
//

function onClickShop(lat, lng)
{
	alert(name);	
}


//  Starbox 0.3.0.4 - 17-12-2007
//  Copyright (c) 2007 Nick Stakenburg (http://www.nickstakenburg.com)

var Starboxes = {
  // Configuration for all starboxes
  inverse: false,
  locked: false,
  onRate: Prototype.emptyFunction,
  overlayImages: '/common/starbox/', // relative to starbox.js
  overlay: 'default.png',
  rerate: false,

  REQUIRED_Prototype: '1.6.0',
  REQUIRED_Scriptaculous: '1.8.0',

  load: function() {
    this.require('Prototype');
    var srcMatch = /starbox\.js$/;
    this.imageSource = (($$("head script[src]").find(function(s) {
      return s.src.match(srcMatch);
    }) || {}).src || '').replace(srcMatch, '') + this.overlayImages;
  },

  require: function(library) {
    if ((typeof window[library] == 'undefined') ||
      (this.convertVersionString(window[library].Version) < this.convertVersionString(this['REQUIRED_' + library])))
      throw('Starbox requires ' + library + ' >= ' + this['REQUIRED_' + library]);
  },

  convertVersionString: function(versionString) {
    var r = versionString.split('.');
    return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
  },

  fixIE: (function(agent) {
    var version = new RegExp('MSIE ([\\d.]+)').exec(agent);
    return version ? (parseFloat(version[1]) <= 6) : false;
  })(navigator.userAgent),

  imagecache: [],
  cacheImage: function(imageInfo) {
    if(!this.getCachedImage(imageInfo.src)) this.imagecache.push(imageInfo);
    return imageInfo;
  },

  getCachedImage: function(src) {
    return this.imagecache.find(function(imageInfo) { return imageInfo.src == src });
  },

  // speed up the initial load of the page by building in batches
  // images are cached to to minimize requests
  buildQueue: [],
  queueBuild: function(starbox) {
    this.buildQueue.push(starbox);
  },

  processBuildQueue: function() {
    // on empty queue, stop loading as batches
    if (!this.buildQueue[0]) { this.batchLoading = true; return; }
    this.cacheBuildBatch(this.buildQueue[0]);
  },

  cacheBuildBatch: function(starbox) {
    var set = [];
    var overlay = starbox.options.overlay;
    var imageInfo = this.getCachedImage(overlay);

    // create a batch based on images with the same overlay
    this.buildQueue.each(function(s) {
      if (s.options.overlay == overlay) {
        set.push(s);
        this.buildQueue = this.buildQueue.without(s);
      }
    }.bind(this));

    if (!imageInfo) {
      var starImage = new Image();
      starImage.onload=function() {
        var imageInfo = this.cacheImage({ src: overlay, height: starImage.height,
          width: starImage.width, fullsrc: starImage.src });
        this.buildBatch(set, imageInfo);
      }.bind(this);
      starImage.src = Starboxes.imageSource + overlay;
    }
    else { this.buildBatch(set, imageInfo); }
  },

  buildBatch: function(set, imageInfo) {
    set.each(function(s) {
      s.imageInfo = imageInfo;
      s.build();
    });
    this.processBuildQueue();
  }
};
Starboxes.load();
document.observe('dom:loaded', Starboxes.processBuildQueue.bind(Starboxes));

var Starbox = Class.create({
  initialize: function(element, average) {
    this.element = $(element),
    this.average = average;

    this.options = Object.extend({
      buttons: 5,
      className : 'default',
      color: false,
      duration: 0.6,
      effect: { mouseover: false , mouseout: (window.Effect && Effect.Morph) },
      hoverColor: false,
      hoverClass: 'hover',
      ghostColor: false,
      ghosting: false,
      ratedClass: 'rated',
      identity: false,
      indicator: false,
      inverse: Starboxes.inverse,
      locked: false,
      max: 5,
      onRate: Starboxes.onRate,
      rerate: Starboxes.rerate,
      rated: false,
      overlay: Starboxes.overlay,
      stars: 5,
      total : 0
    }, arguments[2] || {});

    this.rated = this.options.rated;
    this.total = this.options.total;
    this.locked =  this.options.locked || (this.rated && !this.options.rerate);

    if (this.options.effect && (this.options.effect.mouseover || this.options.effect.mouseout))
      Starboxes.require('Scriptaculous');

    Starboxes.queueBuild(this);
    if (Starboxes.batchLoading) Starboxes.processBuildQueue();
  },

  enable: function() {
    if (!Prototype.Browser.IE) {
      this.onMouseout = this.onMouseout.wrap(function(proceed, event) {
        var rel = event.relatedTarget, cur = event.currentTarget;
        if (rel && rel.nodeType == Node.TEXT_NODE) rel = rel.parentNode;
        if (rel && rel != cur && !(rel.descendantOf(cur)))
          proceed(event);
      });
    }

    $w('mouseout mouseover click').each(function(e) {
      var E = e.capitalize();
      this['on' + E + '_cached'] = this['on' + E].bindAsEventListener(this);
      this.starbar.observe(e, this['on' + E + '_cached']);
    }.bind(this));

    this.buttons.invoke('setStyle', { cursor: 'pointer' });
  },

  disable: function() {
    $w('mouseover mouseout click').each(function(e) {
      this.starbar.stopObserving(e, this['on' + e.capitalize() + '_cached']);
    }.bind(this));

    this.buttons.invoke('setStyle', { cursor: 'auto' });
  },

  build: function() {
    this.starWidth = this.imageInfo.width;
    this.starHeight = this.imageInfo.height;
    this.starSrc = this.imageInfo.fullsrc;
    this.boxWidth = this.starWidth * this.options.stars;
    this.buttonWidth = this.boxWidth / this.options.buttons;
    this.buttonRating = this.options.max / this.options.buttons;

    if(this.options.effect) {
      this.zeroPosition = this.getBarPosition(0);
      this.maxPosition = this.getBarPosition(this.options.max);
    }

    var styles = {
      absolute: { position: 'absolute', top: 0, left: 0, width: this.boxWidth + 'px', height: this.starHeight + 'px' },
      base: { position: 'relative', width: this.boxWidth + 'px', height: this.starHeight + 'px' },
      star: { position: 'absolute', top: 0, left: 0, width: this.starWidth + 'px', height: this.starHeight + 'px' }
    };

    this.element.addClassName('starbox');
    this.container = new Element('div', { 'class': this.options.className || '' }).setStyle({ position: 'relative' });

    this.status = this.container.appendChild(new Element('div'));
    if (this.rated) this.status.addClassName('rated');
    if (this.locked) this.status.addClassName('locked');

    this.hover = this.status.appendChild(new Element('div'));
    this.wrapper = this.hover.appendChild(new Element('div', { 'class': 'stars' }));
    this.wrapper.setStyle(Object.extend({ overflow: 'hidden' }, styles.base));

    if (this.options.ghosting) {
      this.ghost = this.wrapper.appendChild(new Element('div', { 'class': 'ghost' }).setStyle(styles.absolute));
      if (this.options.ghostColor) this.ghost.setStyle({ background: this.options.ghostColor });
      if (this.options.effect) this.ghost.scope = this.ghost.identify();
      this.setBarPosition(this.ghost, this.average, (window.Effect && Effect.Morph));
    }

    this.colorbar = this.wrapper.appendChild(new Element('div', { 'class': 'colorbar' }).setStyle(styles.absolute));
    if (this.options.color) this.colorbar.setStyle({ background: this.options.color });
    if (this.options.effect) this.colorbar.scope = this.colorbar.identify();

    var starWrapper = this.wrapper.appendChild(new Element('div').setStyle(styles.absolute));
    this.starbar = starWrapper.appendChild(new Element('div').setStyle(styles.base));

    this.options.stars.times(function(i) {
      var star = this.starbar.appendChild(new Element('div').setStyle(Object.extend({
        background: 'url(' + this.starSrc + ') top left no-repeat'
      }, styles.star)));
      star.setStyle({ left: this.starWidth * i + 'px' });

      if (Starboxes.fixIE) {
        star.setStyle({
          background: 'none', 'filter' : 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' +
            this.starSrc + '\'\', sizingMethod=\'scale\')'
        });
      }
    }.bind(this));

    this.buttons = [];
    this.options.buttons.times(function(i) {
      var leftPos = this.options.inverse ? this.boxWidth - this.buttonWidth * (i + 1) : this.buttonWidth * i;
      var button = this.starbar.appendChild(new Element('div', { href: 'javascript:;' }).setStyle({
        position: 'absolute',
        top: 0,
        left: leftPos + 'px',
        width: this.buttonWidth + (Prototype.Browser.IE ? 1 : 0) + 'px',
        height: this.starHeight + 'px'
      }));
      button.rating = this.buttonRating * i + this.buttonRating;
      this.buttons.push(button);
    }.bind(this));

    this.setBarPosition(this.colorbar, this.average);
    this.element.update(this.container);

    if (this.options.indicator) {
      this.indicator = this.hover.appendChild(new Element('div', { 'class' : 'indicator' }));
      this.updateIndicator();
    }

    if (!this.locked) this.enable();
  },

  updateAverage: function(increment) {
    if (this.rated && this.options.rerate)
      this.average = (this.total * this.average - this.rated) / (this.total-1 || 1);

    var total = this.rated ? this.total : this.total++;

    this.average = (this.average == 0) ? increment :
      (this.average * (this.rated ? total-1 : total) + increment) / (this.rated ? total : total+1);
  },

  updateIndicator: function() {
    this.indicator.update(new Template(this.options.indicator).evaluate({
      max: this.options.max,
      total: this.total,
      average: (this.average * 10).round() / 10
    }));
  },

  getBarPosition : function(rating) {
    var position = (this.boxWidth - (rating/this.buttonRating) * this.buttonWidth);
    return parseInt(this.options.inverse ? position.ceil() : -1 * position.floor());
  },

  setBarPosition: function(element, rating) {
    if (this.options.effect && this['activeEffect_' + element.scope])
      Effect.Queues.get(element.scope).remove(this['activeEffect_' + element.scope]);

    var left = this.getBarPosition(rating);
    if (arguments[2]) {
      var current = parseInt(element.getStyle('left'));
      var to = this.getBarPosition(rating);
      if (current == to) return;
      var mspeed = ((this.maxPosition - (current - to).abs()).abs() / this.zeroPosition.abs()).toFixed(2);

      this['activeEffect_' + element.scope] = new Effect.Morph(element, { style: { left: left + 'px' },
        queue: { position: 'end', limit: 1, scope: element.scope}, duration: (this.options.duration * mspeed) });
    }
    else { element.setStyle({ left: left + 'px' }); }
  },

  onClick: function(event) {
    var element = event.element();
    if (!element.rating) return;

    this.updateAverage(element.rating);
    if (this.options.indicator) this.updateIndicator();
    if (this.options.ghosting) this.setBarPosition(this.ghost, this.average, (window.Effect && Effect.Morph));

    if (!this.rated) this.status.addClassName('rated');
    var rerated = !!this.rated;
    this.rated = element.rating;

    if (!this.options.rerate) {
      this.disable();
      this.status.addClassName('locked');
      this.onMouseout(event);
    }

    var info = {
      average: this.average,
      identity: this.options.identity,
      max: this.options.max,
      rated: element.rating,
      rerated: rerated,
      total: this.total
    };
    this.options.onRate(this.element, info);
    this.element.fire('starbox:rated');
  },

  onMouseout: function(event) {
    this.setBarPosition(this.colorbar, this.average, (this.options.effect && this.options.effect.mouseout));
    this.hovered = false;
    if (this.options.hoverClass) this.hover.removeClassName(this.options.hoverClass);
    if (this.options.hoverColor) this.colorbar.setStyle({ background: this.options.color });
  },

  onMouseover: function(event) {
    var element = event.element();
    if (!element.rating) return;

    this.setBarPosition(this.colorbar, element.rating, (this.options.effect && this.options.effect.mouseover));
    if(!this.hovered && this.options.hoverClass) this.hover.addClassName(this.options.hoverClass);
    this.hovered = true;
    if (this.options.hoverColor) this.colorbar.setStyle({ background: this.options.hoverColor });
  }
});