/*  general.js
 *  Authored by Kelvin Tse
 *
 *  Copyright 2007 IDG Australia
/*--------------------------------------------------------------------------*/

var browserName=navigator.appName; 

isMSIE = false;
if (browserName=="Microsoft Internet Explorer")
{
	isMSIE = true;
}

function getElem(ident) {
	var exp, obj;

	if (document.getElementById) {
		exp = "document.getElementById('" + ident + "')";
	}
	else if (document.all) {
		exp = "document.all['" + ident + "']";
	}

	return eval(exp);
}


function getWidth(obj) {

	if (typeof(obj) == "string") {
		obj = getElem(obj);	
	}
	if (obj == undefined) return;

	return (obj.offsetWidth);

}

function getHeight(obj) {

	if (typeof(obj) == "string") {
		obj = getElem(obj);	
	}
	if (obj == undefined) return;

	return (obj.offsetHeight);

}

function resize_h(id, from, to, step, count) {

	if (count == undefined) count = 10;
	obj = getElem(id);	
	if (obj == undefined) return;
	if (to < from) { step = -1 * step }

	obj.style.overflow = "hidden";
	obj.style.height = from + 'px';
	setTimeout("resize_h_step('" + id + "'," + to + "," + step + "," + (count - 1) + ")", 0);
}

function resize_h_step(id, to, step, count) {

	obj = getElem(id);	
	if (obj == undefined) return;

	currheight = obj.offsetHeight;
	if ((count == 0) || (Math.abs(currheight - to) < Math.abs(step))) {
		obj.realheight = currheight + (to - currheight);
		obj.style.height = "auto";
		if (isMSIE) { obj.style.height = "1%"; }
		if (obj.rundone != undefined) { obj.rundone(); obj.rundone = function() {}; }
	} else {
		currheight += step;
		obj.style.height = currheight + 'px';
		step *= 3;
		setTimeout("resize_h_step('" + id + "'," + to + "," + step + "," + (count - 1) + ")", 10);
	}
}

function getChild(parent,tagname,classname) {
	
	elements = parent.getElementsByTagName(tagname);
	if (elements.length == 0) { return null; }
	for (i=0;i<elements.length;i++) {
		if (elements[i].className == classname) {
			return elements[i];
		}
	}
}
function move(obj,cood_x,cood_y) {
	setTimeout(mv, 10, obj, cood_x, cood_y);
}

function mv(obj,cood_x,cood_y) {
	//alert(cood_x + ' ' + cood_y);
}

document.getElementsByClassNameconflicted = childbyClass = function(className, parentElement) {
	var children = parentElement.getElementsByTagName('*');
	var elements = [], child;
	for (var i = 0, length = children.length; i < length; i++) {
	  child = children[i];
	  if (child.className == className)
	    elements.push(child);
	}
	return elements;
};

function elem(element) {
  if (typeof element == 'string')
    element = document.getElementById(element);
  return element;
}

function opacity(id, opacStart, opacEnd, step) {
	object = elem(id);
	changeOpac(opacStart, object);
	clearTimeout(object.opacTimeout);
	if (opacStart > opacEnd) { stepdir = -1 * step; }
	else { stepdir = step; }
	object.opacTimeout = setTimeout("_opacity('" + object.id + "'," + opacEnd + "," + stepdir + ")",0);
}

function _opacity(id,opacEnd,stepdir) {
	object = elem(id);	
	if ((opac = getStyle(object,"opacity")) == undefined)
		currOp = 100;
	else
		currOp = opac * 100;
	if (Math.abs(opacEnd - currOp) < Math.abs(stepdir)) {
		currOp = opacEnd;
		changeOpac(currOp, object);
	}
	if (currOp == opacEnd) {
		if (object.runEnd) { object.runEnd(); /*object.runEnd = null;*/ }
		return;
	}
	changeOpac((currOp + stepdir), object);
	object.opacTimeout = setTimeout("_opacity('" + id + "'," + opacEnd + "," + stepdir + ")",20);
}

function changeOpac(opacity, object) {
	object = elem(object);
	object.style.opacity = (opacity / 100);
	object.style.MozOpacity = (opacity / 100);
	object.style.KhtmlOpacity = (opacity / 100);
	if (opacity == 100) object.style.filter = '';
	else object.style.filter = "alpha(opacity="+opacity+")";
}

