/// <reference path="jquery_1.5.2_vsdoc.js" />
var domainName = document.domain, trackInfo;
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24284963-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);

(function () {
  var PITMga = document.createElement('script'); PITMga.type = 'text/javascript'; PITMga.async = true;
  PITMga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(PITMga, s);
  
  var baseURL = 'http://www.buildwithpropane.com/Dashboard';
  var jQuery, currentSpecs;
  var doc = (document.getElementsByTagName("head")[0] || document.documentElement);
  if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.6.2') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type", "text/javascript");
    script_tag.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");
    var completed = false;
    script_tag.onload = script_tag.onreadystatechange = function () {
      if (!completed && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
        completed = true; 
        jQuery = window.jQuery.noConflict(true);
        main();
      }
    };
    doc.appendChild(script_tag);
  }
  else {
    jQuery = window.jQuery;
    main();
  }
  function main() {
    var css_link = jQuery("<link />", { rel: "stylesheet", type: "text/css", href: baseURL + "/site.css" });
    css_link.appendTo('head');
    var iPages, iCurrent = 0, m_specs, overlay = jQuery('<div id="PITMoverlay"></div>'),
        toolContainer = jQuery('<div id="PITMToolContainer"><div id="PITMToolHeader"><a href="#">Close</a></div><div id="PITMToolBody"><p id="PITMToolBodyCopy"></p><iframe id="PITMTool" frameborder="0" border="0" cellpadding="0"></iframe></div></div>'),
        toolHeader = toolContainer.children('#PITMToolHeader'),
        toolCloseBtn = toolHeader.children('a'),
        toolBody = toolContainer.children('#PITMToolBody'),
        toolCopy = toolBody.children('#PITMToolBodyCopy'),
        tool = toolBody.children('#PITMTool');
    window.onresize = function () { resizeOverlay(); }
    jQuery(document).ready(function () {
      var build = "All";
      if (typeof PITMBuild != "undefined" && PITMBuild != "") build = PITMBuild;
      trackInfo = domainName + "/" + build;
      _gaq.push(['_trackPageview', trackInfo]);
      var PITM = jQuery('#PITM');
      PITM.html('<div id="PITMLoadingImg"><img src="' + baseURL + '/Images/loadingSmall.gif" /></div>');
      var jsonp_url = baseURL + "/service.svc/GetDashboardData?callback=?";
      jQuery.getJSON(
        jsonp_url,
        { build: build },
        function (data) {
          m_specs = data.Specs;
          PITM.html(data.html);
          var parentWidth = PITM.parent().width(), wid = PITM.width();
          if (wid > parentWidth) wid = parentWidth;
          if (wid > 900) wid = 900;
          var percent = wid / 450, content = jQuery('#PITMContainer');
          var contentScaled = content.height() * percent;
          PITM.css({ 'height': contentScaled + 'px' });
          if (jQuery.browser.msie) content.css({ 'zoom': percent });
          else content.css({ '-moz-transform': 'scale(' + percent + ')', '-webkit-transform': 'scale(' + percent + ')', '-o-transform': 'scale(' + percent + ')', 'transform': 'scale(' + percent + ')' });
          iPages = data.pages;
          InitDashboard();
        });
    });
    function InitDashboard() {
      toolCloseBtn.click(function (e) { e.preventDefault(); HideOverlay(); });
      overlay.click(HideOverlay);
      jQuery('body').append(overlay);
      jQuery('body').append(toolContainer);
      jQuery('#PITMNavButtons').find('a').each(function () { jQuery(this).click(function (e) { e.preventDefault(); SwitchTo(jQuery(this)); }); });
      if (iPages > 1) {
        jQuery('#PreviousNav').click(function (e) { e.preventDefault(); _gaq.push(['_trackPageview', trackInfo + "/Pagination/PreviousClicked"]); if (iCurrent <= 0) iCurrent = iPages - 1; else iCurrent--; GoToNavPage(); });
        jQuery('#NextNav').click(function (e) { e.preventDefault(); _gaq.push(['_trackPageview', trackInfo + "/Pagination/NextClicked"]); if (iCurrent == iPages - 1) iCurrent = 0; else iCurrent++; GoToNavPage(); });
        jQuery('#PITMPagination').find('a').each(function (index) { jQuery(this).click(function (e) { e.preventDefault(); if (iCurrent == index) return; iCurrent = index; GoToNavPage(); }); });
      }
      jQuery('.PITMItem').find('a').each(function (index) { 
        var target = jQuery(this).attr("target");
        if (!target || target == "") jQuery(this).click(function (e) { e.preventDefault(); currentSpecs = m_specs[index]; ShowItem(); }) 
      });
      if (jQuery.isFunction(window.DashboardLoaded)) DashboardLoaded();
    }
    function HideOverlay(){
      overlay.hide();
      toolContainer.hide();
      tool.attr('src', '');
    }
    function SwitchTo(button) {
      var layer = button.data('layer');
      _gaq.push(['_trackPageview', trackInfo + "/" + layer.replace('PITM', '')]);
      jQuery('#PITMBody').children().hide();
      jQuery('#' + layer).show();
      jQuery('#PITMNavButtons a.NavOn').removeClass('NavOn').addClass('NavOff');
      button.removeClass('NavOff').addClass('NavOn');
    }
    function GoToNavPage() {
      jQuery('#PITMNavButtons').animate({ left: (-406 * iCurrent) + 'px' }, 750);
      jQuery('#PITMPagination').find('a').each(function (index) { if (index == iCurrent) jQuery(this).children('img').removeClass('ButtonOff'); else jQuery(this).children('img').addClass('ButtonOff'); });
    }
    function ShowItem() {
      if (currentSpecs.srcLinkTo == "") {
        var w = currentSpecs.width + 40;
        tool.attr('src', currentSpecs.src + "?domain=" + domainName).attr('width', currentSpecs.width).attr('height', currentSpecs.height);
        toolHeader.css('width', w);
        toolBody.css('width', w);
        toolCopy.html(currentSpecs.copy);
        resizeOverlay();
        overlay.show();
        toolContainer.show();
      }
      else {
        _gaq.push(['_trackEvent', currentSpecs.desc, trackInfo]);
        window.open(currentSpecs.srcLinkTo);
      }
    }
    function resizeOverlay() {
      if (currentSpecs) {
        var win = jQuery(window), doc = jQuery(document), scrollT = jQuery(window).scrollTop();
        var winHeight = win.height(), winWidth = win.width();
        var docHeight = doc.height(), docWidth = doc.width();
        var toolHeight = toolContainer.height() + 10;
        if (toolHeight + scrollT > docHeight) docHeight = scrollT + toolHeight;
        overlay.height(docHeight + 'px');
        var w = currentSpecs.width + 40;
        var midX = ((winWidth - w) / 2);
        if (midX < 0) midX = 0;
        var midY = ((winHeight - toolHeight) / 2);
        if (midY < 0) midY = 0;
        midY = scrollT + midY;
        toolContainer.css({ left: midX + 'px', top: midY + 'px' });
      }
    }
  }
})();

//   <Tool Name > | <Site URL> | <Tool Interaction 1> | <Tool Interaction 2> | <Tool Interaction 3>
//   Structure will provide us:
//       A high-level overview of the most used tools
//       Within each tool we will be able to quickly identify who is using the tool
