/* *******************************************************
J  a  v  a  c  a  t  z
Filename:   [project].js
Purpose:    This file is created to hold common functions
            that are used in project only.
            Please do not put other codes in this file.
Created by: KY 2010.04.09
******************************************************* */

function bodyOnLoader(navId1,navId2,navId3,navId4,navId5) {
	if($('nav-'+navId1)) $('nav-'+navId1).addClassName('current');
	if($('nav-'+navId2)) $('nav-'+navId2).addClassName('current');
	if($('nav-'+navId3)) $('nav-'+navId3).addClassName('current');
	if($('nav-'+navId4)) $('nav-'+navId4).addClassName('current');
	if($('nav-'+navId5)) $('nav-'+navId5).addClassName('current');
	if($('side-'+navId1)) $('side-'+navId1).addClassName('current');
	if($('side-'+navId2)) $('side-'+navId2).addClassName('current');
	if($('side-'+navId3)) $('side-'+navId3).addClassName('current');
	if($('side-'+navId4)) $('side-'+navId4).addClassName('current');
	if($('side-'+navId5)) $('side-'+navId5).addClassName('current');
	kyMenu.init();
	kyMenu2.init();
	
	if(typeof kyMenu3 == 'object') kyMenu3.init();
	if (typeof javacatzSendToFriend == 'object') javacatzSendToFriend.init();
	if (typeof javacatzSocialBookmark == 'object') javacatzSocialBookmark.init();

	if($('side')) $('side').select('a.current').each(
		function(aCurrent) {
			if($(aCurrent).up('li').down('ul')) {
				$(aCurrent).up('li').down('ul').show().addClassName('currentUl');
			}
		}
	);
}

document.observe('dom:loaded', domLoaded = function() {
	// for Effect.Morph for fontsize adjust
	if($('main')) $('main').setStyle({ 'fontSize':'100%' });
	$$('.fckOnly').each(function (node) { $(node).remove(); });
	disableOverlay.init();
	
    $$('ul').each(function(node) {
        var li = $(node).firstDescendant();
        if ($(li)) li.addClassName('first');
        var lis = $(node).childElements();
        var li_last = $(node).childElements().last();
        if ($(li_last)) li_last.addClassName('last');
    });

    $$('ol').each(function(node) {
        var li = $(node).firstDescendant();
        if ($(li)) li.addClassName('first');
        var li_last = $(node).childElements().last();
        if ($(li_last)) li_last.addClassName('last');
    });

    $$('table.tableAltRow').each(function(table) {
        $(table).select('tr').each(function(tr,i) {
			if(i%2) $(tr).addClassName('tr_even');
			else $(tr).addClassName('tr_odd');
		});
    });
	
	$$('.ul','.subUl').each(function(node) {
        var li = $(node).down('.li').up().childElements().first();
        if ($(li)) li.addClassName('first');
        var lis = $(node).childElements();
        var li_last = $(node).down('.li').up().childElements().last();
        if ($(li_last)) li_last.addClassName('last');
    });
	
	$$('tr').each(function(tr) {
        var cell = $(tr).firstDescendant();
        if ($(cell)) cell.addClassName('first');
        var cell_last = $(tr).childElements().last();
        if ($(cell_last)) cell_last.addClassName('last');
	});

    $$('table').each(function (table) {
        if ($(table).down('td')) $(table).down('td').up('tr').addClassName('firstbody'); 
    });
	
	$$('.item').each(function(node,index) {
		if(index==0) $(node).addClassName('first');
	});

    /* screenmode */
    var screenmode = getParameter("screenmode");
    if (screenmode != null && screenmode != "") {
        if (screenmode.toLowerCase() == "print") printPage2();
    }
    if (getParameter("screenmode") == "print") {
        $$('select').each(function(node) { $(node).disabled = true; });
        $$('input').each(function(node) { $(node).disabled = true; });
    }

    /* ie flash object focus */
    /* ieupdate(); */
    fixPNG();
	
	if(typeof pageBanner == 'object' && $('pageBanner')) pageBanner.init();
	
	backTop();
	setInterval("backTop();", 2000);
	
	simpleBanners.init();
	
    if ($$('.accordion').length > 0) {
        $$('.accordion').each(
			function(accordion) {
			    $(accordion).select('.accordionTitle').each(
					function(title) {
					    $(title).setStyle({ 'cursor': 'pointer' });
					   //  $(title).insert({ 'top': new Element('div').addClassName('close').update(new Element('a', { 'href': 'javascript:void(0);' }).update('Close')) });
					    $(title).observe('mouseenter', function(e) {
					        $(this).up('.accordionItem').addClassName('hover');
					    });
					    $(title).observe('mouseleave', function(e) {
					        $(this).up('.accordionItem').removeClassName('hover');
					    });
					    $(title).observe('click', function(e) {
							var content = $(this).up('.accordionItem').down('.accordionContent');
							if($(content).up('.accordionItem').hasClassName('active')) {
								new Effect.SlideUp($(content), { duration: 0.5, beforeStart: function() {
									if(!$(content).visible()) {
										$(content).up('.accordionItem').addClassName('active');
									}
								}, afterFinish: function() {
									if($(content).visible()) {
										$(content).up('.accordionItem').addClassName('active');
									} else {
										$(content).up('.accordionItem').removeClassName('active');
									}
								} });
							} else {
								new Effect.SlideDown($(content), { duration: 0.5, delay: 0.5,
									beforeStart: function() {
										if($(content).up('.accordion').hasClassName('openOne')) {
											var thisItem = $(content).up('.accordionItem');
											$(content).up('.accordion').select('.accordionItem.active').each( function(activeItem) {
												if($(thisItem) != $(activeItem)) {
													var activeContent = $(activeItem).down('.accordionContent');
													$(activeContent).up('.accordionItem').removeClassName('active');
													new Effect.SlideUp($(activeContent), { duration: 0.5 });
												}
											} );
										}
										if(!$(content).visible()) {
											$(content).up('.accordionItem').addClassName('active');
										}
									},
									afterFinish: function() {
										if($(content).visible()) {
											$(content).up('.accordionItem').addClassName('active');
										} else {
											$(content).up('.accordionItem').removeClassName('active');
										}
									}
								});
							}
					    });
					}
				);
			}
		);
    }
});

