// globals
// link targets Must be id
var g_ajaxTargetDefault = 'content';
var g_ajaxTarget = g_ajaxTargetDefault;
var g_ajaxLoading = false;
var g_ajaxLink = '';
var g_firstRun = true;
var g_nContentHeightFixed = 516;
var g_flashAmIReady = 0;

$(document).ready(function() {
	$('head').append('<link rel="shortcut icon" href="http://www.millennium.net.my/favicon.ico">');
	var nTmp = getcookie('mill_fsess');
	nTmp=0;
	if(nTmp!=null) {
		// redirect
		if(parseInt(nTmp)>0) {
			CLASSID='index.html#';
			init();
			initWindow();
			swfobject.embedSWF("flash/nav.swf", "flashcontent", "955", "97", "8.0.0", "expressinstall.swf", null, {wmode:"transparent"});
			g_firstRun = false;
			return;
		}
	}
	var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
	if(CLASSID=='index.html'&&g_firstRun) {
		if(plugin!=null) {
			$('#content').css('overflow', 'visible');
			//$('body').css('overflow', 'hidden');
			$('body').css('position', 'relative');
			$('#flash_intro_content').width($(document).width());
			$('#flash_intro_content').height($(document).height());
			$('#flash_bg').width($(window).width());
			$('#flash_bg').height($(window).height());
			$('#flash_skip').css('left', ($(window).width()-$('#flash_skip').width())+'px');
			$('#flash_skip').css('top', ($(window).height()-$('#flash_skip').height())+'px');
			$('#flash_skip').click(function() {
				init();
				initWindow();
				$('#flash_skip').remove();
				$('#flash_intro').remove();
				$('#flash_overlay').remove();
				g_flashAmIReady = 1;
				swfobject.embedSWF("flash/nav.swf", "flashcontent", "955", "97", "8.0.0", "expressinstall.swf", null, {wmode:"transparent"});
				flash_showPage2();
			});
			
			swfobject.embedSWF("flash/index2.swf?t="+new Date().getTime(), "flash_intro_content", $('#flash_intro').width(), $('#flash_intro').height(), "9.0.0", "expressinstall.swf", null, {wmode:"transparent"});
			swfobject.embedSWF("flash/tagline.swf", "overlayslogan", $('#overlayslogan').width(), $('#overlayslogan').height(), "9.0.0", "expressinstall.swf", null, {wmode:"transparent"});
		} else {
			init();
			initWindow();
			$('#flash_overlay').remove();
			setTimeout(flash_showPage2, 2000);
		}
	} else {
		init();
		swfobject.embedSWF("flash/nav.swf", "flashcontent", "955", "97", "8.0.0", "expressinstall.swf", null, {wmode:"transparent"});
	}
	if(plugin==null)
		$('#flashcontent').append('<a href="http://www.adobe.com/go/EN_US-H-GET-FLASH" target="_blank"><img border="0" src="images/get_adobe_flash_player.png"></a>');
});
$(window).load(function() {
	if(CLASSID=='index.html'&&g_firstRun) return;
	initWindow();
	g_firstRun = false;
});
$(window).resize(function(){
	if(CLASSID=='index.html'&&g_firstRun) return;
  updateFooter();
});
function flash_showPage()
{
	init();
	initWindow();
	swfobject.embedSWF("flash/nav.swf", "flashcontent", "955", "97", "8.0.0", "expressinstall.swf", null, {wmode:"transparent"});
	g_flashAmIReady = 1;
	setTimeout(flash_showPage2, 2000);
}
function flash_showPage2()
{
	// load latest news
	$.ajax({url: 'newshighlight.php',
		type: 'GET',
		cache: false,
		dataType: 'xml',
		timeout: 20000,
		error: function() {
		},
		success: function(data){
			var objItems = data.getElementsByTagName('item');
			var szRet = '';
			var szDate = '';
			var szTitleLink = '';
			var szDescription = '';
			var arrTmp = '';
			for(i=0;i<objItems.length&&i<4;i++) {
				szDate = objItems[i].getElementsByTagName('pubDate')[0].childNodes[0].nodeValue;
				arrTmp = szDate.split(' ');
				szDate = arrTmp[0]+' '+arrTmp[1]+' '+arrTmp[2]+' '+arrTmp[3];
				szDescription = objItems[i].getElementsByTagName('description')[0].childNodes[0].nodeValue;
				szTitleLink = objItems[i].getElementsByTagName('title')[0].childNodes[0].nodeValue;
				szTitleLink = "<a title='"+szDescription+"' href='"+objItems[i].getElementsByTagName('link')[0].childNodes[0].nodeValue+"'>"+szTitleLink+"</a>";
				szRet += "<li>"+szDate+": "+szTitleLink+"</li>";
			}
			if(szRet!='') {
				$('#latestnews').html("<ul>"+szRet+"</ul>");
			}
			updateFooter();
		}
	});
	
	$('#flash_intro').remove();
	$('#flash_skip').remove();
	//$('body').css('overflow', 'visible');
	g_firstRun = false;
	putcookie('mill_fsess', 1, null);
}
function updateFooter(bEndCheck)
{
	if(CLASSID=='index.html') {
		$('#bottom').css('position', 'relative');
		if(parseInt($(document).height())>parseInt($(window).height())) {
			$('#bottom').css('margin', '0');
			$('#bottom').css('position', 'relative');
		} else {
			$('#bottom').css('margin', '-80px 0 0 0');
			$('#bottom').css('position', 'fixed');
		}
		return;
	}
	if($('#content').attr('isAnimating')==1) return;
	var myHeight = $('#bottom').outerHeight({margin:true});
  var nTmp2 = parseInt($('#sidenav').css('padding-bottom'));
  var nTmp = $(window).height()-myHeight-nTmp2;
  // don't do anything if it's already at the bottom or just after an ajax call
  if(bEndCheck) {
  	//if(nTmp>g_nContentHeightFixed) {
	  	var nDiff = nTmp-g_nContentHeightFixed;
	  	$('#sidenav').height(parseInt($('#sidenav').attr('baseheight'))+nDiff);
	  	CSBfleXcroll('sidenav');
	  	
	  	if($('#scrollcontent')) {
		  	$('#scrollcontent').height(parseInt($('#scrollcontent').attr('baseheight'))+nDiff);
		  	CSBfleXcroll('scrollcontent');
		  }
		//}
  	return;
  }
  $('#content').attr('isAnimating', 1);
  nTmp = parseInt($(window).height())-myHeight-nTmp2;
  if(g_firstRun)
  	$('#content').height(nTmp);
  $('#content').animate({'height': nTmp}, 1000, "easeInOutSine", function() {$(this).attr('isAnimating', 0);updateFooter(true);});
}
function initWindow(bAjax)
{
	if($('#sidenav').attr('baseheight')==undefined) {
		$('#sidenav').attr('baseheight', $('#sidenav').height());
	}
	if($('#scrollcontent')) {
		if($('#scrollcontent').attr('baseheight')==undefined)
			$('#scrollcontent').attr('baseheight', $('#scrollcontent').height());
	}
	$('#content').attr('isAnimating', 0);
	
	if(CLASSID=='index.html') return;
	updateFooter();
	
	if(!bAjax||g_ajaxTarget==g_ajaxTargetDefault)
	try{
		// set mouseover effect
		$('img[rollover]').each(function(index){
			if($(this).parent().attr('href')==CLASSID) {
				$(this).attr('norollover', $(this).attr('src'));
				$(this).attr('src', $(this).attr('rollover'));
			} else {
				$(this).attr('norollover', $(this).attr('src'));
				$(this).hover(function () {
						$(this).attr('src', $(this).attr('rollover'));
					},
					function () {
						$(this).attr('src', $(this).attr('norollover'));
					}
				);
			}
		});
	}catch(e) {}
	if(!bAjax)
		ddaccordion.init({
			headerclass: "lv1", //Shared CSS class name of headers group
			contentclass: "submenu", //Shared CSS class name of contents group
			revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click" or "mouseover
			mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
			collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
			defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
			onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
			animatedefault: false, //Should contents open by default be animated into view?
			persiststate: false, //persist state of opened contents within browser session?
			toggleclass: ["", "selected"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
			togglehtml: ["", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
			animatespeed: "normal", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
			oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
				//do nothing
			},
			onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
				initScroller();
			}
		});
	
	// reset all the links
	$('#content a').each(function() {
		var szHREF = $(this).attr('href');
		if(szHREF.indexOf('#')==0||$(this).attr('ajaxIgnore')==1||$(this).attr('target')=='_blank'||szHREF.indexOf('http://')==0) return;
		if(szHREF=='index.html') return;
		$(this).attr('ajaxURL', szHREF);
		$(this).attr('href', '#'+szHREF);
		$(this).attr('ajaxIgnore', 1);
		$(this).click(function() {return linkUp(this);});
	});
}
function initScroller()
{
	CSBfleXcroll('sidenav');
	CSBfleXcroll('scrollcontent');
}

