/*
*	General Javascript functions
*/

// Sets the day / night / holiday classes
var DayNightHols = {
	init: function () {
		if (!window.$holiday) $holiday = false;			// Set holiday externally or in this section
		if ($holiday) $('body').addClass ('holiday');	//Actual holiday theme to be applied via CSS

		var hour = new Date().getHours();
		if (hour < 5 || hour >= 17) $('body').addClass ('night');
	}
}

// Swap Flash header based on current category
var FlashHeader = {
	init: function () {
		var src = 'birds';
		if ($('body').hasClass('holiday')) { /* Use for holiday-specific flash header */ }
		else if ($('#page').hasClass('personal')) { src = 'kite'; }
		else if ($('#page').hasClass('corporate')) { src = 'clouds'; }
		else if ($('#page').hasClass('investor')) { src = 'birds'; }
		else if ($('#page').hasClass('about')) { src = 'hotairballoon'; }
		else if ($('#page').hasClass('privilege')) { src = 'clouds'; }
		else if ($('#page').hasClass('private')) { src = 'plane'; }

		$('#site-flash')
			.empty ()
			.flash
			(
				{
					src:'/assets/flash/'+src+'.swf',
					height: 59,
					width: 300,
					wmode: 'transparent'
				},
				{ update:false },
				function (htmlOptions) { $(this).append($.fn.flash.transform(htmlOptions)); }
			);
	}
}

// Fixes IE 'Click to activate' requirement
var FlashFix = {
	init: function () {
		if (jQuery.browser.msie)
			$('#main-content object').not($('#UOB-assist object'))
				.each (function() { this.outerHTML = this.outerHTML; });
	}
}

/*
*	Javascript Include - handles misc content-related functions
*	Works around problems with using jQuery, due to excessive use of document.write in current content
*/
var JSI = {
	/* Tag injector - used to write HTML during runtime, synchronously */
	inject: function (tag, attrs, content) {
		document.write ('<' + tag);
		if (attrs) for (attr in attrs) { document.write (' ' + attr + '="' + attrs[attr] + '"'); }
		document.write ('>');
		if (content) document.write (content);
		document.write ('</' + tag + '>');
	},
	/* Load XML synchronously & works around lack of mis-set HTTP headers */
	loadXML: function (url) {
		var xmlDoc = null;
		try
		{
			if (window.ActiveXObject)
			{
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async = false;
				xmlDoc.load(url);
			}
			else if (window.XMLHttpRequest)
			{
				var req = new XMLHttpRequest();
				req.open("GET", url, false);
				req.send(null);
				xmlDoc = (window.DOMParser)? (new DOMParser()).parseFromString(req.responseText, 'text/xml'): req.responseXML;
			}
			else
			{
				xmlDoc = document.implementation.createDocument("", "", null);
				xmlDoc.async = false;
				xmlDoc.load (url);
			}
		}
		catch (e) { /* Do nothing */ }
		return xmlDoc;
	}
}

