/*******************************************************************************
 * @license
 * Copyright (c) 2011, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials are made 
 * available under the terms of the Eclipse Public License v1.0 
 * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*******************************************************************************
 * @license
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials are made 
 * available under the terms of the Eclipse Public License v1.0 
 * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 
 * 
 * Contributors: IBM Corporation - initial API and implementation
 ******************************************************************************/
/*******************************************************************************
 * @license
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials are made 
 * available under the terms of the Eclipse Public License v1.0 
 * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*******************************************************************************
 * @license
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials are made 
 * available under the terms of the Eclipse Public License v1.0 
 * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 
 * 
 * Contributors: IBM Corporation - initial API and implementation
 ******************************************************************************/
/*******************************************************************************
 * @license
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials are made 
 * available under the terms of the Eclipse Public License v1.0 
 * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 
 * 
 * Contributors: IBM Corporation - initial API and implementation
 ******************************************************************************/
/*

 Copyright (c) 2011, 2012 IBM Corporation and others.
 All rights reserved. This program and the accompanying materials are made 
 available under the terms of the Eclipse Public License v1.0 
 (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 

 Contributors:
 IBM Corporation - initial API and implementation

 Copyright (c) 2012 IBM Corporation and others.
 All rights reserved. This program and the accompanying materials are made 
 available under the terms of the Eclipse Public License v1.0 
 (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 

 Contributors: IBM Corporation - initial API and implementation

 Copyright (c) 2012 IBM Corporation and others.
 All rights reserved. This program and the accompanying materials are made 
 available under the terms of the Eclipse Public License v1.0 
 (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 

 Contributors: IBM Corporation - initial API and implementation

 Copyright (c) 2012 IBM Corporation and others.
 All rights reserved. This program and the accompanying materials are made 
 available under the terms of the Eclipse Public License v1.0 
 (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 

 Contributors: IBM Corporation - initial API and implementation

 Copyright (c) 2012 IBM Corporation and others.
 All rights reserved. This program and the accompanying materials are made 
 available under the terms of the Eclipse Public License v1.0 
 (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 

 Contributors:
 IBM Corporation - initial API and implementation
*/
(function(){var j=this;if(!j.define)j.define=function(m){j.orion=j.orion||{};j.orion.PluginProvider=m();j.eclipse=j.orion;delete j.define}})();
define("orion/plugin",[],function(){return function(j){function m(a){p&&(typeof ArrayBuffer==="undefined"&&(a=JSON.stringify(a)),p===self?p.postMessage(a):p.postMessage(a,"*"))}function n(a,c){return c&&c instanceof XMLHttpRequest?{status:c.status,statusText:c.statusText}:c}function o(a){return function(c){e&&m({serviceId:a,method:"dispatchEvent",params:[c]})}}function i(a){if(a.source===p){var c=typeof a.data!=="string"?a.data:JSON.parse(a.data);if(typeof c.cancel==="string"){var b=l[c.id];b&&(delete l[c.id],
b.cancel&&b.cancel(c.cancel))}else{var a=c.serviceId,f=c.method,g=c.params,k=h[a],d=k.implementation,e=d[f];f==="addEventListener"?(f=g[0],k.listeners[f]=k.listeners[f]||o(a),g=[f,k.listeners[f]]):f==="removeEventListener"&&(f=g[0],g=[f,k.listeners[f]],delete k.listeners[f]);var q={id:c.id,result:null,error:null};try{(b=e.apply(d,g))&&typeof b.then==="function"?(l[c.id]=b,b.then(function(a){delete l[c.id];q.result=a;m(q)},function(a){if(l[c.id])delete l[c.id],q.error=a?JSON.parse(JSON.stringify(a,
n)):a,m(q)},function(){m({requestId:c.id,method:"progress",params:Array.prototype.slice.call(arguments)})})):(q.result=b,m(q))}catch(i){q.error=i?JSON.parse(JSON.stringify(i,n)):i,m(q)}}}}var d=j,h=[],e=!1,l={},p=null;this.updateHeaders=function(a){if(e)throw Error("Cannot update headers. Plugin Provider is connected");d=a};this.registerServiceProvider=this.registerService=function(a,c,b){if(e)throw Error("Cannot register service. Plugin Provider is connected");typeof a==="string"?a=[a]:Array.isArray(a)||
(a=[]);var f=null,g=[];for(f in c)typeof c[f]==="function"&&g.push(f);h[h.length]={names:a,methods:g,implementation:c,properties:b||{},listeners:{}}};this.connect=function(a,c){if(!e){if(typeof window==="undefined")p=self;else if(window!==window.parent)p=window.parent;else if(window.opener!==null)p=window.opener;else{c&&c("No valid plugin target");return}addEventListener("message",i,!1);for(var b=[],f=0;f<h.length;f++)b.push({serviceId:f,names:h[f].names,methods:h[f].methods,properties:h[f].properties});
m({method:"plugin",params:[{headers:d||{},services:b}]});e=!0}a&&a()};this.disconnect=function(){e&&(removeEventListener("message",i),p=null,e=!1)}}});
(function(j,m){typeof define==="function"&&define.amd?define("orion/Deferred",[],m):typeof exports==="object"?module.exports=m():(j.orion=j.orion||{},j.orion.Deferred=m())})(this,function(){function j(l){if(e)o?(i.next={fn:l,next:null},i=i.next):i=o={fn:l,next:null};else{e=!0;do{for(;l;){l();if(d)o?i.next=d:o=d,i=h;o?(d=o.next,h=i,l=o.fn,o=i=null):l=null}e=!1}while(e)}}function m(d){return function(){d.apply(null,arguments)}}function n(d){function h(){for(;f;){var a=f;f=f.next;var b=a.deferred,d=
e.isResolved()?"resolve":"reject";if(a[d])try{var k=a[d](c);k&&typeof k.then==="function"?(a.cancel=k.cancel,k.then(m(b.resolve),m(b.reject),b.progress)):b.resolve(k)}catch(i){b.reject(i)}else b[d](c)}f=g=null}function a(a){if(b){if(a)throw Error("already "+b);return!0}return!1}var c,b,f,g,k=!1,e=this;this.reject=function(g,d){a(d)||(b="rejected",c=g,f&&j(h));return e.promise};this.resolve=function(g,d){a(d)||(b="resolved",c=g,f&&j(h));return e.promise};this.progress=function(b,c){if(!a(c))for(var g=
f;g;)g.progress&&g.progress(b),g=g.next;return e.promise};this.cancel=function(b,c){if(!a(c))return k=!0,d&&(b=d(b)||b),b=b||Error("canceled"),j(e.reject.bind(e,b)),b;for(var g=f;g;){if(g.canceling)return delete g.canceling,h(),g.cancel&&g.cancel(b);g=g.next}};this.then=function(a,c,d){var k={resolve:a,reject:c,progress:d,cancel:this.cancel,deferred:new n(function(a){k.canceling=!0;return k.cancel&&k.cancel(a)})};f?g.next=k:f=k;g=k;b&&j(h);return k.deferred.promise};this.isResolved=function(){return b===
"resolved"};this.isRejected=function(){return b==="rejected"};this.isFulfilled=function(){return!!b};this.isCanceled=function(){return k};this.promise={then:this.then,cancel:this.cancel,isResolved:this.isResolved,isRejected:this.isRejected,isFulfilled:this.isFulfilled,isCanceled:this.isCanceled};this.notify=this.progress;this.state=function(){return b||"pending"};this.promise.state=this.state}var o,i,d,h,e=!1;n.all=function(d,e){function a(a,c){g.isFulfilled()||(f[a]=c,--b===0&&g.resolve(f))}function c(b,
c){if(!g.isFulfilled()){if(e)try{a(b,e(c));return}catch(f){c=f}g.reject(c)}}var b=d.length,f=[],g=new n(function(a){d.forEach(function(b){b.cancel&&b.cancel(a)})});b===0?g.resolve(f):d.forEach(function(b,f){b.then(a.bind(null,f),c.bind(null,f))});return g.promise};n.when=function(d,e,a,c){var b;if(!(d&&typeof d.then==="function"))b=new n,b.resolve(d),d=b.promise;return d.then(e,a,c)};return n});
define("orion/xhr",["orion/Deferred"],function(j){function m(j){return encodeURIComponent(j).replace(/[!'()*]/g,function(i){return"%"+i.charCodeAt(0).toString(16).toUpperCase()})}function n(j,i,d,h){var e=typeof d.response!=="undefined"?d.response:d.responseText,j={args:i,response:e,responseText:typeof e==="string"?e:null,status:d.status,url:j,xhr:d};if(typeof h!=="undefined")j.error=h;return j}return function(o,i,d){var d=d||{},h=arguments.length>3&&arguments[3]?arguments[3]:new XMLHttpRequest,e=
new j,l=d.headers||{},p=d.log||!1,a,c;typeof l["X-Requested-With"]==="undefined"&&(l["X-Requested-With"]="XMLHttpRequest");if(d.query&&typeof d.query==="object"){var b=d.query,f=Object.keys(b),g=[];for(c=0;c<f.length;c++){var k=f[c],s=b[k];g.push(m(k)+"="+m(s))}g.length&&(b=i.split("#"),b[0]+=(b[0].indexOf("?")===-1?"?":"&")+g.join("&"),i=b.join("#"))}if(typeof d.data!=="undefined"&&(o==="POST"||o==="PUT"))a=d.data;if(typeof d.responseType==="string")h.responseType=d.responseType;h.onreadystatechange=
function(){if(h.readyState===4){var a=n(i,d,h),b=h.status;200<=b&&b<400?e.resolve(a):(p&&typeof console!=="undefined"&&console.log(Error(h.statusText)),e.reject(a))}};try{h.open(o,i,!0);if(typeof d.timeout==="number")typeof h.timeout==="number"?(h.timeout=d.timeout,h.addEventListener("timeout",function(){e.reject(n(i,d,h,"Timeout exceeded"))})):setTimeout(function(){e.state()!=="resolved"&&e.state()!=="rejected"&&e.reject(n(i,d,h,"Timeout exceeded"))},d.timeout);var r=Object.keys(l);for(c=0;c<r.length;c++){var q=
r[c];h.setRequestHeader(q,l[q])}h.send(a)}catch(t){e.reject(n(i,d,h,t))}return e}});
define("plugins/site/siteServiceImpl",["require","orion/xhr"],function(j,m){function n(a){var c=document.createElement("a");c.href=a;return c.href}function o(){var a=j.toUrl("._"),a=n(a);return a.substring(0,a.length-2)}function i(a){return a.indexOf(":")!==-1?a.substring(a.indexOf(window.location.host)+window.location.host.length):a}function d(a,c){for(var b,f=a.split("/"),g=f[1],d=0;d<c.length;d++){var e=c[d];if(e.Id===g){f[1]=e.Name;b=f.join("/");break}}return b}function h(a,c,b){if(b&&typeof b.data!==
"undefined")b.data=JSON.stringify(b.data);return m.apply(null,Array.prototype.slice.call(arguments)).then(function(a){return JSON.parse(a.response||null)})}function e(a){this.projects={};this.getProjects=function(c){var b={"Orion-Version":"1"};this.projects[c]||(this.projects[c]=h("GET",a,{headers:b}).then(function(a){for(var a=a.Workspaces,d,e=0;e<a.length;e++)if(d=a[e],d.Id===c)break;return h("GET",d.Location,{headers:b}).then(function(a){return a.Children||[]})}));return this.projects[c]}}function l(a,
c){var b="http://localhost:"+c+i(o());return[["/",a+"/bundles/org.eclipse.orion.client.core/web/index.html"],["/",a+"/bundles/org.eclipse.orion.client.core/web"],["/",a+"/bundles/org.eclipse.orion.client.editor/web"],["/org.dojotoolkit/dojo",a+"/bundles/org.eclipse.orion.client.core/web/dojo"],["/org.dojotoolkit/dojox",a+"/bundles/org.eclipse.orion.client.core/web/dojox"],["/file",b+"file"],["/prefs",b+"prefs"],["/workspace",b+"workspace"],["/org.dojotoolkit",b+"org.dojotoolkit"],["/users",b+"users"],
["/authenticationPlugin.html",b+"authenticationPlugin.html"],["/login",b+"login"],["/loginstatic",b+"loginstatic"],["/useremailconfirmation",b+"useremailconfirmation"],["/site",b+"site"],["/",a+"/bundles/org.eclipse.orion.client.git/web"],["/gitapi",b+"gitapi"],["/",a+"/bundles/org.eclipse.orion.client.users/web"],["/xfer",b+"xfer"],["/filesearch",b+"filesearch"],["/index.jsp",b+"index.jsp"],["/plugins/git",b+"plugins/git"],["/plugins/user",b+"plugins/user"],["/logout",b+"logout"],["/mixlogin/manageopenids",
b+"mixlogin/manageopenids"],["/openids",b+"openids"],["/task",b+"task"],["/help",b+"help"]].map(function(a){return{Source:a[0],Target:a[1]}})}function p(a,c){this.filePrefix=a;this.cache=new e(c)}p.prototype={getSiteConfigurations:function(){var a=j.toUrl("site._"),a=a.substring(0,a.length-2);return h("GET",a,{headers:{"Orion-Version":"1"},timeout:15E3}).then(function(a){return a.SiteConfigurations})},loadSiteConfiguration:function(a){return h("GET",a,{headers:{"Orion-Version":"1"},timeout:15E3})},
createSiteConfiguration:function(a,c,b,f,d){a={Name:a,Workspace:c,HostHint:a.replace(/ /g,"-").replace(/[^A-Za-z0-9-_]/g,"").toLowerCase()};if(b)a.Mappings=b;if(f)a.HostHint=f;if(d)a.HostingStatus=d;b=j.toUrl("site._");b=b.substring(0,b.length-2);return h("POST",b,{data:a,headers:{"Content-Type":"application/json; charset=utf-8","Orion-Version":"1"},timeout:15E3})},updateSiteConfiguration:function(a,c){return h("PUT",a,{data:c,headers:{"Content-Type":"application/json; charset=utf-8","Orion-Version":"1"},
timeout:15E3})},deleteSiteConfiguration:function(a){return h("DELETE",a,{headers:{"Orion-Version":"1"},timeout:15E3})},toInternalForm:function(a){var c=i(this.filePrefix),a=i(a),b;a.indexOf(c)===0&&(b=a.substring(c.length));b[b.length-1]==="/"&&(b=b.substring(0,b.length-1));return b},toFileLocation:function(a){var c=j.toUrl(this.filePrefix+a+"._"),c=c.substring(0,c.length-2);(function(a){return a.filter(function(a){return a!==""})})(a.split("/")).length===1&&(c+="/");return i(n(c))},getMappingObject:function(a,
c,b){var f=this.toInternalForm(c);return this.cache.getProjects(a.Workspace).then(function(a){a=d(f,a);return{Source:b,Target:f,FriendlyPath:a||b}})},getMappingProposals:function(a){var c=this;return this.cache.getProjects(a.Workspace).then(function(a){return a.map(function(a){return{Source:"/"+a.Name,Target:c.toInternalForm(a.Location),FriendlyPath:"/"+a.Name}})})},updateMappingsDisplayStrings:function(a){return this.cache.getProjects(a.Workspace).then(function(c){for(var b=a.Mappings,f=0;f<b.length;f++){var g=
b[f];if(/^\//.test(g.Target))g.FriendlyPath=d(g.Target,c)}return a})},parseInternalForm:function(a,c){return/^\//.test(c)?this.cache.getProjects(a.Workspace).then(function(a){for(var d=c.split("/"),g=0;g<a.length;g++){var e=a[g];if(d[1]===e.Name)return d[1]=e.Id,d.join("/")}}):null},isSelfHostingSite:function(a){var c=this;return this.cache.getProjects(a.Workspace).then(function(b){return b.some(function(b){for(var b=c.toInternalForm(b.Location),b=l(b),d=0;d<b.length;d++){var e;a:{e=a.Mappings;for(var h=
b[d],i=0;i<e.length;i++){var j=e[i];if(j.Source===h.Source||j.Target===h.Target){e=!0;break a}}e=!1}if(!e)return!1}return!0})})},convertToSelfHosting:function(a,c,b){c=this.toInternalForm(c);b=l(c,b);a.Mappings=b;return a},getURLOnSite:function(a,c){var b=a.Mappings,d=this.toInternalForm(c.Location);if(!b)return null;for(var e=0;e<b.length;e++){var h=b[e];if(h.Target===d)return a.HostingStatus.URL+(h.Source[0]!=="/"?"/":"")+h.Source+(c.Directory?"/":"")}return null}};return{SiteImpl:p}});
define("plugins/site/sitePlugin",["orion/plugin","plugins/site/siteServiceImpl"],function(j,m){function n(d){var a=document.createElement("a");a.href=d;d=a.href;try{if(window.location.host===parent.location.host&&window.location.protocol===parent.location.protocol)return d.substring(parent.location.href.indexOf(parent.location.host)+parent.location.host.length)}catch(c){}return d}function o(d){return[{source:"Location|Directory"},{source:"Location",match:"^"+d}]}var i=n("../../site"),d=n("../../file"),
h=n("../../workspace"),e=document.createElement("a");e.href="../../mixloginstatic/LoginWindow.html";e=new j({name:"Orion Site Service",version:"1.0",description:"This plugin provides virtual site support for client web applications in your Orion workspace.",login:e.href});e.registerService("orion.navigate.command",null,{id:"orion.site.viewon",nameKey:"View on Site",tooltipKey:"View this file or folder on a web site hosted by Orion",nls:"orion/nls/messages",forceSingleItem:!0,validationProperties:o(d),
uriTemplate:"{OrionHome}/sites/view.html#,file={Location}"});e.registerService("orion.page.link.related",null,{id:"orion.site.viewon",nameKey:"View on Site",tooltipKey:"View this file or folder on a web site hosted by Orion",nls:"orion/nls/messages",validationProperties:o(d),uriTemplate:"{OrionHome}/sites/view.html#,file={Location}"});var l=document.createElement("a");l.href="/";e.registerService("orion.site",new m.SiteImpl(d,h),{id:"orion.site.default",name:""+l.hostname+" Orion file system",canSelfHost:!0,
pattern:i,filePattern:d});e.connect()});