/*
 * Copyright (c) 2006 AWS Convergence Technologies, Inc. 
 * All rights reserved.
 * Visit http://www.aws.com
 *
 * WBButtonBar.js
 *
 * This scripts contains the WBButtonBar resuable class. A WBButtonBar object deals
 * with a drawer interface and the corresponding buttons, keeping track of on/off
 * states, drawer heights and background images.
 */

// Object definition
function WBButtonBar(forecast_element,forecast_image_element,radar_element,radar_image_element,cams_element,cams_image_element,more_element,more_image_element,drawer_element,drawer_container)
{
	this.forecastimage = forecast_image_element;
	this.forecastelement = forecast_element;
	this.smallforecastheight = 143;
	this.forecastheight = 143;
	this.forecaststate = 'off';
	this.forecastoffsource = 'images/front/buttons/forecast/off.png';
	this.forecastonsource = 'images/front/buttons/forecast/on.png';
	
	this.radarimage = radar_image_element;
	this.radarelement = radar_element;
	this.smallradarheight = 180;
	this.radarheight = 180;
	this.radarstate = 'off';

	this.radar_off_us = 'images/front/buttons/radar/off.png';
	this.radar_off_international = 'images/front/buttons/satellite/off.png';
	this.radaroffsource = this.radar_off_us;
	
	this.radar_on_us = 'images/front/buttons/radar/on.png';
	this.radar_on_international = 'images/front/buttons/satellite/on.png';
	this.radaronsource = this.radar_on_us;
	
	this.camsimage = cams_image_element;
	this.camselement = cams_element;
	this.smallcamsheight = 180;
	this.camsheight = 180;
	this.camsstate = 'off';
	
	this.cams_off_us = 'images/front/buttons/cams/off.png';
	this.cams_off_international = 'images/front/buttons/cams/disabled.png';
	this.camsoffsource = this.cams_off_us;
	
	this.cams_on_us = 'images/front/buttons/cams/on.png';
	this.cams_on_international = 'images/front/buttons/cams/disabled.png';
	this.camsonsource = this.cams_on_us;
	
	this.moreimage = more_image_element;
	this.moreelement = more_element;
	this.smallmoreheight = 20;
	this.moreheight = 20;
	this.morestate = 'off';
	
	this.moreoffsource_us = 'images/front/buttons/more/off.png';
	this.moreoffsource_international = 'images/front/buttons/search/off.png';
	this.moreonsource_us = 'images/front/buttons/more/on.png';
	this.moreonsource_international = 'images/front/buttons/search/on.png';
	this.moreonsource = 'images/front/buttons/more/on.png';
	
	this.moreoffsource = this.noactivemoreoffsource_us;
	
	this.drawer = drawer_element;
	this.drawer_element = drawer_container;
	this.drawer.object = this;
	this.showBack = false;
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a forecast click.
WBButtonBar.prototype.clickForecast = function ()
{
	if(!this.drawer.stretchAnimator)
	{
		if(this.forecaststate == 'on'){
			this.turnAllImagesOff();
			this.drawerClose();
		}
		else
		{
			this.turnAllImagesOff();
			this.forecaststate = 'on';
			this.forecastimage.src = this.forecastonsource;
			this.drawerMove();
		}
	}
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a radar click.
WBButtonBar.prototype.clickRadar = function ()
{
	if(!this.drawer.stretchAnimator)
	{
		if(this.radarstate == 'on'){
			this.turnAllImagesOff();
			this.drawerClose();
		}
		else
		{
			this.turnAllImagesOff();
			this.radarstate = 'on';
			this.radarimage.src = this.radaronsource;
			this.drawerMove();
		}
	}
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a cams click.
WBButtonBar.prototype.clickCams = function (type)
{
	if(!this.drawer.stretchAnimator && type == 0)
	{
		if(this.camsstate == 'on'){
			this.turnAllImagesOff();
			this.drawerClose();
		}
		else
		{
			this.turnAllImagesOff();
			this.camsstate = 'on';
			this.camsimage.src = this.camsonsource;
			this.drawerMove();
		}
	}
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on an more click.
WBButtonBar.prototype.clickMore = function ()
{
	if(!this.drawer.stretchAnimator)
	{
		if(this.morestate == 'on'){
			this.turnAllImagesOff();
			this.drawerClose();
		}
		else
		{
			this.turnAllImagesOff();
			this.morestate = 'on';
			this.moreimage.src = this.moreonsource;
			this.drawerMove();
		}
	}
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a forecast click.
WBButtonBar.prototype.mouseForecast = function (type)
{
	if(this.forecaststate == 'off')
	{
		if(type == true)
			forecast_button_image.src = "images/front/buttons/forecast/mover.png";
		else
			forecast_button_image.src = "images/front/buttons/forecast/off.png";
	}

}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a radar click.
WBButtonBar.prototype.mouseRadar = function (mouse,type)
{
	if(type == true)
	{
		if(this.radarstate == 'off')
		{
			if(mouse == true)
				radar_button_image.src = "images/front/buttons/satellite/mover.png";
			else
				radar_button_image.src = "images/front/buttons/satellite/off.png";
		}	
	}
	else
	{
		if(this.radarstate == 'off')
		{
			if(mouse == true)
				radar_button_image.src = "images/front/buttons/radar/mover.png";
			else
				radar_button_image.src = "images/front/buttons/radar/off.png";
		}
	}
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a cams click.
WBButtonBar.prototype.mouseCams = function (mouse,type)
{
	if(this.camsstate == 'off' && type == false)
	{
		if(mouse == true)
			cams_button_image.src = "images/front/buttons/cams/mover.png";
		else
			cams_button_image.src = "images/front/buttons/cams/off.png";
	}
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on an more click.
WBButtonBar.prototype.mouseMore = function (mouse,type)
{
	if(type == true)
	{
		if(this.morestate == 'off')
		{
			if(mouse == true)
				more_button_image.src = "images/front/buttons/search/mover.png";
			else
				more_button_image.src = "images/front/buttons/search/off.png";
		}
	}
	else
	{
		if(this.morestate == 'off')
		{
			if(mouse == true)
				more_button_image.src = "images/front/buttons/more/mover.png";
			else
				more_button_image.src = "images/front/buttons/more/off.png";
		}
	}
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a drawer resize.
WBButtonBar.prototype.drawerMove = function ()
{
	if(this.forecaststate == 'on'){
		this.drawer.onFinished = function () {
			this.object.drawer_element.style.setProperty("opacity","1");
			this.object.forecastelement.style.setProperty("opacity","1");
		};
		this.drawer.changeHeight(this.forecastheight);
	}
	else if(this.radarstate == 'on'){
		this.drawer.onFinished = function () {
			this.object.drawer_element.style.setProperty("opacity","1");
			this.object.radarelement.style.setProperty("opacity","1");
		};
		this.drawer.changeHeight(this.radarheight);
	}
	else if(this.camsstate == 'on'){
		this.drawer.onFinished = function () {
			this.object.drawer_element.style.setProperty("opacity","1");
			this.object.camselement.style.setProperty("opacity","1");
		};
		this.drawer.changeHeight(this.camsheight);
	}
	else if(this.morestate == 'on'){
		this.drawer.onFinished = function () {
			this.object.drawer_element.style.setProperty("opacity","1");
			this.object.moreelement.style.setProperty("opacity","1");
		};
		this.drawer.changeHeight(this.moreheight);
	}
	else {
		if(this.showBack != true)
		{
			this.drawer.onFinished = function () {
				this.object.drawer_element.style.setProperty("opacity","0");
			};
		}
		this.drawer.changeHeight(0);
	}
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Turns all the background images in the widget off.
WBButtonBar.prototype.turnAllImagesOff = function ()
{
	this.forecastelement.style.setProperty("opacity","0");
	this.radarelement.style.setProperty("opacity","0");
	this.camselement.style.setProperty("opacity","0");
	this.moreelement.style.setProperty("opacity","0");
	
	this.forecaststate = 'off';
	this.forecastimage.src = this.forecastoffsource;
	this.radarstate = 'off';
	this.radarimage.src = this.radaroffsource;
	this.camsstate = 'off';
	this.camsimage.src = this.camsoffsource;
	this.morestate = 'off';
	this.moreimage.src = this.moreoffsource;
}

// Do not call this directly, only from an initialized WBButtonBar object.
// returns the drawer to the off state. All images are turned to their off state.
WBButtonBar.prototype.drawerClose = function ()
{
	this.turnAllImagesOff();
	this.drawerMove();
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called to show the back of the widget. This allows us to shrink before we transition
// for a better UI look.
WBButtonBar.prototype.showBack = function ()
{
	showBack();
	this.drawer.onFinished = null;
}

WBButtonBar.prototype.changeType = function (type)
{
	if(type == true)
	{
		this.radaroffsource = this.radar_off_international;
		this.radaronsource = this.radar_on_international;
		this.moreoffsource = this.moreoffsource_international;
		this.moreonsource = this.moreonsource_international;
		document.getElementById("cams_button").style.setProperty("opacity",".5");
		document.getElementById("alert_badge").style.setProperty("opacity",".2");
		document.getElementById("more_observations").style.setProperty("opacity","0");
		document.getElementById("alert_badge").style.setProperty("cursor","default");
		document.getElementById("more_observations").style.setProperty("cursor","default");
		fclolink.innerText = "Highs Tomorrow";
		fchilink.innerText = "Extended Forecast";
		rdrnationallink.innerText = "Precipitation Map";
		radartitle.innerText = "Satellite";
		document.getElementById("rdrregionallink").style.setProperty("opacity","0");
		document.getElementById("rdrsatlink").style.setProperty("opacity","0");
		document.getElementById("rdranimate").style.setProperty("opacity","0");
		document.getElementById("rdrenlarge").style.setProperty("left","85px");
		document.getElementById("radartitle").style.setProperty("left","230px");
		document.getElementById("fcthree").style.setProperty("opacity","0");
		document.getElementById("morelinkthree").style.setProperty("opacity","0");
		fcbg.src = 'images/front/drawer/forecast/background.png';
		document.getElementById("fcone").style.setProperty("left","0");
		document.getElementById("fctwo").style.setProperty("left","60");
		activeAlertsNumber = 0;
		alert_button_image.src = 'images/front/buttons/alerts/' + getAlertString() + '_nmo.png';
	}
	else
	{
		this.radaroffsource = this.radar_off_us;
		this.radaronsource = this.radar_on_us;
		this.moreoffsource = this.moreoffsource_us;
		this.moreonsource = this.moreonsource_us;
		document.getElementById("cams_button").style.setProperty("opacity","1");
		document.getElementById("alert_badge").style.setProperty("opacity","1");
		document.getElementById("more_observations").style.setProperty("opacity","1");
		document.getElementById("alert_badge").style.setProperty("cursor","pointer");
		document.getElementById("more_observations").style.setProperty("cursor","pointer");
		fclolink.innerText = "Lows Tonight";
		fchilink.innerText = "Highs Tomorrow";
		document.getElementById("fchilink").style.setProperty("opacity","1");
		rdrnationallink.innerText = "Satellite Radar";
		radartitle.innerText = "Radar";
		document.getElementById("rdrregionallink").style.setProperty("opacity","1");
		document.getElementById("rdrsatlink").style.setProperty("opacity","1");
		document.getElementById("rdranimate").style.setProperty("opacity","1");
		document.getElementById("rdrenlarge").style.setProperty("left","120px");
		document.getElementById("radartitle").style.setProperty("left","237px");
		document.getElementById("fctwo").style.setProperty("opacity","1");
		document.getElementById("fcthree").style.setProperty("opacity","1");
		document.getElementById("morelinkthree").style.setProperty("opacity","1");
		fcbg.src = 'images/front/drawer/forecast/background.png';
		document.getElementById("fcone").style.setProperty("left","0");
		document.getElementById("fctwo").style.setProperty("left","60");
		activeAlertsNumber = 0;
		alert_button_image.src = 'images/front/buttons/alerts/' + getAlertString() + '_nmo.png';
	}
	
	if(this.radarstate == 'on')
	{
		this.radarimage.src = this.radaronsource;
	}
	else
	{
		this.radarimage.src = this.radaroffsource;
	}
	
	if(this.morestate == 'on')
	{
		this.moreimage.src = this.moreonsource;
	}
	else
	{
		this.moreimage.src = this.moreoffsource;
	}
}

WBButtonBar.prototype.setMoreTabHeight = function (height)
{
	this.moreheight = parseInt(height);
	this.smallmoreheight = parseInt(height);
	if(this.morestate == 'on'){
		this.drawer.changeHeight(this.moreheight);
	}
}

WBButtonBar.prototype.setBezelHeight = function (height)
{
	this.drawer.beginHeight = height + this.drawer.originalHeight;
	this.drawer.windowResizeOffsetX = height + this.drawer.originalXOffset;
	this.drawer.windowResizeOffsetY = height + this.drawer.originalYOffset;
	
	if(this.morestate == 'on'){
		this.drawer.changeHeight(this.moreheight);
		this.drawer.onFinished = function () {
			if( moreContentObj.tabContentArray[1] != null)
			{
				spunit.innerHTML = moreContentObj.tabContentArray[1];
			}
			else
			{
				spunit.innerHTML = "<P></P>";
			}
		}
	}else if(this.radarstate == 'on'){
		this.drawer.changeHeight(this.radarheight);
		this.drawer.onFinished = function () {
			if( moreContentObj.tabContentArray[1] != null)
			{
				spunit.innerHTML = moreContentObj.tabContentArray[1];
			}
			else
			{
				spunit.innerHTML = "<P></P>";
			}
		}
	}else if(this.camsstate == 'on'){
		this.drawer.changeHeight(this.camsheight);
		this.drawer.onFinished = function () {
			if( moreContentObj.tabContentArray[1] != null)
			{
				spunit.innerHTML = moreContentObj.tabContentArray[1];
			}
			else
			{
				spunit.innerHTML = "<P></P>";
			}
		}
	}else if(this.forecaststate == 'on'){
		this.drawer.changeHeight(this.forecastheight);
		this.drawer.onFinished = function () {
			if( moreContentObj.tabContentArray[1] != null)
			{
				spunit.innerHTML = moreContentObj.tabContentArray[1];
			}
			else
			{
				spunit.innerHTML = "<P></P>";
			}
		}
	}else{
		this.drawer.changeHeight(0);
		this.drawer.onFinished = function () {
			if( moreContentObj.tabContentArray[1] != null)
			{
				spunit.innerHTML = moreContentObj.tabContentArray[1];
			}
			else
			{
				spunit.innerHTML = "<P></P>";
			}
		}
	}
}
