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