Event.observe(window, 'resize', function() { resizewindow(); } );
function resizewindow() {
	if($$('body').length > 0) {
		var width = $$('body')[0].getWidth();
		var height = $$('body')[0].getHeight();
		if(width < document.viewport.getWidth()) width = document.viewport.getWidth();
		if(height < document.viewport.getHeight()) height = document.viewport.getHeight();
		if($('navDisableLayer')) $('navDisableLayer').setStyle({ 'width':width+'px', 'height':height+'px' });
	}
	backTop();
}

function backTop() {
	if($('main') && $('main').down('.mainInner')) {
	    var mainInner = $('main').down('.mainInner');
	    var lastModified = '';
	    if ($('ctl00_lastModified')) lastModified = $('ctl00_lastModified').value;
		if (mainInner.getHeight() > document.viewport.getHeight()) {
		    var backTop = new Element('div', { 'id': 'backTop' }).addClassName('backTop').update('<div class="divleft">' + lastModified + '</div><div class="divback"><a href="#" onclick="return false;">Back to Top</a></div>').observe('click', function (e) { $('page').scrollTo(); });
			if($('backTop')) $('backTop').replace(backTop);
			else $(mainInner).insert({ 'bottom': backTop });
		} else {
            if ($('backTop')) $('backTop').remove();
            var backTop = new Element('div', { 'id': 'backTop' }).addClassName('backTop').update('<div class="divleft">' + lastModified + '</div>');
            if ($('backTop')) $('backTop').replace(backTop);
            else $(mainInner).insert({ 'bottom': backTop });
		}
	}
}

