﻿/*extern $, Dom, Event, YAHOO, document */
/*global HOMEPAGE */
/*members Get_Cookie, TabView, addListener, cookie, data, doChangeTabIE6, 
    doCookie, env, filters, get, getAncestorByTagName, getDate, ie, init, 
    length, onDOMReady, replace, set, setCookie, setDate, setTheCookie, 
    split, src, tabOff, tabOn, tabs, toGMTString, ua, widget
*/
HOMEPAGE = {
	data: {
		tabs: 'HomePageTabs',
		// The following are used only by IE6 to help with PNG transparency
		tabOn:  '/images/index_right_top_item.png',
		tabOff: '/images/index_right_top_unselected.png'
	},
	init: function () {
		this.data.tabs = $(this.data.tabs);
		this.data.tabs = new YAHOO.widget.TabView(this.data.tabs);
		if (YAHOO.env.ua.ie === 6) {
			this.data.tabs.addListener('activeTabChange', this.doChangeTabIE6, this, true);
		}
		
		this.data.tabs.addListener('activeTabChange', this.setTheCookie, this, true);
		
		this.doCookie();
	},
	
	setTheCookie: function ()
	{
		// First we need to see if there is already a cookie. If so we read it and act
		// accordingly. If it's not we set it.

		var tabs   = this.data.tabs.get('tabs');
		var active = this.data.tabs.get('activeTab');
		var src, tab, x = tabs.length; 
		var index;

		while (x--) {
			tab = tabs[x];
			if (tab === active) {
				index = x;
				break;
			}
		}

		this.setCookie("currentTab", index, 1);
	},

	doCookie: function () {
		if (this.Get_Cookie('currentTab')) {
			this.data.tabs.set('activeIndex', this.Get_Cookie('currentTab'));
		}
	},

	setCookie: function (c_name, value, expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie =
		encodeURIComponent(c_name) + "=" +
		encodeURIComponent(value) +
		((expiredays === null) ? "" : ";expires=" + exdate.toGMTString());
	},

	Get_Cookie: function (check_name) {
		// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split(';');
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f

		var i = a_all_cookies.length;
		while (i--) {
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split('=');

			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

			// if the extracted name matches passed check_name
			if (cookie_name === check_name) {
				b_cookie_found = true;

				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if (a_temp_cookie.length > 1) {
					cookie_value = decodeURIComponent(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
				}

				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
			}

			a_temp_cookie = null;
			cookie_name = '';
		}

		if (!b_cookie_found) {
			return null;
		}
	},

	/** IE6 needs help with filters for the PNGs on the tabs.
	  * The filter that enabled PNG transparency is set once using a CSS
	  * behaviour. When the background image changes due to the changing
	  * className on the LI, the filter does not get updated. This is
	  * unfortunate, since the filter needs a pointer to the URL of the
	  * current background image to work.  This code will loop through the
	  * tabs and update that pointer.
	  */
	doChangeTabIE6: function () {
		var tabs   = this.data.tabs.get('tabs');
		var active = this.data.tabs.get('activeTab');
		var li, src, tab, x = tabs.length;
		while (x--) {
			tab = tabs[x];
			src = (tab === active) ? this.data.tabOn : this.data.tabOff;
			li  = tab.get('labelEl');
			li  = Dom.getAncestorByTagName(li, 'li');
			li.filters[0].src = src;
		}
	}
};

Event.onDOMReady(HOMEPAGE.init, HOMEPAGE, true);
window.onresize = TabViewRefresh;

function TabViewRefresh() {
	//alert('Resizing.');
	var curIndex = HOMEPAGE.data.tabs.get('activeIndex');
	curIndex = Number(curIndex);
	if (curIndex === 2) {
		HOMEPAGE.data.tabs.set('activeIndex', 0);
	} else {
		HOMEPAGE.data.tabs.set('activeIndex', curIndex + 1);
	}
	HOMEPAGE.data.tabs.set('activeIndex', curIndex);
}
