/* JSINQ, Copyright (c) 2009 Kai Jäger, governed by an MIT-style license. */ 
if(typeof jsinq=="undefined"){jsinq={}}(function(){function c(){}c.prototype=new Error();c.prototype.name="InvalidOperationException";c.prototype.message="Operation is not valid due to the current state of the object.";function e(f){this.message+=f}e.prototype=new Error();e.prototype.name="ArgumentOutOfRangeException";e.prototype.message="Specified argument was out of the range of valid values.\r\nParameter name: ";function a(){}a.prototype={equals:function(g,f){return g==f}};(function(){var f=new a();a.getDefault=function(){return f}})();function b(){}b.prototype={compare:function(g,f){return g<f?-1:(g>f?1:0)}};(function(){var f=new b();b.getDefault=function(){return f}})();function d(f){if(arguments.length==0){f=[]}else{if(typeof f.length=="undefined"||(f.length>0&&typeof f[0]=="undefined")){f=[f]}}this.getEnumerator=function(){return new function(){var g=-1;this.moveNext=function(){++g;return g<f.length};this.current=function(){if(g<0||g>=f.length){throw new c()}return f[g]};this.reset=function(){g=-1}}}}d.empty=function(){return new d()};d.range=function(h,g){if(g<0){throw new e()}var f=function(){this.getEnumerator=function(){return new function(){var j=-1;var i=false;this.moveNext=function(){i=false;if(j<g-1){i=true;++j;return true}return false};this.current=function(){if(i){return h+j}else{throw new c()}};this.reset=function(){j=-1}}}};f.prototype=d.prototype;return new f()};d.repeat=function(f,h){if(h<0){throw new e()}var g=function(){this.getEnumerator=function(){return new function(){var j=-1;var i=false;this.moveNext=function(){i=false;if(j<h-1){i=true;++j;return true}return false};this.current=function(){if(i){return f}else{throw new c()}};this.reset=function(){j=-1;i=false}}}};g.prototype=d.prototype;return new g()};d.prototype=(function(){function g(l){return l}function i(l){if(arguments.length==0||arguments[0]==a.getDefault()){this.comparer=null}this.comparer=arguments[0];this.primitiveItems={};this.complexItems=[]}i.prototype={lookUp:function(m,p){var r;if(this.comparer==null&&(typeof m=="string"||typeof m=="number"||typeof m=="boolean"||typeof m=="null")){var q=this.primitiveItems[m];if(typeof q!="undefined"){r=p(q)}else{r=p()}if(r.length>1){this.primitiveItems[m]=r[1]}return r[0]}else{var o=this.complexItems.length;var n;for(var l=0;l<o;l++){n=this.complexItems[l];if((this.comparer!=null&&this.comparer.equals(n.key,m))||(this.comparer==null&&n.key==m)){r=p(n.element);if(r.length>1){n.element=r[1]}return r[0]}}r=p();if(r.length>1){this.complexItems.push({key:m,element:r[1]})}return r[0]}},keyExists:function(l){return this.lookUp(l,function(m){return[arguments.length==1]})},get:function(l){return this.lookUp(l,function(m){if(arguments.length==0){throw false}return[m]})},put:function(m,n,l){if(arguments.length<3){l=false}return this.lookUp(m,function(o){if(arguments.length==0||l){return[true,n]}else{return[false]}})},toArray:function(){var l=[];for(var m in this.primitiveItems){l.push({key:m,element:this.primitiveItems[m]})}return l.concat(this.complexItems)},empty:function(){this.primitiveItems={};this.complexItems=[]}};var h=1;var f=-1;function k(l){return function(n){var r=this;var q=[];var o=b.getDefault();if(arguments.length>=3){o=arguments[1];q=arguments[2]}else{if(arguments.length>=2){if(arguments[1] instanceof Array){q=arguments[1]}else{o=arguments[1]}}}var m=[[n,l,o]];m=m.concat(q);var p=function(){this.getEnumerator=function(){return new function(){var t=null;function s(){var u=r.toArray();u.sort(function(D,B){var E=0;var y;var C;var A;for(var z=m.length-1;z>=0;z--){E=0;y=m[z][0];C=m[z][1];A=m[z][2];var x=y(D);var w=y(B);var v=A.compare(x,w);if(v!=0){return v*C}}return 0});t=(new d(u)).getEnumerator()}this.moveNext=function(){if(t==null){s()}return t.moveNext()};this.current=function(){if(t==null){throw new c()}return t.current()};this.reset=function(){if(t!=null){t.reset()}}}};this.thenBy=function(t,u){var s=k(h);if(arguments.length<2){u=b.getDefault()}return s.call(r,t,u,m)};this.thenByDescending=function(t,u){var s=k(f);if(arguments.length<2){u=b.getDefault()}return s.call(r,t,u,m)}};p.prototype=d.prototype;return new p()}}function j(l){return function(o,m,r,n,p){var s=this;if(arguments.length<5){p=a.getDefault()}var q=function(){this.getEnumerator=function(){return new function(){var z=null;var A=s.getEnumerator();var t=false;var w;var u;var y=null;var v=-1;function x(){z=new i(p);var B=o.getEnumerator();while(B.moveNext()){var D=B.current();var C=r(D);if(!z.put(C,[D])){var E=z.get(C);E.push(D)}}}this.moveNext=function(){if(z==null){x()}var C;var B;if(l){t=A.moveNext();if(t){C=A.current();B=m(C);w=C;if(z.keyExists(B)){u=new d(z.get(B))}else{u=d.empty()}}}else{if(y!=null&&++v<y.length){u=y[v];t=true}else{t=false;while(A.moveNext()){C=A.current();B=m(C);if(z.keyExists(B)){y=z.get(B);u=y[0];v=0;t=true;w=C;break}else{continue}}}}return t};this.current=function(){if(!t){throw new c()}return n(w,u)};this.reset=function(){A.reset()}}}};q.prototype=d.prototype;return new q()}}return{aggregate:function(){var o=this.getEnumerator();if(!o.moveNext()){throw new c()}var l;var n;var m=g;if(arguments.length>=2){l=arguments[0];n=arguments[1];if(arguments.length>=3){m=arguments[2]}o.reset()}else{n=arguments[0];l=o.current()}while(o.moveNext()){l=n(l,o.current())}return m(l)},all:function(l){var m=this.getEnumerator();while(m.moveNext()){if(!l(m.current())){return false}}return true},any:function(l){var n=this.getEnumerator();if(arguments.length==0){return n.moveNext()}var m=[];while(n.moveNext()){if(l(n.current())){return true}}return false},average:function(l){if(arguments.length==0){l=g}var n=0;var m=this.aggregate(0,function(o,p){++n;return o+l(p)});return m/n},concat:function(l){var n=this;var m=function(){this.getEnumerator=function(){return new function(){var q=n.getEnumerator();var o=l.getEnumerator();var r=q;var p=true;this.moveNext=function(){if(!r.moveNext()){if(p){r=o;return r.moveNext()}return false}return true};this.current=function(){return r.current()};this.reset=function(){p=true;q.reset();o.reset();r=q}}}};m.prototype=d.prototype;return new m()},contains:function(m,l){if(arguments.length==1){l=a.getDefault()}return this.any(function(n){return l.equals(n,m)})},count:function(l){var m=0;var o=this.getEnumerator();var n=typeof l=="function";while(o.moveNext()){if((n&&l(o.current()))||!n){++m}}return m},defaultIfEmpty:function(l){var m=!this.any(function(n){return true});if(m){return new d(l)}return this},distinct:function(m){var o=this;var l=arguments.length>0;var n=function(){this.getEnumerator=function(){return new function(){var q=o.getEnumerator();var p;if(l){p=new i(m)}else{p=new i()}this.moveNext=function(){while(q.moveNext()){if(p.put(q.current(),true)){return true}}return false};this.current=function(){return q.current()};this.reset=function(){q.reset();p.empty()}}}};n.prototype=d.prototype;return new n()},elementAt:function(l){var m;var n=this.any(function(o){m=o;return l--==0});if(!n){throw new e("index")}return m},elementAtOrDefault:function(m,l){try{return this.elementAt(m)}catch(n){return l}},except:function(l,m){if(arguments.length<2){m=a.getDefault()}return this.where(function(n){return !l.any(function(o){return m.equals(n,o)})})},first:function(l){if(arguments.length==0){try{return this.elementAt(0)}catch(o){throw new c()}}else{var m;var n=this.any(function(p){if(l(p)){m=p;return true}return false});if(!n){throw new c()}return m}},firstOrDefault:function(){if(arguments.length==1){var m=arguments[0]}else{if(arguments.length>1){var l=arguments[0];var m=arguments[1]}}try{if(arguments.length>1){return this.first(l)}else{return this.first()}}catch(n){return m}},groupBy:function(){var m=arguments[0];var p=g;var l=g;var n=a.getDefault();if(arguments.length==2){if(typeof arguments[1].equals=="function"){n=arguments[1]}else{if(arguments[1].arity>=2){l=arguments[1]}else{p=arguments[1]}}}else{if(arguments.length==3){if(arguments[1].arity>=2){l=arguments[1];n=arguments[2]}else{p=arguments[1];if(typeof arguments[2].equals=="function"){n=arguments[2]}else{l=arguments[2]}}}else{if(arguments.length>3){p=arguments[1];l=arguments[2];n=arguments[3]}}}var q=this;var o=function(){this.getEnumerator=function(){return new function(){var s=null;var r=-1;function t(){var x=q.getEnumerator();var w=new i(n);while(x.moveNext()){var v=x.current();var u=m(v);if(w.keyExists(u)){var y=w.get(u);y.push(p(v))}else{w.put(u,[p(v)])}}s=w.toArray()}this.moveNext=function(){if(s==null){t()}++r;return r<s.length};this.current=function(){if(r<0||r>=s.length){throw new c()}var w=s[r];var v=new d(w.element);if(l!=g){return l(w.key,v)}else{var u=function(x,y){this.getKey=function(){return x};this.getEnumerator=function(){return y.getEnumerator()}};u.prototype=d.prototype;return new u(w.key,v)}};this.reset=function(){r=-1}}}};o.prototype=d.prototype;return new o()},groupJoin:j(true),intersect:function(l,m){if(arguments.length<2){m=a.getDefault()}return this.distinct(m).where(function(n){return l.contains(n,m)})},join:j(false),last:function(l){var n=typeof l=="function";var m;var o=true;this.any(function(p){if(!n||l(p)){m=p;o=false}return false});if(o){throw new c()}return m},lastOrDefault:function(){if(arguments.length==1){var m=arguments[0]}else{if(arguments.length>1){var l=arguments[0];var m=arguments[1]}}try{if(arguments.length>1){return this.last(l)}else{return this.last()}}catch(n){return m}},max:function(m){if(arguments.length==0){m=g}var l=true;return this.aggregate(function(n,o){if(l){n=m(n);l=false}return Math.max(n,m(o))})},min:function(m){if(arguments.length==0){m=g}var l=true;return this.aggregate(function(n,o){if(l){n=m(n);l=false}return Math.min(n,m(o))})},orderBy:k(h),orderByDescending:k(f),reverse:function(){var m=this;var l=function(){this.getEnumerator=function(){return new function(){var n=null;this.moveNext=function(){if(n==null){n=new d(m.toArray().reverse()).getEnumerator()}return n.moveNext()};this.current=function(){if(n==null){throw new c()}return n.current()};this.reset=function(){if(n!=null){n.reset()}}}}};l.prototype=d.prototype;return new l()},select:function(l){var n=this;var m=function(){this.getEnumerator=function(){return new function(){var p=n.getEnumerator();var o=-1;this.moveNext=function(){++o;return p.moveNext()};this.current=function(){return l(p.current(),o)};this.reset=function(){p.reset();o=-1}}}};m.prototype=d.prototype;return new m()},selectMany:function(l,m){var o=arguments.length>1;var p=this;var n=function(){this.getEnumerator=function(){return new function(){var u=p.getEnumerator();var t=null;var q=null;var s=false;var r=0;this.moveNext=function(){s=false;var v=true;while(true){if(q==null||!q.moveNext()){if(!u.moveNext()){break}t=u.current();q=l(t,r++).getEnumerator();v=false}if(v||q.moveNext()){s=true;break}}return s};this.current=function(){if(!s){throw new c()}if(o){return m(t,q.current())}else{return q.current()}};this.reset=function(){s=false;q=null;t=null;u.reset();r=0}}}};n.prototype=d.prototype;return new n()},sequenceEqual:function(m,n){if(this==m){return true}if(arguments.length<2){n=a.getDefault()}var o=this.getEnumerator();var l=m.getEnumerator();while(o.moveNext()&&l.moveNext()){if(!n.equals(o.current(),l.current())){return false}}return true},single:function(l){var n=arguments.length>0;var o=this.getEnumerator();if(!o.moveNext()){throw new c()}var m=o.current();if(o.moveNext()||(n&&!l(m))){throw new c()}return m},singleOrDefault:function(){if(arguments.length==1){var m=arguments[0]}else{if(arguments.length>1){var l=arguments[0];var m=arguments[1]}}try{if(arguments.length>1){return this.single(l)}else{return this.single()}}catch(n){return m}},skip:function(l){if(l==0){return this}return this.skipWhile(function(n,m){return m<l})},skipWhile:function(l){var n=this;var m=function(){this.getEnumerator=function(){return new function(){var q=n.getEnumerator();var p=false;var o=0;this.moveNext=function(){if(!p){var r=false;while((r=q.moveNext())&&l(q.current(),o++)){}p=true;return r}else{return q.moveNext()}};this.current=function(){return q.current()};this.reset=function(){q.reset();p=false;o=0}}}};m.prototype=d.prototype;return new m()},sum:function(l){if(arguments.length==0){l=g}return this.aggregate(0,function(m,n){return m+l(n)})},take:function(l){if(l==0){return d.empty()}return this.takeWhile(function(n,m){return m<l})},takeWhile:function(l){var n=this;var m=function(){this.getEnumerator=function(){return new function(){var q=n.getEnumerator();var p=true;var o=0;this.moveNext=function(){if(p){p=q.moveNext()&&l(q.current(),o++)}return p};this.current=function(){if(!p){throw new c()}return q.current()};this.reset=function(){q.reset();p=true;o=0}}}};m.prototype=d.prototype;return new m()},toArray:function(){var m=this.getEnumerator();var l=[];while(m.moveNext()){l.push(m.current())}return l},union:function(l,m){if(arguments.length<2){m=a.getDefault()}return this.concat(l).distinct(m)},where:function(l){var n=this;var m=function(){this.getEnumerator=function(){return new function(){var q=n.getEnumerator();var p;var o=false;this.moveNext=function(){o=false;while(q.moveNext()){p=q.current();if(l(p)){o=true;break}}return o};this.current=function(){if(!o){throw new c()}return p};this.reset=function(){q.reset();o=false}}}};m.prototype=d.prototype;return new m()}}})();this.InvalidOperationException=c;this.ArgumentOutOfRangeException=e;this.EqualityComparer=a;this.Comparer=b;this.Enumerable=d}).call(jsinq);