/*
 * easyXDM 
 * http://easyxdm.net/
 * Copyright(c) 2009, Øyvind Sean Kinsey, oyvind@kinsey.no.
 * 
 * MIT Licensed - http://easyxdm.net/license/mit.txt
 * 
 */
 (function(j,u,b,c,q,a){var y=this;var t=0;var v=Function.prototype;var A=/^(http.?:\/\/([^\/\s]+))/,B=/[\-\w]+\/\.\.\//,m=/([^:])\/\//g;var f=/msie [67]/.test(navigator.userAgent.toLowerCase());function l(F,H){var G=typeof F[H];return G=="function"||(!!(G=="object"&&F[H]))||G=="unknown"}function i(F,G){return !!(typeof(F[G])=="object"&&F[G])}var n=(function(){if(l(j,"addEventListener")){return function(H,F,G){H.addEventListener(F,G,false)}}else{return function(F,H,G){F.attachEvent("on"+H,G)}}}());var r=(function(){if(l(j,"removeEventListener")){return function(I,G,H,F){I.removeEventListener(G,H,F)}}else{return function(F,H,G){F.detachEvent("on"+H,G)}}}());function x(F){return F.match(A)[2]}function E(F){return F.match(A)[1]}function d(F){F=F.replace(m,"$1/");if(!F.match(/^(http||https):\/\//)){var G=(F.substring(0,1)==="/")?"":b.pathname;if(G.substring(G.length-1)!=="/"){G=G.substring(0,G.lastIndexOf("/")+1)}F=b.protocol+"//"+b.host+G+F}while(B.test(F)){F=F.replace(B,"")}return F}function k(F,I){var K="",H=F.indexOf("#");if(H!==-1){K=F.substring(H);F=F.substring(0,H)}var J=[];for(var G in I){if(I.hasOwnProperty(G)){J.push(G+"="+a(I[G]))}}return F+((F.indexOf("?")===-1)?"?":"&")+J.join("&")+K}var h=(function(){var H={},I,G=b.search.substring(1).split("&"),F=G.length;while(F--){I=G[F].split("=");H[I[0]]=q(I[1])}return H}());function e(F){return typeof F==="undefined"}function p(){var G={};var H={a:[1,2,3]},F='{"a":[1,2,3]}';if(JSON&&typeof JSON.stringify==="function"&&JSON.stringify(H).replace((/\s/g),"")===F){return JSON}if(Object.toJSON){if(Object.toJSON(H).replace((/\s/g),"")===F){G.stringify=Object.toJSON}}if(typeof String.prototype.evalJSON==="function"){H=F.evalJSON();if(H.a&&H.a.length===3&&H.a[2]===3){G.parse=function(I){return I.evalJSON()}}}if(G.stringify&&G.parse){p=function(){return G};return G}return null}function w(F,G,H){var J;for(var I in G){if(G.hasOwnProperty(I)){if(I in F){J=G[I];if(typeof J==="object"){w(F[I],J,H)}else{if(!H){F[I]=G[I]}}}else{F[I]=G[I]}}}return F}function D(F){var G;if(F.props.name&&f){G=u.createElement('<iframe name="'+F.props.name+'"/>')}else{G=u.createElement("IFRAME")}if(F.props.name){G.id=G.name=F.props.name;delete F.props.name}if(F.onLoad){G.loadFn=function(){F.onLoad(G.contentWindow)};n(G,"load",G.loadFn)}if(F.container){G.border=G.frameBorder=0;F.container.appendChild(G)}else{G.style.position="absolute";G.style.left="-2000px";G.style.top="0px";u.body.appendChild(G)}w(G,F.props);G.id=G.name;return G}var C=(function(){if(l(j,"XMLHttpRequest")){return function(){return new XMLHttpRequest()}}else{var F=(function(){var H=["Microsoft","Msxml2","Msxml3"],G=H.length;while(G--){try{F=H[G]+".XMLHTTP";var J=new ActiveXObject(F);return F}catch(I){}}}());return function(){return new ActiveXObject(F)}}}());function s(G){var I=C(),K=[],J,F;w(G,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded","X-Requested-With":"XMLHttpRequest"},success:v,error:function(M){throw new Error(M)},data:{},type:"plain"},true);F=G.method=="POST";for(var H in G.data){if(G.data.hasOwnProperty(H)){K.push(a(H)+"="+a(G.data[H]))}}J=K.join("&");I.open(G.method,G.url+(F?"":"?"+J),true);for(var L in G.headers){if(G.headers.hasOwnProperty(L)){I.setRequestHeader(L,G.headers[L])}}I.onreadystatechange=function(){if(I.readyState==4){if(I.status>=200&&I.status<300){if(G.type=="json"){try{G.success(p().parse(I.responseText))}catch(M){G.error("An error occured. Error parsing JSON: "+M.message)}}else{G.success(I.responseText)}}else{G.error("An error occured. Status code: "+I.status)}I.onreadystatechange=v}};I.send(F?J:"")}function o(I,H){if(typeof I=="string"){I=[I]}var G,F=I.length;while(F--){G=I[F];G=new RegExp(G.substr(0,1)=="^"?G:("^"+G.replace(/(\*)/g,".$1").replace(/\?/g,".")+"$"));if(G.test(H)){return true}}return false}function g(H){var M=H.protocol,G;H.isHost=H.isHost||e(h.xdm_p);if(!H.props){H.props={}}if(!H.isHost){H.channel=h.xdm_c;H.secret=h.xdm_s;H.remote=h.xdm_e;M=h.xdm_p;if(H.acl&&!o(H.acl,H.remote)){throw new Error("Access denied for "+H.remote)}}else{H.remote=d(H.remote);H.channel=H.channel||"default"+t++;H.secret=Math.random().toString(16).substring(2);if(e(M)){if(E(b.href)==E(H.remote)){M="4"}else{if(l(j,"postMessage")||l(u,"postMessage")){M="1"}else{if(l(j,"ActiveXObject")&&l(j,"execScript")){M="3"}else{if(navigator.product==="Gecko"&&"frameElement" in j&&navigator.userAgent.indexOf("WebKit")==-1){M="5"}else{if(H.remoteHelper){H.remoteHelper=d(H.remoteHelper);M="2"}else{M="0"}}}}}}}switch(M){case"0":w(H,{interval:100,delay:2000,useResize:true,useParent:false,usePolling:false},true);if(H.isHost){if(!H.local){var K=b.protocol+"//"+b.host,F=u.body.getElementsByTagName("img"),L;var I=F.length;while(I--){L=F[I];if(L.src.substring(0,K.length)===K){H.local=L.src;break}}if(!H.local){H.local=j}}var J={xdm_c:H.channel,xdm_p:0};if(H.local===j){H.usePolling=true;H.useParent=true;H.local=b.protocol+"//"+b.host+b.pathname+b.search;J.xdm_e=H.local;J.xdm_pa=1}else{J.xdm_e=d(H.local)}if(H.container){H.useResize=false;J.xdm_po=1}H.remote=k(H.remote,J)}else{w(H,{channel:h.xdm_c,remote:h.xdm_e,useParent:!e(h.xdm_pa),usePolling:!e(h.xdm_po),useResize:H.useParent?false:H.useResize})}G=[new easyXDM.stack.HashTransport(H),new easyXDM.stack.ReliableBehavior({timeout:H.interval*1.5}),new easyXDM.stack.QueueBehavior({encode:true,maxLength:4000-H.remote.length}),new easyXDM.stack.VerifyBehavior({initiate:H.isHost}),new easyXDM.stack.QueueBehavior()];break;case"1":G=[new easyXDM.stack.PostMessageTransport(H),new easyXDM.stack.QueueBehavior()];break;case"2":G=[new easyXDM.stack.NameTransport(H),new easyXDM.stack.QueueBehavior(),new easyXDM.stack.VerifyBehavior({initiate:H.isHost}),new easyXDM.stack.QueueBehavior()];break;case"3":G=[new easyXDM.stack.NixTransport(H),new easyXDM.stack.QueueBehavior()];break;case"4":G=[new easyXDM.stack.SameOriginTransport(H)];break;case"5":G=[new easyXDM.stack.FrameElementTransport(H)];break}return G}function z(I){var J,H={incoming:function(L,K){this.up.incoming(L,K)},outgoing:function(K,L){this.down.outgoing(K,L)},callback:function(K){this.up.callback(K)},init:function(){this.down.init()},destroy:function(){this.down.destroy()}};for(var G=0,F=I.length;G<F;G++){J=I[G];w(J,H,true);if(G!==0){J.down=I[G-1]}if(G!==F-1){J.up=I[G+1]}}return J}y.easyXDM={version:"2.4.3.93",apply:w,query:h,ajax:s,getJSONObject:p,stack:{}};easyXDM.DomHelper={on:n,un:r,requiresJSON:function(F){if(!i(j,"JSON")){u.write('<script type="text/javascript" src="'+F+'"><\/script>')}}};(function(){var F={};easyXDM.Fn={set:function(G,H){F[G]=H},get:function(H,G){var I=F[H];if(G){delete F[H]}return I}}}());easyXDM.Socket=function(G){var F=z(g(G).concat([{incoming:function(J,I){G.onMessage(J,I)},callback:function(I){if(G.onReady){G.onReady(I)}}}])),H=E(G.remote);this.destroy=function(){F.destroy()};this.postMessage=function(I){F.outgoing(I,H)};F.init()};easyXDM.Rpc=function(H,G){if(G.local){for(var J in G.local){if(G.local.hasOwnProperty(J)){var I=G.local[J];if(typeof I==="function"){G.local[J]={method:I}}}}}var F=z(g(H).concat([new easyXDM.stack.RpcBehavior(this,G),{callback:function(K){if(H.onReady){H.onReady(K)}}}]));this.destroy=function(){F.destroy()};F.init()};easyXDM.stack.SameOriginTransport=function(G){var H,J,I,F;return(H={outgoing:function(L,M,K){I(L);if(K){K()}},destroy:function(){if(J){J.parentNode.removeChild(J);J=null}},init:function(){F=E(G.remote);if(G.isHost){w(G.props,{src:k(G.remote,{xdm_e:b.protocol+"//"+b.host+b.pathname,xdm_c:G.channel,xdm_p:4})});J=D(G);easyXDM.Fn.set(G.channel,function(K){I=K;c(function(){H.up.callback(true)},0);return function(L){H.up.incoming(L,F)}})}else{I=parent.easyXDM.Fn.get(G.channel,true)(function(K){H.up.incoming(K,F)});H.up.callback(true)}}})};easyXDM.stack.PostMessageTransport=function(I){var K,L,G,H;function F(M){if(M.origin){return M.origin}if(M.uri){return E(M.uri)}if(M.domain){return b.protocol+"//"+M.domain}throw"Unable to retrieve the origin of the event"}function J(N){var M=F(N);if(M==H&&N.data.substring(0,I.channel.length+1)==I.channel+" "){K.up.incoming(N.data.substring(I.channel.length+1),M)}}return(K={outgoing:function(N,O,M){G.postMessage(I.channel+" "+N,O||H);M()},destroy:function(){r(j,"message",J);if(L){G=null;L.parentNode.removeChild(L);L=null}},init:function(){H=E(I.remote);if(I.isHost){n(j,"message",function M(N){if(N.data==I.channel+"-ready"){G=("postMessage" in L.contentWindow)?L.contentWindow:L.contentWindow.document;r(j,"message",M);n(j,"message",J);c(function(){K.up.callback(true)},0)}});w(I.props,{src:k(I.remote,{xdm_e:b.protocol+"//"+b.host,xdm_c:I.channel,xdm_p:1})});L=D(I)}else{n(j,"message",J);G=("postMessage" in j.parent)?j.parent:j.parent.document;G.postMessage(I.channel+"-ready",H);c(function(){K.up.callback(true)},0)}}})};easyXDM.stack.FrameElementTransport=function(G){var H,J,I,F;return(H={outgoing:function(L,M,K){I.call(this,L);if(K){K()}},destroy:function(){if(J){J.parentNode.removeChild(J);J=null}},init:function(){F=E(G.remote);if(G.isHost){w(G.props,{src:k(G.remote,{xdm_e:b.protocol+"//"+b.host+b.pathname,xdm_c:G.channel,xdm_p:5})});J=D(G);J.fn=function(K){delete J.fn;I=K;c(function(){H.up.callback(true)},0);return function(L){H.up.incoming(L,F)}}}else{if(u.referrer&&u.referrer!=h.xdm_e){j.parent.location=h.xdm_e}else{if(u.referrer!=h.xdm_e){j.parent.location=h.xdm_e}I=j.frameElement.fn(function(K){H.up.incoming(K,F)});H.up.callback(true)}}}})};easyXDM.stack.NixTransport=function(G){var I,K,J,F,H;return(I={outgoing:function(M,N,L){J(M);L()},destroy:function(){H=null;if(K){K.parentNode.removeChild(K);K=null}},init:function(){F=E(G.remote);if(G.isHost){try{if(!l(j,"getNixProxy")){j.execScript("Class NixProxy\n    Private m_parent, m_child, m_Auth\n\n    Public Sub SetParent(obj, auth)\n        If isEmpty(m_Auth) Then m_Auth = auth\n        SET m_parent = obj\n    End Sub\n    Public Sub SetChild(obj)\n        SET m_child = obj\n        m_parent.ready()\n    End Sub\n\n    Public Sub SendToParent(data, auth)\n        If m_Auth = auth Then m_parent.send(CStr(data))\n    End Sub\n    Public Sub SendToChild(data, auth)\n        If m_Auth = auth Then m_child.send(CStr(data))\n    End Sub\nEnd Class\nFunction getNixProxy()\n    Set GetNixProxy = New NixProxy\nEnd Function\n","vbscript")}H=getNixProxy();H.SetParent({send:function(N){I.up.incoming(N,F)},ready:function(){c(function(){I.up.callback(true)},0)}},G.secret);J=function(N){H.SendToChild(N,G.secret)}}catch(M){throw new Error("Could not set up VBScript NixProxy:"+M.message)}w(G.props,{src:k(G.remote,{xdm_e:b.protocol+"//"+b.host+b.pathname,xdm_c:G.channel,xdm_s:G.secret,xdm_p:3})});K=D(G);K.contentWindow.opener=H}else{if(u.referrer&&u.referrer!=h.xdm_e){j.parent.location=h.xdm_e}else{if(u.referrer!=h.xdm_e){j.parent.location=h.xdm_e}try{H=j.opener}catch(L){throw new Error("Cannot access window.opener")}H.SetChild({send:function(N){y.setTimeout(function(){I.up.incoming(N,F)},0)}});J=function(N){H.SendToParent(N,G.secret)};c(function(){I.up.callback(true)},0)}}}})};easyXDM.stack.NameTransport=function(J){var K;var M,Q,I,O,P,G,F;function N(T){var S=J.remoteHelper+(M?"#_3":"#_2")+J.channel;Q.contentWindow.sendMessage(T,S)}function L(){if(M){if(++O===2||!M){K.up.callback(true)}}else{N("ready");K.up.callback(true)}}function R(S){K.up.incoming(S,G)}function H(){if(P){c(function(){P(true)},0)}}return(K={outgoing:function(T,U,S){P=S;N(T)},destroy:function(){Q.parentNode.removeChild(Q);Q=null;if(M){I.parentNode.removeChild(I);I=null}},init:function(){M=J.isHost;O=0;G=E(J.remote);J.local=d(J.local);if(M){easyXDM.Fn.set(J.channel,function(S){if(M&&S==="ready"){easyXDM.Fn.set(J.channel,R);L()}});F=k(J.remote,{xdm_e:J.local,xdm_c:J.channel,xdm_p:2});w(J.props,{src:F+"#"+J.channel,name:J.channel});I=D(J)}else{J.remoteHelper=J.remote;easyXDM.Fn.set(J.channel,R)}Q=D({props:{src:J.local+"#_4"+J.channel},onLoad:function(){r(Q,"load",Q.loadFn);easyXDM.Fn.set(J.channel+"_load",H);(function S(){if(typeof Q.contentWindow.sendMessage=="function"){L()}else{c(S,50)}}())}})}})};easyXDM.stack.HashTransport=function(H){var K;var P=this,N,I,F,L,U,J,T;var O,G;function S(W){if(!T){return}var V=H.remote+"#"+(U++)+"_"+W;((N||!O)?T.contentWindow:T).location=V}function M(V){L=V;K.up.incoming(L.substring(L.indexOf("_")+1),G)}function R(){if(!J){return}var V=J.location.href,X="",W=V.indexOf("#");if(W!=-1){X=V.substring(W)}if(X&&X!=L){M(X)}}function Q(){I=setInterval(R,F)}return(K={outgoing:function(V,W){S(V)},destroy:function(){j.clearInterval(I);if(N||!O){T.parentNode.removeChild(T)}T=null},init:function(){N=H.isHost;F=H.interval;L="#"+H.channel;U=0;O=H.useParent;G=E(H.remote);if(N){H.props={src:H.remote,name:"local_"+H.channel};if(O){H.onLoad=function(){J=j;Q();K.up.callback(true)}}else{var X=0,V=H.delay/50;(function W(){if(++X>V){throw new Error("Unable to reference listenerwindow")}try{J=T.contentWindow.frames["remote_"+H.channel]}catch(Y){}if(J){Q();K.up.callback(true)}else{c(W,50)}}())}T=D(H)}else{J=j;Q();if(O){T=parent;K.up.callback(true)}else{w(H,{props:{src:H.remote+"#"+H.channel+new Date(),name:"remote_"+H.channel},onLoad:function(){K.up.callback(true)}});T=D(H)}}}})};easyXDM.stack.ReliableBehavior=function(H){var I,F,M,K,O=0,J=0,L=H.tries||5,N=H.timeout,G=0,P;return(I={incoming:function(S,Q){var R=S.indexOf("_"),U=parseInt(S.substring(0,R),10),T;S=S.substring(R+1);R=S.indexOf("_");T=parseInt(S.substring(0,R),10);R=S.indexOf("_");S=S.substring(R+1);if(F&&U===O){j.clearTimeout(F);F=null;if(P){c(function(){P(true)},0)}}if(T!==0){if(T!==G){G=T;S=S.substring(T.length+1);I.down.outgoing(T+"_0_ack",Q);c(function(){I.up.incoming(S,Q)},H.timeout/2)}else{I.down.outgoing(T+"_0_ack",Q)}}},outgoing:function(S,Q,R){P=R;J=0;M={data:G+"_"+(++O)+"_"+S,origin:Q};(function T(){F=null;if(++J>L){if(P){c(function(){P(false)},0)}}else{I.down.outgoing(M.data,M.origin);F=c(T,H.timeout)}}())},destroy:function(){if(F){j.clearInterval(F)}I.down.destroy()}})};easyXDM.stack.QueueBehavior=function(J){var K,F=[],M=true,H="",L,G=0;function I(){if(M||F.length===0||L){return}M=true;var N=F.shift();K.down.outgoing(N.data,N.origin,function(O){M=false;if(N.callback){c(function(){N.callback(O)},0)}I()})}return(K={init:function(){if(e(J)){J={}}G=J.maxLength?J.maxLength:0;K.down.init()},callback:function(N){M=false;I();K.up.callback(N)},incoming:function(Q,O){var P=Q.indexOf("_"),N=parseInt(Q.substring(0,P),10);H+=Q.substring(P+1);if(N===0){if(J.encode){H=q(H)}K.up.incoming(H,O);H=""}},outgoing:function(R,O,Q){if(J.encode){R=a(R)}var N=[],P;if(G){while(R.length!==0){P=R.substring(0,G);R=R.substring(P.length);N.push(P)}}else{N.push(R)}while((P=N.shift())){F.push({data:N.length+"_"+P,origin:O,callback:N.length===0?Q:null})}I()},destroy:function(){L=true;K.down.destroy()}})};easyXDM.stack.VerifyBehavior=function(J){var K,I,G,H=false;function F(){I=Math.random().toString(16).substring(2);K.down.outgoing(I)}return(K={incoming:function(N,L){var M=N.indexOf("_");if(M===-1){if(N===I){K.up.callback(true)}else{if(!G){G=N;if(!J.initiate){F()}K.down.outgoing(N)}}}else{if(N.substring(0,M)===G){K.up.incoming(N.substring(M+1),L)}}},outgoing:function(N,L,M){K.down.outgoing(I+"_"+N,L,M)},callback:function(L){if(J.initiate){F()}}})};easyXDM.stack.RpcBehavior=function(L,G){var I,N=G.serializer||p();var M=0,K={};function F(O){O.jsonrpc="2.0";I.down.outgoing(N.stringify(O))}function J(O,Q){var P=Array.prototype.slice;return function(){var R=arguments.length,T,S={method:Q};if(R>0&&typeof arguments[R-1]==="function"){if(R>1&&typeof arguments[R-2]==="function"){T={success:arguments[R-2],error:arguments[R-1]};S.params=P.call(arguments,0,R-2)}else{T={success:arguments[R-1]};S.params=P.call(arguments,0,R-1)}K[""+(++M)]=T;S.id=M}else{S.params=P.call(arguments,0)}F(S)}}function H(O,Q,T,R){if(!T){if(Q){F({id:Q,error:{code:-32601,message:"Procedure not found."}})}return}var V=false,U,S;if(Q){U=function(X){if(V){return}V=true;F({id:Q,result:X})};S=function(X){if(V){return}V=true;F({id:Q,error:{code:-32099,message:"Application error: "+X}})}}else{U=S=v}try{var W=T.method.apply(T.scope,R.concat([U,S]));if(!e(W)){U(W)}}catch(P){S(P.message)}}return(I={incoming:function(P,O){var Q=N.parse(P);if(Q.method){if(G.handle){G.handle(Q,F)}else{H(Q.method,Q.id,G.local[Q.method],Q.params)}}else{var R=K[Q.id];if(Q.error){if(R.error){R.error(Q.error)}}else{if(R.success){R.success(Q.result)}}delete K[Q.id]}},init:function(){if(G.remote){for(var O in G.remote){if(G.remote.hasOwnProperty(O)){L[O]=J(G.remote[O],O)}}}I.down.init()},destroy:function(){for(var O in G.remote){if(G.remote.hasOwnProperty(O)&&L.hasOwnProperty(O)){delete L[O]}}I.down.destroy()}})}})(window,document,location,window.setTimeout,decodeURIComponent,encodeURIComponent);
