    //<![CDATA[
    
// (c) Mikael Högqvist, ZIB 2006-2007

// SPARQL queries for the map (MapModel)

function Model() {}

// querystr and a callback handling the results
Model.prototype.sites = function(viewcb) {
        var q = 'PREFIX glue:<http://www.gac-grid.org/schema/gridmap#> \
                 PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> \
                 PREFIX geo:<http://www.w3.org/2003/01/geo/wgs84_pos#> \
                 SELECT ?site ?longitude ?latitude \
                 WHERE { ?site rdf:type glue:Site . \
                 OPTIONAL { ?site geo:long ?longitude . \
                            ?site geo:lat ?latitude . }}'

        var queryObj = new Query(q, viewcb);        
        queryObj.query(q);
    }


Model.prototype.telescopes = function(viewcb) {
        var q = 'PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \
PREFIX rtml: <http://www.rtml.org/v3.2> \
PREFIX x2r: <http://www.astrogrid-d.org/2007/08/14-xml2rdf#> \
SELECT DISTINCT ?site ?longitude ?latitude \
WHERE { graph ?g {?tel rdf:type rtml:Telescope . \
        ?tel rtml:name ?site . \
        ?tel rtml:Location ?loc . \
        ?loc rtml:Latitude ?lat . \
        ?lat x2r:value ?latitude . \
        ?loc rtml:EastLongitude ?long . \
        ?long x2r:value ?longitude . \
        ?loc rtml:Latitude ?alt . \
        ?alt x2r:value ?altitude . \
}}'

        var queryObj = new Query(q, viewcb);        
        queryObj.query(q);
    }

Model.prototype.filters = function(telname, viewcb) {
        var q = 'PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \
PREFIX rtml: <http://www.rtml.org/v3.2> \
SELECT DISTINCT ?FilterType \
WHERE { \
graph ?g{?tel rtml:name "TELNAME" . \
         ?tel rtml:Camera ?cam . \
         ?cam rtml:FilterWheel ?fw . \
         ?fw  rtml:Filter ?Filter . \
         ?Filter rtml:type ?FilterType . \
}}'
        //        OPTIONAL { ?cam rtml:name ?Camera . } \
        q = q.replace('TELNAME', telname);
        
        var queryObj = new Query(q, viewcb);
        queryObj.query(q);
    }

Model.prototype.clusters = function(hostname, viewcb) {
        var q = 'PREFIX glue:<http://www.gac-grid.org/schema/gridmap#> \
                 PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> \
                 SELECT ?queue ?freecpus ?totalcpus ?waitingjobs ?totaljobs ?runningjobs \
                 WHERE { graph ?g {<HOSTNAME> glue:CE ?queue . \
                         ?queue glue:FreeCPUs ?freecpus . \
                         ?queue glue:TotalCPUs ?totalcpus . \
                         ?queue glue:WaitingJobs ?waitingjobs . \
                         ?queue glue:TotalJobs ?totaljobs . \
                         ?queue glue:RunningJobs ?runningjobs . }}'

        q = q.replace('HOSTNAME', hostname);
        
        var queryObj = new Query(q, viewcb);        
        queryObj.query(q);
    }

Model.prototype.jobs = function(hostname, viewcb) {
    // #FROM NAMED <http://icecore.gac-grid.org/context/gridmap/jobs#context>
    var q = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \
PREFIX jobs: <http://www.gac-grid.org/schema/jobs#> \
\
SELECT ?name ?state ?jobid \
\
WHERE { graph ?g {?jobid rdf:type jobs:Job . \
?jobid jobs:site <HOSTNAME> . \
?jobid jobs:currentState ?cs . \
?jobid jobs:jobname ?name . \
?cs jobs:state ?state . \
FILTER(?state = 'Active' || ?state = 'ACTIVE') }\
}"                     
     //  && ?state != 'Done'
     //FILTER(?state != 'Failed' && ?state != 'done')

        q = q.replace('HOSTNAME', hostname);
        
        var queryObj = new Query(q, viewcb);
        queryObj.query(q);
}

