isIphone = false;
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { 
   if (document.cookie.indexOf("iphone_redirect=false") == -1) isIphone = true;
}

isBlackberry = (navigator.userAgent.match(/BlackBerry/i))? true : false;
isBlackberry4x = (isBlackberry && navigator.userAgent.match(/\/4\./i));	

isInHttps = (url_prepend != '' && window.location.toString().indexOf('https') != -1);

/*
function s_gi()
{
	return new test();
}


var test = $.Class.create({
	initialize: function(properties){
	},
	
	tl:function(){
	}
});
*/
/******************************************************/
function parseInterwovenXml(xml)
{
	return xml;
	var xml_start = 0;
	xml = xml.substr(xml.indexOf('<pre>') + 5, xml.length);
	xml = xml.substr(0, xml.indexOf('</pre>'));
	xml = xml.replace(/\&lt\;/g, '<');
	xml = xml.replace(/\&gt\;/g, '>');
	xml = xml.replace(/\&quot\;/g, '"');
	return xml;
}

jQuery.createXMLDocument = function(string)
{
	var browserName = navigator.appName;
	var doc;
	if (browserName == 'Microsoft Internet Explorer')
	{
		doc = new ActiveXObject('Microsoft.XMLDOM');
		doc.async = 'false'
		doc.loadXML(string);
	} else {
		doc = (new DOMParser()).parseFromString(string, 'text/xml');
	}
	return doc;
}

$.fn.wait = function(time, type) {
	time = time || 1000;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
			$(self).dequeue();
		}, time);
	});
};