// copied from kadoorie proj
var disableOverlay = {
    init: function () {
        var layer_disable_overlay = new Element('div', { 'id': 'layer_disable_overlay' }).hide();
        $('page').insert({ 'before': layer_disable_overlay });
    },
    show: function (duration, delay) {
        disableOverlay.onresize();
        if (!duration) duration = 0.25;
        if (!delay) delay = 0;
        $$('select').each(function (node) { $(node).addClassName('invisibleSelect'); });
        new Effect.Appear($('layer_disable_overlay'), { duration: duration, delay: delay });
    },
    hide: function (duration, delay) {
        if (!duration) duration = 0.25;
        if (!delay) delay = 0;
        new Effect.Fade($('layer_disable_overlay'), { duration: duration, delay: delay, afterFinish: function () { $$('select').each(function (node) { $(node).removeClassName('invisibleSelect'); }); } });
    },
    onresize: function () {
        if ($('layer_disable_overlay')) {
            var viewportWidth = document.viewport.getWidth();
            var bodyWidth = $$('body').first().getWidth();
            var width = viewportWidth;
            if (bodyWidth > viewportWidth) width = bodyWidth;
            var viewportHeight = document.viewport.getHeight();
            var bodyHeight = $$('body').first().getHeight();
            var height = viewportHeight;
            if (bodyHeight > viewportHeight) height = bodyHeight;
            $('layer_disable_overlay').setStyle({ 'width': width + 'px', 'height': height + 'px' });
        }
    }
};

var video = {
    show: function (fullpath, title) {
        disableOverlay.show();
        var width = 640;
        var height = 480;
        var left = (document.viewport.getScrollOffsets()[0] + (document.viewport.getWidth() - width) / 2);
        if (left < 0) left = 0;
        var top = (document.viewport.getScrollOffsets()[1] + (document.viewport.getHeight() - height - 60) / 2);
        if (top < 0) top = 0;
        if ($('layerVideoPlayer')) $('layerVideoPlayer').remove();
        var layerVideoPlayer = new Element('div', { 'id': 'layerVideoPlayer' }).absolutize().hide().setStyle({ 'backgroundColor': '#fff', 'marginLeft': left + 'px', 'marginTop': top + 'px', 'width': width + 'px', 'height': 'auto', 'zIndex': '99999' }).update('<div class="align_left"><a href="javascript:video.hide();" class="float_right" style="margin:3px 3px 3px 10px;" title="Close"><img src="img/button/close.gif" alt="Close" title="Close" /></a><div style="font-size:109%;font-weight:bold;margin:3px;">' + title + '</div></div><div id="videoPlayerFlash" style="width:' + width + 'px;height:' + height + 'px;"></div>');
        $('page').insert({ 'before': layerVideoPlayer });
        new Effect.Appear($(layerVideoPlayer), { duration: 1.0, afterFinish: function () { swfobject.embedSWF('flash/media_list_player.swf', 'videoPlayerFlash', width, height, '9.0.0', 'flash/expressInstall.swf', { 'autoplay': '1', 'path': fullpath }, { 'quality': 'high', 'bgcolor': '#000000', 'play': 'true', 'loop': 'false' }); } });
        $('layer_disable_overlay').observe('click', function (e) { video.hide(); });
    },
    hide: function () {
        $('layer_disable_overlay').stopObserving('click');
        disableOverlay.hide();
        if ($('layerVideoPlayer')) {
            $('videoPlayerFlash').remove();
            new Effect.Fade($('layerVideoPlayer'), { duration: 1.0, afterFinish: function () { $('layerVideoPlayer').remove(); } });
        }
    }
};


