function getNow(){
	return Date && Date.now ?
			Date.now() :
			new Date();
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
function changeImagesArray(array) {
	if (preloadFlag == true) {
		var d = document; var img;
		for (var i=0; i<array.length; i+=2) {
			img = null; var n = array[i];
			if (d.images) {img = d.images[n];}
			if (!img && d.getElementById) {img = d.getElementById(n);}
			if (img) {img.src = array[i+1];}
		}
	}
}
function changeImages() {
	changeImagesArray(changeImages.arguments);
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		pre_home_on = newImage('images/home-on.gif');
		pre_news_on = newImage('images/news-on.gif');
		pre_contact_on = newImage('images/contact-on.gif');
		pre_careers_on = newImage('images/careers-on.gif');
		pre_company_on = newImage('images/company-on.gif');
		preloadFlag = true;
	}
}
(function(){
	preloadImages();
	if($.browser.safari)
		$("td[@id] div.menu").each(function(){
			$(this).addClass("menuForSafari");
			$(this).removeClass("menu");
		});
	
});
function showMenuLoop(div, step, width, content, starttime){
	var w = $(div).width() + step;
	var dis = getNow() - starttime;
	if(w >= width || dis > 500){
		div.style.width = width;
		div.style.marginLeft = -width;
		$(div).html(content);
	}else{
		div.style.width = w;
		div.style.marginLeft = -w;
		setTimeout(function(){
			showMenuLoop(div, step, width, content, starttime);
		}, 50);
	}
}
function showMenu(td){
	var div = $("div:first", td)[0];
	//div.style.display = "";
	//return ;
	var display = div.style.display;
	if(display == "none"){
		
		var content = $(div).html();
		if(!$(td).attr("menucontent"))
			$(td).attr("menucontent", $(div).html());
		if(!$(td).attr("menuwidth"))
			$(td).attr("menuwidth", $(div).width());
		var content = $(td).attr("menucontent");
		var width = $(td).attr("menuwidth");
		$(div).html("");
		
		var step = Math.max(width / 5, 30);
		div.style.width = step;
		div.style.marginLeft = -step;
		div.style.display = "";
		setTimeout(function(){
			showMenuLoop(div, step, width, content,getNow());
		}, 50);
	}
}
function hideMenuLoop(div, step, width, content, starttime){
	var w = $(div).width() - step;
	var dis = getNow() - starttime;
	if(w <= 0 || dis > 500){
		div.style.display = "none";
		div.style.width = width;
		div.style.marginLeft = - width;
		$(div).html(content);
	}else{
		div.style.width = w;
		div.style.marginLeft = -w;
		setTimeout(function(){
			hideMenuLoop(div, step, width, content, starttime);
		}, 50);
	}
}
function hideMenu(td){
	var div = $("div:first", td)[0];
	//div.style.display = "none";
	//return ;
	var display = div.style.display;
	if(display != "none"){
		
		if(!$(td).attr("menucontent"))
			$(td).attr("menucontent", $(div).html());
		if(!$(td).attr("menuwidth"))
			$(td).attr("menuwidth", $(div).width());
		var content = $(td).attr("menucontent");
		var width = $(td).attr("menuwidth");
		$(div).html("");
	
		var step = Math.max(width / 5, 30);
		div.style.width = width - step;
		div.style.marginLeft = -(width - step);
		setTimeout(function(){
			hideMenuLoop(div, step, width, content, getNow());
		}, 50);		
	}
}
function toggleMenu(td){
	var div = $("div:first", td)[0];
	var display = div.style.display;
	if(display != "none")
		hideMenu(td);
	else
		showMenu(td);
}