/******************************************************/
$(document).ready(function(){
	
	/**************** FLASH *******************/
	if(isBlackberry || isIphone || isBlackberry4x)
	{
		$('#main_promo_flash_area').addClass('main_promo_mobile');
	}
	else if($('#main_promo_flash_area').length > 0)
	{	
		var fo = new SWFObject("/uobvm/assets/flash/homepage_banner.swf", "packages", "720", "272", "8", "#FFFFFF");
		fo.addParam("allowscriptaccess", "always");
		fo.addParam("wmode", "transparent");
		
		var xmlURL = url_prepend + '/uobvm/banners.xml';
		//var xmlURL = 'banners.xml';
		
		var bannersURLPrefix = url_prepend;
		
		fo.addParam("flashvars", "xmlURL="+xmlURL + '&bannersURLPrefix=' + bannersURLPrefix);
		fo.write("main_promo_flash_area");
		//alert('Hello -  ' + url_prepend);
	}

	/************************* TICKER TEXT **************************************/
	tickerTextManager = new TickerTextManager({
		el		: $('div[class~=ticker] > div[class~=text_area] > div[class~=entries]').eq(0)
	});
	
	$('div[class~=ticker] > div[class~=arrow_left]').eq(0).bind('click', function(){
		tickerTextManager.previousEntry();
	});
	
	$('div[class~=ticker] > div[class~=arrow_right]').eq(0).bind('click', function(){
		tickerTextManager.nextEntry();
	});
	
	var url = url_prepend + '/assets/assist/xml/global_annc.xml';
	
	$.ajax({ 
		dataType	: 'text',
		url		: url,
		error		: function(a, e){
			//alert(e);
		},
		success		: function(xml){
			try{
				xml = $.createXMLDocument(xml);
			}
			catch(e){
				alert(e);
			}
			var itemNodes = $(xml).find('item');
			for(i=0; i<itemNodes.length; i++)
			{
				if(isInHttps && itemNodes.eq(i).attr('showInPtwo') != '1')
				{
					continue;
				}
				var text = itemNodes.eq(i).attr('displayText');
				var url = itemNodes.eq(i).attr('link');
				var target = '_self';
				tickerTextManager.addTextEntry(text, url, target);
			}
			
			/*
			if(isInHttps)
			{ 
				tickerTextManager.nextEntry();
				return;
			}
			*/

			var url = url_prepend + '/assets/assist/xml/global_news.xml';

			$.ajax({ 
				dataType	: 'text',
				url		: url,
				success		: function(xml){
					try{
						xml = $.createXMLDocument(xml);
					}
					catch(e){
						alert(e);
					}
					var itemNodes = $(xml).find('item');
					for(i=0; i<itemNodes.length; i++)
					{
						if(isInHttps && itemNodes.eq(i).attr('showInPtwo') != '1')
						{
							continue;
						}

						var text = itemNodes.eq(i).attr('displayText');
						var url = itemNodes.eq(i).attr('link');
						var target = '_self';
						tickerTextManager.addTextEntry(text, url, target);
					}
					
					tickerTextManager.nextEntry();
				}
			});
		}
	});
	/****************** TABCONTENTS ********************/
	var el = $('div[class~=tab_content]').eq(0);
	
	homePageTabContent = new HomePageTabContent({
		el							: el,
		elBtnClose					: el.find('div[class~=tab_content_header] > div[class~=copy] > img').eq(0),
		elTitle						: el.find('div[class~=tab_content_header] > div[class~=copy] > div[class~=title_container]').eq(0)
	});
	
	el.bind('showing', {tickerTextManager : tickerTextManager}, function(){
        if(isBlackberry4x) 
    	{
          evt.data.tickerTextManager._el.css('display', 'none');
    	}
		
		$('#main_promo_flash_area').css('visibility', 'hidden');
	});
	
	el.bind('hiding', {tickerTextManager : tickerTextManager}, function(){
		if(currentMoreTab) currentMoreTab.hideContent();
		currentMoreTab = null;
		$('#main_promo_flash_area').css('visibility', 'visible');
		if(isBlackberry4x) evt.data.tickerTextManager._el.css('display', 'block');
	});

	var currentMoreTab = null;
	var moreTabs = $(document).find('div[class~=left_nav_tabs] > ul[class~=nav_tabs] > li[class~=tab]');
	
	for(var i=0; i<moreTabs.length; i++)
	{
		var el = moreTabs.eq(i);
		var moreTab = new MoreTab({
			el					: el,
			tabTitle			: el.attr('tabTitle'),
			tabContentElId		: el.attr('tabContentElId'),
			onLoadFunction		: eval('onTabContentLoaded' + (i+1)),
			onUnloadFunction	: eval('onTabContentUnload' + (i+1))
		});
		
		el.bind('tabMouseOver', {target:moreTab}, function(evt){
			if(currentMoreTab == evt.data.target) return;
			if(currentMoreTab) currentMoreTab.hideContent();
			currentMoreTab = evt.data.target;
			currentMoreTab.showContent();
			homePageTabContent.show(currentMoreTab.getTabContentElId(), currentMoreTab.getTabTitle());
			
		});
		
		/*
		if(isBlackberry)
		{
			el.bind('tabClicked', {target:moreTab}, function(evt){
				if(currentMoreTab == evt.data.target)
				{
					homePageTabContent.fadeOut();
					return;
				}
				
				if(currentMoreTab) currentMoreTab.dehighlight();
				currentMoreTab = evt.data.target;
				currentMoreTab.highlight();
				homePageTabContent.show(currentMoreTab.getAjaxURL(), currentMoreTab.getTabTitle());
			});
		}
		*/
	}
});


var TickerTextManager = $.Class.create({
	/******* PROPERTIES *******/
	_el:null,
	_stepSize:363,
	_entries:null,
	_currentEntryIndex:-1,
	_timer:null,
	
	/******* CONSTRUCTOR ********/
	initialize: function(properties){
		this._el = properties.el;
		this._entries = [];
	},
	
	addTextEntry: function(text, url, target){
		var el=$('<div class="entry"></div>');
		this._el.append(el);
		
		el.bind('mouseover', {self:this}, function(evt){
			evt.data.self.clearTimer();
		});
		
		el.bind('mouseout', {self:this}, function(evt){
			evt.data.self.setTimer(evt.data.self._entries[evt.data.self._currentEntryIndex].getDisplaySecs());
		});
		
		this._entries.push(new TickerTextEntry({
			el			: el,
			text		: text,
			url			: url,
			target		: target
		}));
	},
	
	clearTimer:function(){
		if(this._timer)
		{
			clearTimeout(this._timer);
			this._timer = null;
		}
	},
	
	setTimer: function(time){
		this.clearTimer();
		
		tickerTextManager = this;
		this._timer = setTimeout('tickerTextManager.nextEntry()', time);
	},
	
	nextEntry: function(){
		if(this._entries.length < 2) return;
		
		if(this._entries[this._currentEntryIndex]) this._entries[this._currentEntryIndex].fadeOut('left');
		
		this._currentEntryIndex ++;
		if(this._currentEntryIndex > this._entries.length - 1) this._currentEntryIndex = 0;
		this._entries[this._currentEntryIndex].fadeIn('right');
		
		this.setTimer(this._entries[this._currentEntryIndex].getDisplaySecs());
	},
	
	previousEntry: function(){
		if(this._entries.length < 2) return;
		
		if(this._entries[this._currentEntryIndex]) this._entries[this._currentEntryIndex].fadeOut('right');
		
		this._currentEntryIndex --;
		if(this._currentEntryIndex < 0) this._currentEntryIndex = this._entries.length - 1;
		this._entries[this._currentEntryIndex].fadeIn('left');
		
		this.setTimer(this._entries[this._currentEntryIndex].getDisplaySecs());
	}
});

