/* =================================
			AAA Functionality Start
==================================== */

hsq.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = hsq.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var selected = 'small';
var selectedClassName = 'selected';
function _aaa(ele) {
	if (hsq.cookie('aaaSize')) {
		var e = ele;
		//var r = document.getElementById(selected);
		hsq('.aaa a.selected').removeClass(selectedClassName);
		hsq('body').css({'fontSize':hsq.cookie('aaaSize')});
		if (ele == undefined) {
			ele = hsq.cookie('aaaEle');
			e = document.getElementById(ele);
		}
		hsq(e).addClass(selectedClassName);
		selected = ele.id;
	} else {
		var r = document.getElementById(selected);
		r.className = selectedClassName;
	}
}

function aaa(size, ele) {
	hsq('.selectedLink').removeClass(selectedClassName);
	hsq(ele).addClass(selectedClassName);
	hsq.cookie('aaaSize', size);
	hsq.cookie('aaaEle', ele.id);
	_aaa(ele);
}

/* =================================
			AAA Functionality End
==================================== */

/* =================================
			Search Functionality Start
==================================== */

function setupSearch(button, text){
var default_message = "Search by keyword(s)";
	btn = document.getElementById(button);
	txt = document.getElementById(text);
	txt.value = default_message;

	txt.onfocus = function() {
		if(this.value == default_message) {
			this.value='';
		}
	}
	
	txt.onblur = function() {
		if(this.value == default_message || this.value == '') {
			this.value=default_message;
		}
	}

	btn.onclick = function(){
		txt = document.getElementById(text);  
		var val = trim(txt.value, ' ');
		if(val==default_message|val==""){
			alert("Please enter keyword(s) to search.");
			txt.value = '';
			txt.focus();
			return false;
		}
	}
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/* =================================
			Search Functionality End
==================================== */


hsq(document).ready(function() {
	
	// AAA Setup
	_aaa();
	
	/*if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) //test for MSIE x.x;
	{
		var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		if (ieversion <= 7)
		{		
			// FOR TOP NAVIGATION
			hsq('.aaa a:not(.selected)').hover(function() {
				hsq(this).find('img').css('margin-top','-2.0833em');
			},function() {
				hsq(this).find('img').removeAttr('style');
			});			
		}
	}	*/
	
	
	// Search Setup
	hsq('input#searchbutton').hover(function() {
		$this = hsq(this);
		var src = $this.attr('src').replace('.gif','_hover.gif');
		$this.attr('src', src);
	}, function() {
		$this = hsq(this);
		var src = $this.attr('src').replace('_hover.gif','.gif');
		$this.attr('src', src);
	});
	setupSearch("searchbutton","searchterm");
	
	// Banner Setup
	if(hsq('.bannerRotator').length != 0)
	{
		bannerRotator();
	}
	
	
	/*
	// Tabular Data
	if(hsq('table').length != 0)
	{
		hsq("table:not(.skip) tr:not(.skip)").hover(function() {
			hsq(this).addClass("highlight");
		}, function() {
			hsq(this).removeClass("highlight");
		});
	}
	*/
	
	// For Board of directors and Corporate governance page
	if(hsq('.accordian').length != 0)
	{
		hsq('.accordian .moreDetails').each(function() {
			var $this = hsq(this);
			//if($this.find('.moreDetails').length != 0)
			{
				$this.hide();
				$this.after('<img src="/images/bullet.gif" alt="" class="linkBullet"><a href="" class="moreLink">Show more</a>');
			}
		});
		
		hsq('.accordian a.moreLink').click(function() {
			var $this = hsq(this);
			var $moreDetails = $this.parent().find('.moreDetails');
			if($moreDetails.is(':visible'))
			{
				$moreDetails.slideUp('fast');
				$this.text('Show more');
			}
			else
			{
				hsq('.accordian div.moreDetails').slideUp('fast');
				hsq('.accordian a.moreLink').text('Show more');
				$moreDetails.slideDown('fast');
				$this.text('Show less');
			}
			return false;
		});
	}
	
	
	// For tab pages
	if(hsq('ul.tabs').length != 0)
	{
		hsq(".tabPanel:not(:first)").hide();
	 	hsq(".tabPanel:first").show();
		
		hsq("ul.tabs a").click(function(){
			hsq("ul.tabs a").removeClass('selected');
			stringref = hsq(this).addClass('selected').attr("href");
			hsq('.tabPanel:not('+stringref+')').hide();
			hsq('.tabPanel' + stringref).show();
			return false;
		});
	}
	
	
	if(hsq('.imageGallery').length != 0)
	{
		hsq('.imageGallery a').lightBox();
	}
});


/* ====================================
			Banner Rotator Functions Start
======================================= */

function bannerRotator() {
	//Set the opacity of all images to 0
	hsq('div.bannerRotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	hsq('div.bannerRotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 5000 = change to next image after 5 seconds
	setInterval('rotateBanner()',5000);
}

function rotateBanner() {	
	//Get the first image
	var currentBanner = (hsq('div.bannerRotator ul li.show')?  hsq('div.bannerRotator ul li.show') : hsq('div.bannerRotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((currentBanner.next().length) ? ((currentBanner.next().hasClass('show')) ? hsq('div.bannerRotator ul li:first') :currentBanner.next()) : hsq('div.bannerRotator ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);

	//Hide the current banner image
	currentBanner.animate({opacity: 0.0}, 1000).removeClass('show');
};

/* ====================================
			Banner Rotator Functions End
======================================= */