Model.prototype.detailedJobInfo = function(jobid, viewcb) {
    //              #FROM NAMED <http://icecore.gac-grid.org/context/gridmap/jobs#context>
    var q = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \
             PREFIX jobs: <http://www.gac-grid.org/schema/jobs#> \
             SELECT DISTINCT ?name ?state ?user ?last_change \
\
             WHERE { graph ?g { <JOBID> jobs:jobname ?name . \
                     <JOBID> jobs:user ?user . \
                     <JOBID> jobs:currentState ?cs . \
                     ?cs jobs:state ?state . \
                     ?cs jobs:timeStamp ?last_change . \
                     }"

     //FILTER(?state != 'Failed' && ?state != 'done')

        while(q.indexOf('JOBID') != -1)
            q = q.replace('JOBID', jobid);
        
        var queryObj = new Query(q, viewcb);
        queryObj.query(q);
}


Model.prototype.timeline = function(date, user, host, ressource, limit) {

/*     var month = date.substr(5,2); */
/*     var newmonth = d2((((month-1) % 12)+1) -1); */
/*     var newdate = date.replace(/-..-/,'-'+newmonth+'-'); */
// FILTER (  ?start > '"+newdate+"' && ?start < '"+date+"' ) . \

    var q = "PREFIX ur: <http://www.gridforum.org/2003/ur-wg#> \
SELECT ?jobid ?state ?start ?stop ?name ?user ?host \
WHERE { graph ?g { \
 ?n1 ur:Resource \""+ressource+"\" . \
 ?n1 ur:StartTime ?start . \
  FILTER (  ?start < '"+date+"' ) . \
 ?n1 ur:Host ?host . \
  FILTER (REGEX(?host, '"+host+"')) . \
 ?n1 ur:UserIdentity ?UserIdentity . \
 ?UserIdentity ur:GlobalUserName ?user . \
  FILTER (REGEX(?user, '"+user+"')) . \
 ?n1 ur:JobIdentity ?job_id . \
 ?job_id ur:LocalJobId ?jobid . \
 ?n1 ur:Status ?state . \
  OPTIONAL { ?n1 ur:EndTime ?stop } . \
  OPTIONAL { ?n1 ur:JobName ?name } . \
}} ORDER BY DESC(?start) LIMIT "+limit
    return escape(q);
}

//FILTER (?stop = '') . \
//?n1 ur:EndTime ?ts . \
//FILTER (!REGEX(?start, 'nodeID://')) . \



function setFILTER(variable) {
    var userInput = document.getElementById('userInput').value;
    document.getElementById('userInput').value='';
    document.getElementById(variable).innerHTML = userInput;
    onLoad();
}

function d2(digit) { 
    if(digit<10) { return ('0'+digit); } else { return digit; }
}

function isodate(D) { 
    return D.getFullYear()+"-"+d2(D.getMonth()+1)+"-"+d2(D.getDate())+
        "T"+d2(D.getHours())+":"+d2(D.getMinutes())+":"+d2(D.getSeconds());
}

//Begin: code for ISO 8601 info text.
var tjs_w3c = (document.getElementById)  ? true : false;
var tjs_ie4 = (document.all && !tjs_w3c) ? true : false;
var tjs_ie5 = (document.all &&  tjs_w3c) ? true : false;
var tjs_ns4 = (document.layers) ? true : false;
var mouseX = 0;
var mouseY = 0;

function tjs_getElement( id ) {
  if( tjs_ns4 ) return findlayer( id, document );
  else if( tjs_ie4 ) return document.all[id];
  else return document.getElementById( id );
}
function tjs_show( id ) {
  var elem = tjs_getElement( id );
  if( tjs_ns4 ) elem.visibility       = "show";
          else  elem.style.visibility = "visible";
}
function tjs_hide( id ) {
  var elem = tjs_getElement( id );
  if( tjs_ns4 ) elem.visibility       = "hide";
          else  elem.style.visibility = "hidden";
}
function tjs_move( id, x, y ) {
  var elem = tjs_getElement( id );
  if( tjs_ns4 ) elem.moveTo( x, y );
         else { elem.style.left = x + 'px';
                elem.style.top  = y + 'px'; }
}
function tjs_trackMouseEvent( evt ) {
  if( !tjs_ie4 && !tjs_ie5 ) {
    mouseX = evt.pageX;
    mouseY = evt.pageY;
  } else {
    mouseX = event.clientX;
    mouseY = event.clientY;
    if( document.body.scrollLeft )
      mouseX += document.body.scrollLeft;
    if( document.body.scrollTop  )
      mouseY += document.body.scrollTop;
  }
  return false;
}
//End: code for ISO 8601 info text.
