var DataGrid=Class.create();DataGrid.ALADIN_MAX_STACK=18;DataGrid.aladinCommandFromUrls=function(a){num_commands=a.length;if(num_commands>DataGrid.ALADIN_MAX_STACK){num_commands=DataGrid.ALADIN_MAX_STACK}commands=[];for(i=0;i<num_commands;i++){url=a[i].replace("$","__");commands.push("get File("+url+");")}return commands.join("\n")};DataGrid.prototype={initialize:function(c,b,a){this.id=c;this.selectedVisibleRows=$H();this.selectedRowsSize=0;this.setOptions(a);this.defaultRules={".data-grid-port table tbody tr":function(d){d.onmouseover=function(e){d.style.backgroundColor=this.options.hoverRowColour}.bind(this),d.onmouseout=function(e){this.colourizeRow(d)}.bind(this)}.bind(this),};this.setRules()},setOptions:function(a){this.options={upperPanelId:"upper",lowerPanelId:"lower",skyMapsId:"skymaps",infoTypeNotice:"info",errorTypeNotice:"error",throbberId:this.id+"_throbber",throbberImageSrc:"/images/general/default/indicator.gif",flashNoticeId:this.id+"_flash_notice",rowColour:{even:"transparent",odd:"#d6e7ff"},hoverRowColour:"#90ee90",selectRowWidgetClass:"row-selector",rowBasePrefixId:"data_grid_row_",pagerCountId:"data_grid_pager_count_id",selectionCounterId:"selection_counter",gridUrl:"/data_grid/data_grid/list_resources",getSelectedRowsSizeUrl:"/data_grid/data_grid/get_selected_rows_size",selectAllRowsUrl:"/data_grid/data_grid/select_all_rows",selectAllRowsLimit:1000,deselectAllRowsUrl:"/data_grid/data_grid/deselect_all_rows",addToCartUrl:"/data_grid/data_grid/add_records_to_cart",findUrlsForUrl:"/data_grid/data_grid/find_urls_for",aladinBaseUrl:"http://vizier.cfa.harvard.edu/viz-bin/nph-aladin.pl",aladinRowsLimit:10,wesixRowsLimit:10,voplotUrl:"/data_grid/data_grid/records_as_votable",findIdsForUrl:"/data_grid/data_grid/find_ids_for",getCartSizeUrl:"/access/cart/get_cart_size",cartRowsLimit:1000};Object.extend(this.options,a||{})},setRules:function(a){this.rules=a||this.defaultRules;this.registerRules();this.applyRules()},registerRules:function(){Behaviour.register(this.rules)},applyRules:function(){Behaviour.applyRulesOn(this.defaultRules)},startThinking:function(b,a){$(this.options.throbberId).style.display="inline";this.showNotice(b,a)},stopThinking:function(){$(this.options.throbberId).style.display="none"},onStartRefreshGrid:function(){this.startThinking()},onStopRefreshGrid:function(){this.stopThinking();this.setRules();this.colourizeRows();this.setSelectedVisibleRows();this.getSelectedRowsSize();this.updateSelectionCounter()},colourizeRow:function(a){if(a.rowIndex%2==0){a.style.backgroundColor=this.options.rowColour.even}else{a.style.backgroundColor=this.options.rowColour.odd}},colourizeRows:function(){$A($(this.id).tBodies[0].rows).each(function(a){this.colourizeRow(a)}.bind(this))},setShowNoticeStyle:function(a){switch(a){case this.options.infoTypeNotice:$(this.options.flashNoticeId).style.color="#ffffff";$(this.options.flashNoticeId).style.backgroundColor="#f90";break;case this.options.errorTypeNotice:$(this.options.flashNoticeId).style.color="#ffffff";$(this.options.flashNoticeId).style.backgroundColor="#ff0000";break}},showNotice:function(b,a){if(!b){b=""}if(!a){a=this.options.errorTypeNotice}this.setShowNoticeStyle(a);$(this.options.flashNoticeId).innerHTML=b},getOptionSelected:function(a){try{return $(a).options[$(a).selectedIndex].value}catch(b){optionElem=$(a);optionSelected=null;for(i=0;i<optionElem.length;i++){if(optionElem[i].selected){optionSelected=optionElem[i].value;break}}return optionSelected}},isSelectedRowsEmpty:function(){if(this.selectedRowsSize>0){return false}else{return true}},getRowFromSelectionWidget:function(a){if(a){return a.parentNode.parentNode}else{return null}},getSelectionWidgetFromRow:function(a){return document.getElementsByClassName(this.options.selectRowWidgetClass,a)[0]},selectCheckbox:function(a){if(a){row=this.getRowFromSelectionWidget(a);if(row&&!this.selectedVisibleRows[row.id]){a.checked=true;this.selectedVisibleRows[row.id]=true;this.selectedRowsSize=this.selectedRowsSize+1}}},deselectCheckbox:function(a){if(a){row=this.getRowFromSelectionWidget(a);if(row&&this.selectedVisibleRows[row.id]){a.checked=false;this.selectedVisibleRows[row.id]=null;this.selectedRowsSize=this.selectedRowsSize>0?this.selectedRowsSize-1:0}}},toggleSelected:function(a){if(!a){return}if(a.checked){this.selectCheckbox(a)}else{this.deselectCheckbox(a)}this.updateSelectionCounter()},getRecordIdFromRowId:function(a){rowBasePrefixRegExpId=new RegExp("^"+this.options.rowBasePrefixId+"(.*)$");matches=a.match(rowBasePrefixRegExpId);return matches[1]},getSelectedVisibleRows:function(){recordIds=[];this.selectedVisibleRows.keys().each(function(a){if(this.selectedVisibleRows[a]){recordIds.push(this.getRecordIdFromRowId(a))}}.bind(this));return recordIds},setSelectedVisibleRows:function(){this.selectedVisibleRows=$H();$A($(this.id).tBodies[0].rows).each(function(b){try{checkbox=this.getSelectionWidgetFromRow(b);if(checkbox&&checkbox.checked){this.selectedVisibleRows[b.id]=true}}catch(a){}}.bind(this))},getVisibleRowsAsString:function(){visibleRowsStr=[];$A($(this.id).tBodies[0].rows).each(function(b){try{checkbox=this.getSelectionWidgetFromRow(b);recordId=this.getRecordIdFromRowId(b.id);visibleRowsStr.push(recordId+":"+checkbox.checked)}catch(a){}}.bind(this));return visibleRowsStr.join(",")},getVisibleRowIds:function(){visibleRowIds=[];$A($(this.id).tBodies[0].rows).each(function(a){visibleRowIds.push(this.getRecordIdFromRowId(a.id))}.bind(this));return visibleRowIds},setVisibleRowsById:function(a){this.selectedVisibleRows=$H();$A($(this.id).tBodies[0].rows).each(function(b){recordId=this.getRecordIdFromRowId(b.id);if(a.indexOf(recordId)!=-1){this.selectCheckbox(this.getSelectionWidgetFromRow(b))}}.bind(this))},getSelectedRowsSize:function(){new Ajax.Request(this.options.getSelectedRowsSizeUrl,{asynchronous:true,onLoading:function(a){this.startThinking()}.bind(this),onFailure:function(a){this.stopThinking();this.showNotice("&nbsp;Error trying to get selected rows size.&nbsp;")}.bind(this),onSuccess:function(a){this.setSelectedRowsSize(a.responseText);this.stopThinking()}.bind(this)})},setSelectedRowsSize:function(a){try{this.selectedRowsSize=parseInt(a)}catch(b){this.showNotice("&nbsp;Error trying to parse selected rows size.&nbsp;");this.selectedRowsSize=0}this.updateSelectionCounter()},updateSelectionCounter:function(){$(this.options.selectionCounterId).innerHTML="("+this.selectedRowsSize+")"},selectAllVisibleRows:function(){this.showNotice("");$A($(this.id).tBodies[0].rows).each(function(a){this.selectCheckbox(this.getSelectionWidgetFromRow(a))}.bind(this));this.updateSelectionCounter()},deselectAllVisibleRows:function(){this.showNotice("");$A($(this.id).tBodies[0].rows).each(function(a){this.deselectCheckbox(this.getSelectionWidgetFromRow(a))}.bind(this));this.updateSelectionCounter()},deselectAllRows:function(){new Ajax.Request(this.options.deselectAllRowsUrl,{asynchronous:true,onLoading:function(a){this.startThinking("&nbsp;Deselecting all rows, please wait...&nbsp;",this.options.infoTypeNotice)}.bind(this),onFailure:function(a){this.stopThinking();this.showNotice("&nbsp;Error trying to deselect all rows.&nbsp;")}.bind(this),onSuccess:function(a){$A($(this.id).tBodies[0].rows).each(function(b){this.deselectCheckbox(this.getSelectionWidgetFromRow(b))}.bind(this));this.selectedVisibleRows=$H();this.selectedRowsSize=0;this.updateSelectionCounter();this.showNotice("");this.stopThinking()}.bind(this)})},_doSelectAllRows:function(c,a,b){new Ajax.Request(this.options.selectAllRowsUrl,{asynchronous:true,parameters:c+"&limit="+a+"&offset="+b+"&visible_row_ids="+this.getVisibleRowIds().join(","),onLoading:function(d){if(a==-1){this.startThinking("&nbsp;Selecting all rows, please wait...&nbsp;",this.options.infoTypeNotice)}else{this.startThinking("&nbsp;Selecting your rows, please wait...&nbsp;",this.options.infoTypeNotice)}}.bind(this),onFailure:function(d){this.stopThinking();this.showNotice("&nbsp;Error trying to select all rows.&nbsp;")}.bind(this),onSuccess:function(d){responseStr=d.responseText.split("||");selectedRowsSize=responseStr[0];visibleRowIdsStr=responseStr[1];$A($(this.id).tBodies[0].rows).each(function(e){this.deselectCheckbox(this.getSelectionWidgetFromRow(e))}.bind(this));this.setVisibleRowsById($A(visibleRowIdsStr.split(",")));this.setSelectedRowsSize(selectedRowsSize);if(selectedRowsSize==0){this.showNotice("&nbsp;Not possible select rows in this range.&nbsp;",this.options.infoTypeNotice)}else{if(a==-1){this.showNotice("&nbsp;All "+this.selectedRowsSize+" rows are selected.&nbsp;",this.options.infoTypeNotice)}else{this.showNotice("&nbsp;Your range are selected.&nbsp;",this.options.infoTypeNotice)}}this.stopThinking()}.bind(this)})},selectAllRows:function(a){pagerCountValue=parseInt($(this.options.pagerCountId).value);paramOffset=0;paramLimit=this.options.selectAllRowsLimit;if((pagerCountValue>paramLimit)&&(paramLimit!=-1)){selectElemId=this.id+"_select_all_rows_range_id";message="<label>Please select a range:</label></br></br><select id='"+selectElemId+"'>";count=0;inf=0;sup=0;offset=0;do{inf=(count*paramLimit)+1;sup=(count+1)*paramLimit;tsup=sup;if(sup>=pagerCountValue){tsup=pagerCountValue}offset=count*paramLimit;message+="  <option value='"+offset+"'>["+inf+" - "+tsup+"]</option>";count+=1}while(sup<pagerCountValue);message+="</select>";Dialog.alert(message,{windowParameters:{width:300,height:200},okLabel:"Okay",ok:function(b){paramOffset=this.getOptionSelected(selectElemId);this._doSelectAllRows(a,paramLimit,paramOffset)}.bind(this)})}else{if(pagerCountValue>0){this._doSelectAllRows(a,pagerCountValue,paramOffset)}}},reloadGrid:function(a){params=$H(a).merge($H({selected_visible_rows:this.getVisibleRowsAsString()}));new Ajax.Updater(this.options.lowerPanelId,this.options.gridUrl,{method:"post",evalScripts:true,parameters:params.toQueryString(),onLoading:function(b){this.startThinking()}.bind(this),onFailure:function(b){this.stopThinking()}.bind(this),onSuccess:function(b){this.onStopRefreshGrid()}.bind(this)})},switchCoordinatesFormatTo:function(a,b){stateHash=b.parseQuery();stateHash.coords_format=a;this.reloadGrid(stateHash)},addSelectedRowsToCart:function(){if(!this.isSelectedRowsEmpty()){new Ajax.Request(this.options.getCartSizeUrl,{method:"get",onLoading:function(a){this.startThinking("&nbsp;Getting Cart rows size, please wait...&nbsp;",this.options.infoTypeNotice)}.bind(this),onFailure:function(a){this.stopThinking();this.showNotice("&nbsp;Error trying to get Cart rows size.&nbsp;")}.bind(this),onSuccess:function(a){this.showNotice("");this.stopThinking();try{cartSize=parseInt(a.responseText);if((cartSize+this.selectedRowsSize)<=this.options.cartRowsLimit){new Ajax.Request(this.options.addToCartUrl,{method:"post",parameters:$H({selected_visible_rows:this.getVisibleRowsAsString()}).toQueryString(),onLoading:function(c){this.startThinking("&nbsp;Adding selected rows to Cart, please wait...&nbsp;",this.options.infoTypeNotice)}.bind(this),onFailure:function(c){this.stopThinking();this.showNotice("&nbsp;Error trying to add selected rows to Cart.&nbsp;")}.bind(this),onSuccess:function(c){if($(this.options.skyMapsId)){$(this.options.skyMapsId).dwfs.updateDWFs()}this.showNotice("&nbsp;Selected rows added to Cart.&nbsp;",this.options.infoTypeNotice);this.stopThinking()}.bind(this)})}else{alert("You are trying to add to Cart "+this.selectedRowsSize+" item(s) and your Cart have "+cartSize+" items and the maximum is "+this.options.cartRowsLimit+", please remove some items from your Cart and try again.")}}catch(b){this.showNotice("&nbsp;Some error happened trying to add to Cart.&nbsp;")}}.bind(this)})}else{alert("Please select some item to add to Cart")}},viewSelectedRowsInAladin:function(a){if(!this.isSelectedRowsEmpty()){if(a){$(this.options.lowerPanelId).innerHTML=""}if(this.selectedRowsSize>this.options.aladinRowsLimit){alert("Only the first "+this.options.aladinRowsLimit+" selected rows will be sent to Aladin.")}new Ajax.Request(this.options.findUrlsForUrl,{method:"post",parameters:$H({selected_visible_rows:this.getVisibleRowsAsString(),limit:this.options.aladinRowsLimit}).toQueryString(),onLoading:function(b){this.startThinking("&nbsp;Sending selected rows to Aladin, please wait...&nbsp;",this.options.infoTypeNotice)}.bind(this),onFailure:function(b){this.stopThinking();this.showNotice("&nbsp;Error sending selected rows to Aladin.&nbsp;")}.bind(this),onSuccess:function(b){this.showNotice("");this._launchAladin(b);this.stopThinking()}.bind(this)})}else{alert("Please select some item to send to Aladin")}},_launchAladin:function(a){urls=a.responseText.split(/\s+/);aladinCmd=DataGrid.aladinCommandFromUrls(urls);aladinQueryUrl=this.options.aladinBaseUrl+"?script="+escape(aladinCmd);$(this.options.lowerPanelId).innerHTML='<iframe id="aladin_frame" width="100%" height="100%" frameborder="0" src="'+aladinQueryUrl+'"></iframe>'},viewSelectedRowsInVOPlot:function(a,b){if(!this.isSelectedRowsEmpty()){if(b){$(this.options.lowerPanelId).innerHTML=""}new Ajax.Request(this.options.voplotUrl,{method:"post",parameters:$H({selected_visible_rows:this.getVisibleRowsAsString()}).toQueryString(),onLoading:function(c){this.startThinking("&nbsp;Sending selected rows to VOPlot, please wait...&nbsp;",this.options.infoTypeNotice)}.bind(this),onFailure:function(c){this.stopThinking();this.showNotice("&nbsp;Error sending selected rows to VOPlot.&nbsp;")}.bind(this),onSuccess:function(c){this.showNotice("");this._launchVOPlot(c,a);this.stopThinking()}.bind(this)})}else{alert("Please select some item to open in VOPlot")}},_launchVOPlot:function(a,b){viewUrl=a.responseText;if(b){$(this.options.lowerPanelId).innerHTML='<iframe id="voplot_frame" width="100%" height="100%" frameborder="0" src="'+viewUrl+'"></iframe>'}else{window.open(viewUrl,"VOPlot","resizable=1, status=1")}},submitSelectedRowsToWesix:function(a){if(!this.isSelectedRowsEmpty()){if(a){$(this.options.lowerPanelId).innerHTML=""}if(this.selectedRowsSize>this.options.wesixRowsLimit){alert("Only the first "+this.options.wesixRowsLimit+" selected rows will be sent to WESIX.")}new Ajax.Request(this.options.findIdsForUrl,{method:"post",parameters:$H({selected_visible_rows:this.getVisibleRowsAsString(),limit:this.options.wesixRowsLimit}).toQueryString(),onLoading:function(b){this.startThinking("&nbsp;Sending selected rows to WESIX, please wait...&nbsp;",this.options.infoTypeNotice)}.bind(this),onFailure:function(b){this.stopThinking();this.showNotice("&nbsp;Error sending selected rows to WESIX.&nbsp;")}.bind(this),onSuccess:function(b){recordIds=b.responseText.split(",");this.showNotice("");this.stopThinking();wesixQuery=new Wesix({records:recordIds,throbberImageSrc:this.options.throbberImageSrc,throbberImageId:this.options.throbberId,flashNoticeId:this.options.flashNoticeId});wesixQuery.showInputForm()}.bind(this)})}else{alert("Please select some item to send to WESIX")}},querySiapPopup:function(b,a){if(!a){a="default"}new Ajax.Request(b,{asynchronous:true,evalScripts:true,onLoading:function(c){this.startThinking("&nbsp;Making SIAP query, please wait...&nbsp;",this.options.infoTypeNotice)}.bind(this),onComplete:function(d){this.showNotice("");this.stopThinking();nwindow=window.open("","","width=800,height=600,toolbar=0,menubar=1,scrollbars=yes,resizable=yes");var c="<html><head><title>NOAO VO Portal: Live Query</title><link href=/stylesheets/themes/"+a+"/votable/cvotable.css rel=Stylesheet type=text/css /><script src=/javascripts/cprototype.js type=text/javascript><\/script></head><body>"+d.responseText+"</body></html>";nwindow.document.write(c);nwindow.document.close()}.bind(this)})}};