﻿if (!window.HomepageBanner)
	HomepageBanner = {};

var Mastercontrol;
var SelectorButtons;
var timeId = null;
var buttonClicked = null;
var TrafficMode = null;
var WeatherMode = null;
var GasMode = null;
   
        
HomepageBanner.Page = function() 
{
}

HomepageBanner.Page.prototype =
{
    handleLoad: function(control, userContext, rootElement) {
        Mastercontrol = control;
        this.control = control;
        //this.fonts = null;
        this.bannerImages = null;
        
		this.control.settings.enableFramerateCounter = false;
		
		var multiDownLoader = new Aarf.Silverlight.MultiDownLoader(this.control);
		//var inputArray = ["fonts/Segoe.zip","images/Home/BannerImages.zip"];
		var inputArray = ["images/Home/BannerImages.zip"];
        TrafficMode = new HomepageBanner.pageMode(this.control, "Traffic");
        GasMode = new HomepageBanner.pageMode(this.control, "Gas");
        WeatherMode = new HomepageBanner.pageMode(this.control, "Weather");
        
        multiDownLoader.download(inputArray, Silverlight.createDelegate(this, this.onInitialDownloadsComplete));
        
        this.control.content.findName("Wipedown").Begin();
        TrafficMode.Button.turnOn();
        buttonClicked = "One";
        this.control.content.findName("WipeOff").Begin();
        timeId = window.setInterval("StartTimer()",20000);
    },
    
    onInitialDownloadsComplete: function(multiDownloader, responseArray)
    {
        // get items from multidownloader
        for (var i = 0; i < responseArray.length; i++)
        {
            switch (responseArray[i].sender.uri)
            {
                case "fonts/Segoe.zip" :
                    this.fonts=responseArray[i].sender;
                    break;
                case "images/Home/BannerImages.zip" :
                    this.bannerImages=responseArray[i].sender;
                    break;
            }
        }
        
        this.bindData(this);
    },
    
    bindData: function(page)
    {
        this.SetFonts(TrafficMode,page,"Traffic");
        this.SetFonts(GasMode,page,"Gas");
        this.SetFonts(WeatherMode,page,"Weather");
       
        TrafficMode.Banner.setSource(page.bannerImages, 'banner1.jpg');
        GasMode.Banner.setSource(page.bannerImages, 'banner2.jpg');
        WeatherMode.Banner.setSource(page.bannerImages, 'banner3.jpg');
    },
    
    SetFonts: function(AppMode, page, modeName){
        AppMode.UpperText.Text = "\" " + AppMode.UpperText.Text;
        AppMode.LowerText2.Text = AppMode.LowerText2.Text + " \"";
//        if (modeName != "Traffic"){
//            AppMode.LowerText2.Text = AppMode.LowerText2.Text + " \"";
//        }
//        else{
//            AppMode.LowerText1.Text =  AppMode.LowerText1.Text + " \"";
//        }
    }
}

// Start the timer
function StartTimer(){
       this.control = Mastercontrol;
       this.TrafficMode = TrafficMode;
       this.GasMode = GasMode;
       this.WeatherMode = WeatherMode;
       
       if(TrafficMode.BannerText.Visibility == "Visible"){
            SetPageMode("Gas");
       }
       else if(GasMode.BannerText.Visibility == "Visible"){
            SetPageMode("Weather");
       }
       else if(WeatherMode.BannerText.Visibility == "Visible"){
            SetPageMode("Traffic");
       }

       this.control.content.findName("Wipedown").Begin();
       this.control.content.findName("WipeOff").Begin();
    }

function SetPageMode(mode) {
        if(mode == "Traffic"){
            buttonClicked = "One";
            TrafficMode.turnOn();
            WeatherMode.turnOff();
            GasMode.turnOff(); 
        }
        else if(mode == "Gas"){
            buttonClicked = "Two";
            TrafficMode.turnOff();
            GasMode.turnOn();
            WeatherMode.turnOff();
            
        }
        else if(mode == "Weather"){
            buttonClicked = "Three";
            TrafficMode.turnOff();
            GasMode.turnOff(); 
            WeatherMode.turnOn();
             
        }
    }