/*
*	Semi-legacy support - Selects specified tab and (optional) bullet
*	Expects id's in the form of #tab{n} and #tab{n}-bullet{m}
*/
var TabContent = {
	select: function (tab, bullet) {
		if (tab) JSTab.group[tab-1].click();
		if (bullet) JSListMenu.select('tab'+tab+'-bullet'+bullet);
		//focus tab
		//TabContent.focus ();
	},
	initLinks: function (links) {
		var out = '';	// DEBUG
		$(links).each (function () {
			var match = false; var ctab = 0; var cbullet = 0;
			if (match = this.href.match(/([^#]*#(.+))/))
			{
				var hash = match[2];
				out += hash;	//DEBUG
				if (match = hash.match(/tab(\d+)/)) { ctab = match[1]; }
				if (match = hash.match(/bullet(\d+)/)) { cbullet = match[1]; }
			}
			if (ctab || cbullet) $(this).click (function () { TabContent.select(ctab, cbullet); return false; });
		});
		return out;	// DEBUG
	},
	focus: function () { $(document).scrollTopSmooth (($('#js-tabs-control').offset() || $('#js-tabs').offset()).top); }
}

jQuery.fn.scrollTopSmooth = function (target, self) {
	self = self || this;
	var cur = self.scrollTop();
	var delta = 35; var interval = 20;
	if (Math.abs(target - cur) <= delta)
	{
		self.scrollTop (target);
	}
	else
	{
		self.scrollTop (cur + delta*((target > cur)? 1: -1));
		setTimeout (function () { jQuery.fn.scrollTopSmooth(target, self) }, interval);
	}
}

/***************************
	LEGACY FUNCTIONS
****************************/

// Switch pages based on menu choice - 2nd level landing pages
function GoProd (s)
{
	var d = s.options[s.selectedIndex].value;
	window.top.location.href = d;
	s.selectedIndex = 0;
}

// centerTab - Deposit
function Show(obj_ID)
{
	HideAll();
    document.getElementById(obj_ID).style.display="block";
	document.getElementById('img'+obj_ID).src = "images/common/icon/close.gif";
}
function Hide(obj_ID)
{
 document.getElementById(obj_ID).style.display="none";
}
function HideAll()
{
    // Grab all elements that have tagname input
    var divArr = document.getElementsByTagName("div");

    // Loop through those elements and alert to user
    for (var i = 0; i < divArr.length; i++)
    {
        if ('divBenefit,divRateFees,divRequirementt,divContactForm'.indexOf(divArr[i].id) < 0)
        {
            if (divArr[i].style.display == "block")
            {
                divArr[i].style.display = "none"
                document.getElementById('img'+divArr[i].id).src = "images/common/icon/icon_collapse.gif";
            }
            else if (divArr[i].style.visibility == "visible")
            {
                divArr[i].style.visibility = "hidden"
            }
        }
    }
}
// end centerTab - Deposit
//********************************************************************************************//

function show_UOB_Locator(locator_service)
{
	var locator_url = "";

	locator_url = 'http://test2.street-directory.com/uob/popup_result.cgi?preselect=' + locator_service;
	//window.open(locator_url,'UOBLocator','toolbar=no,directories=no,resizable=yes,status=yes,menubar=yes,location=no,scrollbars=yes,width=620,height=500,maximize=yes,minimize=yes');

	var address="http://test2.street-directory.com/uob/popup_result.cgi?preselect="+locator_service+"&keepThis=true&TB_iframe=true&height=540&width=590";
	tb_show("ATM Locator", address, false);
}

function print_this_page(url)
{
	window.print();
}

function bookmark_this_page(url, title)
{
	if (document.all)// ie
		window.external.AddFavorite(url, title);
	else if (window.sidebar) // firefox
	    window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print)
	{ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
}



//********************************************************************************************//
// Font size style switcher

function setActiveStyleSheet(title) {
  var i, a, main;

  var curStyle = getActiveStyleSheet();

  if(curStyle == null){
	 if(title == "large"){
	 	title = "font2";
	 }
	 else{
		title = "font1";
	 }
  }
  else{
  if(title == "large"){
	if(curStyle == "font3"){
		title = "font3";
	}
	else if(curStyle == "font2"){
		title = "font3";
	}
	else if(curStyle == "font1"){
		title = "font2";
	}
  }
  else if(title == "small"){
	  if(curStyle == "font3"){
		  title = "font2";
	  }
	  else if(curStyle == "font2"){
		  title = "font1";
	  }
	  else if(curStyle == "font1"){
			title = "font1";
	  }
  }
  }

  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
		a.disabled = true;
      if(a.getAttribute("title") == title){
		  a.disabled = false;
	  }
	  else{
		  a.disabled = true;
	  }
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
/*
window.onload = function(e) {
  var cookie = readCookie("styleside");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("styleside", title, 365);
}

var cookie = readCookie("styleside");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);*/

// end Font size style switcher
//********************************************************************************************//


