/* JSINQ, Copyright (c) 2009 Kai Jäger, governed by an MIT-style license. */ 
if(typeof jsinq=="undefined"){jsinq={}}(function(){function g(u,s,v,t){this.message=u;this.line=s;this.offset=v;this.context=t}g.prototype=new Error();g.prototype.name="QueryTranslationException";var f=["break","else","new","var","case","finally","return","void","catch","for","switch","while","continue","function","this","with","default","if","throw","delete","in","try","do","instanceof","typeof","abstract","enum","int","short","boolean","export","interface","static","byte","extends","long","super","char","final","native","synchronized","class","float","package","throws","const","goto","private","transient","debugger","implements","protected","volatile","double","import","public","from","where","select","group","into","orderby","join","let","on","equals","by","ascending","descending"];var m=["\u000A","\u000D","\u2028","\u2029","\u0009","\u000B","\u000C","\u0020","\u00A0","\u1680","\u180E","\u2000","\u2001","\u2002","\u2003","\u2004","\u2005","\u2006","\u2007","\u2008","\u2009","\u200A","\u202F","\u205F","\u3000"];var o=["<",">","<=",">=","==","!=","===","!==","+","-","*","%","<<",">>",">>>","&","|","^","&&","||","=","+=","-=","*=","%=","<<=",">>=",">>>=","&=","|=","^=","/","/="];var k=4;function c(s){return(function(){var t={};for(var u=0;u<s.length;u++){t[s[u]]=true}return function(v){return typeof t[v]!="undefined"}})()}function q(s){s=s.charAt(0);return s=="$"||s=="_"||(s>="a"&&s<="z")||(s>="A"&&s<="Z")}function i(s){return q(s)||n(s)}function n(s){return s>="0"&&s<="9"}function r(s){return n(s)||s>="a"&&s<="f"||s>="A"&&s<="F"}var d=c(f);var p=c(m);var j=c(o);function b(s){this.message=s;this.unparsedCharacters=-1}b.prototype.isPartialSuccess=function(s){s=s.replace(/^\s+/g,"");return this.unparsedCharacters>-1&&this.unparsedCharacters<s.length};var h=(function(){var s=this;function t(v,u){var y=this;var w=null;if(typeof y=="function"){w=function(z){var B=y(z);if(B instanceof b){return B}var A=v(B.rest);if(A instanceof b){A.unparsedCharacters=B.rest.length;return A}return{parsed:B.parsed.concat(A.parsed),rest:A.rest}}}else{w=v}for(var x in s){if(typeof s[x]=="function"){w[x]=s[x]}}w.label=u;return w}this.terminal=function(u){return t.call(this,function(w){var v=u.length;if(w.substring(0,v)==u){return{parsed:[u],rest:w.substring(v)}}else{return new b("'"+u+"' expected")}},"'"+u+"'")};this.oneOf=function(){var v=[];for(var u=0;u<arguments.length;u++){v.push(arguments[u])}return t.call(this,function(x){var w;var B=x.length;var z=null;for(var y=0;y<v.length;y++){w=v[y](x);if(!(w instanceof b)){return w}else{if(w.isPartialSuccess(x)){B=w.unparsedCharacters;z=w}}}if(z){return z}var C=[];for(var y=0;y<v.length;y++){C.push(v[y].label)}var A=C.pop();if(C.length>0){A="or "+A}return new b("Expecting "+C.join(", ")+" "+A)})};this.zeroOrMore=function(u){return t.call(this,function(w){var x=w;var y=[];var v;do{v=u(x);if(!(v instanceof b)){x=v.rest;y=y.concat(v.parsed)}}while(!(v instanceof b));if(v.isPartialSuccess(x)){return v}return{parsed:y,rest:x}})};this.oneOrMore=function(u){return t.call(this,function(w){var x=w;var y=[];var v;do{v=u(x);if(!(v instanceof b)){x=v.rest;y=y.concat(v.parsed)}}while(!(v instanceof b));if(v.isPartialSuccess(x)){return v}if(y.length>0){return{parsed:y,rest:x}}else{return new b("Expecting one or more "+w.label)}})};this.optional=function(u){return t.call(this,function(w){var v=u(w);if(!(v instanceof b)){return v}else{if(v.isPartialSuccess(w)){return v}return{parsed:[],rest:w}}})};this.skip=function(u){return t.call(this,function(w){var v=u(w);if(!(v instanceof b)){v.parsed=[]}return v})};this.asParser=function(u){var v=this;return function(){return t.call(this,function(w){return v(w)},u)}};this.lazy=function(u){return t.call(this,function(v){return u()(v)})};this.group=function(u){return t.call(this,function(w){var v=u(w);if(!(v instanceof b)){return{parsed:[v.parsed],rest:v.rest}}else{return v}})};this.flatten=function(u){return t.call(this,function(w){var v=u(w);if(!(v instanceof b)){return{parsed:[v.parsed.join(" ")],rest:v.rest}}else{return v}})};this.whitespace=function(){return t.call(this,function(u){if(u.length>0&&p(u.charAt(0))){return{parsed:[u.substring(0,1)],rest:u.substring(1)}}else{return new b("Expecting whitespace")}},"whitespace")};this.mandatoryWhitespace=this.skip(this.oneOrMore(this.whitespace())).asParser("whitespace");this.optionalWhitespace=this.skip(this.zeroOrMore(this.whitespace())).asParser("whitespace");this.identifier=function(){return t.call(this,function(u){if(u.length<1){return new b("Unexpected end of file")}if(!q(u.charAt(0))){return new b("Invalid character found, expecting identifier.")}var x=1;while(x<u.length&&i(u.charAt(x))){++x}var w=u.substring(0,x);if(d(w)){return new b("Reserved word '"+w+"' cannot be used as identifier")}var v=new RegExp("^\\$[0-9]+$","g");if(v.test(w)){w="_$qp["+w.substring(1)+"]"}return{parsed:[w],rest:u.substring(x)}},"identifier")};this.binaryOperator=function(){return t.call(this,function(v){for(var w=k;w>=1;w--){var u=v.substring(0,w);if(j(u)){return{parsed:u,rest:v.substring(w)}}}return new b("Expecting binary operator")},"binary operator")};this.stringLiteral=function(){return t.call(this,function(v){if(v.length<2||v.charAt(0)!='"'&&v.charAt(0)!="'"){return new b("Malformed string literal")}var u=v.charAt(0);var x=u;var y;for(var w=1;w<v.length;w++){y=v.charAt(w);if(y==u&&x!="\\"){++w;return{parsed:[v.substring(0,w)],rest:v.substring(w)}}x=y}return new b("Unterminated string literal")},"string literal")};this.numericLiteral=function(){return t.call(this,function(A){if(A.length<1){return new b("Unexpected end of file")}var y=n;var C=true;var z=false;var w=false;var B=0;if(A.substring(0,2).toLowerCase()=="0x"){y=r;C=false;B=2}else{if(A.charAt(0)=="."){C=false;z=true;B=1}}var v=B;for(var u=B;u<A.length;u++){var x=A.charAt(u);if(C&&x=="."){C=false;z=true}else{if(z&&(x=="e"||x=="E")){z=false;w=true}else{if(w&&(x=="+"||x=="-")){w=false}else{if(y(x)){w=false}else{break}}}}++v}if(v==B){return new b("Invalid number")}return{parsed:[A.substring(0,v)],rest:A.substring(v)}},"number literal")};this.functionExpressionBody=function(){return t.call(this,function(w){if(w.charAt(0)!="{"){return new b("'{' expected")}var y="";var A=false;var v="";var u=1;for(var x=1;x<w.length;x++){var z=w.charAt(x);if(!A){if(z=="{"){++u}else{if(z=="}"){--u}else{if(z=='"'||z=="'"){A=true;v=z}}}}else{if(z==v&&y!="\\"){A=false}}y=z;if(u==0){++x;return{parsed:[w.substring(0,x)],rest:w.substring(x)}}}return new b("Unexpected end of file")},"function expression")};this.arrayLiteral=this.terminal("[").optionalWhitespace().optional(this.optional(this.lazy(function(){return s.secondaryExpression()})).zeroOrMore(this.optionalWhitespace().terminal(",").optionalWhitespace().optional(this.lazy(function(){return s.secondaryExpression()}))).optionalWhitespace()).optionalWhitespace().terminal("]").asParser("array literal");this.propertyName=this.oneOf(this.identifier(),this.stringLiteral(),this.numericLiteral()).asParser("property name");this.propertyNameAndValue=this.propertyName().optionalWhitespace().terminal(":").optionalWhitespace().lazy(function(){return s.secondaryExpression()}).asParser("property name and value");this.objectLiteral=this.terminal("{").optionalWhitespace().optional(this.propertyNameAndValue().zeroOrMore(this.optionalWhitespace().terminal(",").optionalWhitespace().propertyNameAndValue())).optionalWhitespace().terminal("}").asParser("object literal");this.literal=this.oneOf(this.terminal("null"),this.terminal("true"),this.terminal("false"),this.terminal("Infinity"),this.numericLiteral(),this.stringLiteral(),this.arrayLiteral(),this.objectLiteral()).asParser("literal");this.primaryExpression=this.oneOf(this.terminal("this"),this.identifier(),this.literal(),this.terminal("(").optionalWhitespace().lazy(function(){return s.expression()}).optionalWhitespace().terminal(")")).asParser("primary expression");this.parameters=this.terminal("(").optionalWhitespace().optional(this.lazy(function(){return s.secondaryExpression()}).zeroOrMore(this.optionalWhitespace().terminal(",").optionalWhitespace().lazy(function(){return s.secondaryExpression()}))).optionalWhitespace().terminal(")").asParser("arguments");this.functionExpression=this.terminal("function").optional(this.mandatoryWhitespace().identifier()).optionalWhitespace().terminal("(").optionalWhitespace().optional(this.identifier().zeroOrMore(this.optionalWhitespace().terminal(",").optionalWhitespace().identifier())).optionalWhitespace().terminal(")").optionalWhitespace().functionExpressionBody().asParser("function expression");this.leftHandSideExpression=this.zeroOrMore(this.terminal("new").mandatoryWhitespace()).oneOf(this.primaryExpression(),this.functionExpression()).zeroOrMore(this.oneOf(this.optionalWhitespace().parameters(),this.optionalWhitespace().terminal("[").optionalWhitespace().lazy(function(){return s.secondaryExpression()}).optionalWhitespace().terminal("]"),this.optionalWhitespace().terminal(".").optionalWhitespace().identifier())).asParser("left-hand-side expression");this.unaryExpression=this.zeroOrMore(this.oneOf(this.terminal("delete").mandatoryWhitespace(),this.terminal("void").mandatoryWhitespace(),this.terminal("typeof").mandatoryWhitespace(),this.terminal("++"),this.terminal("--"),this.terminal("+"),this.terminal("-"),this.terminal("~"),this.terminal("!")).optionalWhitespace()).leftHandSideExpression().zeroOrMore(this.optionalWhitespace().oneOf(this.terminal("++"),this.terminal("--"))).asParser("unary expression");this.secondaryExpression=this.flatten(this.unaryExpression().zeroOrMore(this.optionalWhitespace().oneOf(this.binaryOperator(),this.terminal("in").mandatoryWhitespace(),this.terminal("instanceof").mandatoryWhitespace()).optionalWhitespace().unaryExpression())).asParser("expression");this.expression=this.flatten(this.secondaryExpression().zeroOrMore(this.optionalWhitespace().terminal(",").optionalWhitespace().secondaryExpression())).asParser("expression");this.fromClause=this.terminal("from").group(this.mandatoryWhitespace().identifier().mandatoryWhitespace().skip(this.terminal("in")).mandatoryWhitespace().expression()).asParser("from clause");this.letClause=this.terminal("let").group(this.mandatoryWhitespace().identifier().optionalWhitespace().skip(this.terminal("=")).optionalWhitespace().expression()).asParser("let clause");this.whereClause=this.terminal("where").group(this.mandatoryWhitespace().expression()).asParser("where clause");this.joinClause=this.terminal("join").group(this.mandatoryWhitespace().identifier().mandatoryWhitespace().skip(this.terminal("in")).mandatoryWhitespace().expression().mandatoryWhitespace().skip(this.terminal("on")).mandatoryWhitespace().expression().mandatoryWhitespace().skip(this.terminal("equals")).mandatoryWhitespace().expression()).asParser("join clause");this.joinIntoClause=this.terminal("join").group(this.mandatoryWhitespace().identifier().mandatoryWhitespace().skip(this.terminal("in")).mandatoryWhitespace().expression().mandatoryWhitespace().skip(this.terminal("on")).mandatoryWhitespace().expression().mandatoryWhitespace().skip(this.terminal("equals")).mandatoryWhitespace().expression().mandatoryWhitespace().skip(this.terminal("into")).mandatoryWhitespace().identifier()).asParser("join into clause");this.orderingDirection=this.oneOf(this.skip(this.terminal("ascending")),this.terminal("descending")).asParser("ordering direction");this.ordering=this.group(this.secondaryExpression().optional(this.mandatoryWhitespace().orderingDirection())).asParser("ordering");this.orderings=this.ordering().zeroOrMore(this.optionalWhitespace().skip(this.terminal(",")).optionalWhitespace().ordering()).asParser("orderings");this.orderByClause=this.terminal("orderby").group(this.mandatoryWhitespace().orderings()).asParser("orderby clause");this.selectClause=this.terminal("select").group(this.mandatoryWhitespace().expression()).asParser("select clause");this.groupClause=this.terminal("group").group(this.mandatoryWhitespace().expression().mandatoryWhitespace().skip(this.terminal("by")).mandatoryWhitespace().expression()).asParser("group by clause");this.selectOrGroupClause=this.group(this.oneOf(this.selectClause(),this.groupClause())).asParser("select or group by clause");this.queryBodyClause=this.group(this.oneOf(this.fromClause(),this.letClause(),this.whereClause(),this.joinIntoClause(),this.joinClause(),this.orderByClause())).asParser("query body");this.queryBodyClauses=this.queryBodyClause().zeroOrMore(this.mandatoryWhitespace().queryBodyClause()).asParser("query body");this.queryBody=this.optional(this.queryBodyClauses().mandatoryWhitespace()).selectOrGroupClause().optional(this.group(this.mandatoryWhitespace().lazy(function(){return s.queryContinuation()}))).asParser("query body");this.queryContinuation=this.terminal("into").group(this.mandatoryWhitespace().identifier().mandatoryWhitespace().lazy(function(){return s.queryBody()})).asParser("into clause");this.queryExpression=this.optionalWhitespace().group(this.fromClause()).mandatoryWhitespace().queryBody();return this.queryExpression}).call({});var a=(function(){var x=1;var A=2;var v=3;var s="_$ti";var E=0;var B={};function u(F){return F.substring(0,s.length)==s}function z(){E++;var G=s+E;var F=[];for(var H=0;H<arguments.length;H++){if(u(arguments[H])){F.push(arguments[H])}}B[G]=F;return G}function y(L,K){var F=["function(",L.join(", "),") {"];var M=[];var I=[];var N=[];for(var J=0;J<L.length;J++){if(u(L[J])){N.push(L[J]);do{var G=N.pop();M.push("with (",G,") {");I.push("}");var H=B[G];if(B[G].length>0){N.push(H[0])}else{break}}while(true)}}F=F.concat(M);F.push(K);F=F.concat(I);F.push("}");return F.join("")}var w=[[{pattern:["*","into",null],transformer:function(G,I){var F=G[I[1]];G[I[1]]=F[1][1];for(var H=2;H<F[1].length;H++){G.splice(I[1]+(H-1),0,F[1][H])}G.unshift(["from",[F[1][0],G.splice(0,I[0])]]);return true}}],[{pattern:["from","select",null],transformer:function(F,I){var G=F[I[0]][1];var H=F[I[1]][1];if(I[1]-I[0]>1){return false}if(G[0]==H[0]){F.splice(I[0],2,["compiled",["(",G[1],").select(",y([G[0]],"return "+G[0]+";"),")"]]);return true}return false}}],[{pattern:["from","from","select",null],transformer:function(F,H){var J=F[H[0]][1];var I=F[H[1]][1];var G=F[H[2]][1];if(H[1]-H[0]>1){return false}F.splice(H[0],2,["compiled",["(",J[1],")"]]);F.splice(H[1],1,["compiled",[".selectMany(",y([J[0]],"return "+I[1]+";"),", ",y([J[0],I[0]],"return "+G[0]+";"),")"]]);return true}},{pattern:["from","from"],transformer:function(F,G){var I=F[G[0]][1];var H=F[G[1]][1];F.splice(G[0],1,["from",[z(I[0],H[0]),I[1]]]);F.splice(G[1],1,["compiled",[".selectMany(",y([I[0]],"return "+H[1]+";"),", ",y([I[0],H[0]],"return { '"+I[0]+"':"+I[0]+", '"+H[0]+"':"+H[0]+"};"),")"]]);return true}},{pattern:["from","let"],transformer:function(G,I){var H=G[I[0]][1];var F=G[I[1]][1];G.splice(I[0],1,["from",[z(H[0],F[0]),H[1]]]);G.splice(I[1],1,["compiled",[".select(",y([H[0]],"return {'"+H[0]+"': "+H[0]+", '"+F[0]+"': "+F[1]+"};"),")"]]);return true}},{pattern:["from","where"],transformer:function(G,I){var H=G[I[0]][1];var F=G[I[1]][1];G.splice(I[1],1,["compiled",[".where(",y([H[0]],"return "+F[0]+";"),")"]]);return true}},{pattern:["from","join","select",null],transformer:function(F,J){var H=F[J[0]][1];var G=F[J[1]][1];var I=F[J[2]][1];if(J[2]-J[1]>1){return false}if(G.length>4){return false}F.splice(J[0],1,["compiled",["(",H[1],")"]]);F.splice(J[1],2,["compiled",[".join(",G[1],", ",y([H[0]],"return "+G[2]+";"),", ",y([G[0]],"return "+G[3]+";"),", ",y([H[0],G[0]],"return "+I[0]+";"),")"]]);return true}},{pattern:["from","join"],transformer:function(F,I){var H=F[I[0]][1];var G=F[I[1]][1];if(G.length>4){return false}F.splice(I[0],1,["from",[z(H[0],G[0]),H[1]]]);F.splice(I[1],1,["compiled",[".join(",G[1],", ",y([H[0]],"return "+G[2]+";"),", ",y([G[0]],"return "+G[3]+";"),", ",y([H[0],G[0]],"return {'"+H[0]+"': "+H[0]+", '"+G[0]+"': "+G[0]+"};"),")"]]);return true}},{pattern:["from","join","select",null],transformer:function(F,J){var H=F[J[0]][1];var G=F[J[1]][1];var I=F[J[2]][1];if(J[2]-J[1]>1){return false}if(G.length<5){return false}F.splice(J[0],1,["compiled",["(",H[1],")"]]);F.splice(J[1],2,["compiled",[".groupJoin(",G[1],", ",y([H[0]],"return "+G[2]+";"),", ",y([G[0]],"return "+G[3]+";"),", ",y([H[0],G[4]],"return "+I[0]+";"),")"]]);return true}},{pattern:["from","join"],transformer:function(F,I){var H=F[I[0]][1];var G=F[I[1]][1];if(G.length<5){return false}F.splice(I[0],1,["from",[z(H[0],G[4]),H[1]]]);F.splice(I[1],1,["compiled",[".groupJoin(",G[1],", ",y([H[0]],"return "+G[2]+";"),", ",y([G[0]],"return "+G[3]+";"),", ",y([H[0],G[4]],"return {'"+H[0]+"': "+H[0]+", '"+G[4]+"': "+G[4]+"};"),")"]]);return true}},{pattern:["from","orderby"],transformer:function(F,H){var G=F[H[0]][1];var L=F[H[1]][1];var K="";if(L[0].length>1){K="Descending"}var J=[".orderBy",K,"(",y([G[0]],"return "+L[0][0]+";"),")"];for(var I=1;I<L.length;I++){K="";if(L[I].length>1){K="Descending"}J.push(".thenBy",K,"(",y([G[0]],"return "+L[I][0]+";"),")")}F.splice(H[1],1,["compiled",J]);return true}}],[{pattern:["from","select",null],transformer:function(F,I){var G=F[I[0]][1];var H=F[I[1]][1];F.splice(I[0],1,["compiled",["(",G[1],")"]]);F.splice(I[1],1,["compiled",[".select(",y([G[0]],"return "+H[0]+";"),")"]]);return true}}],[{pattern:["from","group",null],transformer:function(G,I){var H=G[I[0]][1];var F=G[I[1]][1];G.splice(I[0],1,["compiled",["(",H[1],")"]]);var J=[".groupBy(",y([H[0]],"return "+F[1]+";")];if(H[0]!=F[0]){J.push(", ",y([H[0]],"return "+F[0]+";"))}J.push(")");G.splice(I[1],1,["compiled",J]);return true}}]];function D(G,J){var I;for(I=0;I<G.length;I++){if(G[I][0]=="from"&&G[I][1][1] instanceof Array){D(G[I][1][1],J)}else{if(G[I][0]=="compiled"){for(var H=0;H<G[I][1].length;H++){if(G[I][1][H] instanceof Array){D(G[I][1][H],J)}}}}var F=J(G,I,G[I]);if(F==x){break}else{if(F==v){return}}}J(G,I,null)}function C(M){var Q=w.length;var K;var N;var L;var F;var I;var O;var P;function J(S,T,W){var R=null;if(W!=null){R=W[0]}if(R=="compiled"){return A}var Y=L[F];var X=L[F+1];if(R==Y){I.push(T);++F}else{if(Y=="*"){if(R==X&&typeof I[F]!="undefined"){F+=2;I.push(T)}else{if(typeof I[F]=="undefined"){I[F]=0}I[F]++}}else{if(W==null){F=0;I=[]}return x}}if(W==null){var U=x;if(F>=L.length){var V=N.transformer(S,I);if(V){U=v;O=true;P=true}}F=0;I=[];return U}return A}for(var H=0;H<Q;H++){transformationBlock=w[H];K=transformationBlock.length;do{P=false;for(var G=0;G<K;G++){N=transformationBlock[G];do{L=N.pattern;F=0;I=[];O=false;D(M,J)}while(O)}}while(P)}return M}function t(H){var G=[];function F(I){var K;for(K=0;K<I.length;K++){if(I[K][0]=="compiled"){for(var J=0;J<I[K][1].length;J++){if(I[K][1][J] instanceof Array){F(I[K][1][J])}else{G=G.concat(I[K][1][J])}}}}}F(H);return G.join("")}return function(F){E=0;B={};if(F instanceof b){throw F}else{if(F.rest.replace(/^\s+/,"").length>0){throw new b("End of file expected",F.rest.length)}}F=C(F.parsed);return"return "+t(F)+";"}})();function e(u,z){var t=z.length-u.unparsedCharacters;var x={line:1,offset:1,context:0};var s=0;var w="";for(var v=0;v<t;v++){var y=z.charAt(v);if(y=="\r"||y=="\n"&&w!="\r"){++x.line;s=v}}x.offset=t-s;x.context=z.substring(t);return x}function l(v){var t=[];var u=null;try{u=new Function("_$qp",a(h(v)))}catch(w){if(w instanceof b){var s=e(w,v);throw new g(w.message,s.line,s.offset,s.context)}else{throw w}}this.setValue=function(x,y){if(u==null){throw new jsinq.InvalidOperationException()}t[x]=y};this.execute=function(){if(u==null){throw new jsinq.InvalidOperationException()}return u(t)};this.getQueryFunction=function(){if(u==null){throw new jsinq.InvalidOperationException()}return u}}this.QueryTranslationException=g;this.Query=l}).call(jsinq);