//Master object for each mode
HomepageBanner.pageMode = function(control, modeName){
    this.control = control;
    if (modeName == "Traffic"){
        this.Banner = this.control.content.findName("TrafficBanner");
        this.BannerText = this.control.content.findName("TrafficText");
        this.BannerUpperText = this.control.content.findName("TrafficUpperText");
        this.UpperText = this.control.content.findName("TrafficUpperText1");
        this.LowerText1 = this.control.content.findName("TrafficLowerText1");
        this.LowerText2 = this.control.content.findName("TrafficLowerText3");
        this.MSNDirectText1 = this.control.content.findName("TrafficMSNDirectTM1");
        this.MSNDirectText2 = this.control.content.findName("TrafficMSNDirectTM2");
        this.MSNDirectText3 = this.control.content.findName("TrafficMSNDirectTM3");
        this.Link = new HomepageBanner.Link(this.control,"txtTraffic","arrowlnkTraffic","arrowlnkTrafficHover");
        this.DeviceLink = new HomepageBanner.Link(this.control,"txtTrafficDevices","arrowlnkTrafficDevices","arrowlnkTrafficDevicesHover");
        this.TrafficRDSLink = new HomepageBanner.Link(this.control,"txtTrafficRDSDevices","arrowlnktxtTrafficRDSDevices","arrowlnktxtTrafficRDSDevicesHover");
        this.Button = new HomepageBanner.SelectorButton(this.control, "One", "innerOne", "outerOne");
    }
    else if (modeName == "Gas"){
        this.Banner = this.control.content.findName("GasBanner");
        this.BannerText = this.control.content.findName("GasText");
        this.BannerUpperText = this.control.content.findName("GasUpperText");
        this.UpperText = this.control.content.findName("GasUpperText1");
        this.MSNDirectText1 = this.control.content.findName("GasMSNDirectTM1");
        this.MSNDirectText2 = this.control.content.findName("GasMSNDirectTM2");
        this.MSNDirectText3 = this.control.content.findName("GasMSNDirectTM3");
        this.LowerText1 = this.control.content.findName("GasLowerText1");
        this.LowerText2 = this.control.content.findName("GasLowerText2");
        this.Link = new HomepageBanner.Link(this.control,"txtGas","arrowlnkGas","arrowlnkGasHover");
        this.DeviceLink = new HomepageBanner.Link(this.control,"txtGasDevices","arrowlnkGasDevices","arrowlnkGasDevicesHover")
        this.Button = new HomepageBanner.SelectorButton(this.control, "Two", "innerTwo", "outerTwo");
    }
    else if (modeName == "Weather"){
        this.Banner = this.control.content.findName("WeatherBanner");
        this.BannerText = this.control.content.findName("WeatherText");
        this.BannerUpperText = this.control.content.findName("WeatherUpperText");
        this.UpperText = this.control.content.findName("WeatherUpperText1");
        this.MSNDirectText1 = this.control.content.findName("WeatherMSNDirectTM1");
        this.MSNDirectText2 = this.control.content.findName("WeatherMSNDirectTM2");
        this.MSNDirectText3 = this.control.content.findName("WeatherMSNDirectTM3");
        this.LowerText1 = this.control.content.findName("WeatherLowerText1");
        this.LowerText2 = this.control.content.findName("WeatherLowerText2");
        this.Link = new HomepageBanner.Link(this.control,"txtWeather","arrowlnkWeather","arrowlnkWeatherHover");
        this.DeviceLink = new HomepageBanner.Link(this.control,"txtWeatherDevices","arrowlnkWeatherDevices","arrowlnkWeatherDevicesHover");
        this.Button = new HomepageBanner.SelectorButton(this.control, "Three", "innerThree", "outerThree");
    }
}
HomepageBanner.pageMode.prototype = {
    turnOn:function() {
        this.Banner.Visibility = "Visible";
        this.BannerText.Visibility = "Visible";
        this.BannerUpperText.Visibility = "Visible";
        this.Button.turnOn();
        this.Link.turnOn();
    },
    turnOff: function() {
        this.Banner.Visibility = "Collapsed";
        this.BannerText.Visibility = "Collapsed";
        this.BannerUpperText.Visibility = "Collapsed";
        this.Button.turnOff();
        this.Link.turnOff();
    }
}

HomepageBanner.SelectorButton = function(control, buttonName, innerName, outerName) {
    this.control = control;
    this.buttonName = buttonName;
    this.button = this.control.content.findName(buttonName);
    this.innerCanvas = this.control.content.findName(innerName);
    this.outerCanvas = this.control.content.findName(outerName);
    this.isSelected = false;
    this.clickListener = null;

    this.outerCanvas.AddEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
    this.outerCanvas.AddEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
    this.button.AddEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
    this.outerCanvas.AddEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
}
HomepageBanner.SelectorButton.prototype = {
    handleMouseEnter: function(sender, eventArgs) {
        this.outerCanvas.Cursor = "Hand";
        if(this.buttonName != buttonClicked){
            this.innerCanvas.Background="#FFff9500";
            this.innerCanvas.Opacity="0.5";
            this.outerCanvas.Background="#FFFFFFFF";
            this.button.Foreground="#FFFFFFFF";
        }
    },
    handleMouseLeave: function(sender, eventArgs) {
        if(this.buttonName == buttonClicked){
            this.innerCanvas.Background="#FFff9500";
            this.outerCanvas.Background="#FFff9500";
            this.button.Foreground="#FFFFFFFF";
        }
        else {
            this.innerCanvas.Background="#FFFFFFFF";
            this.outerCanvas.Background="#FFFFFFFF";
            this.button.Foreground="#FF000000";
        }
        this.innerCanvas.Opacity="1.0";
    },
    handleMouseDown: function(sender, eventArgs) {
        window.clearInterval(timeId);
        this.control = Mastercontrol;
        if (this.buttonName == "One")
          {
              SetPageMode("Traffic");
          }
        else if (this.buttonName == "Two")
          {
              SetPageMode("Gas");                
          }
        else if (this.buttonName == "Three")
          {
              SetPageMode("Weather");
          }
        this.control.content.findName("Wipedown").Begin();
        this.control.content.findName("WipeOff").Begin();
        timeId = window.setInterval("StartTimer()",20000);
    },
    turnOn: function(){
        this.button.Foreground = "#FFFFFFFF";
        this.innerCanvas.Background="#FFff9500";
        this.outerCanvas.Background="#FFff9500";
    },
    turnOff: function(){
        this.button.Foreground = "#FF000000";
        this.innerCanvas.Background="#FFFFFFFF";
        this.outerCanvas.Background="#FFFFFFFF";
    }
}