/*
 * 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 WBThreeButtonBar(location_element,location_image_element,advanced_element,advanced_image_element,contact_us_element,contact_us_image_element,drawer_object)
{
	this.locationimage = location_image_element;
	this.locationelement = location_element;
	this.locationheight = 85;
	this.locationstate = 'off';
	this.locationoffsource = 'images/back/buttons/location/off.png';
	this.locationonsource = 'images/back/buttons/location/on.png';
	
	this.advancedimage = advanced_image_element;
	this.advancedelement = advanced_element;
	this.updateheight = 65;
	this.noupdateheight = 45;
	this.advancedheight = this.noupdateheight;
	this.advancedstate = 'off';
	this.advancedoffsource = 'images/back/buttons/advanced/off.png';
	this.advancedonsource = 'images/back/buttons/advanced/on.png';

	this.contact_usimage = contact_us_image_element;
	this.contact_uselement = contact_us_element;
	this.contact_usheight = 104;
	this.contact_usstate = 'off';
	this.contact_usoffsource = 'images/back/buttons/contact_us/off.png';
	this.contact_usonsource = 'images/back/buttons/contact_us/on.png';
	
	this.drawer = drawer_object;
	this.drawer.object = this;
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a location click.
WBThreeButtonBar.prototype.clickLocation = function ()
{
	if(!this.drawer.stretchAnimator && this.locationstate == 'off')
	{
		this.turnAllImagesOff();
		this.locationstate = 'on';
		this.locationimage.src = this.locationonsource;
		this.drawerMove();
	}
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a advanced click.
WBThreeButtonBar.prototype.clickAdvanced = function ()
{
	if(!this.drawer.stretchAnimator && this.advancedstate == 'off')
	{
		this.turnAllImagesOff();
		this.advancedstate = 'on';
		this.advancedimage.src = this.advancedonsource;
		this.drawerMove();
	}
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a contact_us click.
WBThreeButtonBar.prototype.clickContact_Us = function ()
{
	if(!this.drawer.stretchAnimator && this.contact_usstate == 'off')
	{
		this.turnAllImagesOff();
		this.contact_usstate = 'on';
		this.contact_usimage.src = this.contact_usonsource;
		this.drawerMove();
	}
}


// Do not call this directly, only from an initialized WBButtonBar object.
// Called on a drawer resize.
WBThreeButtonBar.prototype.drawerMove = function ()
{
	if(this.locationstate == 'on'){
		this.drawer.onFinished = function () {
			this.object.locationelement.style.setProperty("opacity","1");
			this.object.locationelement.style.setProperty("z-index","7999");
		};
		this.drawer.changeHeight(this.locationheight);
	}
	else if(this.advancedstate == 'on'){
		if(design.newVersionAvailable == 1)
		{
			this.advancedheight = this.updateheight;
		}
		else
		{
			this.advancedheight = this.noupdateheight;
		}
		this.drawer.onFinished = function () {
			this.object.advancedelement.style.setProperty("opacity","1");
			this.object.advancedelement.style.setProperty("z-index","7999");
			if(design.newVersionAvailable == 1)
			{
				updateText.innerHTML = "<IMG SRC='images/back/other/update_tri.png' WIDTH=5 HEIGHT=8> Update to Version " + design.newVersionNumber;
				notifiedofupdate = true;
			}
			else{
				updateText.innerHTML = "";
			}
		};
		this.drawer.changeHeight(this.advancedheight);
	}
	else if(this.contact_usstate == 'on'){
		this.drawer.onFinished = function () {
			this.object.contact_uselement.style.setProperty("opacity","1");
			this.object.contact_uselement.style.setProperty("z-index","7999");
		};
		this.drawer.changeHeight(this.contact_usheight);
	}
	else { this.drawer.changeHeight(0); }
}

// Do not call this directly, only from an initialized WBButtonBar object.
// Turns all the background images in the widget off.
WBThreeButtonBar.prototype.turnAllImagesOff = function ()
{
	if(unitsMenu.value)
	{
		widget.setPreferenceForKey(unitsMenu.value,forWidgetInstance("Units"));
	}

	if(cityMenu.value && stationsMenu.value && !stationsMenu.disabled && !cityMenu.disabled)
	{
		widget.setPreferenceForKey(cityMenu.value,forWidgetInstance("zipCode"));
		widget.setPreferenceForKey(stationsMenu.value,forWidgetInstance("stationID"));
		widget.setPreferenceForKey(stationsDataObj.cityList[cityMenu.selectedIndex].replace(" (International)",""),forWidgetInstance("countryName"));
		widget.setPreferenceForKey(stationsDataObj.cityTypeList[cityMenu.selectedIndex],forWidgetInstance("Type"));
		button_bar.changeType(widgetPrefs.getType());
	}
	if(document.getElementById('updateCB').checked){
		widget.setPreferenceForKey(1,"updateNotification");
	}
	else{
		widget.setPreferenceForKey(0,"updateNotification");
	}
	errormessage.innerText = "";
	back_progress_animator.stop();
	glassbutton._container.children[1].innerText = 'Save';
	glassbutton.onclick = shrinkBack;
	this.locationelement.style.setProperty("opacity","0");
	this.locationelement.style.setProperty("z-index","100");
	this.advancedelement.style.setProperty("opacity","0");
	this.advancedelement.style.setProperty("z-index","100");
	this.contact_uselement.style.setProperty("opacity","0");
	this.contact_uselement.style.setProperty("z-index","100");
	
	this.locationstate = 'off';
	this.locationimage.src = this.locationoffsource;
	this.advancedstate = 'off';
	this.advancedimage.src = this.advancedoffsource;
	this.contact_usstate = 'off';
	this.contact_usimage.src = this.contact_usoffsource;
}

// 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.
WBThreeButtonBar.prototype.closeDrawer = function (callBack)
{
	this.turnAllImagesOff();
	this.drawerMove();

	//this.locationimage.src = this.locationonsource;

	if(callBack)
	{
		this.drawer.onFinished = callBack;
	}
	else
	{
		this.drawer.onFinished = null;
	}
}