var simpleBanners = {
	timeout: 7,					// seconds
	timer: new Array(),		// setInterval for each pageBanners
	init: function() {
		$$('.pageBanners').each( function(pageBanners) {
			$(pageBanners).down('.banner').addClassName('active').show().setStyle({'zIndex':2});
			var id = $(pageBanners).identify();
			var numberOfBanners = $(id).select('.banner').length;
			if(numberOfBanners > 1) $(id).down('.bannerControls').show();
			clearTimeout(simpleBanners.timer[id]);
			simpleBanners.timer[id] = null;
			simpleBanners.timer[id] = setTimeout("simpleBanners.next('" + id + "');", simpleBanners.timeout * 1000);
		} );
	},
	prev: function(id) {
		if(!$(id)) id = $$('.pageBanners').first().identify();
		var numberOfBanners = $(id).select('.banner').length;
		if(numberOfBanners < 2) return;
		if(!simpleBanners.timer[id]) return;
		clearTimeout(simpleBanners.timer[id]);
		simpleBanners.timer[id] = null;
		var activeBanner = $(id).down('.active');
		var nextBanner = $(activeBanner).previous('.banner');
		if(!nextBanner) nextBanner = $(id).select('.banner').last();
		new Effect.Fade($(activeBanner), {
			duration: 1,
			beforeStart: function() {
				$(activeBanner).setStyle({'zIndex':2}).removeClassName('active');
				$(nextBanner).show().setStyle({'zIndex':1}).addClassName('active');
			},
			afterFinish: function() {
				$(activeBanner).hide().setStyle({'zIndex':0}).removeClassName('active');
				$(nextBanner).show().setStyle({'zIndex':2}).addClassName('active');
				clearTimeout(simpleBanners.timer[id]);
				simpleBanners.timer[id] = null;
				simpleBanners.timer[id] = setTimeout("simpleBanners.next('" + id + "');", simpleBanners.timeout * 1000);
			}
		});
	},
	next: function(id) {
		if(!$(id)) id = $$('.pageBanners').first().identify();
		var numberOfBanners = $(id).select('.banner').length;
		if(numberOfBanners < 2) return;
		if(!simpleBanners.timer[id]) return;
		clearTimeout(simpleBanners.timer[id]);
		simpleBanners.timer[id] = null;
		var activeBanner = $(id).down('.active');
		var nextBanner = $(activeBanner).next('.banner');
		if(!nextBanner) nextBanner = $(id).select('.banner').first();
		new Effect.Fade($(activeBanner), {
			duration: 1,
			beforeStart: function() {
				$(activeBanner).show().setStyle({'zIndex':2}).removeClassName('active');
				$(nextBanner).show().setStyle({'zIndex':1}).addClassName('active');
			},
			afterFinish: function() {
				$(activeBanner).setStyle({'zIndex':0});
				$(nextBanner).setStyle({'zIndex':2});
				clearTimeout(simpleBanners.timer[id]);
				simpleBanners.timer[id] = null;
				simpleBanners.timer[id] = setTimeout("simpleBanners.next('" + id + "');", simpleBanners.timeout * 1000);
			}
		});
	}
}



function showIndex(key,thisNode) {
    $(thisNode).up('.alphabetIndex').select('.current').each(function (node) { $(node).removeClassName('current'); });
    if (key) {
        if ($('ctl00_ContentPlaceHolder1_indexList_' + key)) {
            $$('.indexList').each(
				    function (node) {
				        $(node).hide();
				        // if($(node).identify() != 'indexList_'+key) new Effect.BlindUp($(node), { duration: 0.3 });
				    });
            $('ctl00_ContentPlaceHolder1_indexList_' + key).show();
            // new Effect.BlindDown($('indexList_'+key), { duration: 0.3, delay: 0.3 });
            $(thisNode).addClassName('current');
        }
    } else {
        $$('.indexList').each(
			    function (node) {
			        $(node).show();
			        // new Effect.BlindDown($(node), { duration: 0.3 });
			    });
        $(thisNode).addClassName('current');
    }
}