var TickerTextEntry = $.Class.create({
	/******* PROPERTIES *******/
	_el:null,
	_textEl:null,
	_displaySecs:1,
	_displayWidth:0,
	_textWidth:0,
	
	/******* CONSTRUCTOR ********/
	initialize: function(properties){
		this._el = properties.el;
		this._el.css('overflow', 'hidden');
		
		this._textEl = $('<div></div>');
		
		this._el.append(this._textEl);
		
		this._displaySecs = properties.displaySecs * 1000;
		this._displayWidth = this._el.width();
		
		if(properties.url)
		{
			this._textEl.html('<a href="'+ properties.url +'" target="'+properties.target+'">' + properties.text + '</a>');
		}
		else
		{
			this._textEl.html(properties.text);
		}
		
		var tempEl = $('<table><tr><td nowrap="true"><div>'+this._textEl.html()+'</div></td></tr></table>');
		$('body').append(tempEl);
		this._textWidth = Math.ceil(tempEl.width());
		tempEl.remove();
		tempEl = null;
		
		if(this._textWidth > this._displayWidth)
		{
			this._textEl.css({
				'width'		: this._textWidth + 'px'
			});
			
			this._el.bind('mouseover', {self:this}, function(evt){
				evt.data.self.pauseTextAnimation();
			});
			
			this._el.bind('mouseout', {self:this}, function(evt){
				evt.data.self.resumeTextAnimation();
			});
		}
		
		//calculate the display secs
		this._displaySecs = (this._textWidth <= this._displayWidth)? 3000 : 3000 + ((this._textWidth - this._displayWidth) * 20);
		
		this.forceHide();
	},
	
	getDisplaySecs:function(){
		return this._displaySecs;
	},
	
	iphoneResetFontSize:function(){
		//cos iphone some how enlarges the text. need to hard code here
		this._textEl.css({
			'font-size'	: '12px !important' 
		});
		
		this._textEl.find('a').css({
			'font-size'	: '12px !important'
		});
	},
	
	animateText:function(){
		
		if(isIphone)
		{
			this.iphoneResetFontSize();
		}
		
		if(this._textWidth <= this._displayWidth) return;
		
		this._textEl.css('marginLeft', '0px');
		this._textEl.wait(1000).startAnimation({
			marginLeft:-1*this._textWidth + this._displayWidth
		}, this._displaySecs - 3000, 'linear');
	},
	
	pauseTextAnimation:function(){
		this._textEl.pauseAnimation();
	},
	
	resumeTextAnimation:function(){
		this._textEl.resumeAnimation();
	},
	
	fadeIn:function(fromDirection){
		
		this._textEl.stop(true, true);

		if(isIphone)
		{
			this.iphoneResetFontSize();
		}
		
		var self = this;
		var startMargin = (fromDirection == 'left')? -1 * this._el.width() : this._el.width();
		
		this._el.css({
			'marginLeft'	: startMargin,
			'display'		: 'block'
		});
		
		this._el.animate({
			marginLeft:0
		}, 300, function(){
			self.animateText();
		});
	},
	
	fadeOut:function(toDirection){
		
		var endMargin = (toDirection == 'left')? -1 * this._el.width() : this._el.width();
		
		var self = this;
		this._textEl.stop(true, true);
		this._el.css({
			'marginLeft'	: 0
		});
		
		this._el.animate({
			marginLeft:endMargin
		}, 300, function(){
			self.forceHide();
		});
		
		
	},
	
	forceHide:function(){
		this._el.css({
			display		: 'none'
		});
		
		this._textEl.stop(true, true);
	}
});

