// JavaScript Document


$(document).ready(function(){
	if(!is_mobile)
	{
		if(site_dir == 'us')
		{ // show US t's and c's in an iframe using thickbox api
			if(!readCookie('us_agree')){ //if the cookie hasn't been set.
				tb_show(null, '/message/lang/us/?&KeepThis=true&TB_iframe=true&height=375&width=760&modal=true',null);
			}
		}
	
		if(site_dir == 'hk_en' || site_dir == 'hk_zh')
		{ // show HK t's and c's in an iframe using thickbox api
			if(!readCookie('hk_agree'))
			{ //if the cookie hasn't been set.
	
				if(site_dir == 'hk_en')
				{
					tb_show(null, '/message/lang/hk_en/?&KeepThis=true&TB_iframe=true&height=375&width=760&modal=true',null);
				}
				else if(site_dir == 'hk_zh')
				{
					tb_show(null, '/message/lang/hk_zh/?&KeepThis=true&TB_iframe=true&height=375&width=760&modal=true',null);
				}
			}
		}
	}
	highlightMenu();

	
	// expand if cliclekd on dt collapse all
	$(".faq_open").click(function (e) {
		e.preventDefault();
        //$('dd:visible').slideUp('fast');
        $(this).next('div').slideDown('fast');
    });
	// collapse close button
	$(".faq_close").click(function (e) {
		e.preventDefault();
		$(this).parent().slideUp('fast');
	});

	$('select#ha_months').change(function(e)
	{
		var archivedperiod = $("option:selected", this).val();
		document.location.href = archivedperiod;
	});


	//analytics tracking for downloads
	$('a[href]').click(function(e)
	{
		href = $(this).attr('href');
		filetypes = Array('.pdf','.doc','.xls','.csv');
		for(c=0;c<filetypes.length;c++)
		{
		   if (href.indexOf(filetypes[c]) !== -1)
		   {
				pageTracker._trackEvent("Download", "Documents", href);
				//alert(href);
		   }
		}
		filetypes = Array('.mov','.mp4');
		for(c=0;c<filetypes.length;c++)
		{
		   if (href.indexOf(filetypes[c]) !== -1)
		   {
				pageTracker._trackEvent("Download", "Video", href);
				//alert(href);
		   }
		}
	});
	getNewsflash();
	
	columns_ajax = new FS_ajaxFeed(['shareInfo','leftData'],"/ajax/columns/"+site_dir+"/",60000);
	columns_ajax.loadAjaxData();
	
});



function getNewsflash()
{
	if($('#newsFlash').length)
	{
		query = document.location.search.substring(1);
		pairs = query.split("&"); 
		url = "/newsflash/get_message/"+site_dir+"/";
		for(var i = 0; i < pairs.length; i++) {
			var pos = pairs[i].indexOf('=');
			if (pos == -1) continue;
			var argname = pairs[i].substring(0,pos); 
			var value = pairs[i].substring(pos+1);
			if(argname == 'show_newsflash' && value == 'true')
			{
				url = "/newsflash/admin/get_message/";
			}
		} 		
		
		$.get(url, function(xmlDataSet) {
			items = $('newsflash',xmlDataSet).children();
			returned = $(items[0]).text();
			if(returned != 0)
			{
				$('#newsFlash').html(returned.replace(/^\s+|\s+$/g, ''));
				$('#newsFlash').show();
			}
		});	
	}
}






function highlightMenu(){ // attempt to highlight the current item in the navigation
	$("#leftNav").find("a").each(function (i) {
		if($(this).attr('href') == location.pathname) {
			$(this).addClass("leftCurrent");
		} 
	});
	$("#siteTopBar").find("a").each(function (i) {
		if($(this).attr('href') == location.pathname) {
			$(this).addClass("topCurrent");
		} 
	});
}



// used only for the US site.
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 var 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;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}