function linkUp(objLink) {
	if(g_ajaxLoading) return true;
	var szURL = $(objLink).attr('ajaxURL');
	var bGo = false;
	if(szURL==undefined) {
		szURL = objLink.substr(1);
		bGo = true;
	} else {
		if($(objLink).attr('ajaxTarget')==undefined)
			g_ajaxTarget = g_ajaxTargetDefault;
		else
			g_ajaxTarget = $(objLink).attr('ajaxTarget');
		
		bGo = true;
	}
	if(bGo==false||szURL==undefined) return true;
	g_ajaxLoading = true;
	showLoading(g_ajaxLoading);
	g_ajaxLink = objLink;
	$.ajax({url: szURL,
		type: 'GET',
		cache: false,
		dataType: 'html',
		timeout: 20000,
		error: function() {
			alert('Network connection error or page not found.');
			g_ajaxLoading = false;
			showLoading(g_ajaxLoading);
		},
		success: function(data){
			var objDiv = document.createElement('div');
			objDiv.innerHTML = data;
			if($('#'+g_ajaxTarget).html()==null)
				g_ajaxTarget = g_ajaxTargetDefault;
			var szHTML = $(objDiv).find('#'+g_ajaxTarget).html();
			
			$('#'+g_ajaxTarget).html(szHTML);
			initWindow(true);
			g_ajaxLoading = false;
			showLoading(g_ajaxLoading);
			try{$(g_ajaxLink).trigger('updateacc');}catch(e) {}
			g_ajaxLink = '';
		}
	});
	return true;
}
function flash_amIReady()
{
	return g_flashAmIReady;
}
function flash_init()
{
	$('#flash_overlay').remove();
}
function showLoading(bShow)
{
	if(bShow) {
		var nTarget = '#col2';
		if($(nTarget).html()==null) nTarget = '#col4';
		var nWidth = $(nTarget).width();
		var nHeight = $(nTarget).height();
		$(nTarget).append('<div id="soul_overlay" style="position:absolute;background:#ffffff;opacity:0.8;filter:alpha(opacity=80);left:0;top:0;width:'+nWidth+'px;height:'+(nHeight)+'px;z-index:50;"><img src="i/white.gif" width="'+nWidth+'" height="'+nHeight+'" /></div><img id="soul_loading" style="position:absolute;display:block;left:'+(nWidth/2)+'px;top:'+(nHeight/2)+'px;width:24px;height24px;z-index:101;" src="i/loader.gif" />');
	} else {
		$('#soul_overlay').remove();
		$('#soul_loading').remove();
	}
}