/* Global URI class */
var URI = function (g) { var d = null; var i = null; var j = null; var k = null; var e = null; var l = null; var h = null; var m = null; var n = {}; var o = { ftp: 21, ssh: 22, telnet: 23, smtp: 25, http: 80, https: 443 }; var p = encodeURIComponent; var s = decodeURIComponent; var u = new RegExp("(?:([-+a-z0-9]+)://((.[^:]*):?(.*)?@)?(.[^:/]*):?([0-9]{1,6})?)?([/.].[^?#]*)?([?](.[^#]*))?#?(.*)?", "i"); var t = function (a) { g = a; var b; if (g && (b = u.exec(g))) { d = b[1] && b[1].toLowerCase(); j = b[3]; k = b[4]; i = b[5] && b[5].toLowerCase(); e = b[6] && parseInt(b[6], 10); l = b[7]; h = b[9]; m = b[10]; if (!e && d) e = o[d]; if (h && h.length) { var c = h.split('&'); for (var q = 0; q < c.length; q++) { var r = c[q].split('='); n[s(r[0])] = r.length > 1 ? s(r[1]) : null } } } }; var v = function () { if (!f.port) return true; for (var a in o) { if (a == f.scheme && f.port != o[f.scheme]) return false } return true }; t(g); var f; return f = { scheme: d, host: i, username: j, password: k, port: e, path: l, fragment: m, variables: n, parse: function (a) { t(a); this.scheme = d; this.host = i; this.username = j; this.password = k; this.port = e; this.path = l; this.fragment = m; this.variables = n }, queryString: function () { var a = []; for (var b in this.variables) { var c; if (this.variables[b] != null) c = p(b) + '=' + p(this.variables[b]); else c = p(b); a.push(c) } return a.length && a.join('&') || null }, toString: function () { var a = "", b = null; if (this.scheme) a = this.scheme + "://"; if (this.username) a += this.username; if (this.username && this.password) a += ":"; if (this.password) a += this.password; if (this.username) a += "@"; if (this.host) a += this.host; if (!v()) a += ":" + this.port; if (this.path) a += this.path; if (b = this.queryString()) a += "?" + b; if (this.fragment) a += "#" + this.fragment; return a } } };
var uri = new URI(window.location);

/* Visa Helper Functions 
------------------------------*/

function flashToVisaEventTracking(category, action, label) {
   _gaq.push(["_trackEvent", category, action, label]);
}
function flashToVisaPageTracking(url) {
   _gaq.push(["_trackPageview", url]);
}

function PopSbEmail(URL) {
   window.name = "main";
   Popwin = window.open(URL, "VisaG", "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=no,width=645,height=580");
}


/* Popup SME/PME email */
function SendArticleEmail(link) {
   var mail_content = "mailto:?subject=Article%20from%20Visa.ca&body=" + link;
   var postman = window.open(mail_content, "envelope");
   if (postman && postman.open && !postman.closed) {
      postman.close();
   }

}

function ArticlePrintPreview(doc_body) {
   window.name = "main";
   var PrintWin = window.open("", "VisaArticlePrint", "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=yes,resizable=no,width=612,height=700");
   PrintWin.document.write('<!DOCTYPE html><html style="margin: 10px; width: 575px;"><head><link type="text/css" rel="stylesheet" href="/themes/visa.global.css" /><link type="text/css" rel="stylesheet" href="/themes/visa.level.css" /><link type="text/css" rel="stylesheet" href="/themes/visa.print.css" /><script type="text/javascript" src="/scripts/modernizr-1.7.min.js"></script></head><body>');
   PrintWin.document.write( '<header style="margin-bottom: 30px; height: 50px; display: block;"><div class="vui-logo vui-logo-header" id="header-logo" style="float: none; position: absolute; top: 10px; left: 10px; margin: 0;"><span>Visa</span></div><!--<a class="vui-icon vui-icon-article-print" style="position: absolute; right: 10; top: 10;">Print</a>--></header>');
   PrintWin.document.write( '<section id="article">' + doc_body.toString() + '</section>');
   PrintWin.document.write('</body></html>');
}

/* Setup 
------------------------------*/
jQuery(document).ready(function () {

   /** Tracking **/
   var options = {
      allowHash: true,
      allowLocalhost: false,
      cookiePath: "/",
      debug: false,
      trackOwnDomain: true,
      ua: "UA-8460445-16"
   }
   /* Retrieve all anchors on the page and hook up  Visa Tracking */
   $("a").tbwaga(options);

   /** Search Form **/
   var $form_input = $("form input#keywords");
   var dfi_en = "search";
   var dfi_fr = "recherche";
   $form_input.focusin(function () {
      if (this.value == dfi_en || this.value == dfi_fr) {
         this.value = "";
      }
   }).focusout(function () {
      if (this.value.length <= 0) {
         this.value = $("html").attr("lang") == "fr" ? dfi_fr : dfi_en;
      }
   });

   // retrieve the nav
   var $nav = $("nav#page-content-navigation");
   // retrieve all lists (ul's) after nav's.
   var $all_lists = $nav.find("ul").first().find("ul");
   // retrieve the list-item we are on
   var path = uri.path;
   var parts = path.substring(1).split("/");
   var $page = $nav.find("a[href$='" + path + "']");
   if ($page.length == 0 && parts[parts.length - 1] == "") {
      // no page found, try with index if no file is supplied
      path = path + "index.jsp";
      $page = $nav.find("a[href$='" + path + "']");
   }
   if ($page.length == 0) {
      // no page found, try with no file
      path = uri.path.substring(1, uri.path.lastIndexOf("/") + 1);
      $page = $nav.find("a[href$='" + path + "']");
   }

   // retrieve uri list sequence
   var $page_hierarchy = $page.parents("li");
   // retrieve uri category list-item
   var $page_category = $page_hierarchy.last();
   // retrieve uri list-item anchors
   var $page_anchors = $page_hierarchy.find("a:first");
   // retrieve all list-sequence children lists
   var $page_children = $page_hierarchy.find("ul:first");

   // hide all lists(ul's) that are not in our list-sequence
   $all_lists.not($page_children).css("display", "none");
   // highlight category area
   $page_category.addClass("active");
   // highlight uri level sequence
   $page_anchors.addClass("selected");
   // show the nav
   //TODO: find a way around the flicker/push-left after nav appears.
   $nav.css("display", "");



   /** Find email/print toolbar and hook up **/
   $("#article a.vui-icon-article-email").click(function () {
      var link = $(this).attr("rel");
      SendArticleEmail(link);
      return false;
   });
   $("#article a.vui-icon-article-print").click(function () {
      var $document = $("#article").clone();
      $document.find("menu").remove();
      ArticlePrintPreview($document.html());
      return false;
   });
});




