$(function(){

	// --- time convert function --- //
	$.timeConversion = function(timeZone){
//		var newTime = new Date().getTimezoneOffset().getTime() + timeZone * (60*60*1000)
		
		d = new Date();
		localTime = d.getTime();
		localOffset = d.getTimezoneOffset() * 60000;
		utc = localTime + localOffset;
		var newTime = utc + (3600000 * timeZone);
		return newTime;
	};

	$(document).ready(function(){

		// --- Jakarta clock --- //
		var jakarta_timezone=$.timeConversion(+7);

		$("#clock_jakarta").clock({
			"format": "24",
			"calendar": "false",
			"timestamp": jakarta_timezone
		});
		
		// --- Hong Kong clock --- //
		var hongkong_timezone=$.timeConversion();

		$("#clock_hongkong").clock({
			"format": "24",
			"calendar": "false",
			"timestamp": hongkong_timezone
		});
		
		// --- Toyko clock --- //
		var tokyo_timezone=$.timeConversion();

		$("#clock_tokyo").clock({
			"format": "24",
			"calendar": "false",
			"timestamp": tokyo_timezone
		});
		
		// --- clock label rotation --- //
		$.rotate = function(elem){
			var total_li = $(elem).children().length;
			var total_height = 25 * total_li;
		
			if((parseInt($(elem).css("top")) - 25) > -total_height)
			{
				top = "-=25px";
			} else {
				top = 0 + "px";
			}
			
			$(elem).animate({
				top: top
			});
		};
		
		// --- clock label random helper function --- //
		jQuery.extend({
			random: function(X) {
				return Math.floor(X * (Math.random() % 1));
			},
			randomBetween: function(MinV, MaxV) {
				return MinV + jQuery.random(MaxV - MinV + 1);
			}
		});
		
		// --- random clock label --- //
		$.labelToggle = function(elem){
			var total_li = $(elem).children().length;
			var index = $.randomBetween(0, total_li-1);
			var total_height = 25 * total_li;
			var top = index * 25;

			if(-top < -total_height)
			{
				top = 0;
			}

			$(elem).css(
				"top", -top + "px"
			);
		};
		
//		setInterval("$.rotate('#indonesia_region')", 3000);
//		setInterval("$.rotate('#china_region')", 3000);
//		setInterval("$.rotate('#japan_region')", 3000);

		$.labelToggle("#indonesia_region");
		$.labelToggle('#china_region');
		$.labelToggle('#japan_region');
	
		// --- initialization for the Our Ports and the Top Navigation animation --- //
		$("#header_outPorts_dropdown_container").hide();
		$("#header_ourPorts_dropdown").css("height","0px");
		$("#header_ourPorts_dropdown ul").hide();
		
		$("#nav_left .expandable ul").css("height","0px");
		$("#nav_left .expandable ul li").hide();
	});
	
	// --- animation for Our Ports --- //
	$("#header_ourPorts").hover(
		function(){
			var no_of_countries = $("#header_ourPorts_dropdown ul li").length;
			var height_count = Math.ceil(no_of_countries/2);
			var drop_height = Math.ceil((185/5)*height_count) -10;
			var drop_height_string = drop_height+'px';
			//alert(drop_height);
			$("#header_outPorts_dropdown_container").stop().show();
			$("#header_ourPorts_dropdown").stop().animate({
				height:drop_height_string
			}, 500);
			$("#header_ourPorts_dropdown ul").stop().show();
		},
		function(){
			$("#header_outPorts_dropdown_container").stop().hide();
			$("#header_ourPorts_dropdown ul").stop().hide();
			$("#header_ourPorts_dropdown").stop().css("height","0px");
		}
	);
	
	// --- animation for Top Navigation --- //
	$("#nav_left .expandable").hover(
		function(){
		
			var ul_height = 0;
			
			$(this).find("ul li").each(function(){
				ul_height = ul_height + $(this).height();
			});

			$(this).find("ul li").stop().show();
			$(this).find("ul").stop().animate({
				height:ul_height + "px"
			}, 500);
		},
		function(){
			$("#nav_left .expandable ul").stop().css("height","0px");
			$("#nav_left .expandable ul li").stop().hide();
		}
		
	);
});
