// ===============================
// = HSBC HTML Backup general JS =
// = Dependencies: swfObject 2.1 =
// = Author: Tim Leung			 =
// ===============================


var flashTimeout;

$(document).ready(function () {
	checkFlashCookie();
	
	flashTimeout = setTimeout(function () {
		if ($('object#mainFlash').length !== 0) setHtmlLinkDisplay();
	}, 3000);
        
	// testing - please remove
	//setTimeout(setHtmlLinkDisplay, 1500);
	
	windowCloseButton();
});


/*
* shouldDisplayFlash - a getter / setter for flash display
* Call with a boolean argument to set state, calling without an argument will return the stored state.
*/
var shouldDisplayFlash = function (state) {
	// store flash option in a cookie
	var expires = new Date(new Date().getTime() + 6 * 30 * 24 * 3600000);// 6 months

	if (typeof state === 'undefined') {
		return (document.cookie.indexOf('flash=false') === -1);
	}
	else {
		state = (state != false);
		document.cookie = 'flash='+ state +'; expires='+ expires +'; path=/';
		return state;
	}
};

function checkFlashCookie () {
	var viewMode = shouldDisplayFlash();
	if (!viewMode) {
		$('#non_flash').css({ display:'none' });
		$('#non_js').css({ display:'inline' });
		$('#flash_link').css({ display:'inline' }).click(handleHtmlLink);
	}
	else {
		$('#non_flash').css({ display:'block', opacity:0.0 });
		$('#html_link').css({ display:'inline' }).click(handleHtmlLink);
	}
	
	function handleHtmlLink (e) {
		e.preventDefault();
		shouldDisplayFlash(!viewMode);
		window.location.reload();
	}
};

// flash control called from movie
function setHtmlLinkDisplay (action) {
	clearTimeout(flashTimeout);
	var actionIsHide = (/hide/i).test(action);
	
	var duration = (actionIsHide) ? 700 : 1200,
		finalOpacity = (actionIsHide) ? 0.0 : 1.0;
	$('#non_flash').animate({ opacity: finalOpacity }, duration, function () {
		if (actionIsHide) {
			// hide the information link
			$('#info_link').addClass('hide');
		}
	});
};

function windowCloseButton() {
	var closeButton = $('#close_window');
	closeButton.click( function() {window.close();});
}