function getStyle(el,styleProp)
{
	var x = elem(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(hyphenate(styleProp));
	else if (document.defaultView.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null)[hyphenate(styleProp)];			
	return y;
}

function hyphenate(string)
{
	return string.replace(/\w[A-Z]/g, function(match){
		return (match.charAt(0)+'-'+match.charAt(1).toLowerCase());
	});
}

function Point(x, y) {
	this.x = x;
	this.y = y;
}

function getObjCoords(o) {
	var oX = 0;
	var oY = 0;
	if (o.offsetParent) {
		while (1) {
			oX+=o.offsetLeft;
			oY+=o.offsetTop;
			if (!o.offsetParent) {
				break;
			}
			o=o.offsetParent;
		}
	} else if (o.x) {
		oX+=o.x;
		oY+=o.y;
	}
	//alert(oX + ":" + oY);
	return new Point(oX, oY);
}

function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth,myHeight];
}

function ss_changeSlide(dynename, num) {
	var data = eval(dynename);
	if ((data['current'] != null) && (num == data['current'])) return;
	//document.getElementById(dynename + '_cDesc').innerHTML = data['titles'][num];
	//document.getElementById(dynename + '_cDesc').href = data['links'][num];
	document.getElementById(dynename + '_s' + num).style.visibility = 'visible';
	document.getElementById(dynename + '_tn' + num).className = 'selected';
	if (data['current'] != null) {
		document.getElementById(dynename + '_s' + data['current']).style.visibility = 'hidden';
		document.getElementById(dynename + '_tn' + data['current']).className = '';
	}
	data['current'] = num;
}

function commerical_break(divname, timeout) {
	var threecolumns = elem(divname);
	threecolumns.style.display = 'none';
	$('#skin_flash_outer').css('display','none');
	var commerical_break_layer = elem('commerical_break_layer');
	winsize = getWindowSize();
	commerical_break_layer.style.display = 'block';
	commerical_break_layer.style.height = winsize[1] + 'px';
	setTimeout ( "commerical_break_close('" + divname + "')", timeout);
}

function commerical_break_close(divname) {
	$('#commerical_break_layer').css('display', 'none');
	$('#'+divname).css('display', 'block');
	$('#skin_flash_outer').css('display','block');
	if ('function' == typeof showPagePeel) {
		showPagePeel();
	}
	//alert(pagepeelcount);
	//clearTimeout(pagepeelcount);
	return false;
}

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);
}

function careerone_joblist_init() {
	$('.careerone-promo-jobpage').css('display','none');
	$("#careerone-promo-topjobs").data('max',$('.careerone-promo-jobpage').length);
	$("#careerone-promo-topjobs").data('rot',1);
	if ($("#careerone-promo-topjobs").data('max') > 0)
		c1_joblist_rotate_timeout = setTimeout("careerone_joblist_rotate()",0);
	$("#careerone-promo-topjobs").mouseover(function(){
		$("#careerone-promo-topjobs").data('entered', true);
	}).mouseout(function(){
		$("#careerone-promo-topjobs").data('entered', false);
		clearTimeout(c1_joblist_rotate_timeout);
		c1_joblist_rotate_timeout = setTimeout("careerone_joblist_rotate()",6000);
	});
}
function careerone_joblist_switch(i) {
	$('.careerone-promo-jobpage').css('display','none');
	$('#careerone-topjobs-pagelinks .sel').removeClass('sel');
	$('#careerone-topjobs-pagelink-'+i).addClass('sel');
	$('#careerone-promo-topjobs-'+i).fadeIn("fast");
	if (i == $("#careerone-promo-topjobs").data('max')) {
		$("#careerone-promo-topjobs").data('rot',1);
	} else {
		$("#careerone-promo-topjobs").data('rot',i+1);
	}
	clearTimeout(c1_joblist_rotate_timeout);
	c1_joblist_rotate_timeout = setTimeout("careerone_joblist_rotate()",6000);
}
function careerone_joblist_rotate() {
	if ($("#careerone-promo-topjobs").data('entered') != true)
		careerone_joblist_switch($("#careerone-promo-topjobs").data('rot'));
}

/* for sitemap */
function sitemap_link_init() {
	ascroll_link_init('sitemaplink');
}
function ascroll_link_init(id) {
	$('a[href*=#]#'+id).click(function() {
			var $target = $(this.hash);
			$target = $target.length && $target
			|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body')
				.animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		//}
	});
}
/* end */


// Social sharing for articles
$(document).ready(function() {
  $('#article_tools_print').click(function(e) { e.preventDefault(); window.print(); });
  $('#article_tools_rss').click(function(e) { e.preventDefault(); });
  $('#article_tools_share').click(function(e) {
    e.preventDefault();
    $('html,body').animate({ scrollTop: $("#article_actions").offset().top }); // Scroll to share icons
  });
});


