// Copyright 2012 YDN Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Base utilities for YDN-UI module.
 *
 * @author kyawtun@yathit.com (Kyaw Tun)
 */

goog.provide('ydn.ui');


/**
 * @type {Document} default template document
 * @private
 */
ydn.ui.template_doc_ = null;


/**
 * @type {?string} default template document URL.
 * @private
 */
ydn.ui.template_url_ = null;


/**
 * Set default template document.
 * @param {Document|string} doc Document or URL of the document to load. Note:
 * Document will be load synchronously.
 */
ydn.ui.setTemplateDocument = function(doc) {
  if (goog.isString(doc)) {
    ydn.ui.template_url_ = doc;
  } else {
    ydn.ui.template_doc_ = doc;
  }
};


/**
 * Load template synchronously.
 * @return {Document}
 * @private
 */
ydn.ui.getTemplateDocument_ = function() {
  if (!ydn.ui.template_doc_) {
    if (goog.DEBUG && !ydn.ui.template_url_) {
      throw new Error('default template document not set.');
    }
    var xhr = new XMLHttpRequest();
    xhr.open('GET', ydn.ui.template_url_, false);
    xhr.onload = function() {
      var parser = new DOMParser();
      ydn.ui.template_doc_ = parser.parseFromString(xhr.responseText, 'text/html');
      xhr = null;
    };
    xhr.send();
  }
  return ydn.ui.template_doc_;
};


/**
 * @param {string} id get template element by id from template document.
 * @param {Document=} opt_doc template document. Default to default template
 * document.
 * @return {Element}
 */
ydn.ui.getTemplateById = function(id, opt_doc) {
  var doc = opt_doc || ydn.ui.getTemplateDocument_();
  var el = doc.documentElement.querySelector('#' + id);
  if (!document.body.contains(el)) {
    el = document.importNode(el, true);
    document.body.appendChild(el);
  }
  return /** @type {Element} */ (el);
};


/**
 * Instead of creating a new tab, open like a dialog box.
 * @param {Event} e
 */
ydn.ui.openPageAsDialog = function(e) {
  e.preventDefault();
  var w = 200;
  var h = 100;
  var wh = e.target.getAttribute('data-window-height');
  var ww = e.target.getAttribute('data-window-width');
  if (ww) {
    w = parseInt(ww, 10);
  }
  if (wh) {
    h = parseInt(wh, 10);
  }
  // dual monitor solution
  var left = (window.innerWidth / 2) - (w / 2) + window.screenLeft;
  var top = (window.innerWidth / 2) - (h / 2) + window.screenTop;
  var url = e.target.href;
  window.open(url, undefined, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
};

@@ -2071,16 +2071,71 @@
 _doc) %7B%0A
+  var el = document.getElementById(id);%0A  if (!el) %7B%0A  
   var do
@@ -2182,19 +2182,17 @@
 t_();%0A  
+ 
-var
  el = do
@@ -2230,24 +2230,28 @@
 ('#' + id);%0A
+  %7D%0A
   if (!docum
@@ -2555,17 +2555,17 @@
 var w = 
-2
+6
 00;%0A  va
@@ -2570,17 +2570,17 @@
 var h = 
-1
+4
 00;%0A  va

update jstd config