$(document).ready(function(){

//am.widgets = [];
am.energySpectrum = ["#999","#0db14b","#80C342",
                           "#BFD730","#FFF200","#FAA61A",
                           "#F15F22","#D72229"];



// This page provides left/right scroll arrows, similar to those on the Home Page.
var path = window.location.pathname;
var widgetSelector;


widgetSelector = new am.WidgetScroller({
      btnNext:'#carousel_next',
      btnPrev:'#carousel_prev'
});


if ( path == '/secure/appliances/appliances') {
      widgetSelector = new am.WidgetScroller({
    	  btnNext:'#appliances_carousel_next',
    	  btnPrev:'#appliances_carousel_prev',
    	  carouselName: '#appliances_carousel'
      });
}


// Hack to load swingometer at right time.
if (document.body.id == 'cur_page_energy_swingometer' ) {
  //am.Ajax.init();
  am.loadObject('Swingometer');
  // Hardcode the data to avoid irritaion with bad ajax calls.
  //am.SwingoCountryWidget.data =

}


/**
 * Hack to override this function so all it does is just show the action, rather than doing an AJAX call to the api.
 */
am.DemoDashboard = am.UIComponent.extend({
  init: function(){
    //log('demo dashboard init');
   },
  onOffRequest : function(theDiv, plugNum){
    var deviceID = $("#widget_appliances_device_id_"+plugNum).text();
    // Toggle the switch:
    var theState = $("#widget_appliances_on_off_message_"+plugNum).text();
    if (theState == 'on') {
       $("#widget_appliances_on_off_icon_"+plugNum)[0].className = "widget_appliances_off_button";
       theState = 'off';
    } else {
      $("#widget_appliances_on_off_icon_"+plugNum)[0].className = "widget_appliances_on_button";
       theState = 'on';
    }
    $("#widget_appliances_on_off_message_"+plugNum).text(theState);
    
  }
});

am.Dashboard = new am.DemoDashboard;

/**
 * Catch any paths we don't wont people following.
 */

  $('a').click(function(){

    // Paths to catch.
    var paths = [
      'system/manage',
      'adddevices'
    ];

    for (i in paths) {
      if (this.href.indexOf(paths[i]) != -1) {
       return showPopUp();
      }
    }

  });

  $('li.charts_option_item:not(.highlighted').click(function() {
    showPopUp();
  });


  function showPopUp() {
    var popup = $('#popup_buyalertme');
    popup.center().css({display:'block'});
    $(".popup_overlay").css({display: "block", height:getDocHeight()});
    return false;
  }


  $('.popup_close').click(function(){
    
    $('#popup_buyalertme').css({display:'none'});
    $(".popup_overlay").css({display: "none"});

  })


	// Detect flash on the graphing pages.
	var embedded = $('embed');
	if ( embedded.length > 0 ) {
		// Detect flash	
		flashVersion = infosoftglobal.FusionChartsUtil.getPlayerVersion();
		if ( flashVersion.major == 0) {
			embedded.parent('div').html('<br />This demo requires <a href="http://get.adobe.com/flashplayer/">Flash</a> to view charts.');
			$('div[id^="bill_prediction"], #bill_prediction_end_bubble').hide();
		}
	}



});



function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