var calendarDetails = {
	openall: function(thisNode) {
		$(thisNode).up('.calendarDay').select('a.detail').each( function(node) {
			new Effect.BlindDown($(node).up('li').down('.details'), { duration: 0.5, afterFinish: function() { $(node).up('li').addClassName('open'); } });
		} );
	},
	open: function(thisNode) {
		new Effect.BlindDown($(thisNode).up('li').down('.details'), { duration: 0.5, afterFinish: function() { $(thisNode).up('li').addClassName('open'); } });
	},
	close: function(thisNode) {
		new Effect.BlindUp($(thisNode).up('li').down('.details'), { duration: 0.5, afterFinish: function() { $(thisNode).up('li').removeClassName('open'); } });
	},
	toggle: function(thisNode) {
		if($(thisNode).up('li').hasClassName('open')) {
			new Effect.BlindUp($(thisNode).up('li').down('.details'), { duration: 0.5, afterFinish: function() { $(thisNode).up('li').removeClassName('open'); } });
		} else {
			new Effect.BlindDown($(thisNode).up('li').down('.details'), { duration: 0.5, afterFinish: function() { $(thisNode).up('li').addClassName('open'); } });
		}
	},
	showLayer: function(thisNode) {
		$$('.calendarLayer').each( function(node) { $(node).hide(); } );
		var calendarLayer = $(thisNode).up('td').down('.calendarLayer');
		$(calendarLayer).setStyle({'right':'auto'}).show();
		if($(calendarLayer).cumulativeOffset().left + $(calendarLayer).getWidth() + 30 > document.viewport.getWidth()) {
			$(calendarLayer).setStyle({'right':'5px'});
		}
	}
};

var javacatzSendToFriend = {
    version: '1.0.2009.11.12',
    currentNode: null,
    id: 'layer-javacatzSendToFriend',
    init: function () {
        if (!Prototype || parseFloat(Prototype.Version) < 1.6) return;
        var url = encodeURIComponent(document.location);
        var title = encodeURIComponent(document.title);
        var id = javacatzSendToFriend.id;
        var layer = new Element('div', { 'id': id }).hide();
        $$('body')[0].insert({ 'bottom': layer });
        var innerLayer = new Element('div', { 'class': 'padding' });
        $(layer).update(innerLayer);
        $(innerLayer).insert(new Element('div', { 'class': 'title' }).update('Send this page to a friend').insert(new Element('div', { 'class': 'closeButton', title: 'Close' }).update('&times;').observe('click', function () { javacatzSendToFriend.toggle(); })));

        // show iframe
        var iframeLayer = '<iframe id="' + id + '_iframe" name="' + id + '_iframe" class="iframe" frameborder="0" marginwidth="0" marginheight="0" src="js/emailtofriend.aspx?url=' + url + '&title=' + title + '"></iframe>';
        $(innerLayer).insert(iframeLayer);

        // powered by Javacatz
        var poweredByJavacatz = new Element('div', { 'class': 'poweredByJavacatz' }).update(new Element('a', { 'href': 'http://www.javacatz.com/', 'target': '_blank', 'title': 'Javacatz' }).update('Powered by Javacatz'));
        $(innerLayer).insert(poweredByJavacatz);

        window.onresize = javacatzSendToFriend.onResize;
    },
    toggle: function (node) {
        var id = javacatzSendToFriend.id;
        javacatzSendToFriend.currentNode = node;
        if ($(node) && !$(id).visible()) $(id).setStyle({
            'left': $(node).cumulativeOffset()[0] + 'px',
            'top': $(node).getOffsetParent().cumulativeOffset()[1] + $(node).getOffsetParent().getHeight() + 2 + 'px'
        });
        new Effect.toggle($(id), 'appear', { 'duration': 0.3, afterSetup: function () {
            if ($(id).cumulativeOffset()[0] + $(id).getWidth() > $(id).getOffsetParent().getWidth()) {
                $(id).setStyle({ 'left': 'auto', 'right': '10px' });
            }
        }
        });
    },
    onResize: function () {
        var id = javacatzSendToFriend.id;
        var node = javacatzSendToFriend.currentNode;
        if ($(node) && $(id)) $(id).setStyle({
            'left': $(node).cumulativeOffset()[0] + 'px',
            'top': $(node).getOffsetParent().cumulativeOffset()[1] + $(node).getOffsetParent().getHeight() + 2 + 'px'
        });
        if ($(id).cumulativeOffset()[0] + $(id).getWidth() > $(id).getOffsetParent().getWidth()) {
            $(id).setStyle({ 'left': 'auto', 'right': '10px' });
        }
    }
};