var HomePageTabContent = $.Class.create({
	/******* PROPERTIES *******/
	_el:null,
	_elBtnClose:null,
	_elOverlayBG:null,
	_elTitle:null,
	_isShown:true,
	_ajaxObj:null,
	_currentContentElId:null,
	
	/******* CONSTRUCTOR ********/
	initialize: function(properties){
		this._el = properties.el;
		this._elBtnClose = properties.elBtnClose
		this._elTitle = properties.elTitle;
		
		this._elOverlayBG = $('<div class="tab_content_dark_overlay"></div>');
		
		if(!isBlackberry4x) $('body').append(this._elOverlayBG);
		else $('body').before(this._elOverlayBG);
		
		this._elOverlayBG.css('height', $(document).height() + 'px');
		
		this._elOverlayBG.bind('mouseover', {self: this}, function(evt){
			evt.data.self.fadeOut();
		});
		
		this._elBtnClose.bind('click', {self: this}, function(evt){
			evt.data.self.fadeOut();
		});
		
		this.forceHide();
	},
	
	fadeOut: function(){
		if(!this._isShown) return;
		this.forceHide();
	},
	
	show: function(contentElId, title){
		if(this._isShown && this._currentContentElId == contentElId) return;
		
		this._currentContentElId = contentElId;
		
		this._elTitle.html(title);
		
		this._el.trigger('showing');
		this.fadeIn();
	},
	
	fadeIn: function(){
		if(this._isShown) return;
		
		var self = this;
		
		this._el.css('display', 'block');
		this._elOverlayBG.css('height', $(document).height() + 'px');
		this._elOverlayBG.css('display', 'block');
		
		this._isShown = true;

		this._el.trigger('show');
	},
	
	forceHide: function(){
		this._el.trigger('hiding');
		this._isShown = false;
		
		this._el.css('display', 'none');
		this._elOverlayBG.css('display', 'none');
	}
});


var MoreTab = $.Class.create({
	/******* PROPERTIES *******/
	_el:null,
	_tabTitle:null,
	_tabContentElId:null,
	_onLoadFunction:null,
	_onUnloadFunction:null,
	_isShown:true,
	
	/******* CONSTRUCTOR ********/
	initialize: function(properties){
		this._el = properties.el;
		this._tabTitle = properties.tabTitle;
		this._tabContentElId = properties.tabContentElId;
		this._onLoadFunction = properties.onLoadFunction;
		this._onUnloadFunction = properties.onUnloadFunction;
		
		this._el.bind('click', {self : this}, function(evt){
			evt.data.self.onClick();
		});
		
		this._el.bind('mouseover', {self : this}, function(evt){
			evt.data.self.onMouseOver();
		});
		
		this._el.bind('mouseout', {self : this}, function(evt){
			evt.data.self.onMouseOut();
		});
		
		this.hideContent();
	},
	
	highlight:function(){
		this._el.addClass('tab_on');
	},
	
	dehighlight:function(){
		this._el.removeClass('tab_on');
	},
	
	showContent:function(){
		if(this._isShown) return;
		
		$('#' + this._tabContentElId).css('display', 'block');
		this._onLoadFunction();
		
		this.highlight();
		this._isShown = true;
	},
	
	hideContent:function(){
		if(!this._isShown) return;
		
		this._onUnloadFunction();
		$('#' + this._tabContentElId).css('display', 'none');
		
		this.dehighlight();
		this._isShown = false;
	},
	
	onClick:function(){
		this._el.trigger('tabClicked');
	},
	
	onMouseOver:function(){
		this._el.trigger('tabMouseOver');
	},
	
	onMouseOut:function(){
		this._el.trigger('tabMouseOut');
	},
	
	getTabTitle:function(){
		return this._tabTitle;
	},
	
	getTabContentElId:function(){
		return this._tabContentElId;
	}
});
