

// IE6 PNG fix
if(typeof DD_belatedPNG !== 'undefined'){
	DD_belatedPNG.fix('.header-promo, .header-promo div, .ui-tab-buttons a.direct-link-icon:link, .ui-tab-buttons a.direct-link-icon:visited, .icons li a span, .icons li.hover a span, .link.unordered a:link, .link.unordered a:visited');
}

// Initialise tabs
$('.tabs')
	.tabs()
	.bind('ui-tab:change', function(e, $tab){
		// Switch between BU styles
		$('body').attr('class', $('body').attr('class').replace(/personal|business|teg/g, $tab.attr('href').split('#')[1].replace('tab-', '')));
	});

// Add direct links to tab buttons
$('<a class="direct-link-icon" href="http://telstra.com.au/personal" title="Go to telstra.com.au/personal">Go to telstra.com.au/personal</a>').appendTo('#tab-button-personal');
$('<a class="direct-link-icon" href="http://telstrabusiness.com/" title="Go to telstrabusiness.com">Go to telstrabusiness.com</a>').appendTo('#tab-button-business');
$('<a class="direct-link-icon" href="http://telstraenterprise.com/" title="Go to telstraenterprise.com">Go to telstraenterprise.com</a>').appendTo('#tab-button-teg');

// Add 'hover' classname when icon is hovered over
$('.icons li').hover(function(){
	$(this).addClass('hover');
}, function(){
	$(this).removeClass('hover');
});


// Social buttons 

// Number format util
/* Usage:
	num: number of people
	abbrev: bool of whether to use SI units
*/
function numFormat(num, abbrev){
	if(num < 10000){
		abbrev = false;
	}
	if(abbrev){
		num = num/100;
		num = Math.floor(num).toFixed(0)/10;
	}
	var count = new String(num);
	var pattern = new RegExp(/(\d+)(\d{3})/);
	while(pattern.test(count)){
		count = count.replace(pattern, '$1' + ',' + '$2');
	}
	if(abbrev){
		return count + "k";
	} else {
		return count;
	}
}

// Facebook
(function(d, s, id) {
	var js, fjs = d.getElementsByTagName(s)[0];
	if (d.getElementById(id)) {return;}
	js = d.createElement(s); js.id = id;
	js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=184074655002290";
	fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

(function($){
	$.getJSON("https://graph.facebook.com/293895497313181?callback=?", function(data){
		document.getElementById("fb-count").innerHTML = numFormat(data.likes, false) + " people";
	})
})($);

// Twitter
(function($){
	$.getJSON("https://api.twitter.com/1/users/show.json?screen_name=Telstra&include_entities=false&callback=?", function(data){
		document.getElementById("twit-count").innerHTML = numFormat(data.followers_count, true) + " followers";
	})
})($);

// Google Plus
(function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();

(function($){
	$.get("/homepage/socialinfo.cfm", function(data){
		var pattern = new RegExp(/gplus-count=\s*(\d+),?/);
		var matchArr = pattern.exec(data);
		if(matchArr){
			document.getElementById("gplus-count").innerHTML = "+"+numFormat(matchArr[1], false);
		}
	});
})($);


